From 0277bb61be6730c75f112a6165deb53fea626802 Mon Sep 17 00:00:00 2001 From: DemonExposer Date: Sat, 16 Jul 2022 15:28:29 +0200 Subject: [PATCH] Implemented while loops and refactored --- Parser.cs | 8 ++++---- Program.cs | 2 +- Tokens/Statements/OnStatement.cs | 25 ++----------------------- Tokens/Statements/Statement.cs | 24 ++++++++++++++++++++++++ Tokens/Statements/WhileLoop.cs | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 28 deletions(-) create mode 100644 Tokens/Statements/WhileLoop.cs diff --git a/Parser.cs b/Parser.cs index 8d6f371..b01369c 100644 --- a/Parser.cs +++ b/Parser.cs @@ -236,12 +236,12 @@ public static Token Parse(Token[] line, int i, List> vars } else if (t is VariableToken vt) { // TODO: make sure multiple arguments get parsed properly if (i + 1 < line.Length && line[i+1] is ParenthesesOperator) vt.Args = Parse(line, i + 1, vars, lines, ref lineNo, depth + 1); - } else if (t is OnStatement onS) { + } else if (t is Statement statement) { Token left = Parse(line, i + 1, vars, lines, ref lineNo, depth + 1); if (left is not ParenthesesOperator po) - throw new FormatException("if statement condition on line " + left.Line + " is missing parentheses"); + throw new FormatException("statement condition on line " + left.Line + " is missing parentheses"); - onS.Left = po; + statement.Left = po; int numBrackets = 0; int j = i+1; @@ -254,7 +254,7 @@ public static Token Parse(Token[] line, int i, List> vars j++; } while (numBrackets != 0); - onS.Right = CurlyBracketsParse(lines, ref lineNo, vars, depth + 1); + statement.Right = CurlyBracketsParse(lines, ref lineNo, vars, depth + 1); } t.Line = line[i].Line; diff --git a/Program.cs b/Program.cs index 299a2e5..d758b11 100644 --- a/Program.cs +++ b/Program.cs @@ -9,7 +9,6 @@ using Boolean = Interpreter.Types.Comparable.Boolean; using Object = Interpreter.Types.Object; using TrieDictionary; -using System.Text.RegularExpressions; using Interpreter.Tokens.Operators.N_Ary; namespace Interpreter; @@ -53,6 +52,7 @@ public static void Main(string[] args) { // Statements bindings.Insert("on", typeof(OnStatement)); + bindings.Insert("while", typeof(WhileLoop)); // Low number for priority means a higher priority priorities.Insert("(", 0); diff --git a/Tokens/Statements/OnStatement.cs b/Tokens/Statements/OnStatement.cs index 2ebb1e8..38703fb 100644 --- a/Tokens/Statements/OnStatement.cs +++ b/Tokens/Statements/OnStatement.cs @@ -1,33 +1,12 @@ -using System.Text; -using Interpreter.Tokens.Operators.Unary; using Boolean = Interpreter.Types.Comparable.Boolean; using Object = Interpreter.Types.Object; namespace Interpreter.Tokens.Statements; public class OnStatement : Statement { - public string Symbol = "on"; - public ParenthesesOperator Left = null!; - public Token Right = null!; - - public override string ToString(int indent) { - StringBuilder sb = new StringBuilder(); - - StringBuilder indentSb = new StringBuilder(); - for (int i = 0; i < indent; i++) - indentSb.Append('\t'); - string indentStr = indentSb.ToString(); - - sb.Append(indentStr).Append(Symbol).Append('\n'); - if (Left != null!) - sb.Append(Left.ToString(indent+1)); - if (Right != null!) - sb.Append(Right.ToString(indent+1)); - - return sb.ToString(); + public OnStatement() { + Symbol = "on"; } public override Object Evaluate() => ((Boolean) Left.Evaluate()).Bool ? Right.Evaluate() : null!; - - public override int Size() => 1 + Left.Size() + Right.Size(); } \ No newline at end of file diff --git a/Tokens/Statements/Statement.cs b/Tokens/Statements/Statement.cs index 2501fbe..dfdca4b 100644 --- a/Tokens/Statements/Statement.cs +++ b/Tokens/Statements/Statement.cs @@ -1,5 +1,29 @@ +using System.Text; +using Interpreter.Tokens.Operators.Unary; + namespace Interpreter.Tokens.Statements; public abstract class Statement : Token { + public string Symbol = null!; + public ParenthesesOperator Left = null!; + public Token Right = null!; + + public override string ToString(int indent) { + StringBuilder sb = new StringBuilder(); + + StringBuilder indentSb = new StringBuilder(); + for (int i = 0; i < indent; i++) + indentSb.Append('\t'); + string indentStr = indentSb.ToString(); + + sb.Append(indentStr).Append(Symbol).Append('\n'); + if (Left != null!) + sb.Append(Left.ToString(indent+1)); + if (Right != null!) + sb.Append(Right.ToString(indent+1)); + + return sb.ToString(); + } + public override int Size() => 1 + Left.Size() + Right.Size(); } \ No newline at end of file diff --git a/Tokens/Statements/WhileLoop.cs b/Tokens/Statements/WhileLoop.cs new file mode 100644 index 0000000..ff346a9 --- /dev/null +++ b/Tokens/Statements/WhileLoop.cs @@ -0,0 +1,17 @@ +using Object = Interpreter.Types.Object; +using Boolean = Interpreter.Types.Comparable.Boolean; + +namespace Interpreter.Tokens.Statements; + +public class WhileLoop : Statement { + public WhileLoop() { + Symbol = "while"; + } + + public override Object Evaluate() { + while (((Boolean) Left.Evaluate()).Bool) + Right.Evaluate(); + + return null!; + } +} \ No newline at end of file