コード例 #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 CodeExecuteFunction(FunctionExpr function)
        {
            CodeSpecialStoreObject("_KNTERRORCODE", 0);
            CodeSpecialStoreObject("_KNTERRORDESCRIPTION", "");

            try
            {
                Type       t;
                object     obj;
                object     objRoot;
                string     funName;
                MethodInfo mi;

                IdentObject ident = new IdentObject(function.FunctionName);

                // Params
                object[] param;
                Type[]   types;
                if (function.Args.Count > 0)
                {
                    param = new object[function.Args.Count];
                    types = new Type[function.Args.Count];
                    for (int i = 0; i < function.Args.Count; i++)
                    {
                        param[i] = GenExpr(function.Args[i]);
                        types[i] = param[i].GetType();
                    }
                }
                else
                {
                    param = null;
                    types = null;
                }

                // Get method
                if (string.IsNullOrEmpty(ident.Member))
                {
                    t       = DefaultFunctionLibraryType;
                    obj     = DefaultFunctionLibrary;
                    funName = ident.RootObj;
                    if (types == null)
                    {
                        mi = t.GetMethod(funName, Type.EmptyTypes);
                    }
                    else
                    {
                        mi = t.GetMethod(funName, types);
                    }
                }
                else
                {
                    if (symbolTable.ContainsKey(ident.RootObj))
                    {
                        objRoot = symbolTable[ident.RootObj];
                        GetObjectMethod(objRoot, ident, out obj, out mi, types);
                    }
                    // method in static class
                    else
                    {
                        obj = null;
                        GetMethodStaticClass(ident, out mi, types);
                    }
                }

                // Execute
                return(mi.Invoke(obj, param));
            }
            catch (Exception ex)
            {
                if (!((bool)CodeReadSymbol(new Variable {
                    Ident = "_KNTERRORTRAP"
                })))
                {
                    throw;
                }
                else
                {
                    CodeSpecialStoreObject("_KNTERRORCODE", 10);
                    CodeSpecialStoreObject("_KNTERRORDESCRIPTION", ex.Message);
                    return(null);
                }
            }
        }