Example #1
0
        private Expression ParsePoW(CharIterator it)
        {
            var exp1 = ParseUnitary(it);

            if (it.Valid() && it.Value() == '^')
            {
                it.MoveNext();
                if (it.Valid() && IsNum(it.Value()))
                {
                    char ch = it.Value();
                    it.MoveNext();
                    if (it.Valid() && IsNum(it.Value()))
                    {
                        throw new Exception("Need a number in range (0-9)");
                    }
                    int n = ch - '0';
                    exp1 = new PowExpression(Op.Pow, exp1, n);
                }
                else
                {
                    throw new Exception("Need a number after ^!");
                }
            }
            return(exp1);
        }
Example #2
0
        public Expression ParseExpression(String str)
        {
            var it   = new CharIterator(str);
            var exp1 = ParseAdd(it);

            if (it.Valid())
            {
                throw new Exception("should reach end of string");
            }
            return(exp1);
        }
Example #3
0
        private Expression ParseAdd(CharIterator it)
        {
            var exp1 = ParseMul(it);

            for (; it.Valid() && (it.Value() == '+' || it.Value() == '-');)
            {
                char ch = it.Value();
                Op   op = ch == '+' ? Op.Add : Op.Sub;
                it.MoveNext();
                var exp2 = ParseMul(it);
                exp1 = new BinaryExpression(op, exp1, exp2);
            }
            return(exp1);
        }
Example #4
0
        private Expression ParseMul(CharIterator it)
        {
            var exp1 = ParsePoW(it);

            for (; it.Valid() && (it.Value() == '*' || it.Value() == '/');)
            {
                char ch = it.Value();
                Op   op = ch == '*' ? Op.Mul : Op.Div;
                it.MoveNext();
                var exp2 = ParsePoW(it);
                exp1 = new BinaryExpression(op, exp1, exp2);
            }
            return(exp1);
        }
Example #5
0
        private double ReadNum(CharIterator it)
        {
            var sb = new StringBuilder();

            // read - +
            if (it.Value() == '-' || it.Value() == '+')
            {
                sb.Append(it.Value());
                it.MoveNext();
            }

            // read integer
            for (; it.Valid() && IsNum(it.Value());)
            {
                sb.Append(it.Value());
                it.MoveNext();
            }

            // read faction
            if (it.Valid() && it.Value() == '.')
            {
                sb.Append(it.Value());
                it.MoveNext();

                // read faction
                for (; it.Valid() && IsNum(it.Value());)
                {
                    sb.Append(it.Value());
                    it.MoveNext();
                }
            }

            // read exponent
            if (it.Valid() && (it.Value() == 'E' || it.Value() == 'e'))
            {
                sb.Append(it.Value());
                it.MoveNext();

                // read - +
                if (it.Valid() && (it.Value() == '-' || it.Value() == '+'))
                {
                    sb.Append(it.Value());
                    it.MoveNext();
                }

                // read integer
                for (; it.Valid() && IsNum(it.Value());)
                {
                    sb.Append(it.Value());
                    it.MoveNext();
                }
            }

            double ret = System.Convert.ToDouble(sb.ToString());

            return(ret);
        }
Example #6
0
        private Expression ParseID(CharIterator it)
        {
            char ch = it.Value();

            if (IsAlpha(ch) || ch == '@')
            {
                String   varname = ReadString(it);
                Variable variable;
                if (!fVarsDir.TryGetValue(varname, out variable))
                {
                    variable        = new Variable(varname);
                    variable.fIndex = fVars.Count;
                    variable.fName  = varname;
                    if (fValues.ContainsKey(varname))
                    {
                        variable.fVal = fValues[varname];
                    }
                    else
                    {
                        variable.fVal = gRnd.Next() % 1000 / 1000.0;
                    }
                    fVars.Add(variable);
                    fVarsDir.Add(varname, variable);
                }
                var id = new IDExpression(variable);
                return(id);
            }
            else if (ch == '-' || ch == '+' || IsNum(ch))
            {
                double v   = ReadNum(it);
                var    num = new NumberExpression(v);
                return(num);
            }
            else if (ch == '(')
            {
                it.MoveNext();
                var expr = ParseAdd(it);
                if (it.Valid() && it.Value() == ')')
                {
                    it.MoveNext();
                    return(expr);
                }
                else
                {
                    throw new Exception("Expect ')'");
                }
            }
            throw new Exception("Need a id or number!");
        }
Example #7
0
        private String ReadString(CharIterator it)
        {
            var sb = new StringBuilder();

            for (; it.Valid();)
            {
                char ch = it.Value();
                if (IsAlpha(ch) || ch == '@' || IsNum(ch))
                {
                    sb.Append(ch);
                    it.MoveNext();
                }
                else
                {
                    break;
                }
            }
            return(sb.ToString());
        }
Example #8
0
 private Expression ParseUnitary(CharIterator it)
 {
     if (it.Valid() && (it.Value() == '-' || it.Value() == '+'))
     {
         var op = it.Value() == '-' ? Op.Negative : Op.Positive;
         it.MoveNext();
         var exp = ParseUnitary(it);
         if (exp is NumberExpression)
         {
             var ne = (NumberExpression)exp;
             if (op == Op.Negative)
             {
                 ne.fValue = -ne.fValue;
             }
             return(ne);
         }
         else
         {
             return(new UnitaryExpression(op, exp));
         }
     }
     return(ParseID(it));
 }