Skip to content

Commit

Permalink
Stack based array cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAndreiM committed Jul 8, 2024
1 parent 26e942b commit 01aecf6
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 79 deletions.
130 changes: 69 additions & 61 deletions src/JukaCompiler/Interpreter/JukaInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class JukaInterpreter : Stmt.IVisitor<Stmt>, Expr.IVisitor<object>
private readonly ServiceProvider serviceProvider;
private readonly JukaEnvironment globals;
private JukaEnvironment environment;
private readonly Dictionary<Expr, int?> locals = [];
private readonly Dictionary<Expr, int?> locals = new();
private readonly Stack<StackFrame> frames = new();
private readonly string globalScope = "__global__scope__";
private readonly int __max_stack_depth__ = 500;
Expand Down Expand Up @@ -54,7 +54,7 @@ internal void Interpret(List<Stmt> statements)
Lexeme lexeme = new(LexemeType.Types.IDENTIFIER, 0, 0);
lexeme.AddToken("main");
Expr.Variable functionName = new(lexeme);
Expr.Call call = new(functionName, false, []);
Expr.Call call = new(functionName, false, new());
Stmt.Expression expression = new(call);
Execute(expression);
}
Expand Down Expand Up @@ -87,7 +87,7 @@ internal void ExecuteBlock(List<Stmt> statements, JukaEnvironment env)
}
Stmt Stmt.IVisitor<Stmt>.VisitBlockStmt(Stmt.Block stmt)
{
ExecuteBlock(stmt.statements, new(environment));
ExecuteBlock(stmt.statements, new(this.environment));
return new Stmt.DefaultStatement();
}

Expand All @@ -113,8 +113,8 @@ Stmt Stmt.IVisitor<Stmt>.VisitClassStmt(Stmt.Class stmt)
environment.Define("super", superclass);
}

Dictionary<string, JukaFunction> functions = [];
foreach(var method in stmt.methods)
Dictionary<string, JukaFunction> functions = new();
foreach (var method in stmt.methods)
{
JukaFunction jukaFunction = new(method, environment, false);
functions.Add(method.StmtLexemeName, jukaFunction);
Expand Down Expand Up @@ -146,8 +146,8 @@ public Stmt VisitIfStmt(Stmt.If stmt)
{
Execute(stmt.thenBranch);
}
else if(stmt.elseBranch != null)
{
else if (stmt.elseBranch != null)
{
Execute(stmt.elseBranch);
}

Expand Down Expand Up @@ -197,9 +197,9 @@ private Stmt.Print VisitPrintAllInternal(Expr expr, Action<object> printAction)
if (variableExpression != null)
{
var arrayData = stackVariableState?.arrayValues[variableExpression.ArrayIndex];
if (arrayData is Expr.Literal literal)
if (arrayData is Expr.Literal)
{
PrintLiteral(literal, printAction);
PrintLiteral((Expr.Literal)arrayData, printAction);
}
}
}
Expand All @@ -220,7 +220,10 @@ private void PrintLiteralExpr(Expr expr, Action<object> printAction)
}
}

private void PrintLiteral(Expr.Literal expr, Action<object> printAction) => printAction(expr.ExpressionLexemeName);
private void PrintLiteral(Expr.Literal expr, Action<object> printAction)
{
printAction(expr.ExpressionLexemeName);
}

