diff options
| author | Joel Martin <github@martintribe.org> | 2014-11-15 20:38:50 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-09 16:16:46 -0600 |
| commit | c3b508af92800f63bf99f41af68f026535f454f5 (patch) | |
| tree | e94c4f555375189c69cebada164afba80f2579bd /cs/step2_eval.cs | |
| parent | 95ec2d81d8661e56b7b98a1c19ad503c5083c1e4 (diff) | |
| download | mal-c3b508af92800f63bf99f41af68f026535f454f5.tar.gz mal-c3b508af92800f63bf99f41af68f026535f454f5.zip | |
C#: use closure for RE func. Shorter type names.
Diffstat (limited to 'cs/step2_eval.cs')
| -rw-r--r-- | cs/step2_eval.cs | 37 |
1 files changed, 13 insertions, 24 deletions
diff --git a/cs/step2_eval.cs b/cs/step2_eval.cs index 1a95a81..0c7db69 100644 --- a/cs/step2_eval.cs +++ b/cs/step2_eval.cs @@ -5,11 +5,11 @@ using System.Collections.Generic; using Mal; using MalVal = Mal.types.MalVal; using MalSymbol = Mal.types.MalSymbol; -using MalInteger = Mal.types.MalInteger; +using MalInt = Mal.types.MalInt; using MalList = Mal.types.MalList; using MalVector = Mal.types.MalVector; using MalHashMap = Mal.types.MalHashMap; -using MalFunction = Mal.types.MalFunction; +using MalFunc = Mal.types.MalFunc; namespace Mal { class step1_eval { @@ -59,7 +59,7 @@ namespace Mal { + Mal.printer._pr_str(a0,true) + "'"); } var el = (MalList)eval_ast(ast, env); - var f = (MalFunction)el[0]; + var f = (MalFunc)el[0]; return f.apply(el.rest()); } @@ -70,43 +70,32 @@ namespace Mal { } // repl - static MalVal RE(Dictionary<string, MalVal> env, string str) { - return EVAL(READ(str), env); - } - - static public MalFunction plus = new MalFunction( - a => (MalInteger)a[0] + (MalInteger)a[1] ); - static public MalFunction minus = new MalFunction( - a => (MalInteger)a[0] - (MalInteger)a[1] ); - static public MalFunction multiply = new MalFunction( - a => (MalInteger)a[0] * (MalInteger)a[1] ); - static public MalFunction divide = new MalFunction( - a => (MalInteger)a[0] / (MalInteger)a[1] ); - static void Main(string[] args) { - string prompt = "user> "; - var repl_env = new Dictionary<string, MalVal> { - {"+", plus}, - {"-", minus}, - {"*", multiply}, - {"/", divide}, + {"+", new MalFunc(a => (MalInt)a[0] + (MalInt)a[1]) }, + {"-", new MalFunc(a => (MalInt)a[0] - (MalInt)a[1]) }, + {"*", new MalFunc(a => (MalInt)a[0] * (MalInt)a[1]) }, + {"/", new MalFunc(a => (MalInt)a[0] / (MalInt)a[1]) }, }; + Func<string, MalVal> RE = (string str) => EVAL(READ(str), repl_env); if (args.Length > 0 && args[0] == "--raw") { Mal.readline.mode = Mal.readline.Mode.Raw; } + + // repl loop while (true) { string line; try { - line = Mal.readline.Readline(prompt); + line = Mal.readline.Readline("user> "); if (line == null) { break; } + if (line == "") { continue; } } catch (IOException e) { Console.WriteLine("IOException: " + e.Message); break; } try { - Console.WriteLine(PRINT(RE(repl_env, line))); + Console.WriteLine(PRINT(RE(line))); } catch (Mal.types.MalContinue) { continue; } catch (Exception e) { |
