diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-16 23:57:50 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-16 23:57:50 -0500 |
| commit | 8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 (patch) | |
| tree | 13e5b2878f19ee24272ead8a92a9cb84b33ad0e5 /cs/step8_macros.cs | |
| parent | a05f7822b10ed4cdd61ed8384299a003baf1c1c6 (diff) | |
| download | mal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.tar.gz mal-8cb5cda46cf3aef847ae3926dc53a5e5f87fe261.zip | |
All: move some fns to core. Major cleanup.
- Don't import/require core until step4.
- Define cond/or macros from step8
Diffstat (limited to 'cs/step8_macros.cs')
| -rw-r--r-- | cs/step8_macros.cs | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/cs/step8_macros.cs b/cs/step8_macros.cs index fe085f9..caa8f0a 100644 --- a/cs/step8_macros.cs +++ b/cs/step8_macros.cs @@ -14,7 +14,7 @@ using MalFunction = Mal.types.MalFunction; using Env = Mal.env.Env; namespace Mal { - class step4_if_fn_do { + class step8_macros { // read static MalVal READ(string str) { return reader.read_str(str); @@ -201,28 +201,22 @@ namespace Mal { static MalVal RE(Env env, string str) { return EVAL(READ(str), env); } - public static Env _ref(Env env, string name, MalVal mv) { - return env.set(name, mv); - } - static void Main(string[] args) { string prompt = "user> "; - var repl_env = new Mal.env.Env(null); - foreach (var entry in Mal.core.ns) { - _ref(repl_env, entry.Key, entry.Value); + // 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); } - _ref(repl_env, "read-string", new MalFunction( - a => reader.read_str(((MalString)a[0]).getValue()))); - _ref(repl_env, "eval", new MalFunction( - a => EVAL(a[0], repl_env))); - _ref(repl_env, "slurp", new MalFunction( - a => new MalString(File.ReadAllText( - ((MalString)a[0]).getValue())))); + repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env))); + // core.mal: defined using the language itself 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))))))))"); int fileIdx = 0; if (args.Length > 0 && args[0] == "--raw") { @@ -248,12 +242,6 @@ namespace Mal { Console.WriteLine(PRINT(RE(repl_env, line))); } catch (Mal.types.MalContinue) { continue; - } catch (Mal.reader.ParseError e) { - Console.WriteLine(e.Message); - continue; - } catch (Mal.types.MalException e) { - Console.WriteLine("Error: " + e.getValue()); - continue; } catch (Exception e) { Console.WriteLine("Error: " + e.Message); Console.WriteLine(e.StackTrace); |
