Ejemplo n.º 1
0
        public void PrintExpressionGroup(ExpressionGroup group)
        {
            _exprText = string.Empty;

            _indentLevel++;
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 3
0
        public string GenerateCode(Expression expr)
        {
            string s = string.Empty;

            switch (expr.Type)
            {
            case ExpressionType.IdentifierExpression:
            {
                IdentifierExpression e = (IdentifierExpression)expr;
                s += e.Identifier.ToString();
            }
            break;

            case ExpressionType.ConstantExpression:
            {
                ConstantExpression e = (ConstantExpression)expr;
                s += e.Constant.Value.ToString();
            }
            break;

            case ExpressionType.ParenthesisExpression:
            {
                ParenthesisExpression e = (ParenthesisExpression)expr;
                s += "(" + GenerateCode(e.Expression) + ")";
            }
            break;

            case ExpressionType.FuncCallExpression: break;

            case ExpressionType.ExponentialExpression: break;

            case ExpressionType.NegativeExpression:
            {
                NegativeExpression e = (NegativeExpression)expr;
                s += "-" + GenerateCode(e.Expression);
            }
            break;

            case ExpressionType.PositiveExpression:
            {
                PositiveExpression e = (PositiveExpression)expr;
                s += "+" + GenerateCode(e.Expression);
            }
            break;

            case ExpressionType.AdditionExpression:
            case ExpressionType.SubtractionExpression:
            case ExpressionType.MultiplicationExpression:
            case ExpressionType.DivisionExpression:
            {
                BinaryExpression e = (BinaryExpression)expr;
                s += GenerateCode(e.Left) + " " + Statics.OperatorTextTable[e.Operator] + " " + GenerateCode(e.Right);
            }
            break;

            case ExpressionType.EquationExpression:
            {
                EquationExpression e = (EquationExpression)expr;
                s += GenerateCode(e.Left) + " " + Statics.OperatorTextTable[e.Operator] + " " + GenerateCode(e.Right);
            }
            break;

            case ExpressionType.ExpressionGroup:
            {
                ExpressionGroup e = (ExpressionGroup)expr;
                foreach (Expression ie in e.Expressions)
                {
                    s += GenerateCode(ie) + "\r\n";
                }
            }
            break;

            default:
                break;
            }
            return(s);
        }