private void PrintLiteral(Expr.LexemeTypeLiteral expr, Action<object> printAction)
{
Expand Down Expand Up @@ -249,7 +252,7 @@ private bool PrintVariableLookUp(Expr.Variable expr, Action<object> printTypeAct
if (o != null)
printTypeAction(o);
}

if (stackVariable.Value is Expr.Literal literal)
{
printTypeAction(literal.ExpressionLexeme?.ToString()!);
Expand Down Expand Up @@ -322,7 +325,7 @@ public Stmt VisitVarStmt(Stmt.Var stmt)
// in the stack frame of foo().
// doing the check of the initializer allows the code
// to put the variable in the callers stack.
object? value = frames.Peek().AddVariable(stmt,this);
object? value = frames.Peek().AddVariable(stmt, this);
if (stmt.exprInitializer is Call)
{
frames.Pop();
Expand All @@ -333,8 +336,8 @@ public Stmt VisitVarStmt(Stmt.Var stmt)
//There could be issues when/if CTOR is implemented
}

environment.Define(stmt.name?.ToString()! , value);
return new Stmt.DefaultStatement();
environment.Define(stmt.name?.ToString()!, value);
return new Stmt.DefaultStatement();
}
public Stmt VisitWhileStmt(Stmt.While stmt)
{
Expand Down Expand Up @@ -393,7 +396,7 @@ public object VisitAssignExpr(Expr.Assign expr)
if (stackVariableState?.expressionContext is Literal context)
{
var literal = context;
var lexemeType = ((LexemeTypeLiteral) stackVariableState.Value!)!.LexemeType;
var lexemeType = ((LexemeTypeLiteral)stackVariableState.Value!)!.LexemeType;

return (
literal,
Expand All @@ -406,7 +409,7 @@ public object VisitAssignExpr(Expr.Assign expr)
var lexemeType = assign.ExpressionLexeme!.LexemeType;

return (
literal,
literal,
lexemeType);
}
}
Expand Down Expand Up @@ -593,65 +596,65 @@ private static bool IsEqual(object a, object b)
switch (expr.callee)
{
case Get:
{
object instanceMethod = expr.callee.Accept(this);
Stmt.Function? declaration = ((JukaFunction)instanceMethod).Declaration;
if (declaration != null)
{
StackFrame instanceStackFrame = new(declaration.StmtLexemeName);
frames.Push(instanceStackFrame);
object? instanceMethodReturn = ((JukaFunction)instanceMethod).Call(declaration.StmtLexemeName, this, null!);
frames.Pop();
return instanceMethodReturn;
}
object instanceMethod = expr.callee.Accept(this);
Stmt.Function? declaration = ((JukaFunction)instanceMethod).Declaration;
if (declaration != null)
{
StackFrame instanceStackFrame = new(declaration.StmtLexemeName);
frames.Push(instanceStackFrame);
object? instanceMethodReturn = ((JukaFunction)instanceMethod).Call(declaration.StmtLexemeName, this, null!);
frames.Pop();
return instanceMethodReturn;
}

break;
}
break;
}
}

StackFrame currentStackFrame = new(expr.callee.ExpressionLexeme.ToString());
frames.Push(currentStackFrame);

List<object?> arguments = [];
Dictionary<string, object?> argumentsMap = [];
List<object?> arguments = new();
Dictionary<string, object?> argumentsMap = new();

foreach (Expr argument in expr.arguments)
{
switch (argument)
{
case Variable lexeme when lexeme.ExpressionLexeme != null:
{
object? variable = environment.Get(lexeme.ExpressionLexeme);
arguments.Add(variable);
argumentsMap.Add(lexeme.ExpressionLexeme.ToString(), variable);
break;
}
{
object? variable = environment.Get(lexeme.ExpressionLexeme);
arguments.Add(variable);
argumentsMap.Add(lexeme.ExpressionLexeme.ToString(), variable);
break;
}
case Literal literal1:
{
Literal literal = literal1;
arguments.Add(item: literal.LiteralValue);
argumentsMap.Add(key: literal.ExpressionLexeme?.ToString()!, literal);
break;
{
Literal literal = literal1;
arguments.Add(literal.LiteralValue);
argumentsMap.Add(literal.ExpressionLexeme?.ToString()!, literal);
break;
}
}
}

switch (argumentsMap.Count)
{
case > 0:
currentStackFrame.AddVariables(argumentsMap);
currentStackFrame.AddVariables(argumentsMap, this);
break;
}

switch (expr.isJukaCallable)
{
case true:
try
{
{
IJukaCallable? jukacall = (IJukaCallable)this.ServiceProvider.GetService(typeof(IJukaCallable))!;
return jukacall.Call(methodName: expr.callee.ExpressionLexeme.ToString(), this, arguments);
}
catch(SystemCallException? sce)
catch (SystemCallException? sce)
{
return sce;
}
Expand All @@ -664,7 +667,7 @@ private static bool IsEqual(object a, object b)
throw new ArgumentException("Wrong number of arguments");
}

return function?.Call(expr.callee.ExpressionLexeme.ToString(),this, arguments);
return function?.Call(expr.callee.ExpressionLexeme.ToString(), this, arguments);
}

public object VisitGetExpr(Expr.Get expr)
Expand Down Expand Up @@ -753,15 +756,20 @@ public object VisitVariableExpr(Expr.Variable expr)
if (expr.ExpressionLexeme != null)
{
var lookUp = LookUpVariable(expr.ExpressionLexeme, expr);
return lookUp ?? throw new("Variable is null");
if (lookUp == null)
{
throw new("Variable is null");
}

return lookUp;
}

throw new JRuntimeException("Visit variable returned null");
}

public object VisitArrayExpr(Expr.ArrayDeclarationExpr expr)
{
_ = frames.Peek();
var currentFrame = frames.Peek();
//if (expr.initializerContextVariableName != null)
// currentFrame.AddStackArray(expr.initializerContextVariableName, expr.ArrayIndex);
return expr.ArraySize;
Expand All @@ -778,13 +786,13 @@ public object VisitArrayAccessExpr(ArrayAccessExpr expr)
var stackVariableState = LookUpVariable(expr.ArrayVariableName, expr) as StackVariableState;

int arraySize = 0;
if (stackVariableState?.expressionContext is ArrayDeclarationExpr)
if (stackVariableState?.expressionContext is ArrayDeclarationExpr arrayContext)
{
arraySize = (int) (stackVariableState?.Value)!;
arraySize = (int)(stackVariableState?.Value)!;
}
else if (stackVariableState?.expressionContext is NewDeclarationExpr context)
{
arraySize = ((ArrayDeclarationExpr) context.NewDeclarationExprInit).ArraySize;
arraySize = ((ArrayDeclarationExpr)context.NewDeclarationExprInit).ArraySize;
}

if (expr.ArrayIndex > arraySize)
Expand Down Expand Up @@ -838,9 +846,9 @@ internal ServiceProvider ServiceProvider

internal void Resolve(Expr expr, int depth)
{
if (locals.Where( f => f.Key.ExpressionLexeme != null && expr.ExpressionLexeme != null && f.Key.ExpressionLexeme.ToString().Equals(expr.ExpressionLexeme.ToString()) ).Count() <= 1)
if (locals.Where(f => f.Key.ExpressionLexeme != null && expr.ExpressionLexeme != null && f.Key.ExpressionLexeme.ToString().Equals(expr.ExpressionLexeme.ToString())).Count() <= 1)
{
locals.Add(expr,depth);
locals.Add(expr, depth);
}
}

Expand All @@ -853,18 +861,18 @@ private bool IsTrue(object? o)
case bool b:
return b;
case LexemeTypeLiteral literal:
{
var boolValue = literal.Literal;
if (boolValue is bool)
{
return IsTrue(boolValue);
}
var boolValue = literal.Literal;
if (boolValue is bool)
{
return IsTrue(boolValue);
}

break;
}
break;
}
}

return true;
}
}
}
}
Loading

0 comments on commit 01aecf6

Please sign in to comment.