コード例 #1
0
    ASTExpression ParseExpressionImpl()
    {
        Token first = lexer.NextToken();

        switch (first.Type)
        {
        case TokenType.True:
            return(new ASTLiteral(true));

        case TokenType.False:
            return(new ASTLiteral(false));

        case TokenType.Number:
            if (first.Source.Contains("."))
            {
                return(new ASTLiteral(float.Parse(first.Source)));
            }
            try {
                int i = int.Parse(first.Source);
            } catch (System.Exception e) {
                Debug.LogError("Integer Invalid: " + first.Source + " len: " + first.Source.Length);
            }
            return(new ASTLiteral(int.Parse(first.Source)));

        case TokenType.String:
            return(new ASTLiteral(first.Source));

        case TokenType.Identifier:
        {
            if (lexer.PeekNext() == TokenType.BracketOpen)
            {
                ASTFuncCall funcCall = new ASTFuncCall(first.Source);

                lexer.NextToken();         // Eat open bracket

                while (true)
                {
                    if (lexer.PeekNext() == TokenType.BracketClose)
                    {
                        lexer.NextToken();
                        break;
                    }

                    ASTExpression arg = ParseExpression();
                    funcCall.AddArg(arg);

                    if (lexer.PeekNext() == TokenType.BracketClose)
                    {
                        lexer.NextToken();
                        break;
                    }

                    MatchNext(TokenType.Comma);
                }

                return(funcCall);
            }
            return(new ASTVariable(first.Source));
        }

        default:
            throw new Exception("Unexpected token " + first);
        }
    }
コード例 #2
0
    ASTStatement ParseStatement()
    {
        if (lexer.PeekNext() == TokenType.Identifier)  // Function call
        {
            ASTFuncCall funcCall = ParseExpression() as ASTFuncCall;
            return(new ASTInlineCall(funcCall));
        }

        Token first = lexer.NextToken();

        switch (first.Type)
        {
        case TokenType.Set:
        {
            Token varNameToken            = MatchNext(TokenType.Identifier);
            List <ASTExpression> indexers = new List <ASTExpression>();

            while (lexer.PeekNext() == TokenType.SquareBraceOpen)
            {
                MatchNext(TokenType.SquareBraceOpen);

                indexers.Add(ParseExpression());

                MatchNext(TokenType.SquareBraceClose);
            }

            MatchNext(TokenType.To);

            ASTExpression expression = ParseExpression();

            return(new ASTSet(varNameToken.Source, expression, indexers));
        }

        case TokenType.If:
        {
            ASTExpression check      = ParseExpression();
            ASTStatements statements = new ASTStatements();
            ASTIf         ifStmnt    = new ASTIf(check, statements);

            MatchNext(TokenType.Do);

            while (lexer.PeekNext() != TokenType.End)
            {
                if (lexer.PeekNext() == TokenType.Else)
                {
                    lexer.NextToken();

                    statements = new ASTStatements();
                    ifStmnt.SetElse(statements);

                    MatchNext(TokenType.Do);
                }
                else if (lexer.PeekNext() == TokenType.Elif)
                {
                    lexer.NextToken();

                    statements = new ASTStatements();
                    ASTIf elseifStmnt = new ASTIf(ParseExpression(), statements);

                    ifStmnt.SetElseIf(elseifStmnt);

                    MatchNext(TokenType.Do);
                }
                statements.AddStatement(ParseStatement());
            }

            lexer.NextToken();

            return(ifStmnt);
        }

        case TokenType.While:
        {
            ASTExpression check      = ParseExpression();
            ASTStatements statements = new ASTStatements();
            ASTWhile      whileStmnt = new ASTWhile(check, statements);

            MatchNext(TokenType.Do);

            while (lexer.PeekNext() != TokenType.End)
            {
                statements.AddStatement(ParseStatement());
            }

            lexer.NextToken();

            return(whileStmnt);
        }

        case TokenType.Return:
        {
            return(new ASTReturn(ParseExpression()));
        }

        default:
            throw new Exception("Unexpected token " + first);
        }

        return(null);
    }
コード例 #3
0
 public ASTInlineCall(ASTFuncCall funcCall)
 {
     this.funcCall = funcCall;
 }