aboutsummaryrefslogtreecommitdiff
path: root/cs/step9_try.cs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-11-15 20:38:50 -0600
committerJoel Martin <github@martintribe.org>2015-01-09 16:16:46 -0600
commitc3b508af92800f63bf99f41af68f026535f454f5 (patch)
treee94c4f555375189c69cebada164afba80f2579bd /cs/step9_try.cs
parent95ec2d81d8661e56b7b98a1c19ad503c5083c1e4 (diff)
downloadmal-c3b508af92800f63bf99f41af68f026535f454f5.tar.gz
mal-c3b508af92800f63bf99f41af68f026535f454f5.zip
C#: use closure for RE func. Shorter type names.
Diffstat (limited to 'cs/step9_try.cs')
-rw-r--r--cs/step9_try.cs47
1 files changed, 22 insertions, 25 deletions
diff --git a/cs/step9_try.cs b/cs/step9_try.cs
index ed3417c..e5da607 100644
--- a/cs/step9_try.cs
+++ b/cs/step9_try.cs
@@ -6,11 +6,11 @@ using Mal;
using MalVal = Mal.types.MalVal;
using MalString = Mal.types.MalString;
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;
using Env = Mal.env.Env;
namespace Mal {
@@ -54,8 +54,8 @@ namespace Mal {
if (a0 is MalSymbol &&
env.find(((MalSymbol)a0).getName()) != null) {
MalVal mac = env.get(((MalSymbol)a0).getName());
- if (mac is MalFunction &&
- ((MalFunction)mac).isMacro()) {
+ if (mac is MalFunc &&
+ ((MalFunc)mac).isMacro()) {
return true;
}
}
@@ -66,7 +66,7 @@ namespace Mal {
public static MalVal macroexpand(MalVal ast, Env env) {
while (is_macro_call(ast, env)) {
MalSymbol a0 = (MalSymbol)((MalList)ast)[0];
- MalFunction mac = (MalFunction) env.get(a0.getName());
+ MalFunc mac = (MalFunc) env.get(a0.getName());
ast = mac.apply(((MalList)ast).rest());
}
return ast;
@@ -148,7 +148,7 @@ namespace Mal {
a1 = ast[1];
a2 = ast[2];
res = EVAL(a2, env);
- ((MalFunction)res).setMacro();
+ ((MalFunc)res).setMacro();
env.set(((MalSymbol)a1).getName(), res);
return res;
case "macroexpand":
@@ -198,11 +198,11 @@ namespace Mal {
MalList a1f = (MalList)ast[1];
MalVal a2f = ast[2];
Env cur_env = env;
- return new MalFunction(a2f, env, a1f,
+ return new MalFunc(a2f, env, a1f,
args => EVAL(a2f, new Env(cur_env, a1f, args)) );
default:
el = (MalList)eval_ast(ast, env);
- var f = (MalFunction)el[0];
+ var f = (MalFunc)el[0];
MalVal fnast = f.getAst();
if (fnast != null) {
orig_ast = fnast;
@@ -222,19 +222,15 @@ namespace Mal {
}
// repl
- static MalVal RE(Env env, string str) {
- return EVAL(READ(str), env);
- }
-
static void Main(string[] args) {
- string prompt = "user> ";
+ var repl_env = new Mal.env.Env(null);
+ Func<string, MalVal> RE = (string str) => EVAL(READ(str), repl_env);
// core.cs: defined using C#
- var repl_env = new env.Env(null);
foreach (var entry in core.ns) {
repl_env.set(entry.Key, entry.Value);
}
- repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env)));
+ repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env)));
MalList _argv = new MalList();
for (int i=1; i < args.Length; i++) {
_argv.conj_BANG(new MalString(args[i]));
@@ -242,11 +238,11 @@ namespace Mal {
repl_env.set("*ARGV*", _argv);
// core.mal: defined using the language itself
- RE(repl_env, "(def! *host-language* \"c#\")");
- RE(repl_env, "(def! not (fn* (a) (if a false true)))");
- RE(repl_env, "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
- RE(repl_env, "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))");
- RE(repl_env, "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))");
+ RE("(def! *host-language* \"c#\")");
+ RE("(def! not (fn* (a) (if a false true)))");
+ RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
+ RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))");
+ RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))");
int fileIdx = 0;
if (args.Length > 0 && args[0] == "--raw") {
@@ -254,23 +250,24 @@ namespace Mal {
fileIdx = 1;
}
if (args.Length > fileIdx) {
- RE(repl_env, "(load-file \"" + args[fileIdx] + "\")");
+ RE("(load-file \"" + args[fileIdx] + "\")");
return;
}
-
+
// repl loop
- RE(repl_env, "(println (str \"Mal [\" *host-language* \"]\"))");
+ RE("(println (str \"Mal [\" *host-language* \"]\"))");
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 (Mal.types.MalException e) {