1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/nim/step6_file.nim
Nicolas Boulenguez 033892777a Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL
See issue #587.
* Merge eval-ast and eval into a single conditional.
* Expand macros during the apply phase, removing lots of duplicate
  tests, and increasing the overall consistency by allowing the macro
  to be computed instead of referenced by name (`((defmacro! cond
  (...)))` is currently illegal for example).
* Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the
  MAL environment.
* Remove macroexpand and quasiquoteexpand special forms.
* Use pattern-matching style in process/step*.txt.

Unresolved issues:
c.2: unable to reproduce with gcc 11.12.0.
elm: the directory is unchanged.
groovy: sometimes fail, but not on each rebuild.
nasm: fails some new soft tests, but the issue is unreproducible when
  running the interpreter manually.
objpascal: unreproducible with fpc 3.2.2.
ocaml: unreproducible with 4.11.1.
perl6: unreproducible with rakudo 2021.09.

Unrelated changes:
Reduce diff betweens steps.
Prevent defmacro! from mutating functions: c forth logo miniMAL vb.
dart: fix recent errors and warnings
ocaml: remove metadata from symbols.

Improve the logo implementation.
Encapsulate all representation in types.lg and env.lg, unwrap numbers.
Replace some manual iterations with logo control structures.
Reduce the diff between steps.
Use native iteration in env_get and env_map
Rewrite the reader with less temporary strings.
Reduce the number of temporary lists (for example, reverse iteration
with butlast requires O(n^2) allocations).
It seems possible to remove a few exceptions: GC settings
(Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES
(Makefile.impls) .
2024-08-05 11:40:49 -05:00

129 lines
3.3 KiB
Nim

import rdstdin, tables, sequtils, os, types, reader, printer, env, core
proc read(str: string): MalType = str.read_str
proc eval(ast: MalType, env: Env): MalType =
var ast = ast
var env = env
while true:
let dbgeval = env.get("DEBUG-EVAL")
if not (dbgeval.isNil or dbgeval.kind in {Nil, False}):
echo "EVAL: " & ast.pr_str
case ast.kind
of Symbol:
let val = env.get(ast.str)
if val.isNil:
raise newException(ValueError, "'" & ast.str & "' not found")
return val
of List:
discard(nil) # Proceed after the case statement
of Vector:
return vector ast.list.mapIt(it.eval(env))
of HashMap:
result = hash_map()
for k, v in ast.hash_map.pairs:
result.hash_map[k] = v.eval(env)
return result
else:
return ast
if ast.list.len == 0: return ast
let a0 = ast.list[0]
if a0.kind == 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]
var let_env = initEnv(env)
case a1.kind
of List, Vector:
for i in countup(0, a1.list.high, 2):
let_env.set(a1.list[i].str, a1.list[i+1].eval(let_env))
else: raise newException(ValueError, "Illegal kind in let*")
ast = a2
env = let_env
continue # TCO
of "do":
let last = ast.list.high
discard (ast.list[1 ..< last].mapIt(it.eval(env)))
ast = ast.list[last]
continue # 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]
continue # TCO
else:
return nilObj
else:
ast = a2
continue # TCO
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, env)
let f = eval(a0, env)
let args = ast.list[1 .. ^1].mapIt(it.eval(env))
if f.kind == MalFun:
ast = f.malfun.ast
env = initEnv(f.malfun.env, f.malfun.params, list(args))
continue # TCO
return f.fun(args)
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) \"\nnil)\")))))"
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()