diff --git a/src/JukaCompiler/Interpreter/JukaInterpreter.cs b/src/JukaCompiler/Interpreter/JukaInterpreter.cs index 51f23eb1..87bf007a 100644 --- a/src/JukaCompiler/Interpreter/JukaInterpreter.cs +++ b/src/JukaCompiler/Interpreter/JukaInterpreter.cs @@ -15,7 +15,7 @@ internal class JukaInterpreter : Stmt.IVisitor, Expr.IVisitor private readonly ServiceProvider serviceProvider; private readonly JukaEnvironment globals; private JukaEnvironment environment; - private readonly Dictionary locals = []; + private readonly Dictionary locals = new(); private readonly Stack frames = new(); private readonly string globalScope = "__global__scope__"; private readonly int __max_stack_depth__ = 500; @@ -54,7 +54,7 @@ internal void Interpret(List 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); } @@ -87,7 +87,7 @@ internal void ExecuteBlock(List statements, JukaEnvironment env) } Stmt Stmt.IVisitor.VisitBlockStmt(Stmt.Block stmt) { - ExecuteBlock(stmt.statements, new(environment)); + ExecuteBlock(stmt.statements, new(this.environment)); return new Stmt.DefaultStatement(); } @@ -113,8 +113,8 @@ Stmt Stmt.IVisitor.VisitClassStmt(Stmt.Class stmt) environment.Define("super", superclass); } - Dictionary functions = []; - foreach(var method in stmt.methods) + Dictionary functions = new(); + foreach (var method in stmt.methods) { JukaFunction jukaFunction = new(method, environment, false); functions.Add(method.StmtLexemeName, jukaFunction); @@ -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); } @@ -197,9 +197,9 @@ private Stmt.Print VisitPrintAllInternal(Expr expr, Action 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); } } } @@ -220,7 +220,10 @@ private void PrintLiteralExpr(Expr expr, Action printAction) } } - private void PrintLiteral(Expr.Literal expr, Action printAction) => printAction(expr.ExpressionLexemeName); + private void PrintLiteral(Expr.Literal expr, Action printAction) + { + printAction(expr.ExpressionLexemeName); + } private void PrintLiteral(Expr.LexemeTypeLiteral expr, Action printAction) { @@ -249,7 +252,7 @@ private bool PrintVariableLookUp(Expr.Variable expr, Action printTypeAct if (o != null) printTypeAction(o); } - + if (stackVariable.Value is Expr.Literal literal) { printTypeAction(literal.ExpressionLexeme?.ToString()!); @@ -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(); @@ -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) { @@ -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, @@ -406,7 +409,7 @@ public object VisitAssignExpr(Expr.Assign expr) var lexemeType = assign.ExpressionLexeme!.LexemeType; return ( - literal, + literal, lexemeType); } } @@ -593,53 +596,53 @@ 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 arguments = []; - Dictionary argumentsMap = []; + List arguments = new(); + Dictionary 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; } @@ -647,11 +650,11 @@ private static bool IsEqual(object a, object b) { 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; } @@ -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) @@ -753,7 +756,12 @@ 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"); @@ -761,7 +769,7 @@ public object VisitVariableExpr(Expr.Variable expr) 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; @@ -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) @@ -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); } } @@ -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; } } -} +} \ No newline at end of file diff --git a/src/JukaCompiler/Interpreter/StackFrame.cs b/src/JukaCompiler/Interpreter/StackFrame.cs index 9a800466..fb79e254 100644 --- a/src/JukaCompiler/Interpreter/StackFrame.cs +++ b/src/JukaCompiler/Interpreter/StackFrame.cs @@ -7,33 +7,34 @@ namespace JukaCompiler.Interpreter { internal class StackFrame { - private readonly Dictionary frameVariables = []; - private readonly string frameName; - private readonly Dictionary variables = []; - private readonly Dictionary variableAndKind = []; + private Dictionary frameVariables = new(); + private string frameName; + private Dictionary variables = new(); + private Dictionary variableAndKind = new(); internal StackFrame(string name) { this.frameName = name; } - internal void AddVariables(Dictionary variables) + internal void AddVariables(Dictionary variables, JukaInterpreter interpreter) { foreach (var variable in variables) { string name = variable.Key; - object? value; + object? value = null; if (variable.Value is Expr.Literal) { value = variable.Value; - AddVariable(name, value, variable.Value.GetType(), null); } - else if(variable.Value != null) + else { value = ((Expr.LexemeTypeLiteral)variable.Value).literal; - AddVariable(name, value, variable.Value.GetType(), null); } + + // + AddVariable(name, value, variable.Value.GetType(), null); } } @@ -41,17 +42,23 @@ internal void AddVariables(Dictionary variables) { if (variable.exprInitializer != null) { - object? variableValue = interpreter.Evaluate(variable.exprInitializer) ?? throw new JRuntimeException("The value of the variable is null"); - if (variable.exprInitializer is not Expr.Call) + object? variableValue = interpreter.Evaluate(variable.exprInitializer); + + if (variableValue == null) + { + throw new JRuntimeException("the value of the variable is null"); + } + + if (!(variable.exprInitializer is Expr.Call)) { string variableName = variable.name?.ToString() ?? throw new JRuntimeException("variable name is missing"); - AddVariable(variableName, variableValue, variableValue.GetType(),variable.exprInitializer); + AddVariable(variableName, variableValue, variableValue.GetType(), variable.exprInitializer); } return variableValue; } - throw new JRuntimeException("Unable to add variable to StackFrame"); + throw new JRuntimeException("unable to add variable"); } internal void AddVariable(string name, object? variableValue, Type? variableKind, Expr? expressionContext) @@ -87,16 +94,22 @@ internal bool UpdateVariable(string name, object? value) internal bool DeleteVariable(string name) { - return variables.Remove(name); + if (variables.ContainsKey(name)) + { + variables.Remove(name); + return true; + } + + return false; } internal bool TryGetStackVariableByName(string name, out StackVariableState? variable) { variable = null; - if(variables.ContainsKey(name)) + if (variables.ContainsKey(name)) { - variable = (StackVariableState?) this.variables[name]; + variable = (StackVariableState?)this.variables[name]; return true; } @@ -109,7 +122,7 @@ internal class StackVariableState internal object? Value; internal Type? type; internal Expr? expressionContext; - internal object[] arrayValues = []; + internal object[] arrayValues; } } -} +} \ No newline at end of file