Beispiel #1
0
        public Expression ParseFactor()
        {
            Expression exp;
            switch (_currentToken.TokenType)
            {
                case AphidTokenType.LeftBrace:
                    exp = ParseObjectExpression();
                    break;

                case AphidTokenType.LeftBracket:
                    exp = ParseArrayExpression();
                    break;

                case AphidTokenType.LeftParenthesis:
                    NextToken();
                    exp = ParseExpression();
                    Match(AphidTokenType.RightParenthesis);
                    break;

                case AphidTokenType.String:
                    exp = ParseStringExpression();
                    break;

                case AphidTokenType.Number:
                    exp = ParseNumberExpression();
                    break;

                case AphidTokenType.Identifier:
                    exp = ParseIdentifierExpression();
                    break;

                case AphidTokenType.functionOperator:
                    exp = ParseFunctionExpression();
                    break;

                case AphidTokenType.forKeyword:
                    exp = ParseForExpression();
                    break;

                case AphidTokenType.retKeyword:
                    exp = ParseReturnExpression();
                    break;

                case AphidTokenType.trueKeyword:
                    exp = new BooleanExpression(true);
                    NextToken();
                    break;

                case AphidTokenType.falseKeyword:
                    exp = new BooleanExpression(false);
                    NextToken();
                    break;

                case AphidTokenType.thisKeyword:
                    exp = new ThisExpression();
                    NextToken();
                    break;

                case AphidTokenType.ifKeyword:
                    exp = ParseIfExpression();
                    break;

                case AphidTokenType.LoadScriptOperator:
                    exp = ParseLoadScriptExpression();
                    break;

                case AphidTokenType.LoadLibraryOperator:
                    exp = ParseLoadLibraryExpression();
                    break;

                case AphidTokenType.nullKeyword:
                    exp = new NullExpression();
                    NextToken();
                    break;

                case AphidTokenType.breakKeyword:
                    exp = new BreakExpression();
                    NextToken();
                    break;

                case AphidTokenType.HexNumber:
                    exp = new NumberExpression((decimal)Convert.ToInt64(_currentToken.Lexeme.Substring(2), 16));
                    NextToken();
                    break;

                case AphidTokenType.PatternMatchingOperator:
                    var matchExp = new PatternMatchingExpression();
                    NextToken();
                    Match(AphidTokenType.LeftParenthesis);
                    matchExp.TestExpression = ParseExpression();
                    Match(AphidTokenType.RightParenthesis);

                    while (true)
                    {
                        var tests = new List<Expression>();

                        while (true)
                        {
                            tests.Add(ParseExpression());

                            if (_currentToken.TokenType == AphidTokenType.Comma)
                            {
                                NextToken();
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (_currentToken.TokenType == AphidTokenType.ColonOperator)
                        {
                            NextToken();

                            var b = ParseExpression();

                            foreach (var t in tests)
                            {
                                matchExp.Patterns.Add(new Tuple<Expression, Expression>(t, b));
                            }
                        }
                        else
                        {
                            matchExp.Patterns.Add(new Tuple<Expression,Expression>(null, tests[0]));
                        }

                        if (_currentToken.TokenType == AphidTokenType.Comma)
                        {
                            NextToken();
                        }
                        else
                        {
                            break;
                        }
                    }

                    exp = matchExp;
                    break;

                default:
                    throw new AphidParserException(_currentToken);
            }

            return exp;
        }
        public Expression ParseFactor()
        {
            Expression exp;
            switch (_currentToken.TokenType)
            {
                case AphidTokenType.LeftBrace:
                    exp = ParseObjectExpression();
                    break;

                case AphidTokenType.LeftBracket:
                    exp = ParseArrayExpression();
                    break;

                case AphidTokenType.LeftParenthesis:
                    NextToken();
                    exp = ParseExpression();
                    Match(AphidTokenType.RightParenthesis);
                    break;

                case AphidTokenType.String:
                    exp = ParseStringExpression();
                    break;

                case AphidTokenType.Number:
                    exp = ParseNumberExpression();
                    break;

                case AphidTokenType.Identifier:
                    exp = ParseIdentifierExpression();
                    break;

                case AphidTokenType.functionOperator:
                    exp = ParseFunctionExpression();
                    break;

                case AphidTokenType.forKeyword:
                    exp = ParseForExpression();
                    break;

                case AphidTokenType.retKeyword:
                    exp = ParseReturnExpression();
                    break;

                case AphidTokenType.trueKeyword:
                    exp = new BooleanExpression(true);
                    NextToken();
                    break;

                case AphidTokenType.falseKeyword:
                    exp = new BooleanExpression(false);
                    NextToken();
                    break;

                case AphidTokenType.thisKeyword:
                    exp = new ThisExpression();
                    NextToken();
                    break;

                case AphidTokenType.ifKeyword:
                    exp = ParseIfExpression();
                    break;

                case AphidTokenType.LoadScriptOperator:
                    exp = ParseLoadScriptExpression();
                    break;

                case AphidTokenType.LoadLibraryOperator:
                    exp = ParseLoadLibraryExpression();
                    break;

                case AphidTokenType.nullKeyword:
                    exp = new NullExpression();
                    NextToken();
                    break;

                case AphidTokenType.breakKeyword:
                    exp = new BreakExpression();
                    NextToken();
                    break;

                case AphidTokenType.HexNumber:
                    exp = new NumberExpression((decimal)Convert.ToInt64(_currentToken.Lexeme.Substring(2), 16));
                    NextToken();
                    break;

                default:
                    throw new AphidParserException(_currentToken);
            }

            switch (_currentToken.TokenType)
            {
                case AphidTokenType.LeftBracket:
                    if (exp is ArrayExpression)
                    {
                        break;
                    }

                    NextToken();
                    var key = ParseExpression();
                    Match(AphidTokenType.RightBracket);
                    exp = new ArrayAccessExpression(exp, key);
                    break;
            }

            return exp;
        }
Beispiel #3
0
 private CodeExpression GenerateImperativeExpression(BooleanExpression node)
 {
     return node.Value ? CodeHelper.True() : CodeHelper.False();
 }
 private AphidObject InterpretBooleanExpression(BooleanExpression expression)
 {
     return new AphidObject(expression.Value);
 }
Beispiel #5
0
 private AphidExpression ParseFactorExpression()
 {
     AphidExpression exp = default(AphidExpression);
     if ((this._currentToken.TokenType == AphidTokenType.LeftBrace)) {
         exp = ParseObjectExpression();
     }
     else {
         if ((this._currentToken.TokenType == AphidTokenType.LeftBracket)) {
             exp = ParseArrayExpression();
         }
         else {
             if ((this._currentToken.TokenType == AphidTokenType.LeftParenthesis)) {
                 NextToken();
                 exp = ParseExpression();
                 Match(AphidTokenType.RightParenthesis);
             }
             else {
                 if ((this._currentToken.TokenType == AphidTokenType.String)) {
                     exp = ParseStringExpression();
                 }
                 else {
                     if ((this._currentToken.TokenType == AphidTokenType.Number)) {
                         exp = ParseNumberExpression();
                     }
                     else {
                         if ((this._currentToken.TokenType == AphidTokenType.Identifier)) {
                             exp = ParseIdentifierExpression();
                             if ((this._currentToken.TokenType == AphidTokenType.definedKeyword)) {
                                 NextToken();
                                 exp = new UnaryOperatorExpression(AphidTokenType.definedKeyword, exp, true);
                             }
                         }
                         else {
                             if ((this._currentToken.TokenType == AphidTokenType.functionOperator)) {
                                 exp = ParseFunctionExpression();
                             }
                             else {
                                 if (((this._currentToken.TokenType == AphidTokenType.retKeyword)
                                             || (this._currentToken.TokenType == AphidTokenType.deleteKeyword))) {
                                     exp = ParseUnaryExpression();
                                 }
                                 else {
                                     if ((this._currentToken.TokenType == AphidTokenType.trueKeyword)) {
                                         exp = new BooleanExpression(true);
                                         NextToken();
                                     }
                                     else {
                                         if ((this._currentToken.TokenType == AphidTokenType.falseKeyword)) {
                                             exp = new BooleanExpression(false);
                                             NextToken();
                                         }
                                         else {
                                             if ((this._currentToken.TokenType == AphidTokenType.thisKeyword)) {
                                                 exp = new ThisExpression();
                                                 NextToken();
                                             }
                                             else {
                                                 if ((this._currentToken.TokenType == AphidTokenType.LoadScriptOperator)) {
                                                     exp = ParseLoadScriptExpression();
                                                 }
                                                 else {
                                                     if ((this._currentToken.TokenType == AphidTokenType.LoadLibraryOperator)) {
                                                         exp = ParseLoadLibraryExpression();
                                                     }
                                                     else {
                                                         if ((this._currentToken.TokenType == AphidTokenType.nullKeyword)) {
                                                             exp = new NullExpression();
                                                             NextToken();
                                                         }
                                                         else {
                                                             if ((this._currentToken.TokenType == AphidTokenType.breakKeyword)) {
                                                                 exp = new BreakExpression();
                                                                 NextToken();
                                                             }
                                                             else {
                                                                 if ((this._currentToken.TokenType == AphidTokenType.HexNumber)) {
                                                                     exp = new NumberExpression(System.Convert.ToInt64(_currentToken.Lexeme.Substring(2), 16));
                                                                     NextToken();
                                                                 }
                                                                 else {
                                                                     if ((this._currentToken.TokenType == AphidTokenType.BinaryNumber)) {
                                                                         exp = new NumberExpression(BinaryNumber.Parse(_currentToken.Lexeme.Substring(2)));
                                                                         NextToken();
                                                                     }
                                                                     else {
                                                                         if ((this._currentToken.TokenType == AphidTokenType.PatternMatchingOperator)) {
                                                                             exp = ParsePatternMatchingExpression();
                                                                         }
                                                                         else {
                                                                             throw new AphidParserException(_currentToken);
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return exp;
 }