Beispiel #1
0
        private void ParsePostfixExpression(ref Expression expr)
        {
            Expression primary = null;

            ParsePrimaryExpression(ref primary);

            if (_errored)
            {
                return;
            }
            if (primary == null)
            {
                return;
            }

            Token token = lex.PreviewNextToken();

            if (Lex.TokenIsOperator(token, OperatorCode.LeftParenthesis))
            {
                if (primary.Type != ExpressionType.IdentifierExpression)
                {
                    //LogStatus(StatusLog.StateType_e.Error, "");
                    _errored = true;
                    return;
                }

                Symbol symbol = ((IdentifierExpression)primary).Identifier.SymbolEntry;

                if (symbol.Type != SymbolType.Function)
                {
                    _errored = true;
                    return;
                }


                Function           func     = (Function)symbol.Value;
                List <Expression>  args     = ParseFunctionArgs(func);
                FuncCallExpression funcCall = new FuncCallExpression(func, args);


                expr = funcCall;
                return;
            }

            expr = primary;
        }
Beispiel #2
0
        private void Solve(Expression expr)
        {
            if (expr == null)
            {
                return;
            }
            string s = string.Empty;

            switch (expr.Type)
            {
            case ExpressionType.IdentifierExpression:
            {
                IdentifierExpression e = (IdentifierExpression)expr;
                double v = _valueTable[e.Identifier.Name];
                _evStack.Push(v);
            }
            break;

            case ExpressionType.ConstantExpression:
            {
                ConstantExpression e = (ConstantExpression)expr;
                _evStack.Push(e.Constant.Value);
            }
            break;

            case ExpressionType.ParenthesisExpression:
            {
                ParenthesisExpression e = (ParenthesisExpression)expr;
                Solve(e.Expression);
            }
            break;

            case ExpressionType.FuncCallExpression:
            {
                FuncCallExpression e = (FuncCallExpression)expr;

                foreach (Expression arg in e.Arguments)
                {
                    Solve(arg);
                }

                List <double> vArgs = new List <double>();
                for (int i = 0; i < e.Arguments.Count; i++)
                {
                    vArgs.Add(_evStack.Pop());
                }
                vArgs.Reverse();

                double v = FuncCall(e.Function, vArgs);
                _evStack.Push(v);
            }
            break;

            case ExpressionType.ExponentialExpression: break;

            case ExpressionType.NegativeExpression:
            {
                NegativeExpression e = (NegativeExpression)expr;

                Solve(e.Expression);
                double v = _evStack.Pop() * -1;
                _evStack.Push(v);
            }
            break;

            case ExpressionType.PositiveExpression:
            {
                PositiveExpression e = (PositiveExpression)expr;

                Solve(e.Expression);
                double v = _evStack.Pop() * -1;
                _evStack.Push(v);
            }
            break;

            case ExpressionType.AdditionExpression:
            {
                AdditionExpression e = (AdditionExpression)expr;

                Solve(e.Left);
                Solve(e.Right);
                double r = _evStack.Pop();
                double l = _evStack.Pop();
                _evStack.Push(l + r);
            }
            break;

            case ExpressionType.SubtractionExpression:
            {
                SubtractionExpression e = (SubtractionExpression)expr;

                Solve(e.Left);
                Solve(e.Right);
                double r = _evStack.Pop();
                double l = _evStack.Pop();
                _evStack.Push(l - r);
            }
            break;

            case ExpressionType.MultiplicationExpression:
            {
                MultiplicationExpression e = (MultiplicationExpression)expr;

                Solve(e.Left);
                Solve(e.Right);
                double r = _evStack.Pop();
                double l = _evStack.Pop();
                _evStack.Push(l * r);
            }
            break;

            case ExpressionType.DivisionExpression:
            {
                DivisionExpression e = (DivisionExpression)expr;

                Solve(e.Left);
                Solve(e.Right);
                double r = _evStack.Pop();
                double l = _evStack.Pop();
                _evStack.Push(l / r);
            }
            break;

            case ExpressionType.EquationExpression:
            {
                EquationExpression e = (EquationExpression)expr;

                Solve(e.Right);
                double v = _evStack.Pop();
                if (e.Left is IdentifierExpression)
                {
                    _valueTable[((IdentifierExpression)e.Left).Identifier.Name] = v;
                }
            }
            break;

            case ExpressionType.ExpressionGroup:
            {
                ExpressionGroup e = (ExpressionGroup)expr;
                foreach (Expression ie in e.Expressions)
                {
                    Solve(ie);
                }
            }
            break;

            default:
                break;
            }
        }