Ejemplo n.º 1
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argArray = DatumHelpers.enumerate(args).ToArray();
     Array.Reverse(argArray);
     c = c.PushTask(new InvokeFunction(function, argArray.Length));
     return argArray.Aggregate(c, (current, arg) => current.Evaluate(env, arg));
 }
Ejemplo n.º 2
0
 public static LexicalEnvironment Extend(LexicalEnvironment env)
 {
     env.Define("symbol->string", new SymbolToString().ToStack());
     env.Define("string->symbol", new StringToSymbol().ToStack());
     env.Define("gensym", new GenSym().ToStack());
     return env;
 }
Ejemplo n.º 3
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env.Define("read", Read.Instance);
     env.Define("open-input-string", OpenInputString.Instance);
     env.Define("eof-object?", IsEof.Instance);
     env.Define("open-input-stream", OpenInputStream.Instance);
     return env;
 }
Ejemplo n.º 4
0
 public void setupFixture()
 {
     e = new Evaluator();
     env = StandardEnvironment.Create();
     var setupDatum = getLispFromResource("setup");
     if (setupDatum != nil)
         foreach(var f in setupDatum.Enumerate())
             e.Evaluate(env, f);
 }
Ejemplo n.º 5
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     return env.Define("task-descriptions", MakeDatumFunction(getTaskDescriptions, ",task-descriptions"))
         .Define("execute-with-error-translator", ExecuteWithErrorTranslator.Instance)
         .Define("env-stack", MakeDatumFunction(getEnvironments, ",env-stack"))
         .Define("pending-results", MakeDatumFunction(getPendingResults, ",pending-results"))
         .Define("throw", MakeDatumFunction(throwMsg, ",throw"))
         .Define("get-env", MakeDatumFunction(getEnv, ",get-env"));
 }
Ejemplo n.º 6
0
 public Datum Evaluate(Statistics statistics, LexicalEnvironment env, Datum datum)
 {
     env.Statistics = statistics;
     var c = Continuation.Create(statistics)
         .PushTask(null)
         .PushResult(null)
         .Evaluate(env, datum);
     return Evaluate(c);
 }
Ejemplo n.º 7
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env = Arithmetic.Extend(env);
     env.Define("append", Append.Instance);
     env = SymbolFunctions.Extend(env);
     ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Builtins.lisp");
     ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Library.lisp");
     env = Reader.AddTo(env);
     return env;
 }
Ejemplo n.º 8
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length != 2)
         throw c.error("Expected 2 arguments: (define <symbol> <expression>). Got {0} instead", argList.Length);
     var name = argList[0].CastSymbol();
     var expression = argList[1];
     c = c.PushTask(
         tc => { env.Define(name, tc.Result);
                 return tc;},
         "define '{0}'", name);
     return c.Evaluate(env, expression);
 }
Ejemplo n.º 9
0
 public static LexicalEnvironment Extend(LexicalEnvironment env)
 {
     return env
         .Define("+", makeOperation("+", (x, y) => x + y))
         .Define("-", makeOperation("-", (x, y) => x - y))
         .Define("*", makeOperation("*", (x, y) => x*y))
         .Define("/", makeOperation("/", (x, y) => x/y))
         .Define("<", makeOperation("<", (x, y) => x < y))
         .Define(">", makeOperation(">", (x, y) => x > y))
         .Define("bit-and", makeOperation("bit-and", (x , y) => x & y))
         .Define("bit-or", makeOperation("bit-or", (x, y) => x | y))
         .Define("bit-shift", makeOperation("bit-shift", (x, y) => x << y));
 }
Ejemplo n.º 10
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     // Invoke a given instance method on an object
     env.Define("make-instance-method", DelegateFunctions.MakeDatumFunction(MakeInstanceMethod, ",make-instance-method"));
     env.Define("get-static-method", DelegateFunctions.MakeDatumFunction(GetStaticMethod, ",get-static-method"));
     env.Define("get-type", new GetTypeFunction().ToStack());
     env.Define("new", new New().ToStack());
     env.Define("atom", new WrapAtom().ToStack());
     // Define "dot" and "slash" as a macros which allow us to use
     // Clojure-style syntax for invoking and referring to methods.
     ResourceLoader.ExecuteResource(env, "LispEngine.ReflectionBinding.ReflectionBuiltins.lisp");
     return env;
 }
