Ejemplo n.º 1
0
        public static DecimalVar op_If(BooleanVar a, DecimalVar b, DecimalVar c)
        {
            if (a.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(a.name, "'If'-function");
            }

            return(new DecimalVar(a.v ? b.v : c.v));
        }
Ejemplo n.º 2
0
 public static StringVar op_ToString(DecimalVar a)
 {
     if (a.empty)
     {
         return(new StringVar());
     }
     else
     {
         return(new StringVar(a.v.ToString()));
     }
 }
Ejemplo n.º 3
0
        public static void /*DecimalVar*/ op_AssignDivision(DecimalVar a, DecimalVar b)
        {
            if (b.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(b.name, "'/='-operator");
            }

            a.v    /= b.v;
            a.empty = false;
            return;
        }
Ejemplo n.º 4
0
        public static DecimalVar op_Round(DecimalVar a, DecimalVar b)
        {
            if (a.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(a.name, "'Round'-function");
            }
            if (b.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(b.name, "'Round'-function");
            }

            return(new DecimalVar(Decimal.Round(a.v, (int)b.v)));
        }
Ejemplo n.º 5
0
        public static void /*DecimalVar*/ op_AssignAddition(DecimalVar a, DecimalVar b)
        {
            if (b.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(b.name, "'+='-operator");
            }

            if (!b.empty)
            {
                a.v    += b.v;
                a.empty = false;
            }
        }
Ejemplo n.º 6
0
        public static DecimalVar op_ToDecimal(StringVar a)
        {
            if (a.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(a.name, "'Decimal'-function");
            }

            DecimalVar r = new DecimalVar();

            Decimal.TryParse(a.v, out r.v);

            return(r);
        }
Ejemplo n.º 7
0
        public static void /*DecimalVar*/ op_AssignSubtraction(DecimalVar a, DecimalVar b)
        {
            if (b.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(b.name, "'-='-operator");
            }

            if (!b.empty)
            {
                a.v    -= b.v;
                a.empty = false;
            }
            //return a;
        }
Ejemplo n.º 8
0
 public static StringVar op_Substring(StringVar what, DecimalVar from, DecimalVar into)
 {
     if (from.v + into.v <= what.v.Length)
     {
         return(new StringVar(what.v.Substring((int)from.v, (int)into.v)));
     }
     else if (from.v < what.v.Length)
     {
         return(new StringVar(what.v.Substring((int)from.v)));
     }
     else
     {
         return(new StringVar());
     }
 }
Ejemplo n.º 9
0
        public static StringVar op_ShiftRight(StringVar a, DecimalVar b)
        {
            if (b.empty)
            {
                throw new EmptyValueNotSuitableForOperationException(b.name, "'>>'-operator");
            }

            string extraspace = "";

            if ((int)b.v - a.v.Length > 0)
            {
                extraspace = new string(' ', (int)b.v - a.v.Length);
            }

            return(new StringVar(extraspace + a.v));
        }
Ejemplo n.º 10
0
 public static DecimalVar op_Max(DecimalVar a, DecimalVar b)
 {
     // Alles is groter dan empty
     if (a.empty)
     {
         if (b.empty)
         {
             return(new DecimalVar());
         }
         else
         {
             return(new DecimalVar(b));
         }
     }
     else
     if (b.empty)
     {
         return(new DecimalVar(a));
     }
     else
     {
         return(new DecimalVar(a.v > b.v ? a.v : b.v));
     }
 }
Ejemplo n.º 11
0
 public static DecimalVar op_Min(DecimalVar a, DecimalVar b)
 {
     // Alles is kleiner dan empty, lijkt mij een handige feature
     if (a.empty)
     {
         if (b.empty)
         {
             return(new DecimalVar());
         }
         else
         {
             return(new DecimalVar(b));
         }
     }
     else
     if (b.empty)
     {
         return(new DecimalVar(a));
     }
     else
     {
         return(new DecimalVar(a.v < b.v ? a.v : b.v));
     }
 }
