summaryrefslogtreecommitdiff
path: root/Compiler/visitors
diff options
context:
space:
mode:
authorMelodi <62381002+Melodi17@users.noreply.github.com>2025-09-05 10:20:03 +1000
committerMelodi <62381002+Melodi17@users.noreply.github.com>2025-09-05 10:20:03 +1000
commit9e8ba6e00dc4a4137a5187dbe5fe5b1394219735 (patch)
treee5a54706a8a85243fdb0fe0f2ed73fb7b7566bcd /Compiler/visitors
parent4ea967c6089bbcaba5d71145546d288e186fba40 (diff)
stuff
Diffstat (limited to 'Compiler/visitors')
-rw-r--r--Compiler/visitors/Definitions.cs21
-rw-r--r--Compiler/visitors/Expressions.cs50
2 files changed, 50 insertions, 21 deletions
diff --git a/Compiler/visitors/Definitions.cs b/Compiler/visitors/Definitions.cs
index e4cf797..c27405e 100644
--- a/Compiler/visitors/Definitions.cs
+++ b/Compiler/visitors/Definitions.cs
@@ -5,7 +5,7 @@ public partial class CCompilerVisitor
private List<(CGrammarParser.FunctionDefinitionContext context,
FunctionReference function, FunctionOverload overload,
List<string> paramNames, List<CType> paramTypes)> _functionBodies = new();
-
+
private VariableReference FindVariable(string varName)
{
foreach (Dictionary<string, VariableReference> variables in this._variableStack)
@@ -42,7 +42,7 @@ public partial class CCompilerVisitor
throw new CompilerException($"Function '{name}' has not been defined or prototyped.");
}
-
+
public override string? VisitFunctionPrototype(CGrammarParser.FunctionPrototypeContext context)
{
string functionName = context.name.Text;
@@ -52,13 +52,15 @@ public partial class CCompilerVisitor
List<string> parameterNames = context._params.Select(x => x.Text).ToList();
FunctionReference function = this.CreateOrGetFunctionReference(functionName, type);
- FunctionOverload overload = function.CreateOverloadWithoutBody(parameterNames, parameterTypes);
+ FunctionOverload overload =
+ function.CreateOverloadWithoutBody(parameterNames, parameterTypes);
function.Overloads.Add(overload);
return null;
}
- public override string? VisitFunctionDefinition(CGrammarParser.FunctionDefinitionContext context)
+ public override string? VisitFunctionDefinition(
+ CGrammarParser.FunctionDefinitionContext context)
{
string functionName = context.name.Text;
CType type = this.VisitCType(context.rtype);
@@ -73,7 +75,12 @@ public partial class CCompilerVisitor
return null;
}
- private void VisitFunctionBody(CGrammarParser.FunctionDefinitionContext context, FunctionReference function, FunctionOverload overload, List<string> paramNames, List<CType> paramTypes)
+ private void VisitFunctionBody(
+ CGrammarParser.FunctionDefinitionContext context,
+ FunctionReference function,
+ FunctionOverload overload,
+ List<string> paramNames,
+ List<CType> paramTypes)
{
this.Branch(function.GetBranchName(overload), function.GetBranchFriendlyName(overload));
@@ -96,9 +103,11 @@ public partial class CCompilerVisitor
this.Instruction("mov 0, V0", "fallback return 0");
this.Instruction("ret");
+
this.Blank();
}
- public override string? VisitVariableDeclaration(CGrammarParser.VariableDeclarationContext context)
+ public override string? VisitVariableDeclaration(
+ CGrammarParser.VariableDeclarationContext context)
{
CType type = this.VisitCType(context.type());
int addr = this.Alloc(CType.Int.Size);
diff --git a/Compiler/visitors/Expressions.cs b/Compiler/visitors/Expressions.cs
index e67688d..1cc4291 100644
--- a/Compiler/visitors/Expressions.cs
+++ b/Compiler/visitors/Expressions.cs
@@ -10,7 +10,7 @@ public partial class CCompilerVisitor
return expressionString;
}
-
+
public override string VisitCallExpression(CGrammarParser.CallExpressionContext context)
{
string functionName = context.ID().GetText();
@@ -25,15 +25,20 @@ public partial class CCompilerVisitor
{
CGrammarParser.ExpressionContext argument = argumentsReversed[i];
string result = this.VisitExpression(argument);
+
+ int indexReverse = argumentsReversed.Length - i - 1;
- this.Instruction($"push {result}", $"arg {overload.ParameterTypes[i]} {overload.ParameterNames[i]}");
+ this.Instruction(
+ $"push {result}", $"arg {overload.ParameterTypes[indexReverse]} {overload.ParameterNames[indexReverse]}");
}
- this.Instruction($"call {function.GetBranchName(overload)}", function.GetBranchFriendlyName(overload));
+ this.Instruction(
+ $"call {function.GetBranchName(overload)}", function.GetBranchFriendlyName(overload));
return "V0";
}
- public override string VisitParenthesizedExpression(CGrammarParser.ParenthesizedExpressionContext context)
+ public override string VisitParenthesizedExpression(
+ CGrammarParser.ParenthesizedExpressionContext context)
{
return this.VisitExpression(context.expression());
}
@@ -44,7 +49,8 @@ public partial class CCompilerVisitor
return $"[${reference.Address:X4}]";
}
- public override string VisitAssignmentExpression(CGrammarParser.AssignmentExpressionContext context)
+ public override string VisitAssignmentExpression(
+ CGrammarParser.AssignmentExpressionContext context)
{
Target target = this.VisitTarget(context.target());
string value = this.VisitExpression(context.expression());
@@ -78,13 +84,15 @@ public partial class CCompilerVisitor
return "V0";
}
- public override string VisitAddressOfExpression(CGrammarParser.AddressOfExpressionContext context)
+ public override string VisitAddressOfExpression(
+ CGrammarParser.AddressOfExpressionContext context)
{
Target target = this.VisitTarget(context.target());
return target.Address;
}
- public override string VisitDereferenceExpression(CGrammarParser.DereferenceExpressionContext context)
+ public override string VisitDereferenceExpression(
+ CGrammarParser.DereferenceExpressionContext context)
{
string value = this.VisitExpression(context.expression());
@@ -97,16 +105,16 @@ public partial class CCompilerVisitor
Target obj = this.VisitTarget(context.obj);
if (obj.Type is not CTypePtr ptr)
throw new CompilerException("Indexer target was not a pointer!");
-
+
this.Instruction($"push {obj.Value}", "store index obj");
string indexer = this.VisitExpression(context.indexer);
- this.Instruction($"mov {indexer}, V1", "store indexer");
- this.Instruction($"pop V0", "restore index obj");
+ this.Instruction($"mov {indexer}, V1", "store indexer");
+ this.Instruction($"pop V0", "restore index obj");
this.Instruction($"mul V1, {ptr.Sub.Size}", $"multiply indexer by {ptr.Sub.Size}");
- this.Instruction($"add AX, V0", "add indexer and index obj");
- this.Instruction($"mov AX, V0", "move to general purpose register");
+ this.Instruction($"add AX, V0", "add indexer and index obj");
+ this.Instruction($"mov AX, V0", "move to general purpose register");
return "[V0]";
}
@@ -115,13 +123,17 @@ public partial class CCompilerVisitor
CType type = this.VisitCType(context.type());
int? arrPtr = null;
- if (context._params.Count == 0)
+ int length = context.INT() != null
+ ? int.Parse(context.INT().GetText())
+ : context._params.Count;
+
+ if (length == 0)
throw new CompilerException("Cannot create empty array");
-
+
foreach (CGrammarParser.ExpressionContext? param in context._params)
{
int itemPtr = Alloc(type.Size);
-
+
if (arrPtr == null)
arrPtr = itemPtr;
@@ -129,6 +141,14 @@ public partial class CCompilerVisitor
this.DynamicMove(type, value, $"[${itemPtr:X4}]");
}
+ int remainingSize = (length - context._params.Count) * type.Size;
+ if (remainingSize < 0)
+ throw new CompilerException("Array length larger than specified length");
+ int blockPtr = Alloc(remainingSize);
+
+ if (arrPtr == null)
+ arrPtr = blockPtr;
+
return $"${arrPtr:X4}";
}
} \ No newline at end of file