Ejemplo n.º 11
0
        private static Datum evaluate(Continuation c, LexicalEnvironment env, Datum args)
        {
            var macroArgs = args.ToArray();
            if (macroArgs.Length % 2 != 0)
                throw c.error("Invalid macro syntax for lambda. Argument count for '{0}' is not even ({1}). Syntax is (lambda [args body]+)", args, macroArgs.Length);

            var argBodies = new List<ArgBody>();
            for (var i = 0; i < macroArgs.Length; i += 2)
            {
                var closureArgs = macroArgs[i];
                var body = macroArgs[i + 1];
                argBodies.Add(new ArgBody(closureArgs, body));
            }
            return new Closure(env, argBodies);
        }
Ejemplo n.º 12
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length < 1)
         throw c.error("Expected at least 1 expression for begin. Got none.");
     // Scope any local definitions.
     var localEnv = env.NewFrame();
     var remaining = argList.Reverse().ToArray();
     for (var i = 0; i < remaining.Length; ++i)
     {
         if (i > 0)
             c = c.PushTask(popResult, "Discard result");
         c = c.Evaluate(localEnv, remaining[i]);
     }
     return c;
 }
Ejemplo n.º 13
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env = env
         .Define("log", Log.Instance)
         .Define("lambda", Lambda.Instance)
         .Define("cons", DelegateFunctions.MakeDatumFunction(DatumHelpers.cons, ",cons"))
         .Define("set-car!", DelegateFunctions.MakeDatumFunction(DatumHelpers.setCar, ",set-car!"))
         .Define("set-cdr!", DelegateFunctions.MakeDatumFunction(DatumHelpers.setCdr, ",set-cdr!"))
         .Define("apply", Apply.Instance)
         .Define("eq?", EqualFunctions.Eq)
         .Define("equal?", EqualFunctions.Equal)
         .Define("quote", Quote.Instance)
         .Define("define", Define.Instance)
         .Define("set!", Set.Instance)
         .Define("begin", Begin.Instance)
         .Define("call-with-current-continuation", CallCC.Instance)
         .Define("eval", Eval.Instance)
         .Define("env", Env.Instance);
     env = DebugFunctions.AddTo(env);
     env = Macro.AddTo(env);
     env = VectorFunctions.AddTo(env);
     return env;
 }
Ejemplo n.º 14
0
 private static Binding findAndCache(LexicalEnvironment e, Symbol symbol)
 {
     if (e.statistics != null)
         e.statistics.Lookups++;
     var id = symbol.ID;
     while(e != null)
     {
         var b = e.findInFrame(id);
         if (b != null)
         {
             symbol.Env = e;
             return (symbol.CachedBinding = b);
         }
         e = e.parent;
     }
     throw undefined(symbol);
 }
Ejemplo n.º 15
0
 /**
  * Used for bootstrapping various .lisp files into the environment.
  */
 public static void ExecuteResource(Statistics statistics, Assembly assembly, LexicalEnvironment env, string resourceFile)
 {
     var evaluator = new Evaluator();
     foreach (var d in ReadDatums(assembly, resourceFile))
         evaluator.Evaluate(statistics, env, d);
 }
Ejemplo n.º 16
0
 public Datum Evaluate(LexicalEnvironment env, Datum datum)
 {
     return(Evaluate(new Statistics(), env, datum));
 }
 public EvaluateFExpression(Datum args, LexicalEnvironment env)
 {
     this.args = args;
     this.env  = env;
 }
Ejemplo n.º 18
0
 public static void ExecuteResource(LexicalEnvironment env, string resourceFile)
 {
     ExecuteResource(new Statistics(), Assembly.GetCallingAssembly(), env, resourceFile);
 }
Ejemplo n.º 19
0
 private static Binding checkCached(LexicalEnvironment e, Symbol symbol)
 {
     if (symbol.Env == null)
         return null;
     while(e != null)
     {
         if (ReferenceEquals(symbol.Env, e))
             return symbol.CachedBinding;
         e = e.parent;
     }
     return null;
 }
