private ASTNode Walk(ParseNode node) { if(node.Children.Count == 0) { switch (node.Name) { case "id": return new IDNode(node.Value); case "val": return new ValueNode(node.Value); } } var left = Walk(node.Children[0]); var right = Walk(node.Children[2]); switch(node.Children[1].Value) { case "is": return new IsNode(left, right); case "not": return new NotNode(left, right); case "and": return new AndNode(left, right); case "or": return new OrNode(left, right); } throw new NotImplementedException(); }
public void EXPRESSION(ParseNode node) { var expressionNode = new ParseNode("expr"); node.AddChild(expressionNode); while (_lookahead.Type != TokenType.CLOSE_PARENTHESIS && _lookahead.Type != TokenType.EOF) { switch (_lookahead.Type) { case TokenType.OPEN_PARENTHESIS: Match(TokenType.OPEN_PARENTHESIS); EXPRESSION(expressionNode); Match(TokenType.CLOSE_PARENTHESIS); continue; case TokenType.IDENTIFIER: IDENTIFIER(expressionNode); continue; case TokenType.OPERATOR: OPERATOR(expressionNode); continue; case TokenType.VALUE: VALUE(expressionNode); continue; default: throw new Exception("Unexpected token, found " + _lookahead.Type); } } }
public ParseNode Parse() { _root = new ParseNode("root"); EXPRESSION(_root); return _root; }
public void OPERATOR(ParseNode node) { node.AddChild(new ParseNode("op", _lookahead.Value)); Match(TokenType.OPERATOR); }
public void IDENTIFIER(ParseNode node) { node.AddChild(new ParseNode("id", _lookahead.Value)); Match(TokenType.IDENTIFIER); }
public void VALUE(ParseNode node) { node.AddChild(new ParseNode("val", _lookahead.Value)); Match(TokenType.VALUE); }
public ASTGenerator(ParseNode parseTree) { AST = Walk(parseTree.Children.First()); }
public void AddChild(ParseNode child) { Children.Add(child); }