Ejemplo n.º 1
0
        /// <summary>Parses an input line containing an intermediate expression term .</summary>
        private InpErrorCodes ParseTerm(string[] tok)
        {
            string s = "";

            // --- get term's name

            if (tok.Length < 2)
            {
                return(0);
            }
            int i = _project.MSXproj_findObject(ObjectTypes.TERM, tok[0]);

            // --- reconstruct the expression string from its tokens

            for (int j = 1; j < tok.Length; j++)
            {
                s += tok[j];
            }

            // --- convert expression into a postfix stack of op codes

            //expr = mathexpr_create(s, getVariableCode);
            MathExpr expr = MathExpr.Create(s, GetVariableCode);

            if (expr == null)
            {
                return(InpErrorCodes.ERR_MATH_EXPR);
            }

            // --- assign the expression to a Term object

            _msx.Term[i].Expr = expr;
            return(0);
        }
Ejemplo n.º 2
0
        public bool IsFullVector()
        {
            return(MathExpr.ListQ());

            //return
            //    this.MathExpr.Head.ToString() == FunctionNames.List &&
            //    this.Evaluator.EvaluateFunctionIsTrue(FunctionNames.VectorQ, this.MathExpr);
        }
Ejemplo n.º 3
0
        public bool IsConstant()
        {
            if (MathExpr.SymbolQ() == false)
            {
                return(false);
            }

            var exprText = MathExpr.ToString();

            return(exprText == "True" || exprText == "False");
        }
Ejemplo n.º 4
0
        public bool IsConstant(bool value)
        {
            if (MathExpr.SymbolQ() == false)
            {
                return(false);
            }

            return
                (value
                ? MathExpr.ToString() == "True"
                : MathExpr.ToString() == "False");
        }
Ejemplo n.º 5
0
 public bool IsFullMatrix()
 {
     return(MathExpr.ListQ());
 }
Ejemplo n.º 6
0
 public bool IsConstantFalse()
 {
     return(MathExpr.SymbolQ() && MathExpr.ToString() == "False");
 }
Ejemplo n.º 7
0
 public bool IsConstantTrue()
 {
     return(MathExpr.SymbolQ() && MathExpr.ToString() == "True");
 }
Ejemplo n.º 8
0
 public bool IsMinusOne()
 {
     return(MathExpr.NumberQ() && MathExpr.ToString() == "-1");
 }
Ejemplo n.º 9
0
 public bool IsZero()
 {
     return(MathExpr.NumberQ() && MathExpr.ToString() == "0");
 }
Ejemplo n.º 10
0
        /// <summary>Parses an input line containing a math expression.</summary>
        private InpErrorCodes ParseExpression(ObjectTypes classType, string[] tok)
        {
            string s = "";

            // --- determine expression type

            if (tok.Length < 3)
            {
                return(InpErrorCodes.ERR_ITEMS);
            }
            int k = Utilities.MSXutils_findmatch(tok[0], Constants.ExprTypeWords);

            if (k < 0)
            {
                return(InpErrorCodes.ERR_KEYWORD);
            }

            // --- determine species associated with expression

            int i = _project.MSXproj_findObject(ObjectTypes.SPECIES, tok[1]);

            if (i < 1)
            {
                return(InpErrorCodes.ERR_NAME);
            }

            // --- check that species does not already have an expression

            if (classType == ObjectTypes.LINK)
            {
                if (_msx.Species[i].PipeExprType != ExpressionType.NO_EXPR)
                {
                    return(InpErrorCodes.ERR_DUP_EXPR);
                }
            }

            if (classType == ObjectTypes.TANK)
            {
                if (_msx.Species[i].TankExprType != ExpressionType.NO_EXPR)
                {
                    return(InpErrorCodes.ERR_DUP_EXPR);
                }
            }

            // --- reconstruct the expression string from its tokens

            for (int j = 2; j < tok.Length; j++)
            {
                s += tok[j];
            }

            // --- convert expression into a postfix stack of op codes

            //expr = mathexpr_create(s, getVariableCode);
            MathExpr expr = MathExpr.Create(s, GetVariableCode);

            if (expr == null)
            {
                return(InpErrorCodes.ERR_MATH_EXPR);
            }

            // --- assign the expression to the species

            switch (classType)
            {
            case ObjectTypes.LINK:
                _msx.Species[i].PipeExpr     = expr;
                _msx.Species[i].PipeExprType = (ExpressionType)k;
                break;

            case ObjectTypes.TANK:
                _msx.Species[i].TankExpr     = expr;
                _msx.Species[i].TankExprType = (ExpressionType)k;
                break;
            }
            return(0);
        }
Ejemplo n.º 11
0
        public override IExpr Build(BuildContext ctx, IExprBuilder initiator, Func <IExpr> next)
        {
            if (ctx.Token is ArithmeticSymbolToken t)
            {
                if (initiator is MathExprBuilder || ctx.BuiltExprs.Count == 0)
                {
                    return(null);
                }

                var prevExpr = ctx.LastExpr;

                IMathExprSuperior superior = null;
                if (prevExpr is IMathExprSuperior)
                {
                    superior = (IMathExprSuperior)prevExpr;
                    prevExpr = superior.B;
                }

                if (!Helpers.TypesMatch(prevExpr.ReturnType, Enums.Type.Number))
                {
                    return(null);
                }

                ctx.NextIndex();

                var nextExpr = next();
                ThrowIfExprIsNull(nextExpr, t);

                if (!Helpers.TypesMatch(nextExpr.ReturnType, Enums.Type.Number))
                {
                    if (_applyStringConcatOptimization)
                    {
                        if (t.Operation == ArithmeticOperation.Add)
                        {
                            // `StringConcatExprBuilder` will only build an expr if A has a string return type
                            if (nextExpr.ReturnType == Enums.Type.String)
                            {
                                ctx.PopExpr();

                                return(new StringConcatExpr(Env)
                                {
                                    Token = t, A = prevExpr, B = nextExpr,
                                });
                            }
                        }
                    }

                    throw new CodeBuildEx(t.IdxS, t.IdxE,
                                          string.Format(tr.operator__0__cannot_be_applied_to_operands_of_type__1,
                                                        t,
                                                        nextExpr.ReturnType
                                                        ));
                }

                ctx.PopExpr();

                var mathExpr = new MathExpr(Env)
                {
                    Token = t, A = prevExpr, B = nextExpr
                };

                if (prevExpr is MathExpr prevMathExpr &&
                    Helpers.GetArithmeticOperationPriority(t.Operation) >
                    Helpers.GetArithmeticOperationPriority(prevMathExpr.Token.Operation)
                    )
                {
                    // rearrange expressions based on operation priority: e.g. "((1 + 1) * 10)" -> "(1 + (1 * 10))"
                    mathExpr.A     = prevMathExpr.B;
                    prevMathExpr.B = mathExpr;

                    // keep previous math expression as the principal one
                    mathExpr = prevMathExpr;
                }

                if (superior != null)
                {
                    superior.B = mathExpr;
                    return(superior);
                }
                else
                {
                    return(mathExpr);
                }
            }

            return(null);
        }