Exemple #1
0
 private static AstNode ParseTerm(Parser.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.AcceptToken(TokenType.Bracket, "["))
     {
         AstNode statement = ArrayInitializerNode.Parse(parser);
         parser.ExpectToken(TokenType.Bracket, "]");
         return(statement);
     }
     else if (parser.MatchToken(TokenType.String))
     {
         return(new StringNode(parser.ExpectToken(TokenType.String).Value.ToString()));
     }
     else if (parser.MatchToken(TokenType.Identifier))
     {
         return(new IdentifierNode(parser.ExpectToken(TokenType.Identifier).Value.ToString()));
     }
     else
     {
         throw new Exception("Unexpected in Parser: " + parser.CurrentToken().Value);
     }
 }
Exemple #2
0
 public static AstNode Parse(Parser.Parser parser)
 {
     if (parser.MatchToken(TokenType.Identifier, "if"))
     {
         return(IfNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "while"))
     {
         return(WhileNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "for"))
     {
         return(ForNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "foreach"))
     {
         return(ForEachNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "try"))
     {
         return(TryNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "func"))
     {
         return(FuncNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "thread"))
     {
         return(ThreadNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Identifier, "return"))
     {
         return(ReturnNode.Parse(parser));
     }
     else if (parser.MatchToken(TokenType.Brace, "{"))
     {
         return(CodeBlock.Parse(parser));
     }
     else
     {
         AstNode expr = ExpressionNode.Parse(parser);
         parser.ExpectToken(TokenType.EndOfLine);
         return(expr);
     }
 }
Exemple #3
0
        public static AstNode Parse(Parser.Parser parser)
        {
            CodeBlock block = new CodeBlock();

            parser.ExpectToken(TokenType.Brace, "{");

            while (!parser.EndOfStream && !parser.MatchToken(TokenType.Brace, "}"))
            {
                block.Children.Add(StatementNode.Parse(parser));
            }

            parser.ExpectToken(TokenType.Brace, "}");
            return(block);
        }
Exemple #4
0
        public static ArgListNode Parse(Parser.Parser parser)
        {
            ArgListNode ret = new ArgListNode();

            parser.ExpectToken(TokenType.Parentheses, "(");

            while (!parser.MatchToken(TokenType.Parentheses, ")"))
            {
                ret.Children.Add(ExpressionNode.Parse(parser));
                if (!parser.AcceptToken(TokenType.Comma))
                {
                    break;
                }
            }
            parser.ExpectToken(TokenType.Parentheses, ")");

            return(ret);
        }
Exemple #5
0
        public static AstNode Parse(Parser.Parser parser)
        {
            parser.ExpectToken(TokenType.Identifier, "func");
            string name = parser.ExpectToken(TokenType.Identifier).Value.ToString();

            parser.ExpectToken(TokenType.Parentheses, "(");

            List <string> result = new List <string>();

            while (parser.MatchToken(TokenType.Identifier))
            {
                result.Add(parser.ExpectToken(TokenType.Identifier).Value.ToString());
                if (!parser.AcceptToken(TokenType.Comma))
                {
                    break;
                }
            }

            parser.ExpectToken(TokenType.Parentheses, ")");
            AstNode body = StatementNode.Parse(parser);

            return(new FuncNode(name, result, body));
        }