aboutsummaryrefslogtreecommitdiff
path: root/cs/step6_file.cs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
committerJoel Martin <github@martintribe.org>2014-04-16 23:57:50 -0500
commit8cb5cda46cf3aef847ae3926dc53a5e5f87fe261 (patch)
tree13e5b2878f19ee24272ead8a92a9cb84b33ad0e5 /cs/step6_file.cs
parenta05f7822b10ed4cdd61ed8384299a003baf1c1c6 (diff)
downloadmal-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/step6_file.cs')
-rw-r--r--cs/step6_file.cs54
1 files changed, 20 insertions, 34 deletions
diff --git a/cs/step6_file.cs b/cs/step6_file.cs
index b02cedb..cb2157e 100644
--- a/cs/step6_file.cs
+++ b/cs/step6_file.cs
@@ -4,7 +4,6 @@ using System.Collections;
using System.Collections.Generic;
using Mal;
using MalVal = Mal.types.MalVal;
-using MalString = Mal.types.MalString;
using MalSymbol = Mal.types.MalSymbol;
using MalInteger = Mal.types.MalInteger;
using MalList = Mal.types.MalList;
@@ -14,7 +13,7 @@ using MalFunction = Mal.types.MalFunction;
using Env = Mal.env.Env;
namespace Mal {
- class step4_if_fn_do {
+ class step6_file {
// read
static MalVal READ(string str) {
return reader.read_str(str);
@@ -59,36 +58,36 @@ namespace Mal {
// apply list
MalList ast = (MalList)orig_ast;
if (ast.size() == 0) { return ast; }
- a0 = ast.nth(0);
+ a0 = ast[0];
String a0sym = a0 is MalSymbol ? ((MalSymbol)a0).getName()
: "__<*fn*>__";
switch (a0sym) {
case "def!":
- a1 = ast.nth(1);
- a2 = ast.nth(2);
+ a1 = ast[1];
+ a2 = ast[2];
res = EVAL(a2, env);
env.set(((MalSymbol)a1).getName(), res);
return res;
case "let*":
- a1 = ast.nth(1);
- a2 = ast.nth(2);
+ a1 = ast[1];
+ a2 = ast[2];
MalSymbol key;
MalVal val;
Env let_env = new Env(env);
for(int i=0; i<((MalList)a1).size(); i+=2) {
- key = (MalSymbol)((MalList)a1).nth(i);
- val = ((MalList)a1).nth(i+1);
+ key = (MalSymbol)((MalList)a1)[i];
+ val = ((MalList)a1)[i+1];
let_env.set(key.getName(), EVAL(val, let_env));
}
return EVAL(a2, let_env);
case "do":
eval_ast(ast.slice(1, ast.size()-1), env);
- orig_ast = ast.nth(ast.size()-1);
+ orig_ast = ast[ast.size()-1];
break;
case "if":
- a1 = ast.nth(1);
+ a1 = ast[1];
MalVal cond = EVAL(a1, env);
if (cond == Mal.types.Nil || cond == Mal.types.False) {
// eval false slot form
@@ -103,14 +102,14 @@ namespace Mal {
}
break;
case "fn*":
- MalList a1f = (MalList)ast.nth(1);
- MalVal a2f = ast.nth(2);
+ MalList a1f = (MalList)ast[1];
+ MalVal a2f = ast[2];
Env cur_env = env;
return new MalFunction(a2f, env, a1f,
args => EVAL(a2f, new Env(cur_env, a1f, args)) );
default:
el = (MalList)eval_ast(ast, env);
- var f = (MalFunction)el.nth(0);
+ var f = (MalFunction)el[0];
MalVal fnast = f.getAst();
if (fnast != null) {
orig_ast = fnast;
@@ -133,26 +132,18 @@ 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) \")\")))))");
@@ -180,14 +171,9 @@ 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);
continue;
}
}