예제 #1
0
        private Expr ParseFactor()
        {
            if (this.index == this.tokens.Count)
            {
                throw new System.Exception("expected expression, got EOF");
            }
            if (this.tokens[this.index] is StringToken)
            {
                StringVal stringLiteral = new StringVal();
                stringLiteral.Value = ((StringToken)this.tokens[this.index]).Value;
                MoveNext();
                return(stringLiteral);
            }
            else if (this.tokens[this.index] is IntToken)
            {
                IntVal intVal = new IntVal();
                intVal.Value = ((IntToken)this.tokens[this.index]).Value;
                MoveNext();
                return(intVal);
            }
            else if (this.tokens[this.index] is FloatToken)
            {
                FloatVal floVal = new FloatVal();
                floVal.Value = ((FloatToken)this.tokens[this.index]).Value;
                MoveNext();
                return(floVal);
            }
            else if (this.tokens[this.index] is DoubleToken)
            {
                DoubleVal douVal = new DoubleVal();
                douVal.Value = ((DoubleToken)this.tokens[this.index]).Value;
                MoveNext();
                return(douVal);
            }
            else if (this.tokens[this.index] is DecimalToken)
            {
                DecimalVal decVal = new DecimalVal();
                decVal.Value = ((DecimalToken)this.tokens[this.index]).Value;
                MoveNext();
                return(decVal);
            }
            else if (this.tokens[this.index] is DateTimeToken)
            {
                DateTimeVal datVal = new DateTimeVal();
                datVal.Value = ((DateTimeToken)this.tokens[this.index]).Value;
                MoveNext();
                return(datVal);
            }
            else if (this.tokens[this.index] is BoolToken)
            {
                BoolVal bolVal = new BoolVal();
                bolVal.Value = ((BoolToken)this.tokens[this.index]).Value;
                MoveNext();
                return(bolVal);
            }
            else if (this.tokens[this.index] == Tokens.Null)
            {
                NullVal nullVal = new NullVal();
                MoveNext();
                return(nullVal);
            }
            else if (this.tokens[this.index] is IdentifierToken)
            {
                string ident = ((IdentifierToken)tokens[index]).Name;

                MoveNext();

                // function expr
                if (MaybeEat(Tokens.LeftBracket))
                {
                    FunctionExpr fun = new FunctionExpr();
                    fun = ParseFunction(ident);
                    return(fun);
                }
                // variable
                else
                {
                    Variable var = new Variable();
                    var.Ident = ident;
                    return(var);
                }
            }
            else if (this.tokens[this.index] == Tokens.New)
            {
                MoveNext(); // eat new
                return(ParseNewObject());
            }
            else if (this.tokens[this.index] == Tokens.LeftBracket)
            {
                // Eat LeftParenthesis
                MoveNext();
                Expr result = ParseExpr();
                Eat(Tokens.RightBracket);
                return(result);
            }
            else if (IsUnaryOperator(this.tokens[index]))
            {
                // Unary Expression
                UnaryExpr result = new UnaryExpr();

                if (MaybeEat(Tokens.Sub))
                {
                    result.Op = TokenToBinOp(Tokens.Sub);
                }
                else if (MaybeEat(Tokens.Add))
                {
                    result.Op = TokenToBinOp(Tokens.Add);
                }
                else if (MaybeEat(Tokens.Not))
                {
                    result.Op = TokenToBinOp(Tokens.Not);
                }
                else
                {
                    throw new System.Exception(string.Format(
                                                   "Operator '{0}' is not supported in unary expressions",
                                                   tokens[index]));
                }

                result.Expression = ParseFactor();
                return(result);
            }
            else
            {
                throw new System.Exception("expected string literal, numeric literal, or variable");
            }
        }
예제 #2
0
파일: CodeRun.cs 프로젝트: afumfer/KNote
        private object CodeExecuteUnaryExpr(UnaryExpr ue)
        {
            object res        = null;
            bool   notSupport = false;
            object ex         = GenExpr(ue.Expression);

            switch (ue.Op)
            {
            case BinOp.Add:
                if (ex == null)
                {
                    notSupport = true;
                }
                else if (ex.GetType() == typeof(int))
                {
                    res = Convert.ToInt32(ex);
                }
                else if (ex.GetType() == typeof(float))
                {
                    res = Convert.ToSingle(ex);
                }
                else if (ex.GetType() == typeof(double))
                {
                    res = Convert.ToDouble(ex);
                }
                else if (ex.GetType() == typeof(decimal))
                {
                    res = Convert.ToDecimal(ex);
                }
                else
                {
                    notSupport = true;
                }

                break;

            case BinOp.Sub:
                if (ex == null)
                {
                    notSupport = true;
                }
                else if (ex.GetType() == typeof(int))
                {
                    res = -1 * Convert.ToInt32(ex);
                }
                else if (ex.GetType() == typeof(float))
                {
                    res = -1 * Convert.ToSingle(ex);
                }
                else if (ex.GetType() == typeof(double))
                {
                    res = -1 * Convert.ToDouble(ex);
                }
                else if (ex.GetType() == typeof(decimal))
                {
                    res = -1 * Convert.ToDecimal(ex);
                }
                else
                {
                    notSupport = true;
                }

                break;

            case BinOp.Not:
                if (ex == null)
                {
                    //notSupport = true;
                    res = 1;
                }
                else if (ex.GetType() == typeof(bool))
                {
                    res = (ex.Equals(false)) ? 1 : 0;
                }
                else
                {
                    res = (ex.Equals(0)) ? 1 : 0;
                }
                break;

            default:
                notSupport = true;
                break;
            }

            if (notSupport)
            {
                throw new ApplicationException(string.Format(
                                                   "The operator '{0}' is not supported  in this expression ", ue.Op));
            }
            else
            {
                return(res);
            }
        }