1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 06:40:14 +03:00
mal/impls/pike/step5_tco.pike
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

131 lines
3.1 KiB
Plaintext

import .Env;
import .Printer;
import .Reader;
import .Readline;
import .Types;
Val READ(string str)
{
return read_str(str);
}
Val EVAL(Val ast, Env env)
{
while(true)
{
Val dbgeval = env.get("DEBUG-EVAL");
if(dbgeval && dbgeval.mal_type != MALTYPE_FALSE
&& dbgeval.mal_type != MALTYPE_NIL)
write(({ "EVAL: ", PRINT(ast), "\n" }));
switch(ast.mal_type)
{
case MALTYPE_SYMBOL:
Val key = ast.value;
Val val = env.get(ast.value);
if(!val) throw("'" + key + "' not found");
return val;
case MALTYPE_LIST:
break;
case MALTYPE_VECTOR:
return Vector(map(ast.data, lambda(Val e) { return EVAL(e, env); }));
case MALTYPE_MAP:
array(Val) elements = ({ });
foreach(ast.data; Val k; Val v)
{
elements += ({ k, EVAL(v, env) });
}
return Map(elements);
default:
return ast;
}
if(ast.emptyp()) return ast;
if(ast.data[0].mal_type == MALTYPE_SYMBOL) {
switch(ast.data[0].value)
{
case "def!":
return env.set(ast.data[1], EVAL(ast.data[2], env));
case "let*":
Env let_env = Env(env);
Val ast1 = ast.data[1];
for(int i = 0; i < sizeof(ast1.data); i += 2)
{
let_env.set(ast1.data[i], EVAL(ast1.data[i + 1], let_env));
}
env = let_env;
ast = ast.data[2];
continue; // TCO
case "do":
Val result;
foreach(ast.data[1..(sizeof(ast.data) - 2)], Val element)
{
result = EVAL(element, env);
}
ast = ast.data[-1];
continue; // TCO
case "if":
Val cond = EVAL(ast.data[1], env);
if(cond.mal_type == MALTYPE_FALSE || cond.mal_type == MALTYPE_NIL)
{
if(sizeof(ast.data) > 3)
ast = ast.data[3];
else
return MAL_NIL;
}
else
ast = ast.data[2];
continue; // TCO
case "fn*":
return Fn(ast.data[2], ast.data[1], env,
lambda(Val ... a) { return EVAL(ast.data[2], Env(env, ast.data[1], List(a))); });
}
}
Val f = EVAL(ast.data[0], env);
array(Val) args = ast.data[1..];
args = map(args, lambda(Val e) { return EVAL(e, env);});
switch(f.mal_type)
{
case MALTYPE_BUILTINFN:
return f(@args);
case MALTYPE_FN:
ast = f.ast;
env = Env(f.env, f.params, List(args));
continue; // TCO
default:
throw("Unknown function type");
}
}
}
string PRINT(Val exp)
{
return pr_str(exp, true);
}
string rep(string str, Env env)
{
return PRINT(EVAL(READ(str), env));
}
int main()
{
Env repl_env = Env(0);
foreach(.Core.NS(); Val k; Val v) repl_env.set(k, v);
rep("(def! not (fn* (a) (if a false true)))", repl_env);
while(1)
{
string line = readline("user> ");
if(!line) break;
if(strlen(line) == 0) continue;
if(mixed err = catch { write(({ rep(line, repl_env), "\n" })); } )
{
if(arrayp(err)) err = err[0];
write(({ "Error: ", err, "\n" }));
}
}
write("\n");
return 0;
}