Beispiel #1
0
        /// <summary>
        /// 执行一个表达式
        /// </summary>
        /// <param name="Expression">表达式语法树</param>
        /// <returns>执行结果</returns>
        public object ExecExpression(ASTNode_Expression Expression)
        {
            switch (Expression.Type)
            {
            case ASTNode.ASTType.BinaryExpression:
            {
                ASTNode_BinaryExpression tBinaryExpression = (ASTNode_BinaryExpression)Expression;
                object tLeftResult  = ExecExpression(tBinaryExpression.Left);
                object tRightResult = ExecExpression(tBinaryExpression.Right);
                return(applyBinaryOperator(tBinaryExpression.BinaryOperator, tLeftResult, tRightResult, Expression.LineNumber));
            }

            case ASTNode.ASTType.UnaryExpression:
            {
                ASTNode_UnaryExpression tUnaryExpression = (ASTNode_UnaryExpression)Expression;
                object tRightResult = ExecExpression(tUnaryExpression.Right);
                return(applyUnaryOperator(tUnaryExpression.UnaryOperator, tRightResult, Expression.LineNumber));
            }

            case ASTNode.ASTType.DigitLiteral:
            {
                ASTNode_DigitLiteral tDigitLiteral = (ASTNode_DigitLiteral)Expression;
                return(tDigitLiteral.Value);
            }

            case ASTNode.ASTType.CallExpression:
            {
                ASTNode_CallExpression tCallExpression = (ASTNode_CallExpression)Expression;
                return(applyCallOperator(
                           tCallExpression.Identifier,
                           tCallExpression.IdentifierLower,
                           tCallExpression.ArgList,
                           Expression.LineNumber
                           ));
            }

            case ASTNode.ASTType.SymbolExpression:
            {
                ASTNode_SymbolExpression tSymbolExpression = (ASTNode_SymbolExpression)Expression;
                return(fetchValueOfIdentifier(
                           tSymbolExpression.Identifier,
                           tSymbolExpression.IdentifierLower,
                           Expression.LineNumber
                           ));
            }

            case ASTNode.ASTType.TupleExpression:
            {
                ASTNode_TupleExpression tTupleExpression = (ASTNode_TupleExpression)Expression;
                return(calcuTupleValue(tTupleExpression, Expression.LineNumber));
            }

            default:
                throw new RuntimeException(Expression.LineNumber, "internal error.");
            }
        }
Beispiel #2
0
        /// <summary>
        /// 解析原子表达式
        /// </summary>
        /// <param name="Lex">词法分析器</param>
        /// <returns>解析结果</returns>
        private static ASTNode_Expression ParseAtomExpression(Lexer Lex)
        {
            ASTNode_Expression tRet;

            if (Lex.CurrentToken == Lexer.Token.DigitLiteral)  // digit_literal
            {
                tRet = new ASTNode_DigitLiteral(Lex.Line, Lex.CurrentDigit);
                Lex.Next();
                return(tRet);
            }
            else if (Lex.CurrentToken == Lexer.Token.Identifier)  // symbol or call_expression
            {
                string tIdentifier = MatchIdentifier(Lex);

                // 检查下一个符号
                if (TryMatchToken(Lex, Lexer.Token.LeftBracket))  // '(' -- call_expression
                {
                    ASTNode_ArgList tArgList = ParseArgList(Lex);
                    MatchToken(Lex, Lexer.Token.RightBracket);  // ')'
                    return(new ASTNode_CallExpression(Lex.Line, tIdentifier, tArgList));
                }
                else  // symbol
                {
                    return(new ASTNode_SymbolExpression(Lex.Line, tIdentifier));
                }
            }
            else if (TryMatchToken(Lex, Lexer.Token.LeftBracket))  // '(' -- bracket_expression
            {
                tRet = ParseBracketExpression(Lex);

                MatchToken(Lex, Lexer.Token.RightBracket);  // ')'
                return(tRet);
            }
            else
            {
                throw new SyntaxException(Lex.Position, Lex.Line, Lex.Row,
                                          String.Format("unexpected token {0}.", Lex.FormatCurrentToken()));
            }
        }