public Env updateFuncEnv(JsObject[] parameters) { (args.Length >= parameters.Length).OrThrows("参数太多"); Env env1 = new Env(this.env); for (int i = 0; i < parameters.Length; i++) { env1.AddDef(this.args[i], parameters[i]); } return(env1); }
public static JsObject EvalFunction(JsExpression x, Env env) { if (x.child.Count == 3) { string[] args = x.child[1].child.Select(i => i.value).ToArray(); JsFunction func = new JsFunction(args, x.child[2], new Env(env)); return(env.AddDef(x.child[0].value, func)); } else { string[] args = x.child[0].child.Select(i => i.value).ToArray(); JsFunction func = new JsFunction(args, x.child[1], new Env(env)); return(func); } }
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)); } // 添加一种类型 dict !!! x.value == "dict" // 添加数组类型 list !!! x.value == "list" //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)); } } }
public JsObject AddDef(string name, JsObject obj) { scope.AddDef(name, obj); return(obj); }