Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        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);
                }
            }
        }
Esempio n. 3
0
        public ParseNode Parse()
        {
            _root = new ParseNode("root");

            EXPRESSION(_root);

            return _root;
        }
Esempio n. 4
0
 public void OPERATOR(ParseNode node)
 {
     node.AddChild(new ParseNode("op", _lookahead.Value));
     Match(TokenType.OPERATOR);
 }
Esempio n. 5
0
 public void IDENTIFIER(ParseNode node)
 {
     node.AddChild(new ParseNode("id", _lookahead.Value));
     Match(TokenType.IDENTIFIER);
 }
Esempio n. 6
0
 public void VALUE(ParseNode node)
 {
     node.AddChild(new ParseNode("val", _lookahead.Value));
     Match(TokenType.VALUE);
 }
Esempio n. 7
0
 public ASTGenerator(ParseNode parseTree)
 {
     AST = Walk(parseTree.Children.First());
 }
Esempio n. 8
0
 public void AddChild(ParseNode child)
 {
     Children.Add(child);
 }