Skip to content

Commit

Permalink
Unsigned fixes & add instruction tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tge-was-taken committed Nov 28, 2024
1 parent 988d97f commit c7c78ad
Show file tree
Hide file tree
Showing 34 changed files with 2,063 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void ReorderProcedures(CompilationUnit compilationUnit)
var nameParts = declaration.Identifier.Text.Split('_');
if (nameParts.Length < 2) continue;
if (!nameParts[nameParts.Length - 2].Equals("index", StringComparison.OrdinalIgnoreCase)) continue;
if (!int.TryParse(nameParts[nameParts.Length - 1], out var index))
if (!uint.TryParse(nameParts[nameParts.Length - 1], out var index))
{
Error($"Unable to parse procedure index {nameParts[nameParts.Length - 1]}. Index will not be changed");
continue;
Expand All @@ -340,7 +340,7 @@ private void AddMissingProcedures(CompilationUnit compilationUnit)
if (!Scope.Procedures.Any(x => x.Value.Index == i))
{
// Add dummy procedure
var procedure = new ProcedureDeclaration(i, TypeIdentifier.Void, new Identifier($"procedure_{i}"), new List<Parameter>(), new CompoundStatement());
var procedure = new ProcedureDeclaration((uint)i, TypeIdentifier.Void, new Identifier($"procedure_{i}"), new List<Parameter>(), new CompoundStatement());
compilationUnit.Declarations.Add(procedure);
Scope.TryDeclareProcedure(procedure, out _);
}
Expand Down Expand Up @@ -588,7 +588,7 @@ private bool TryProcessCompiledFlowScriptImport(FlowScript compiledFlowScript)
{
// Add a declaration for the imported procedure
var procedure = compiledFlowScript.Procedures[i];
var procedureDecl = new ProcedureDeclaration(new IntLiteral(i), TypeIdentifier.Void,
var procedureDecl = new ProcedureDeclaration(new UIntLiteral((uint)i), TypeIdentifier.Void,
new Identifier(procedure.Name),
new List<Parameter>(), null);

Expand Down Expand Up @@ -657,7 +657,7 @@ private bool TryProcessCompiledFlowScriptImport(FlowScript compiledFlowScript)
}

// Add variable declaration to script
var decl = new VariableDeclaration(new VariableModifier(modifier, new IntLiteral(index)),
var decl = new VariableDeclaration(new VariableModifier(modifier, new UIntLiteral((uint)index)),
new TypeIdentifier(valueKind),
new Identifier(valueKind, NameFormatter.GenerateVariableName(modifier, valueKind, (ushort)index, true)),
null);
Expand Down Expand Up @@ -946,7 +946,7 @@ private bool TryEvaluateCompilationUnitBeforeCompilation(CompilationUnit compila
new VariableModifier(VariableModifierKind.Constant),
new TypeIdentifier(ValueKind.Int),
new Identifier(ValueKind.Int, dialog.Name),
new IntLiteral(i)
new UIntLiteral((uint)i)
);

if (!Scope.TryDeclareVariable(declaration))
Expand Down Expand Up @@ -1006,14 +1006,14 @@ private bool TryEvaluateCompilationUnitBeforeCompilation(CompilationUnit compila
}

// Count parameter by type.
short intParameterCount = 0;
short floatParameterCount = 0;
ushort intParameterCount = 0;
ushort floatParameterCount = 0;

foreach (var parameter in procedureDeclaration.Parameters)
{
short count = 1;
ushort count = 1;
if (parameter.IsArray)
count = (short)(((ArrayParameter)parameter).Size);
count = (ushort)(((ArrayParameter)parameter).Size);

if (!Library.UsePOPREG || parameter.Modifier == ParameterModifier.Out)
{
Expand Down Expand Up @@ -1317,7 +1317,7 @@ private bool TryEmitProcedureParameters(List<Parameter> parameters)

// Create declaration
VariableDeclaration declaration;
int count = 1;
uint count = 1;

if (!parameter.IsArray)
{
Expand Down Expand Up @@ -1720,9 +1720,9 @@ private bool TryRegisterVariableDeclaration(VariableDeclaration declaration, out
}

// Declare variable in scope
short size = 1;
ushort size = 1;
if (declaration.IsArray)
size = (short)(((ArrayVariableDeclaration)declaration).Size);
size = (ushort)(((ArrayVariableDeclaration)declaration).Size);

if (!Scope.TryDeclareVariable(declaration, index, size))
{
Expand Down Expand Up @@ -1846,7 +1846,7 @@ private bool TryEmitExpression(Expression expression, bool isStatement)

EmitPushBoolLiteral(boolLiteral);
break;
case IntLiteral intLiteral:
case IIntLiteral intLiteral:
if (isStatement)
{
Error(intLiteral, "A integer literal is an invalid statement");
Expand All @@ -1855,15 +1855,6 @@ private bool TryEmitExpression(Expression expression, bool isStatement)

EmitPushIntLiteral(intLiteral);
break;
case UIntLiteral uintLiteral:
if (isStatement)
{
Error(uintLiteral, "A integer literal is an invalid statement");
return false;
}

EmitPushUIntLiteral(uintLiteral);
break;
case FloatLiteral floatLiteral:
if (isStatement)
{
Expand Down Expand Up @@ -1908,15 +1899,15 @@ private bool TryEmitSubscriptOperator(SubscriptOperator subscriptOperator)

InitializerList arrayInitializer = variable.Declaration.Initializer as InitializerList;

if (subscriptOperator.Index is IntLiteral intLiteral)
if (subscriptOperator.Index is IIntLiteral intLiteral)
{
// Known index
Expression initializer = null;
if (arrayInitializer != null)
initializer = arrayInitializer.Expressions[intLiteral];
initializer = arrayInitializer.Expressions[(int)intLiteral.Value];

if (!TryEmitPushVariableValue(variable.Declaration.Modifier, variable.Declaration.Type.ValueKind,
variable.GetArrayElementIndex(intLiteral),
variable.GetArrayElementIndex((int)intLiteral.Value),
initializer))
{
return false;
Expand All @@ -1932,7 +1923,7 @@ private bool TryEmitSubscriptOperator(SubscriptOperator subscriptOperator)
var falseLabel = CreateLabel($"SubscriptIfNot{i}");

// Emit current index
EmitPushIntLiteral(i);
EmitPushIntLiteral(new UIntLiteral((uint)i));

// Emit index expression
if (!TryEmitExpression(subscriptOperator.Index, false))
Expand Down Expand Up @@ -2053,13 +2044,11 @@ private bool TryEmitCall(CallOperator callExpression, bool isStatement)
continue;

var arg = callExpression.Arguments[i];
if (!(arg.Expression is IntLiteral argInt))
{
// only check constants for now
// TODO: evaluate expressions
continue;
}

// only check constants for now
// TODO: evaluate expressions
if (!(arg is IIntLiteral argInt))
continue;
var index = argInt.Value;
if (index < 0 || index >= mScript.MessageScript.Dialogs.Count)
{
Expand All @@ -2071,7 +2060,7 @@ private bool TryEmitCall(CallOperator callExpression, bool isStatement)
? DialogKind.Message
: DialogKind.Selection;

var dialog = mScript.MessageScript.Dialogs[index];
var dialog = mScript.MessageScript.Dialogs[(int)index];
if (dialog.Kind != expectedDialogKind)
{
Error($"Function call to {callExpression.Identifier.Text} doesn't reference a {expectedDialogKind} dialog, got dialog of type: {dialog.Kind} index: {index}");
Expand Down Expand Up @@ -2181,42 +2170,42 @@ private void AddCompiledProcedure(ProcedureInfo procedure, Procedure compiledPro

private bool TryEmitIntrinsicCall(CallOperator callExpression, bool isStatement)
{
bool TryGetProcedureIndexArgument(CallOperator callExpression, out short index)
bool TryGetProcedureIndexArgument(CallOperator callExpression, out ushort index)
{
index = short.MinValue;
index = ushort.MaxValue;
if (callExpression.Arguments.Count == 1)
{
if (callExpression.Arguments[0].Expression is IntLiteral intArg)
if (callExpression.Arguments[0].Expression is IIntLiteral intArg)
{
index = (short)intArg.Value;
index = (ushort)intArg.Value;
return true;
}
else if (callExpression.Arguments[0].Expression is Identifier identifierArg)
{
if (!Scope.TryGetProcedure(identifierArg.Text, out var proc))
{
index = (short)proc.Index;
index = (ushort)proc.Index;
return true;
}
}
}
return false;
}
bool TryGetLabelIndexArgument(CallOperator callExpression, out short index)
bool TryGetLabelIndexArgument(CallOperator callExpression, out ushort index)
{
index = short.MinValue;
index = ushort.MaxValue;
if (callExpression.Arguments.Count == 1)
{
if (callExpression.Arguments[0].Expression is IntLiteral intArg)
if (callExpression.Arguments[0].Expression is IIntLiteral intArg)
{
index = (short)intArg.Value;
index = (ushort)intArg.Value;
return true;
}
else if (callExpression.Arguments[0].Expression is Identifier identifierArg)
{
if (!mLabels.TryGetValue(identifierArg.Text, out var label))
{
index = (short)label.Index;
index = (ushort)label.Index;
return true;
}
}
Expand All @@ -2228,7 +2217,7 @@ bool TryGetVariableIndexArgument(CallOperator callExpression, out ushort index)
index = ushort.MaxValue;
if (callExpression.Arguments.Count == 1)
{
if (callExpression.Arguments[0].Expression is IntLiteral intArg)
if (callExpression.Arguments[0].Expression is IIntLiteral intArg)
{
index = (ushort)intArg.Value;
return true;
Expand All @@ -2249,7 +2238,7 @@ bool TryGetCommIndexArgument(CallOperator callExpression, out ushort index)
index = ushort.MaxValue;
if (callExpression.Arguments.Count == 1)
{
if (callExpression.Arguments[0].Expression is IntLiteral intArg)
if (callExpression.Arguments[0].Expression is IIntLiteral intArg)
{
index = (ushort)intArg.Value;
return true;
Expand All @@ -2270,10 +2259,8 @@ bool TryGetCommIndexArgument(CallOperator callExpression, out ushort index)
case "__PUSHI":
if (callExpression.Arguments.Count == 1)
{
if (callExpression.Arguments[0].Expression is UIntLiteral pushiArg)
Emit(Instruction.PUSHI(pushiArg.Value));
else if (callExpression.Arguments[0].Expression is IntLiteral pushiArg2)
Emit(Instruction.PUSHI((uint)pushiArg2.Value));
if (callExpression.Arguments[0].Expression is IIntLiteral pushiArg)
Emit(Instruction.PUSHI((uint)pushiArg.Value));
else
{
Error(callExpression, "__PUSHI requires exactly one integer argument.");
Expand Down Expand Up @@ -3273,10 +3260,10 @@ private bool TryEmitSubscriptAssignment(AssignmentOperatorBase assignmentOperato
return false;
}

if (subscriptOperator.Index is IntLiteral intLiteral)
if (subscriptOperator.Index is IIntLiteral intLiteral)
{
// Known index
if (!TryEmitVariableAssignment(variable.Declaration, variable.GetArrayElementIndex(intLiteral)))
if (!TryEmitVariableAssignment(variable.Declaration, variable.GetArrayElementIndex((int)intLiteral.Value)))
return false;
}
else
Expand All @@ -3289,7 +3276,7 @@ private bool TryEmitSubscriptAssignment(AssignmentOperatorBase assignmentOperato
var falseLabel = CreateLabel($"SubscriptAssignmentIfNot{i}");

// Emit current index
EmitPushIntLiteral(i);
EmitPushIntLiteral(new UIntLiteral((uint)i));

// Emit index expression
if (!TryEmitExpression(subscriptOperator.Index, false))
Expand Down Expand Up @@ -3584,7 +3571,7 @@ private void EmitPushBoolLiteral(BoolLiteral boolLiteral)
Emit(Instruction.PUSHIS(0));
}

private void EmitPushIntLiteral(IntLiteral intLiteral)
private void EmitPushIntLiteral(IIntLiteral intLiteral)
{
Trace(intLiteral, $"Pushing int literal: {intLiteral}");

Expand Down Expand Up @@ -4477,7 +4464,7 @@ private void PopScope()
//
// Logging
//
private void Trace(SyntaxNode node, string message)
private void Trace(ISyntaxNode node, string message)
{
if (node.SourceInfo != null)
Trace($"({node.SourceInfo.Line:D4}:{node.SourceInfo.Column:D4}) {message}");
Expand All @@ -4490,7 +4477,7 @@ private void Trace(string message)
mLogger.Trace($"{message}");
}

private void Info(SyntaxNode node, string message)
private void Info(ISyntaxNode node, string message)
{
if (node.SourceInfo != null)
Info($"({node.SourceInfo.Line:D4}:{node.SourceInfo.Column:D4}) {message}");
Expand All @@ -4503,7 +4490,7 @@ private void Info(string message)
mLogger.Info($"{message}");
}

private void Error(SyntaxNode node, string message)
private void Error(ISyntaxNode node, string message)
{
if (node.SourceInfo != null)
Error($"({node.SourceInfo.Line:D4}:{node.SourceInfo.Column:D4}) {message}");
Expand All @@ -4522,7 +4509,7 @@ private void Error(string message)
// Debugger.Break();
}

private void Warning(SyntaxNode node, string message)
private void Warning(ISyntaxNode node, string message)
{
if (node.SourceInfo != null)
Warning($"({node.SourceInfo.Line:D4}:{node.SourceInfo.Column:D4}) {message}");
Expand Down
Loading

0 comments on commit c7c78ad

Please sign in to comment.