protected static Cell DoEval(Cell code, SchemeEnvironment env) { // Update/set (debug) function env.Insert("debug", new Cell(btargs => { if (btargs.Length > 0) { Evaluator.Debug = (btargs[0] == StandardRuntime.True); } Debug = Evaluator.Debug; return(Debug ? StandardRuntime.True : StandardRuntime.False); })); Evaluator.Debug = Debug; Evaluator.Eval(code, env); return(Evaluator.Result); }
static void AddProgramGlobals(SchemeEnvironment env) { List <Cell> history = new List <Cell>(); bool quit = false; bool timing = false; // Add a function to get a history result env.Insert("h", new Cell(args => new Cell(history[(int)(args[0])]))); env.Insert("eval", new Cell((args, subenv) => { if (args.Length > 1) { subenv = args[1].Environment; } return(DoEval(new Cell(args[0]), subenv)); })); env.Insert("env", new Cell((args, subenv) => new Cell(subenv))); env.Insert("str", new Cell(args => new Cell(args[0].ToString()))); env.Insert("env-str", new Cell(args => new Cell(args[0].Environment.ToString()))); env.Insert("exit", new Cell(args => { quit = true; return(StandardRuntime.Nil); })); env.Insert("quit", env.Lookup(new Cell("exit"))); env.Insert("timing", new Cell(args => { if (args.Length > 0) { timing = ((string)args[0] == "on" || args[0] == StandardRuntime.True); Console.Error.WriteLine("Timing is now " + (timing ? "on" : "off")); } return(timing ? StandardRuntime.True : StandardRuntime.False); })); // Add evaluator constants env.Insert("evcell", new Cell("evcell")); env.Insert("evclassic", new Cell("evclassic")); env.Insert("evframe", new Cell("evframe")); // Add function to change evaluator env.Insert("sweval", new Cell(args => { if (args.Length == 0) { if (Evaluator.GetType() == typeof(CellMachineEval)) { return(new Cell("evcell")); } else if (Evaluator.GetType() == typeof(StandardEval)) { return(new Cell("evclassic")); } else if (Evaluator.GetType() == typeof(FrameEval)) { return(new Cell("evframe")); } else { throw new InvalidDataException(); } } ISchemeEval newEvaluator; Cell result; switch (args[0].Value) { case "evcell": newEvaluator = new CellMachineEval(); result = new Cell("evcell"); break; case "evclassic": newEvaluator = new StandardEval(); result = new Cell("evclassic"); break; case "evframe": newEvaluator = new FrameEval(); result = new Cell("evframe"); break; default: return(new Cell("#invalid_argument")); } // Copy details to new evaluator newEvaluator.Debug = Evaluator.Debug; Evaluator = newEvaluator; return(result); })); // Add function to run unit tests on evaluator env.Insert("swtest", new Cell(args => { var results = SchemeEval.RunTests(Evaluator); Cell result = new Cell(CellType.LIST); result.ListValue.Add(new Cell(results.Success)); result.ListValue.Add(new Cell(results.Failures)); return(result); })); env.Insert("help", new Cell(args => { Console.Write("SchemingSharply v {0}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version); #if DEBUG Console.WriteLine(" debug"); #else Console.WriteLine(" release"); #endif Console.WriteLine("Type `quit' or `\\q' to quit"); Console.WriteLine("Type `(env-str (env))' to display environment"); //Console.WriteLine("Use `(eval expr)' or `(eval expr (env))' for testing"); Console.WriteLine("Use `(h n)' to view history item n"); Console.WriteLine("Use `(timing #true)` to enable timing, `(debug #true)` to enable debugging"); Console.WriteLine("Use `(sweval ...)` to get or set evaluator; valid options: evcell, evclassic, evframe"); Console.WriteLine("Use `(swtest)` to run unit tests"); Console.WriteLine("Use `(help)' to display this message again"); Console.WriteLine(); return(StandardRuntime.Nil); })); env.Insert("repl", new Cell(args => { quit = false; while (!quit) { int index = history.Count; string entry = ReadLine(string.Format("{0}> ", index)).Trim(); if (entry.Equals("quit", StringComparison.OrdinalIgnoreCase) || entry.Equals("exit", StringComparison.OrdinalIgnoreCase) || entry == "\\q") { break; } if (entry.Equals("")) { continue; } try { Stopwatch sw = new Stopwatch(); sw.Start(); Cell entered = StandardRuntime.Read(entry); LastExecutedSteps = Evaluator.Steps; Cell result = DoEval(entered, env); string steps = (Evaluator.Steps - LastExecutedSteps).ToString(); if (Evaluator.Steps < LastExecutedSteps) { steps = "??"; } sw.Stop(); Console.WriteLine("===> {0}", result); if (timing) { Console.WriteLine("=== Executed {0} steps in {1}ms", steps, sw.ElapsedMilliseconds); } history.Add(result); } catch (Exception e) { Console.WriteLine("!!!> {0}", e.Message); } } return(StandardRuntime.Nil); })); }