Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from DemonExposer/loops
Browse files Browse the repository at this point in the history
Implemented while loops and refactored
  • Loading branch information
DemonExposer authored Jul 16, 2022
2 parents 2645d02 + 0277bb6 commit b7165dd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
8 changes: 4 additions & 4 deletions Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ public static Token Parse(Token[] line, int i, List<TrieDictionary<Object>> 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;
Expand All @@ -254,7 +254,7 @@ public static Token Parse(Token[] line, int i, List<TrieDictionary<Object>> 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;
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 2 additions & 23 deletions Tokens/Statements/OnStatement.cs
Original file line number Diff line number Diff line change
@@ -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();
}
24 changes: 24 additions & 0 deletions Tokens/Statements/Statement.cs
Original file line number Diff line number Diff line change
@@ -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();
}
17 changes: 17 additions & 0 deletions Tokens/Statements/WhileLoop.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}

0 comments on commit b7165dd

Please sign in to comment.