コード例 #1
0
ファイル: CodeRun.cs プロジェクト: afumfer/KNote
 private Expr ValueToExpr(Type t, object newValue)
 {
     if (t == typeof(int))
     {
         IntVal intVal = new IntVal();;
         intVal.Value = Convert.ToInt32(newValue);
         return(intVal);
     }
     else if (t == typeof(float))
     {
         FloatVal floatVal = new FloatVal();
         floatVal.Value = Convert.ToSingle(newValue);
         return(floatVal);
     }
     else if (t == typeof(double))
     {
         DoubleVal doubleVal = new DoubleVal();
         doubleVal.Value = Convert.ToDouble(newValue);
         return(doubleVal);
     }
     else if (t == typeof(decimal))
     {
         DecimalVal decimalVal = new DecimalVal();
         decimalVal.Value = Convert.ToDecimal(newValue);
         return(decimalVal);
     }
     else if (t == typeof(string))
     {
         StringVal stringVal = new StringVal();
         stringVal.Value = Convert.ToString(newValue);
         return(stringVal);
     }
     else if (t == typeof(DateTime))
     {
         DateTimeVal dateTimeVal = new DateTimeVal();
         dateTimeVal.Value = Convert.ToDateTime(newValue);
         return(dateTimeVal);
     }
     else if (t == typeof(bool))
     {
         BoolVal boolVal = new BoolVal();
         boolVal.Value = Convert.ToBoolean(newValue);
         return(boolVal);
     }
     else
     {
         StringVal stringVal = new StringVal();
         stringVal.Value = Convert.ToString(newValue);
         return(stringVal);
     }
 }
コード例 #2
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");
            }
        }