public static InputNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "Input"); ArgListNode variables = ArgListNode.Parse(parser); return new InputNode(variables); }
public static PrgmNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "Prgm"); string prgmPath = parser.ExpectToken(TokenType.String).Value; return new PrgmNode(prgmPath); }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static DispNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "Disp"); ArgListNode args = ArgListNode.Parse(parser); return new DispNode(args); }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static AstNode Parse(Parser parser) { if (parser.MatchToken(TokenType.Identifier, "If")) return ConditionalNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "While")) return WhileNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "Repeat")) return RepeatNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "For")) return ForNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "Disp")) return DispNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "Input")) return InputNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "Prompt")) return PromptNode.Parse(parser); else if (parser.MatchToken(TokenType.Identifier, "Prgm")) return PrgmNode.Parse(parser); else if (parser.AcceptToken(TokenType.Identifier, "Lbl")) return new LblNode(parser.ExpectToken(TokenType.Identifier).Value); else if (parser.AcceptToken(TokenType.Identifier, "Goto")) return new GotoNode(parser.ExpectToken(TokenType.Identifier).Value); else return ExpressionNode.Parse(parser); }
private static AstNode parseAdditive(Parser parser) { AstNode left = parseMultiplicitive(parser); while (parser.MatchToken(TokenType.Operation) || parser.MatchToken(TokenType.Identifier, "and") || parser.MatchToken(TokenType.Identifier, "or")) { switch ((string)parser.CurrentToken().Value) { case "+": parser.AcceptToken(TokenType.Operation); left = new BinaryOpNode(BinaryOperation.Addition, left, parseMultiplicitive(parser)); continue; case "-": parser.AcceptToken(TokenType.Operation); left = new BinaryOpNode(BinaryOperation.Subtraction, left, parseMultiplicitive(parser)); continue; case "and": parser.AcceptToken(TokenType.Identifier); left = new BinaryOpNode(BinaryOperation.And, left, parseMultiplicitive(parser)); continue; case "or": parser.AcceptToken(TokenType.Identifier); left = new BinaryOpNode(BinaryOperation.Or, left, parseMultiplicitive(parser)); continue; default: break; } break; } return left; }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static RepeatNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "Repeat"); AstNode predicate = ExpressionNode.Parse(parser); AstNode body = StatementNode.Parse(parser); return new RepeatNode(predicate, body); }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static ArgListNode Parse(Parser parser) { ArgListNode ret = new ArgListNode(); ret.Children.Add(ExpressionNode.Parse(parser)); while (parser.AcceptToken(TokenType.Comma)) ret.Children.Add(ExpressionNode.Parse(parser)); return ret; }
private static AstNode parseAssignment(Parser parser) { AstNode left = parseAdditive(parser); if (parser.AcceptToken(TokenType.Assignment)) return new BinaryOpNode(BinaryOperation.Assignment, left, parseAssignment(parser)); else return left; }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static AstNode Parse(Parser parser) { CodeBlockNode block = new CodeBlockNode(); while (!parser.EndOfStream) block.Children.Add(StatementNode.Parse(parser)); return block; }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static ConditionalNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "If"); AstNode predicate = ExpressionNode.Parse(parser); AstNode body = StatementNode.Parse(parser); if (parser.MatchToken(TokenType.Identifier, "Else")) return new ConditionalNode(predicate, body, StatementNode.Parse(parser)); return new ConditionalNode(predicate, body); }
public static ForNode Parse(Parser parser) { parser.ExpectToken(TokenType.Identifier, "For"); parser.ExpectToken(TokenType.Parentheses, "("); string variable = parser.ExpectToken(TokenType.Identifier).Value; parser.ExpectToken(TokenType.Comma); AstNode start = ExpressionNode.Parse(parser); parser.ExpectToken(TokenType.Comma); AstNode end = ExpressionNode.Parse(parser); AstNode step = new NumberNode(1); if (parser.AcceptToken(TokenType.Comma)) step = ExpressionNode.Parse(parser); parser.ExpectToken(TokenType.Parentheses, ")"); AstNode body = StatementNode.Parse(parser); return new ForNode(variable, start, end, step, body); }
private static AstNode parseComparison(Parser parser) { AstNode left = parseFunctionCall(parser); while (parser.MatchToken(TokenType.Comparison)) { switch ((string)parser.CurrentToken().Value) { case "=": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.Equal, left, parseFunctionCall(parser)); continue; case "!=": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.NotEqual, left, parseFunctionCall(parser)); continue; case ">": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.GreaterThan, left, parseFunctionCall(parser)); continue; case "<": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.LessThan, left, parseFunctionCall(parser)); continue; case ">=": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.GreaterThanOrEqual, left, parseFunctionCall(parser)); continue; case "<=": parser.AcceptToken(TokenType.Comparison); left = new BinaryOpNode(BinaryOperation.LessThanOrEqual, left, parseFunctionCall(parser)); continue; default: break; } break; } return left; }
private static AstNode parseFunctionCall(Parser parser) { return parseFunctionCall(parser, parseTerm(parser)); }
private static AstNode parseFunctionCall(Parser parser, AstNode left) { if (parser.MatchToken(TokenType.Parentheses, "(")) { var functionCall = parseFunctionCall(parser, new FunctionCallNode(left, ArgListNode.Parse(parser))); //parser.ExpectToken(TokenType.Parentheses, ")"); return functionCall; } else return left; }
private static AstNode parseMultiplicitive(Parser parser) { AstNode left = parseComparison(parser); while (parser.MatchToken(TokenType.Operation)) { switch ((string)parser.CurrentToken().Value) { case "*": parser.AcceptToken(TokenType.Operation); left = new BinaryOpNode(BinaryOperation.Multiplication, left, parseComparison(parser)); continue; case "/": parser.AcceptToken(TokenType.Operation); left = new BinaryOpNode(BinaryOperation.Division, left, parseComparison(parser)); continue; case "%": parser.AcceptToken(TokenType.Operation); left = new BinaryOpNode(BinaryOperation.Modulus, left, parseComparison(parser)); continue; default: break; } break; } return left; }
/// <summary> /// Parse the specified parser. /// </summary> /// <param name="parser">Parser.</param> public static AstNode Parse(Parser parser) { return parseAssignment(parser); }
private static AstNode parseTerm(Parser parser) { if (parser.MatchToken(TokenType.Number)) return new NumberNode(Convert.ToDouble(parser.ExpectToken(TokenType.Number).Value)); else if (parser.AcceptToken(TokenType.Parentheses, "(")) { AstNode statement = ExpressionNode.Parse(parser); parser.ExpectToken(TokenType.Parentheses, ")"); return statement; } else if (parser.MatchToken(TokenType.Identifier, "Then")) { CodeBlockNode block = new CodeBlockNode(); parser.ExpectToken(TokenType.Identifier, "Then"); while (!parser.EndOfStream && !parser.MatchToken(TokenType.Identifier, "EndIf") && !parser.MatchToken(TokenType.Identifier, "Else")) block.Children.Add(StatementNode.Parse(parser)); if (parser.MatchToken(TokenType.Identifier, "Else")) return block; parser.ExpectToken(TokenType.Identifier, "EndIf"); return block; } else if (parser.MatchToken(TokenType.Identifier, "Else")) { parser.ExpectToken(TokenType.Identifier, "Else"); return StatementNode.Parse(parser); } else if (parser.MatchToken(TokenType.Identifier, "Do")) { CodeBlockNode block = new CodeBlockNode(); parser.ExpectToken(TokenType.Identifier, "Do"); while (!parser.EndOfStream && !parser.MatchToken(TokenType.Identifier, "End")) block.Children.Add(StatementNode.Parse(parser)); parser.ExpectToken(TokenType.Identifier, "End"); return block; } else if (parser.MatchToken(TokenType.String)) return new StringNode((string)parser.ExpectToken(TokenType.String).Value); else if (parser.MatchToken(TokenType.Identifier)) return new IdentifierNode((string)parser.ExpectToken(TokenType.Identifier).Value); else throw new Exception("Unexpected " + parser.CurrentToken().TokenType + " in Parser: " + parser.CurrentToken().Value + "."); }