From ad938128efec85168a5217974c326c348afaf85f Mon Sep 17 00:00:00 2001 From: Vardan2009 <70532109+Vardan2009@users.noreply.github.com> Date: Sat, 31 Aug 2024 13:56:35 +0400 Subject: [PATCH] Changed tuples to `ReturnResult` --- eiger/Execution/BuiltInFunctions/Ascii.cs | 7 +- eiger/Execution/BuiltInFunctions/Cls.cs | 7 +- eiger/Execution/BuiltInFunctions/Color.cs | 7 +- eiger/Execution/BuiltInFunctions/Double.cs | 12 +- eiger/Execution/BuiltInFunctions/Emit.cs | 7 +- eiger/Execution/BuiltInFunctions/Emitln.cs | 7 +- eiger/Execution/BuiltInFunctions/Exit.cs | 7 +- eiger/Execution/BuiltInFunctions/Fread.cs | 7 +- eiger/Execution/BuiltInFunctions/In.cs | 7 +- eiger/Execution/BuiltInFunctions/Inchar.cs | 7 +- eiger/Execution/BuiltInFunctions/Int.cs | 12 +- eiger/Execution/BuiltInFunctions/Rand.cs | 7 +- eiger/Execution/BuiltInFunctions/Time.cs | 7 +- eiger/Execution/BuiltInTypes/Class.cs | 4 +- eiger/Execution/Function.cs | 13 +- eiger/Execution/Interpreter.cs | 212 +++++++++++++-------- eiger/Execution/ReturnResult.cs | 10 + eiger/Program.cs | 2 +- 18 files changed, 230 insertions(+), 112 deletions(-) create mode 100644 eiger/Execution/ReturnResult.cs diff --git a/eiger/Execution/BuiltInFunctions/Ascii.cs b/eiger/Execution/BuiltInFunctions/Ascii.cs index 1a7d7c8..da84efe 100644 --- a/eiger/Execution/BuiltInFunctions/Ascii.cs +++ b/eiger/Execution/BuiltInFunctions/Ascii.cs @@ -12,9 +12,12 @@ class AsciiFunction : BuiltInFunction { public AsciiFunction() : base("ascii", ["chr"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); - return (false, false, new Number(filepath, line, pos, ((String)args[0]).value[0])); + return new() + { + result = new Number(filepath, line, pos, ((String)args[0]).value[0]) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Cls.cs b/eiger/Execution/BuiltInFunctions/Cls.cs index 877f654..d186cf0 100644 --- a/eiger/Execution/BuiltInFunctions/Cls.cs +++ b/eiger/Execution/BuiltInFunctions/Cls.cs @@ -11,10 +11,13 @@ class ClsFunction : BuiltInFunction { public ClsFunction() : base("cls", []) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); Console.Clear(); - return (false, false, new Nix(filepath, line, pos)); + return new() + { + result = new Nix(filepath, line, pos) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Color.cs b/eiger/Execution/BuiltInFunctions/Color.cs index 84b83d3..31d032d 100644 --- a/eiger/Execution/BuiltInFunctions/Color.cs +++ b/eiger/Execution/BuiltInFunctions/Color.cs @@ -11,13 +11,16 @@ class ColorFunction : BuiltInFunction { public ColorFunction() : base("color", ["fg", "bg"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); Console.ForegroundColor = (ConsoleColor)((Number)args[0]).value; Console.BackgroundColor = (ConsoleColor)((Number)args[1]).value; Interpreter.fgColor.value = (int)Console.ForegroundColor; Interpreter.bgColor.value = (int)Console.BackgroundColor; - return (false, false, new Nix(filepath, line, pos)); + return new() + { + result = new Nix(filepath, line, pos) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Double.cs b/eiger/Execution/BuiltInFunctions/Double.cs index 1a809ca..00be99b 100644 --- a/eiger/Execution/BuiltInFunctions/Double.cs +++ b/eiger/Execution/BuiltInFunctions/Double.cs @@ -11,18 +11,24 @@ class DoubleFunction : BuiltInFunction { public DoubleFunction() : base("double", ["val"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); if (args[0] is Number n) { - return (false, true, new Number(filepath, line, pos, n.value)); + return new() + { + result = new Number(filepath, line, pos, n.value) + }; } else if (args[0] is BuiltInTypes.String s) { try { - return (false, true, new Number(filepath, line, pos, Convert.ToDouble(s.value))); + return new() + { + result = new Number(filepath, line, pos, Convert.ToDouble(s.value)) + }; } catch (FormatException) { diff --git a/eiger/Execution/BuiltInFunctions/Emit.cs b/eiger/Execution/BuiltInFunctions/Emit.cs index 7d941dc..a634f63 100644 --- a/eiger/Execution/BuiltInFunctions/Emit.cs +++ b/eiger/Execution/BuiltInFunctions/Emit.cs @@ -11,10 +11,13 @@ class EmitFunction : BuiltInFunction { public EmitFunction() : base("emit", ["value"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); Console.Write(args[0]); - return (false, false, new Nix(filepath, line, pos)); + return new() + { + result = new Nix(filepath, line, pos) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Emitln.cs b/eiger/Execution/BuiltInFunctions/Emitln.cs index 72a07b7..95e25c5 100644 --- a/eiger/Execution/BuiltInFunctions/Emitln.cs +++ b/eiger/Execution/BuiltInFunctions/Emitln.cs @@ -11,10 +11,13 @@ class EmitlnFunction : BuiltInFunction { public EmitlnFunction() : base("emitln", ["value"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); Console.WriteLine(args[0]); - return (false, false, new Nix(filepath, line, pos)); + return new() + { + result = new Nix(filepath, line, pos) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Exit.cs b/eiger/Execution/BuiltInFunctions/Exit.cs index c1cc578..2468a9f 100644 --- a/eiger/Execution/BuiltInFunctions/Exit.cs +++ b/eiger/Execution/BuiltInFunctions/Exit.cs @@ -11,10 +11,13 @@ class ExitFunction : BuiltInFunction { public ExitFunction() : base("exit", ["code"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); System.Environment.Exit(Convert.ToInt32(((Number)args[0]).value)); - return (false, false, new Nix(filepath, line, pos)); + return new() + { + result = new Nix(filepath, line, pos) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Fread.cs b/eiger/Execution/BuiltInFunctions/Fread.cs index da1aac6..10e781c 100644 --- a/eiger/Execution/BuiltInFunctions/Fread.cs +++ b/eiger/Execution/BuiltInFunctions/Fread.cs @@ -12,7 +12,7 @@ class FreadFunction : BuiltInFunction { public FreadFunction() : base("fread", ["path"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); if (args[0] is not String) @@ -21,7 +21,10 @@ public override (bool, bool, Value) Execute(List args, int line, int pos, { string pathStr = path.value; if (File.Exists(pathStr)) - return (false, false, new String(filepath, line, pos, File.ReadAllText(pathStr))); + return new() + { + result = new String(filepath, line, pos, File.ReadAllText(pathStr)) + }; else throw new Errors.EigerError(filepath, line, pos, "File doesn't exist", Errors.EigerError.ErrorType.IOError); diff --git a/eiger/Execution/BuiltInFunctions/In.cs b/eiger/Execution/BuiltInFunctions/In.cs index 0128a76..1ef803e 100644 --- a/eiger/Execution/BuiltInFunctions/In.cs +++ b/eiger/Execution/BuiltInFunctions/In.cs @@ -11,9 +11,12 @@ class InFunction : BuiltInFunction { public InFunction() : base("in", []) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); - return (false, true, new BuiltInTypes.String(filepath, line, pos, Console.ReadLine() ?? "")); + return new() + { + result = new BuiltInTypes.String(filepath, line, pos, Console.ReadLine() ?? "") + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Inchar.cs b/eiger/Execution/BuiltInFunctions/Inchar.cs index de1be47..b45173f 100644 --- a/eiger/Execution/BuiltInFunctions/Inchar.cs +++ b/eiger/Execution/BuiltInFunctions/Inchar.cs @@ -11,9 +11,12 @@ class IncharFunction : BuiltInFunction { public IncharFunction() : base("inchar", []) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); - return (false, true, new BuiltInTypes.String(filepath, line, pos, Console.ReadKey().KeyChar.ToString())); + return new() + { + result = new BuiltInTypes.String(filepath, line, pos, Console.ReadKey().KeyChar.ToString()) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Int.cs b/eiger/Execution/BuiltInFunctions/Int.cs index 5ced0ab..d1f50eb 100644 --- a/eiger/Execution/BuiltInFunctions/Int.cs +++ b/eiger/Execution/BuiltInFunctions/Int.cs @@ -11,18 +11,24 @@ class IntFunction : BuiltInFunction { public IntFunction() : base("int", ["val"]) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); if (args[0] is Number n) { - return (false, true, new Number(filepath, line, pos, Convert.ToInt32(n.value))); + return new() + { + result = new Number(filepath, line, pos, Convert.ToInt32(n.value)) + }; } else if (args[0] is BuiltInTypes.String s) { try { - return (false, true, new Number(filepath, line, pos, Convert.ToInt32(Convert.ToDouble(s.value)))); + return new() + { + result = new Number(filepath, line, pos, Convert.ToInt32(Convert.ToDouble(s.value))) + }; } catch (FormatException) { diff --git a/eiger/Execution/BuiltInFunctions/Rand.cs b/eiger/Execution/BuiltInFunctions/Rand.cs index 8829563..a9ab2dd 100644 --- a/eiger/Execution/BuiltInFunctions/Rand.cs +++ b/eiger/Execution/BuiltInFunctions/Rand.cs @@ -12,9 +12,12 @@ class RandFunction : BuiltInFunction public RandFunction() : base("rand", []) { } readonly Random rand = new(); - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); - return (false, false, new Number(filepath, line, pos, rand.NextDouble())); + return new() + { + result = new Number(filepath, line, pos, rand.NextDouble()) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInFunctions/Time.cs b/eiger/Execution/BuiltInFunctions/Time.cs index 39c3290..7aeddd3 100644 --- a/eiger/Execution/BuiltInFunctions/Time.cs +++ b/eiger/Execution/BuiltInFunctions/Time.cs @@ -11,11 +11,14 @@ class TimeFunction : BuiltInFunction { public TimeFunction() : base("time", []) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string filepath) + public override ReturnResult Execute(List args, int line, int pos, string filepath) { CheckArgs(filepath, line, pos, args.Count); DateTimeOffset now = DateTimeOffset.Now; DateTimeOffset epoch = new(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); - return (false, false, new Number(filepath, line, pos, (now - epoch).TotalSeconds)); + return new() + { + result = new Number(filepath, line, pos, (now - epoch).TotalSeconds) + }; } } \ No newline at end of file diff --git a/eiger/Execution/BuiltInTypes/Class.cs b/eiger/Execution/BuiltInTypes/Class.cs index d8840bd..3a64779 100644 --- a/eiger/Execution/BuiltInTypes/Class.cs +++ b/eiger/Execution/BuiltInTypes/Class.cs @@ -20,7 +20,7 @@ public Class(string filename, int line, int pos, string name, Dictionary args) + public ReturnResult Execute(List args) { // create local symbol table Dictionary localSymbolTable = new(symbolTable); @@ -37,7 +37,7 @@ public Class(string filename, int line, int pos, string name, Dictionary args, int line, int pos, string path); // abstract execute function + public abstract ReturnResult Execute(List args, int line, int pos, string path); // abstract execute function } // custom functions @@ -37,7 +37,7 @@ public Function(ASTNode node, string name, List arg_n, ASTNode root, Dic this.root = root; } - public override (bool, bool, Value) Execute(List args, int line, int pos, string path) + public override ReturnResult Execute(List args, int line, int pos, string path) { CheckArgs(path, line, pos, args.Count); @@ -71,7 +71,7 @@ public InlineFunction(ASTNode node, string name, List arg_n, ASTNode roo this.root = root; } - public override (bool, bool, Value) Execute(List args, int line, int pos, string path) + public override ReturnResult Execute(List args, int line, int pos, string path) { CheckArgs(path, line, pos, args.Count); @@ -85,8 +85,9 @@ public override (bool, bool, Value) Execute(List args, int line, int pos, } // visit the body and return the result - (bool shouldBreak, bool shouldReturn, Value v) = Interpreter.VisitNode(root, localSymbolTable); - return (shouldBreak, true, v); + ReturnResult r = Interpreter.VisitNode(root, localSymbolTable); + r.shouldReturn = true; + return r; } public override string ToString() @@ -101,7 +102,7 @@ abstract class BuiltInFunction : BaseFunction { public BuiltInFunction(string name, List arg_n) : base(new(NodeType.Block, 0, 0, 0, ""), name, arg_n, Interpreter.globalSymbolTable) { } - public override (bool, bool, Value) Execute(List args, int line, int pos, string file) { return (false, false, new Nix(file, line, pos)); } + public override ReturnResult Execute(List args, int line, int pos, string file) { return new() { result = new Nix(file, line, pos) }; } public override string ToString() { diff --git a/eiger/Execution/Interpreter.cs b/eiger/Execution/Interpreter.cs index c3465f9..54c2b2f 100644 --- a/eiger/Execution/Interpreter.cs +++ b/eiger/Execution/Interpreter.cs @@ -77,7 +77,7 @@ public static void ResetSymbolTable() => globalSymbolTable = new(defaultGlobalSymbolTable); // function for visiting a node - public static (bool, bool, Value) VisitNode(ASTNode node, Dictionary symbolTable) + public static ReturnResult VisitNode(ASTNode node, Dictionary symbolTable) { switch (node.type) { @@ -92,7 +92,12 @@ public static (bool, bool, Value) VisitNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitReturnNode(ASTNode node, Dictionary symbolTable) { // ReturnNode structure // ReturnNode // -- value - return (false, true, VisitNode(node.children[0], symbolTable).Item3); + return new() + { + result = VisitNode(node.children[0], symbolTable).result, + shouldReturn = true + }; } // visit block node - public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable, Dictionary? parentSymbolTable = null) + public static ReturnResult VisitBlockNode(ASTNode node, Dictionary symbolTable, Dictionary? parentSymbolTable = null) { // BlockNode structure // BlockNode @@ -129,7 +141,7 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitForToNode(ASTNode node, Dictionary symbolTable) { // ForToNode structure // ForToNode @@ -167,8 +182,8 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitWhileNode(ASTNode node, Dictionary symbolTable) { // WhileNode structure // WhileNode @@ -197,25 +215,29 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitIfNode(ASTNode node, Dictionary symbolTable) { // IfNode structure // IfNode @@ -225,7 +247,7 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitLetNode(ASTNode node, Dictionary symbolTable) { // LetNode structure: // LetNode: variableName @@ -275,18 +300,20 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitFuncCallNode(ASTNode node, Dictionary symbolTable) { // FuncCallNode structure: // FuncCallNode @@ -295,7 +322,7 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary args = PrepareArguments(node, symbolTable); @@ -309,7 +336,7 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitDataclassNode(ASTNode node, Dictionary symbolTable) { // DataclassNode structure // DataclassNode : @@ -325,11 +352,11 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitClassNode(ASTNode node, Dictionary symbolTable) { // ClassNode structure // ClassNode : @@ -345,11 +372,11 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitFuncDefNode(ASTNode node, Dictionary symbolTable) { // FuncDefNode structure // FuncDefNode : @@ -391,11 +418,11 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitFuncDefInlineNode(ASTNode node, Dictionary symbolTable) { // VisitFuncDefInlineNode structure // VisitFuncDefInlineNode : @@ -437,17 +464,20 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitUnaryOpNode(ASTNode node, Dictionary symbolTable) { // get unary operator string unaryOp = node.value ?? throw new EigerError(node.filename, node.line, node.pos, "Invalid Unary Operator", EigerError.ErrorType.RuntimeError); // get right side without unary operator - Value rightSide = VisitNode(node.children[0], symbolTable).Item3; + Value rightSide = VisitNode(node.children[0], symbolTable).result; // prepare a variable for return value Value retVal = unaryOp switch { @@ -455,18 +485,21 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary rightSide.Notted(), _ => throw new EigerError(node.filename, node.line, node.pos, "Invalid Unary Operator", EigerError.ErrorType.RuntimeError), }; - return (false, false, retVal); + return new() + { + result = retVal + }; } // visit binary operator node - static (bool, bool, Value) VisitBinOpNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitBinOpNode(ASTNode node, Dictionary symbolTable) { Value leftSide = new Nix(node.filename, node.line, node.pos); if (node.value != "=") - leftSide = VisitNode(node.children[0], symbolTable).Item3; + leftSide = VisitNode(node.children[0], symbolTable).result; - Value rightSide = VisitNode(node.children[1], symbolTable).Item3; + Value rightSide = VisitNode(node.children[1], symbolTable).result; // prepare a variable for return value Value retVal = node.value switch @@ -492,7 +525,10 @@ public static (bool, bool, Value) VisitBlockNode(ASTNode node, Dictionary HandleCompoundAssignment(node, rightSide, symbolTable, (left, right) => left.DivedBy(right)), _ => throw new EigerError(node.filename, node.line, node.pos, $"{Globals.InvalidOperationStr}: {node.value}", EigerError.ErrorType.InvalidOperationError), }; - return (false, false, retVal); + return new() + { + result = retVal + }; } private static Value HandleAssignment(ASTNode node, Value rightSide, Dictionary symbolTable) @@ -518,26 +554,34 @@ private static Value HandleCompoundAssignment(ASTNode node, Value rightSide, Dic // visit literal node (pretty self-explanatory) - static (bool, bool, Value) VisitLiteralNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitLiteralNode(ASTNode node, Dictionary symbolTable) { + Value v; if (node.value is string && node.value == "true") - return (false, false, new Boolean(node.filename, node.line, node.pos, true)); + v = new Boolean(node.filename, node.line, node.pos, true); else if (node.value is string && node.value == "false") - return (false, false, new Boolean(node.filename, node.line, node.pos, false)); + v = new Boolean(node.filename, node.line, node.pos, false); else if (node.value is string && node.value == "nix") - return (false, false, new Nix(node.filename, node.line, node.pos)); + v = new Nix(node.filename, node.line, node.pos); + else v = Value.ToEigerValue(node.filename, node.line, node.pos, node.value); - return (false, false, Value.ToEigerValue(node.filename, node.line, node.pos, node.value)); + return new() + { + result = v + }; } // visit identifier node (pretty self-explanatory too) - static (bool, bool, Value) VisitIdentifierNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitIdentifierNode(ASTNode node, Dictionary symbolTable) { - return (false, false, GetSymbol(symbolTable, node)); + return new() + { + result = GetSymbol(symbolTable, node) + }; } // visit array node - static (bool, bool, Value) VisitArrayNode(ASTNode node, Dictionary symbolTable) + static ReturnResult VisitArrayNode(ASTNode node, Dictionary symbolTable) { // ArrayNode structure // ArrayNode @@ -548,12 +592,15 @@ private static Value HandleCompoundAssignment(ASTNode node, Value rightSide, Dic List list = []; foreach (ASTNode item in node.children) { - list.Add(VisitNode(item, symbolTable).Item3); + list.Add(VisitNode(item, symbolTable).result); } - return (false, false, new Array(node.filename, node.line, node.pos, [.. list])); + return new() + { + result = new Array(node.filename, node.line, node.pos, [.. list]) + }; } - private static (bool, bool, Value) VisitAttrAccessNode(ASTNode node, Dictionary symbolTable) + private static ReturnResult VisitAttrAccessNode(ASTNode node, Dictionary symbolTable) { // AttrAccessNode structure // AttrAccessNode @@ -567,7 +614,7 @@ private static (bool, bool, Value) VisitAttrAccessNode(ASTNode node, Dictionary< { if (currentNode.type != NodeType.AttrAccess) { - v = VisitNode(node.children[0], symbolTable).Item3.GetAttr(node.children[1]); + v = VisitNode(node.children[0], symbolTable).result.GetAttr(node.children[1]); break; } currentNode = currentNode.children[1]; @@ -581,39 +628,51 @@ private static (bool, bool, Value) VisitAttrAccessNode(ASTNode node, Dictionary< List args = []; // prepare a list for args foreach (ASTNode child in currentNode.children) { - Value val = VisitNode(child, symbolTable).Item3 ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); + Value val = VisitNode(child, symbolTable).result ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); args.Add(val); } - return (false, false, f.Execute(args, node.line, node.pos, node.filename).Item3); + return new() + { + result = f.Execute(args, node.line, node.pos, node.filename).result + }; } else if (v is Class c && currentNode.type == NodeType.FuncCall) { List args = []; // prepare a list for args foreach (ASTNode child in currentNode.children) { - Value val = VisitNode(child, symbolTable).Item3 ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); + Value val = VisitNode(child, symbolTable).result ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); args.Add(val); } - return (false, false, c.Execute(args).Item3); + return new() + { + result = c.Execute(args).result + }; } else - return (false, false, v); + return new() + { + result = v + }; } - private static (bool, bool, Value) VisitElementAccessNode(ASTNode node, Dictionary symbolTable) + private static ReturnResult VisitElementAccessNode(ASTNode node, Dictionary symbolTable) { // ElementAccessNode structure // ElementAccessNode // -- value // -- idx - Value iter = VisitNode(node.children[0], symbolTable).Item3; - Value value = VisitNode(node.children[1], symbolTable).Item3; + Value iter = VisitNode(node.children[0], symbolTable).result; + Value value = VisitNode(node.children[1], symbolTable).result; try { int idx = Convert.ToInt32(((Number)(value ?? throw new EigerError(node.filename, node.line, node.pos, "Index is unknown", EigerError.ErrorType.ParserError))).value); - return (false, false, iter.GetIndex(idx)); + return new() + { + result = iter.GetIndex(idx) + }; } catch (IndexOutOfRangeException) { @@ -621,7 +680,7 @@ private static (bool, bool, Value) VisitElementAccessNode(ASTNode node, Dictiona } } - private static (bool, bool, Value) VisitIncludeNode(ASTNode node, Dictionary symbolTable) + private static ReturnResult VisitIncludeNode(ASTNode node, Dictionary symbolTable) { // IncludeNode structure // IncludeNode @@ -659,7 +718,10 @@ private static (bool, bool, Value) VisitIncludeNode(ASTNode node, Dictionary symbolTable, ASTNode key Value list = GetSymbol(symbolTable, listNode); - int idx = (int)((Number)VisitNode(idxNode, symbolTable).Item3).value; + int idx = (int)((Number)VisitNode(idxNode, symbolTable).result).value; return list.GetIndex(idx); } @@ -711,7 +773,7 @@ static List PrepareArguments(ASTNode node, Dictionary symb List args = new List(); for (int i = 1; i < node.children.Count; i++) { - Value val = VisitNode(node.children[i], symbolTable).Item3 ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); + Value val = VisitNode(node.children[i], symbolTable).result ?? throw new EigerError(node.filename, node.line, node.pos, Globals.ArgumentErrorStr, EigerError.ErrorType.ArgumentError); args.Add(val); } return args; @@ -747,7 +809,7 @@ public static void SetSymbol(Dictionary symbolTable, ASTNode key, ASTNode listNode = key.children[0]; ASTNode idxNode = key.children[1]; Value list = GetSymbol(symbolTable, listNode); - int idx = (int)((Number)VisitNode(idxNode, symbolTable).Item3).value; + int idx = (int)((Number)VisitNode(idxNode, symbolTable).result).value; list.SetIndex(idx, value); } else if (key.type == NodeType.Identifier) diff --git a/eiger/Execution/ReturnResult.cs b/eiger/Execution/ReturnResult.cs new file mode 100644 index 0000000..1ef9976 --- /dev/null +++ b/eiger/Execution/ReturnResult.cs @@ -0,0 +1,10 @@ +using EigerLang.Execution.BuiltInTypes; + +namespace EigerLang.Execution +{ + public class ReturnResult + { + public required Value result; + public bool shouldReturn, shouldBreak, shouldContinue; + } +} diff --git a/eiger/Program.cs b/eiger/Program.cs index f0416c4..80209fc 100644 --- a/eiger/Program.cs +++ b/eiger/Program.cs @@ -78,7 +78,7 @@ public static void Execute(string src, string fn, bool printExprs) foreach (var statement in root.children) { - (bool shouldBreak, bool didReturn, Value val) = Interpreter.VisitNode(statement, Interpreter.globalSymbolTable); + Value val = Interpreter.VisitNode(statement, Interpreter.globalSymbolTable).result; if (printExprs) { string v = Convert.ToString(val) ?? "";