Esempio n. 1
0
        public override Continuation Evaluate(Continuation c, Environment env, Datum args)
        {
            var argArray = DatumHelpers.enumerate(args).ToArray();

            c = c.PushTask(new InvokeFunction(function, argArray.Length));
            return(argArray.Aggregate(c, (current, arg) => current.Evaluate(env, arg)));
        }
Esempio n. 2
0
            public override Continuation Evaluate(Continuation c, Environment env, Datum args)
            {
                var datumArgs = args.ToArray();

                if (datumArgs.Length != 1)
                {
                    throw c.error("Ref expect 1s arguments. {0} passed", datumArgs.Length);
                }

                var assemblyName = datumArgs[0].CastIdentifier();
                var assembly     = Assembly.Load(assemblyName);

                // Keep a hash set to avoid adding things twice.
                // Overload resolution is (hopefully) handled by
                // InvokeMethod.
                var symbols = new HashSet <object>();

                // Add all static methods
                foreach (var t in assembly.GetTypes())
                {
                    // Make each type have a symbol in the environment also.
                    env.Define(t.FullName, t.ToAtom());
                    foreach (var mi in t.GetMethods())
                    {
                        var symbol = string.Format("{0}.{1}", t.FullName, mi.Name);
                        if (mi.IsStatic && symbols.Add(symbol))
                        {
                            env.Define(symbol, new StaticMethod(t, mi.Name).ToStack());
                        }
                    }
                }
                return(c.PushResult(DatumHelpers.nil));
            }
Esempio n. 3
0
 /**
  * Create a "sandbox" environment: the symbols defined in the
  * environment don't provide any way for lisp programs to execute
  * arbitrary code.
  */
 public static Environment CreateSandbox()
 {
     var env = new Environment(EmptyEnvironment.Instance);
     env = CoreForms.AddTo(env);
     env = Builtins.AddTo(env);
     return env;
 }
Esempio n. 4
0
        /**
         * Create a "sandbox" environment: the symbols defined in the
         * environment don't provide any way for lisp programs to execute
         * arbitrary code.
         */
        public static Environment CreateSandbox()
        {
            var env = new Environment(EmptyEnvironment.Instance);

            env = CoreForms.AddTo(env);
            env = Builtins.AddTo(env);
            return(env);
        }
Esempio n. 5
0
 public static Environment AddTo(Environment env)
 {
     env = env.Extend("read", Read.Instance);
     env = env.Extend("open-input-string", OpenInputString.Instance);
     env = env.Extend("eof-object?", IsEof.Instance);
     env = env.Extend("open-input-stream", OpenInputStream.Instance);
     return env;
 }
Esempio n. 6
0
 public static Environment AddTo(Environment env)
 {
     env = env.Extend("read", Read.Instance);
     env = env.Extend("open-input-string", OpenInputString.Instance);
     env = env.Extend("eof-object?", IsEof.Instance);
     env = env.Extend("open-input-stream", OpenInputStream.Instance);
     return(env);
 }
Esempio n. 7
0
 public static Environment AddTo(Environment env)
 {
     env = Arithmetic.Extend(env).ToMutable();
     env = env.Extend("append", Append.Instance);
     env = SymbolFunctions.Extend(env);
     ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Builtins.lisp");
     env = Reader.AddTo(env);
     return env;
 }
Esempio n. 8
0
        /**
         * Used for bootstrapping various .lisp files into the environment.
         */
        public static void ExecuteResource(Assembly assembly, Environment env, string resourceFile)
        {
            var evaluator = new Evaluator();

            foreach (var d in ReadDatums(assembly, resourceFile))
            {
                evaluator.Evaluate(env, d);
            }
        }
Esempio n. 9
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);
 }
Esempio n. 10
0
 public static Environment AddTo(Environment env)
 {
     env = Arithmetic.Extend(env).ToMutable();
     env = env.Extend("append", Append.Instance);
     env = SymbolFunctions.Extend(env);
     ResourceLoader.ExecuteResource(env, "LispEngine.Bootstrap.Builtins.lisp");
     env = Reader.AddTo(env);
     return(env);
 }