Ejemplo n.º 20
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     var p = args as Pair;
     c = c.PushEnv(env).PushTask(new EvaluateExpansion(this, p));
     // Optimization - if this macro has been expanded on this Datum before,
     // use the same expansion.
     // See "macro-cache-in-datum" unit test for a demonstration of why
     // we need to check against "this" also.
     if(p != null)
     {
         var cachedPair = p.Cache as Pair;
         if(cachedPair != null && ReferenceEquals(cachedPair.First, this))
             return c.PushResult(cachedPair.Second);
     }
     c.Statistics.Expansions++;
     return argFunction.Evaluate(c, args);
 }
 public abstract Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args);
Ejemplo n.º 22
0
 public Continuation Evaluate(LexicalEnvironment e, Datum expression)
 {
     return PushEnv(e).PushTask(new EvaluateTask(expression));
 }
Ejemplo n.º 23
0
 public Continuation PushEnv(LexicalEnvironment env)
 {
     return(SetEnvs(envs.Push(env)));
 }
Ejemplo n.º 24
0
 // This is a bit hacky - can't figure out a better way to "supply" the statistics
 // object yet. Basically, I want to expose "get-counter" to Lisp, but not "statistics"
 // itself... I guess.
 public LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env.Define("!get-statistics", DelegateFunctions.MakeFunction(Snapshot, "!get-statistics"));
     env.Define("!get-statistics-delta", DelegateFunctions.MakeFunction <Statistics, Statistics>(Delta, "!get-statistics-delta"));
     return(env);
 }
Ejemplo n.º 25
0
 private static LexicalEnvironment newFrame(LexicalEnvironment parent, FrameBindings bindings)
 {
     return(new LexicalEnvironment(parent, bindings));
 }
Ejemplo n.º 26
0
 public Datum Evaluate(LexicalEnvironment env, Datum datum)
 {
     return Evaluate(new Statistics(), env, datum);
 }
Ejemplo n.º 27
0
 public static LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env.Define("unmacro", Unmacro);
     env.Define("macro", Instance);
     return env;
 }
Ejemplo n.º 28
0
 private static LexicalEnvironment newFrame(LexicalEnvironment parent, FrameBindings bindings)
 {
     return new LexicalEnvironment(parent, bindings);
 }
Ejemplo n.º 29
0
 public Continuation PushEnv(LexicalEnvironment env)
 {
     return SetEnvs(envs.Push(env));
 }
Ejemplo n.º 30
0
 private LexicalEnvironment(LexicalEnvironment parent, IStack<Binding> bindings)
 {
     this.statistics = parent == null ? null : parent.statistics;
     this.parent = parent;
     this.bindings = bindings;
 }
Ejemplo n.º 31
0
 public Closure(LexicalEnvironment env, IEnumerable<ArgBody> argBodies)
 {
     this.env = env;
     this.argBodies = argBodies;
 }
Ejemplo n.º 32
0
 public override Continuation Evaluate(Continuation c, LexicalEnvironment env, Datum args)
 {
     return c.PushResult(evaluate(c, args));
 }
Ejemplo n.º 33
0
 // This is a bit hacky - can't figure out a better way to "supply" the statistics
 // object yet. Basically, I want to expose "get-counter" to Lisp, but not "statistics"
 // itself... I guess.
 public LexicalEnvironment AddTo(LexicalEnvironment env)
 {
     env.Define("!get-statistics", DelegateFunctions.MakeFunction(Snapshot, "!get-statistics"));
     env.Define("!get-statistics-delta", DelegateFunctions.MakeFunction<Statistics, Statistics>(Delta, "!get-statistics-delta"));
     return env;
 }
Ejemplo n.º 34
0
 public Continuation Evaluate(LexicalEnvironment e, Datum expression)
 {
     return(PushEnv(e).PushTask(new EvaluateTask(expression)));
 }
Ejemplo n.º 35
0
 private LexicalEnvironment(LexicalEnvironment parent, IStack <Binding> bindings)
 {
     this.statistics = parent == null ? null : parent.statistics;
     this.parent     = parent;
     this.bindings   = bindings;
 }