Ejemplo n.º 12
0
 public DecimalVar(DecimalVar d)
 {
     name  = d.name;
     v     = d.v;
     empty = d.empty;
 }
Ejemplo n.º 13
0
 public static BooleanVar op_Empty(DecimalVar a)
 {
     return(new BooleanVar(a.empty));
 }
Ejemplo n.º 14
0
 public static DecimalVar op_Max(DecimalVar a, DecimalVar b, DecimalVar c)
 {
     return(op_Max(a, op_Max(b, c)));
 }
Ejemplo n.º 15
0
 public static void /*DecimalVar*/ op_Assign(DecimalVar a, DecimalVar b)
 {
     a.v     = b.v;
     a.empty = b.empty;
 }
Ejemplo n.º 16
0
        public Var Evaluate(string expression)
        {
            System.Globalization.NumberFormatInfo
                numberformat = new System.Globalization.NumberFormatInfo();
            numberformat.NumberDecimalSeparator = ".";

            int i;

            mOperands  = new Stack();
            mOperators = new Stack();

            // Simulate expression in brackets

            PushOperator(new OperatorInfo("(", -1, ""));
            expression = expression + ").";

            bool bLastTokenWasOperand
                = false;

            while (expression != ".")
            {
                // Allow for continue from multiple levels.
                bool bGotoNextPart
                    = false;

                // first eat all spaces
                if (Char.IsWhiteSpace(expression[0]))
                {
                    do
                    {
                        expression = expression.Remove(0, 1);
                    } while (Char.IsWhiteSpace(expression[0]));
                    continue;
                }

                // Look for an operator. If found process it...
                foreach (OperatorInfo op in OperatorInfo.cvOperators.Values)
                {
                    if (expression.StartsWith(op.name) && (!op.IsText || !Char.IsLetter(expression[op.name.Length])))
                    {
                        if (op.name == ")" || op.name == ",")
                        {
                            // Make sure any awaiting operations are processed first
                            do
                            {
                                ProcessOperator();
                            } while (mTopOperator.name != "(" && mTopOperator.name != ",");
                            // Now push the ) and process the () or function call.
                            PushOperator(op);
                            ProcessOperator();
                        }
                        //else if (!bLastTokenWasOperand && !op.function)
                        //{
                        //    throw new OperandExpectedException(0, expression.Substring(0, expression.Length - 2));
                        //}
                        else if ((op.precedence > mTopOperator.precedence) ||
                                 (mTopOperator.name == "("))
                        {
                            // Next operator is higher in precedence. Process it first.
                            PushOperator(op);
                        }
                        else
                        {
                            do
                            {
                                ProcessOperator();
                            } while (op.precedence <= mTopOperator.precedence && mTopOperator.name != "(" && mTopOperator.name != ",");
                            PushOperator(op);
                        }
                        bLastTokenWasOperand = false;
                        expression           = expression.Remove(0, op.name.Length);
                        bGotoNextPart        = true;
                        break;
                    }
                }
                if (bGotoNextPart)
                {
                    continue;
                }

                if (bLastTokenWasOperand)
                {
                    throw new Exceptions.OperatorExpectedException(null, expression.Substring(0, expression.Length - 2));
                }

                // Now check to see if first part of expression is a number
                if (Char.IsDigit(expression, 0))
                {
                    DecimalVar v = new DecimalVar();
                    i = 0;
                    // Eat the integer part of the number
                    do
                    {
                        i++;
                    } while (Char.IsDigit(expression, i));
                    if (expression[i] == '.')
                    {
                        // It is a decimal
                        do
                        {
                            i++;
                        } while (Char.IsDigit(expression, i));
                        v.v = Decimal.Parse(expression.Substring(0, i), System.Globalization.NumberStyles.Number, numberformat);
                    }
                    else
                    {
                        // It is an integer
                        v.v = Int32.Parse(expression.Substring(0, i));
                    }
                    v.empty = false;
                    mOperands.Push(v);
                    expression           = expression.Remove(0, i);
                    bLastTokenWasOperand = true;
                    continue;
                }

                // Check if first part is string constant
                if (expression[0] == '\"')
                {
                    i = 1;
                    StringVar v = new StringVar();
                    while (i < expression.Length && expression[i] != '\"')
                    {
                        if (expression[i] == '\\')
                        {
                            if ((i + 1) == expression.Length)
                            {
                                throw new StringTerminatorExpectedException(null, expression);
                            }
                            switch (expression[i + 1])
                            {
                            case '\"':
                                v.v += "\"";
                                break;

                            case '\\':
                                v.v += "\\";
                                break;

                            case 't':
                                v.v += "\t";
                                break;

                            case 'n':
                                v.v += "\n";
                                break;

                            default:
                                v.v += expression[i + 1].ToString();
                                break;
                            }
                            // Shift additional position
                            i++;
                        }
                        else
                        {
                            v.v += expression[i].ToString();
                        }
                        i++;
                        v.empty = false;
                    }
                    if (i >= expression.Length)
                    {
                        throw new StringTerminatorExpectedException(null, expression.Substring(0, expression.Length - 2));
                    }

                    mOperands.Push(v);
                    expression           = expression.Remove(0, i + 1);
                    bLastTokenWasOperand = true;
                    continue;
                }

                // Eat all identifiers, separated by '.' and make it
                // a big token. Let the delegate decide which part it
                // can process.
                bool bTokenEnded = false;
                i = 0;
                while (!bTokenEnded)
                {
                    if (!Char.IsLetter(expression, i))
                    {
                        bTokenEnded = true;
                        break;
                    }
                    i++;

                    // identifier consists of letter + 0/more letter/digit/_
                    while (Char.IsLetterOrDigit(expression, i) ||
                           expression[i] == '_')
                    {
                        i++;
                    }
                    if (expression[i] != '.')
                    {
                        bTokenEnded = true;
                        break;
                    }
                    i++; // Read over the '.' and get next identifier
                }
                if (i > 0)
                {
                    StringVar sToken = new StringVar(expression.Substring(0, i));

                    Var r = CheckConstantValue(sToken);
                    if (r == null)
                    {
                        r = myTokenResolver.TokenEvaluator(sToken);
                    }

                    if (r == null)
                    {
                        FunctionInfo fi = myTokenResolver.FunctionFinder(sToken.v);
                        if (fi != null)
                        {
                            OperatorInfo oi = new OperatorInfo(fi.Name, 98, null, false, true);
                            PushOperator(oi);
                            expression           = expression.Remove(0, i);
                            bLastTokenWasOperand = true;
                            continue;
                        }
                    }
                    else
                    {
                        // Store the result on the operand stack.
                        mOperands.Push(r);
                        // Remove the tokens we ate, and replace it with the
                        // remainders of the tokenevaluation
                        expression           = expression.Remove(0, i);
                        expression           = sToken.v + expression;
                        bGotoNextPart        = true;
                        bLastTokenWasOperand = true;
                    }
                }

                if (bGotoNextPart)
                {
                    continue;
                }

                throw new UnknownTokenException(-1, expression.Substring(0, i)); //this new StringVar(String.Format("Syntax error. '{0}' unexpected.", expression.Substring(0,i)));
                //throw new ApplicationException(String.Format("Syntax error. '{0}' unexpected.", expression.Substring(0,i)));
            }

            if (bLastTokenWasOperand)
            {
                throw new OperatorExpectedException(-1, expression.Substring(-1, expression.Length - 2));
            }

            //ProcessOperator();
            if (mOperands.Count == 0)
            {
                return(null);
            }
            else
            {
                return(mOperands.Pop() as Var);
            }
        }