public bool InterpreteNextAST() { if (Input.IsEnd()) { return(false); } try { switch (Input.Peek().ASTType) { // Valid cases... case AST.Classification.Say: InterpreteSay(); return(true); case AST.Classification.Set: InterpreteSet(); return(true); case AST.Classification.Choice: InterpreteChoice(); return(true); case AST.Classification.Label: InterpreteLabel(); return(true); case AST.Classification.Jump: InterpreteJump(); return(true); case AST.Classification.Return: InterpreteReturn(); return(true); case AST.Classification.LineBreak: break; // Some invalid cases... Not a statement. case AST.Classification.Identifier: throw new InvalidStatementPassedException("An identifier cannot be used as a statement."); case AST.Classification.String: throw new InvalidStatementPassedException("A string cannot be used as a statement."); case AST.Classification.Boolean: throw new InvalidStatementPassedException("A boolean value cannot be used as a statement."); case AST.Classification.Number: throw new InvalidStatementPassedException("A number value cannot be used as a statement."); case AST.Classification.Of: throw new InvalidStatementPassedException("Of statements cannot be used alone. They must be used with Set statements."); default: throw new InvalidStatementPassedException($"Invalid statement AST {Input.Peek().ASTType} was passed. An element such as Number cannot be a statement."); } if (Input.Peek().ASTType != AST.Classification.LineBreak) { throw new InvalidStatementPassedException("Each statements must be separated by line break."); } Input.Read(); // Remove the line break AST } catch (Exception e) { OutputManager.Exception(e); } return(true); }
public AST ParseNextToken() { if (Input.IsEnd()) { return(null); } AST ParsedAST = null; // Keyword Parsing if (IsKeyword("set")) { ParsedAST = ParseSet(); } else if (IsKeyword("true") || IsKeyword("false")) { ParsedAST = ParseBoolean(); } else if (IsKeyword("choice")) { ParsedAST = ParseChoice(); } else if (IsKeyword("label")) { ParsedAST = ParseLabel(); } else if (IsKeyword("return") || IsKeyword("done")) { ParsedAST = ParseReturn(); } else if (IsKeyword("Jump")) { ParsedAST = ParseJump(); } // Literals and Identifiers else if (Input.Peek().TokenType == Token.Classification.Number) { ParsedAST = ParseNumber(); } else if (Input.Peek().TokenType == Token.Classification.String) { ParsedAST = ParseString(); } else if (Input.Peek().TokenType == Token.Classification.Identifier) { ParsedAST = Maybe(ParseSay, ParseOf); } else if (Input.Peek().TokenType == Token.Classification.LineBreak) { ParsedAST = ParseLineBreak(); } // Highly unlikely, but just in case... else { throw new UnexpectedTokenException($"The type of provided token is unknown ({Input.Peek().TokenType}). What have you done."); } return(ParsedAST); }