Beispiel #1
0
        public JsObject evaluate(Env env)
        {
            JsExpression x = this.value == "" ? this.child[0] : this;

            if (x.child.Count == 0)
            {
                return(CoreInterpreter.EvalSingleExpression(x, env));
            }
            else
            {
                if (x.value == "if")//考虑if内部变量作用域问题!!!Lexical Scope
                {
                    return(CoreInterpreter.EvalIf(x, env));
                }
                else if (x.value == ".")
                {
                    return(CoreInterpreter.EvalPointExpression(x, env));
                }
                else if (x.value == "while")
                {
                    return(CoreInterpreter.EvalWhile(x, env));
                }
                else if (x.value == "return")
                {
                    return(x.child[0].evaluate(env));
                }
                else if (x.value == "new")//build-in
                {
                    return(x.child[0].evaluate(env));
                }
                else if (x.value == "var")//=
                {
                    return(x.child[0].evaluate(env));
                }
                else if (x.value == "=")
                {
                    return(env.AddDef(x.child[0].value, x.child[1].evaluate(env)));
                }
                else if (x.value == "function")
                {
                    return(CoreInterpreter.EvalFunction(x, env));
                }
                //else if (x.value == "list")
                //{
                //    return new LList(this.child.Skip(1).Select(item => item.evaluate(env)));
                //}
                else if (x.value == "{")
                {
                    return(CoreInterpreter.EvalBlock(x, env));
                }
                else if (Env.builtins.ContainsKey(x.value))
                {
                    return(CoreInterpreter.EvalBuiltIn(x, env));
                }
                else
                {
                    //匿名函数和自定义函数调用
                    return(CoreInterpreter.EvalFunctionInvoke(x, env));
                }
            }
        }
Beispiel #2
0
 public JsObject(JsObject[] _objs, Env _scope)
 {
     objs  = (JsObject[])_objs.Clone();
     scope = _scope;
 }
Beispiel #3
0
        public static JsObject EvalBuiltIn(JsExpression x, Env env)
        {
            var args = x.child.ToArray();

            return(Env.builtins[x.value](args, env));
        }
Beispiel #4
0
 public JsFunction(string[] args, JsExpression body, Env env)
 {
     this.args = args;
     this.body = body;
     this.env  = env;
 }
Beispiel #5
0
 public JsArray(JsObject[] _items, Env _scope)
 {
     items = ((JsObject[])_items.Clone()).AsEnumerable() as List <JsObject>;
     scope = _scope;
 }
Beispiel #6
0
 public JsArray()
 {
     items = new List <JsObject>();
     dict  = new Dictionary <JsObject, JsObject>();
     scope = new Env();
 }
Beispiel #7
0
 public JsString(string _v, Env _scope)
 {
     val   = _v;
     scope = _scope;
 }
Beispiel #8
0
 public Env(Env outer = null)
 {
     this.outer = outer;
     this.dict = new Dictionary<string, JsObject>();
 }