public void InitBuiltin(IInterpreter itpr, IStandardIo io, Env env) { _itpr = itpr; _env = env; io.Out.WriteLine("Builtin :: Loading..."); Init(); var modules = new IModule[] { new CoreModule(), new MathModule(), }; foreach (var item in modules) { io.Out.WriteLine($"Builtin :: {item.Name}"); AddModule(item); } var builtinFuncs = new Fun[] { new BuiltinFun(EvalBuiltin) { FunName = "builtin", Limit = false, Args = new List<string>() { "name", "args" } }, new BuiltinFun(EvalType) { FunName = "type", Limit = true, Args = new List<string>() { "t" } }, new BuiltinFun(EvalLoad) { FunName = "load", Limit = true, Args = new List<string>() { "name" } }, new LazyFun(EvalLazy) { FunName = "lazy" }, }; foreach (var item in builtinFuncs) { item.RegisterToEnv(env); } var code = @" load Core "; env.LockVariable = true; foreach (var item in code.Split('\n')) { itpr.Input(item); } env.LockVariable = false; io.Out.WriteLine("Builtin :: OK"); }
private Expr _Fnx() { Pop(); var args = TakeUntil("=>").ToList(); if (!args.Any()) Error("缺少函数名"); Pop("缺少'=>'"); if (args.Any(a => !RgxVar.IsMatch(a))) Error("非法形参"); var fname = args.First(); args.RemoveAt(0); if (args.Count > 2) Error("必须有一或两个参数"); var fun = new Fun() { FunName = fname, Limit = false, Args = args, Writable = !_env.LockVariable }; _env.PutValue(fname, fun); _env.PushNewEnv(); foreach (var item in args) { _env.PutValue(item, new Val() { ValName = item }); } fun.Exp = _Expr(); _env.PopEnv(); return _env.QueryValue(fname); }