Exemplo n.º 1
0
        private void ParsePrimaryExpression(ref Expression expr)
        {
            Token token = lex.Parse();

            if (Lex.TokenIsOperator(token, OperatorCode.LeftParenthesis))
            {
                Expression v = null;
                ParseAdditiveExpression(ref v);
                token = lex.Parse();
                if (Lex.TokenIsNotOperator(token, OperatorCode.RightParenthesis))
                {
                    //LogStatus(StatusLog.StateType_e.Error, "Paren expression wrong");
                    _errored = true;
                    return;
                }
                //expr = MakeNode(AstNodeType_e.Expression_Paren, v);
                expr = new ParenthesisExpression(v);
            }
            else if (Lex.TokenIsOperator(token, OperatorCode.LeftBrace))
            {
                //string text = lex.GetRawStringUntil('}');
                //token = lex.Parse();
                //ExpressionBrace v = new ExpressionBrace();
                //v.BracedText = MakeNode(AstNodeType_e.RawString, "{" + text + "}");
                //expr = MakeNode(AstNodeType_e.Expression_Brace, v);
                //lex.ConsumeAToken();
            }
            else if (Lex.TokenIs(token, TokenType.Identifier))
            {
                Identifier id  = (Identifier)token.Value;
                Symbol     sym = null;
                if (_symtab.ContainsKey(id.Name))
                {
                    sym = _symtab[id.Name];
                }
                else
                {
                    sym = new Symbol(id.Name, SymbolType.Variable, id);
                    _symtab.Add(id.Name, sym);
                }
                id.SymbolEntry = sym;
                expr           = new IdentifierExpression(id);
            }
            else if (Lex.TokenIs(token, TokenType.Constant))
            {
                expr = new ConstantExpression((Constant)token.Value);
            }
            //else if (Lex.TokenIs(token, TokenType_e.Label))
            //{
            //    //need to check if label re-define
            //    Label label = (Label)token.Value;
            //    Symbol sym;
            //    if (_symtab.Contains(label.Name))
            //    {
            //        sym = _symtab[label.Name];
            //    }
            //    else
            //    {
            //        sym = new Symbol(label.Name, SymbolType_e.Label, label);
            //        _symtab.Add(sym);
            //    }
            //    expr = MakeNode(AstNodeType_e.Label, token.Value);
            //}
            //else
            //{
            //    LogStatus(StatusLog.StateType_e.Error, "Errored");
            //    _errored = true;
            //    return;
            //}
        }
Exemplo 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;
            }
        }
Exemplo 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);
        }