Esempio n. 11
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     var argList = args.ToArray();
     if (argList.Length != 2)
         throw c.error("Expected 2 arguments: (set! <symbol> <expression>). Got {0} instead", argList.Length);
     var name = argList[0].CastIdentifier();
     var expression = argList[1];
     c = c.PushTask(new SetName(env, name));
     return c.Evaluate(env, expression);
 }
Esempio n. 12
0
 public static Environment AddTo(Environment env)
 {
     // Invoke a given instance method on an object
     env = env.Extend("make-instance-method", new MakeInstanceMethod().ToStack());
     env = env.Extend("new", new New().ToStack());
     // Bring all static symbols from a particular assembly into the current environment.
     env = env.Extend("ref", new Ref());
     env = env.Extend("atom", new WrapAtom().ToStack());
     // Define "." as a macro to invoke a given method
     ResourceLoader.ExecuteResource(env, "LispEngine.ReflectionBinding.ReflectionBuiltins.lisp");
     return(env);
 }
Esempio n. 13
0
 public static Environment AddTo(Environment env)
 {
     // Invoke a given instance method on an object
     env = env.Extend("make-instance-method", new MakeInstanceMethod().ToStack());
     env = env.Extend("new", new New().ToStack());
     // Bring all static symbols from a particular assembly into the current environment.
     env = env.Extend("ref", new Ref());
     env = env.Extend("atom", new WrapAtom().ToStack());
     // Define "." as a macro to invoke a given method
     ResourceLoader.ExecuteResource(env, "LispEngine.ReflectionBinding.ReflectionBuiltins.lisp");
     return env;
 }
Esempio n. 14
0
        public override Continuation Evaluate(Continuation c, Environment env, Datum args)
        {
            var argList = args.ToArray();

            if (argList.Length != 2)
            {
                throw c.error("Expected 2 arguments: (set! <symbol> <expression>). Got {0} instead", argList.Length);
            }
            var name       = argList[0].CastIdentifier();
            var expression = argList[1];

            c = c.PushTask(new SetName(env, name));
            return(c.Evaluate(env, expression));
        }
Esempio n. 15
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);
                }
            }
        }
Esempio n. 16
0
        private static Datum evaluate(Continuation c, Environment 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);
        }
Esempio n. 17
0
 public override Continuation Evaluate(Continuation c, Environment 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 = new Environment(env);
     var remaining = argList.Reverse().ToArray();
     for (var i = 0; i < remaining.Length; ++i)
     {
         if (i > 0)
             c = c.PushTask(Ignore.Instance);
         c = c.Evaluate(localEnv, remaining[i]);
     }
     return c;
 }
Esempio n. 18
0
 public static Environment AddTo(Environment env)
 {
     env = env
           .Extend("log", Log.Instance)
           .Extend("lambda", Lambda.Instance)
           .Extend("cons", Cons.Instance)
           .Extend("apply", Apply.Instance)
           .Extend("eq?", Eq.Instance)
           .Extend("macro", Macro.Instance)
           .Extend("quote", Quote.Instance)
           .Extend("define", Define.Instance)
           .Extend("set!", Set.Instance)
           .Extend("begin", Begin.Instance)
           .Extend("call/cc", CallCC.Instance)
           .Extend("eval", Eval.Instance)
           .Extend("env", Env.Instance);
     return(env);
 }
Esempio n. 19
0
 public static Environment AddTo(Environment env)
 {
     env = env
         .Extend("log", Log.Instance)
         .Extend("lambda", Lambda.Instance)
         .Extend("cons", Cons.Instance)
         .Extend("apply", Apply.Instance)
         .Extend("eq?", Eq.Instance)
         .Extend("macro", Macro.Instance)
         .Extend("quote", Quote.Instance)
         .Extend("define", Define.Instance)
         .Extend("set!", Set.Instance)
         .Extend("begin", Begin.Instance)
         .Extend("call/cc", CallCC.Instance)
         .Extend("eval", Eval.Instance)
         .Extend("env", Env.Instance);
     return env;
 }
Esempio n. 20
0
        private static Datum evaluate(Continuation c, Environment 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));
        }
