public List<Statement> Parse() { List<Statement> statements = new List<Statement>(); while(true) { while (Match(TokenType.LINE)) ; if (Match("PRINT")) { statements.Add(new PrintStatement(Exp())); } else if (Match("INPUT")) { statements.Add(new InputStatement(Consume(TokenType.IDENTIFIER).Text, chameleon)); } else if (Match("GOTO")) { statements.Add(new GotoStatement(Consume(TokenType.IDENTIFIER).Text, chameleon)); } else if (Match("IF")) { Expression condition = Exp(); Consume("THEN"); string label = Consume(TokenType.IDENTIFIER).Text; statements.Add(new IfThenStatement(condition, label, chameleon)); } else if (Match("EXIT")) { statements.Add(new ExitStatement()); } else if (Match("CLEAR")) { statements.Add(new ClearStatement()); } else if (Match("WAIT")) { statements.Add(new WaitStatement()); } else if (Match("TITLE")) { statements.Add(new TitleStatement(Consume(TokenType.STRING).Text)); } else if (Match(TokenType.LABEL)) { chameleon.labels[Last(1).Text] = statements.Count; } else if (Match(TokenType.IDENTIFIER, TokenType.ASSIGN)) { string name = Last(2).Text; string op = Last(1).Text; Expression value = Exp(); statements.Add(new AssignStatement(name, value, chameleon, op)); } else if (Match(TokenType.IDENTIFIER, TokenType.SPE_OPERATOR)) { string name = Last(2).Text; string op = Last(1).Text; Expression value = new NumberValue(1); statements.Add(new AssignStatement(name, value, chameleon, op)); } else break; } return statements; }
private Expression Op() { Expression expression = Atomic(); while(Match(TokenType.OPERATOR) || Match(TokenType.COMP_OPERATOR)) { string op = Last(1).Text; Expression right; if (op == "++" || op == "--") right = new NumberValue(1); else right = Atomic(); expression = new OperatorExpression(expression, op, right); } return expression; }