diff options
| author | def <dennis@felsin9.de> | 2015-03-01 04:57:38 +0100 |
|---|---|---|
| committer | def <dennis@felsin9.de> | 2015-03-01 04:57:38 +0100 |
| commit | dc7f0b6afd9bd298103c42e7113a69b788d565c4 (patch) | |
| tree | 74006c431fdbfc3898adb28cb24a12f7ceb655a1 | |
| parent | 3603af9666161a66c5a846c33a1b2663044c73af (diff) | |
| download | mal-dc7f0b6afd9bd298103c42e7113a69b788d565c4.tar.gz mal-dc7f0b6afd9bd298103c42e7113a69b788d565c4.zip | |
Nim: step7
| -rw-r--r-- | nim/core.nim | 12 | ||||
| -rw-r--r-- | nim/step7_quote.nim | 143 |
2 files changed, 155 insertions, 0 deletions
diff --git a/nim/core.nim b/nim/core.nim index c32bc6f..afba5e5 100644 --- a/nim/core.nim +++ b/nim/core.nim @@ -22,6 +22,16 @@ proc read_str(xs: varargs[MalType]): MalType = proc slurp(xs: varargs[MalType]): MalType = str readFile(xs[0].str) +proc cons(xs: varargs[MalType]): MalType = + result = list(xs[0]) + for x in xs[1].list: result.list.add x + +proc concat(xs: varargs[MalType]): MalType = + result = list() + for x in xs: + for i in x.list: + result.list.add i + template wrapNumberFun(op: expr): expr = fun proc(xs: varargs[MalType]): MalType = number op(xs[0].number, xs[1].number) @@ -57,4 +67,6 @@ let ns* = { "read-string": fun read_str, "slurp": fun slurp, + "cons": fun cons, + "concat": fun concat, } diff --git a/nim/step7_quote.nim b/nim/step7_quote.nim new file mode 100644 index 0000000..ec8f6fb --- /dev/null +++ b/nim/step7_quote.nim @@ -0,0 +1,143 @@ +import rdstdin, tables, sequtils, os, types, reader, printer, env, core + +proc read(str: string): MalType = str.read_str + +proc is_pair(x: MalType): bool = + x.kind in {List, Vector} and x.list.len > 0 + +proc quasiquote(ast: MalType): MalType = + if not ast.is_pair: + return list(symbol "quote", ast) + elif ast.list[0] == symbol "unquote": + return ast.list[1] + elif ast.list[0].is_pair and ast.list[0].list[0] == symbol "splice-unquote": + return list(symbol "concat", ast.list[0].list[1], + quasiquote(list ast.list[1 .. -1])) + else: + return list(symbol "cons", quasiquote(ast.list[0]), quasiquote(list(ast.list[1 .. -1]))) + +proc eval(ast: MalType, env: var Env): MalType + +proc eval_ast(ast: MalType, env: var Env): MalType = + case ast.kind + of Symbol: + result = env.get(ast.str) + of List: + result = list ast.list.mapIt(MalType, it.eval(env)) + of Vector: + result = vector ast.list.mapIt(MalType, it.eval(env)) + of HashMap: + result = hash_map() + for k, v in ast.hash_map.pairs: + result.hash_map[k] = v.eval(env) + else: + result = ast + +proc eval(ast: MalType, env: var Env): MalType = + template defaultApply = + let el = ast.eval_ast(env) + let f = el.list[0] + case f.kind + of MalFun: + ast = f.malfun.ast + env = initEnv(f.malfun.env, f.malfun.params, list(el.list[1 .. -1])) + else: + return f.fun(el.list[1 .. -1]) + + var ast = ast + while true: + if ast.kind != List: + return ast.eval_ast(env) + + let a0 = ast.list[0] + case a0.kind + of Symbol: + case a0.str + of "def!": + let + a1 = ast.list[1] + a2 = ast.list[2] + return env.set(a1.str, a2.eval(env)) + + of "let*": + let + a1 = ast.list[1] + a2 = ast.list[2] + case a1.kind + of List, Vector: + for i in countup(0, a1.list.high, 2): + env.set(a1.list[i].str, a1.list[i+1].eval(env)) + else: discard + # Continue loop (TCO) + + of "quote": + return ast.list[1] + + of "quasiquote": + ast = ast.list[1].quasiquote + # Continue loop (TCO) + + of "do": + let last = ast.list.high + let el = (list ast.list[1 .. <last]).eval_ast(env) + ast = ast.list[last].eval(env) + # Continue loop (TCO) + + of "if": + let + a1 = ast.list[1] + a2 = ast.list[2] + cond = a1.eval(env) + + if cond.kind in {Nil, False}: + if ast.list.len > 3: ast = ast.list[3] + else: ast = nilObj + else: ast = a2 + + of "fn*": + let + a1 = ast.list[1] + a2 = ast.list[2] + var env2 = env + let fn = proc(a: varargs[MalType]): MalType = + var newEnv = initEnv(env2, a1, list(a)) + a2.eval(newEnv) + return malfun(fn, a2, a1, env2) + + else: + defaultApply() + + else: + defaultApply() + +proc print(exp: MalType): string = exp.pr_str + +var repl_env = initEnv() + +for k, v in ns.items: + repl_env.set(k, v) +repl_env.set("eval", fun(proc(xs: varargs[MalType]): MalType = eval(xs[0], repl_env))) +var ps = commandLineParams() +repl_env.set("*ARGV*", list((if paramCount() > 1: ps[1..ps.high] else: @[]).map(str))) + + +# core.nim: defined using nim +proc rep(str: string): string {.discardable.} = + str.read.eval(repl_env).print + +# core.mal: defined using mal itself +rep "(def! not (fn* (a) (if a false true)))" +rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" + +if paramCount() >= 1: + rep "(load-file \"" & paramStr(1) & "\")" + quit() + +while true: + try: + let line = readLineFromStdin("user> ") + echo line.rep + except Blank: discard + except: + echo getCurrentExceptionMsg() + echo getCurrentException().getStackTrace() |