Esempio n. 21
0
        public override Continuation Evaluate(Continuation c, Environment 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  = new Environment(env);
            var remaining = argList.Reverse().ToArray();

            for (var i = 0; i < remaining.Length; ++i)
            {
                if (i > 0)
                {
                    c = c.PushTask(Ignore.Instance);
                }
                c = c.Evaluate(localEnv, remaining[i]);
            }
            return(c);
        }
Esempio n. 22
0
 public static void ExecuteResource(Environment env, string resourceFile)
 {
     ExecuteResource(Assembly.GetCallingAssembly(), env, resourceFile);
 }
Esempio n. 23
0
 public SetName(Environment env, string name)
 {
     this.env = env;
     this.name = name;
 }
Esempio n. 24
0
 public Closure(Environment env, IEnumerable <ArgBody> argBodies)
 {
     this.env       = env;
     this.argBodies = argBodies;
 }
Esempio n. 25
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     return(c.PushResult(env.ToAtom()));
 }
Esempio n. 26
0
 public EvaluateExpansion(Environment env)
 {
     this.env = env;
 }
Esempio n. 27
0
 public Closure(Environment env, IEnumerable<ArgBody> argBodies)
 {
     this.env = env;
     this.argBodies = argBodies;
 }
Esempio n. 28
0
 public static Environment Extend(Environment env)
 {
     env = env.Extend("symbol->string", new SymbolToString().ToStack());
     env = env.Extend("string->symbol", new StringToSymbol().ToStack());
     return(env);
 }
Esempio n. 29
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     return c.PushResult(evaluate(c, env, args));
 }
Esempio n. 30
0
 public EvaluateExpansion(Environment env)
 {
     this.env = env;
 }
Esempio n. 31
0
 /**
  * Used for bootstrapping various .lisp files into the environment.
  */
 public static void ExecuteResource(Assembly assembly, Environment env, string resourceFile)
 {
     var evaluator = new Evaluator();
     foreach (var d in ReadDatums(assembly, resourceFile))
         evaluator.Evaluate(env, d);
 }
Esempio n. 32
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     var argArray = DatumHelpers.enumerate(args).ToArray();
     c = c.PushTask(new InvokeFunction(function, argArray.Length));
     return argArray.Aggregate(c, (current, arg) => current.Evaluate(env, arg));
 }
Esempio n. 33
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     c = c.PushTask(new EvaluateExpansion(env));
     return argFunction.Evaluate(c, args);
 }
Esempio n. 34
0
 public override Continuation Evaluate(Continuation c, Environment env, Datum args)
 {
     c = c.PushTask(new EvaluateExpansion(env));
     return(argFunction.Evaluate(c, args));
 }
Esempio n. 35
0
 public SetName(Environment env, string name)
 {
     this.env  = env;
     this.name = name;
 }
Esempio n. 36
0
 public static Environment Extend(Environment env)
 {
     env = env.Extend("symbol->string", new SymbolToString().ToStack());
     env = env.Extend("string->symbol", new StringToSymbol().ToStack());
     return env;
 }
Esempio n. 37
0
            public override Continuation Evaluate(Continuation c, Environment env, Datum args)
            {
                var datumArgs = args.ToArray();
                if (datumArgs.Length != 1)
                    throw c.error("Ref expect 1s arguments. {0} passed", datumArgs.Length);

                var assemblyName = datumArgs[0].CastIdentifier();
                var assembly = Assembly.Load(assemblyName);

                // Keep a hash set to avoid adding things twice.
                // Overload resolution is (hopefully) handled by
                // InvokeMethod.
                var symbols = new HashSet<object>();
                // Add all static methods
                foreach (var t in assembly.GetTypes())
                {
                    // Make each type have a symbol in the environment also.
                    env.Define(t.FullName, t.ToAtom());
                    foreach (var mi in t.GetMethods())
                    {
                        var symbol = string.Format("{0}.{1}", t.FullName, mi.Name);
                        if (mi.IsStatic && symbols.Add(symbol))
                            env.Define(symbol, new StaticMethod(t, mi.Name).ToStack());
                    }
                }
                return c.PushResult(DatumHelpers.nil);
            }
Esempio n. 38
0
 public static void ExecuteResource(Environment env, string resourceFile)
 {
     ExecuteResource(Assembly.GetCallingAssembly(), env, resourceFile);
 }