mirror of
https://github.com/kanaka/mal.git
synced 2024-10-27 14:52:16 +03:00
033892777a
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) .
134 lines
4.2 KiB
Haxe
134 lines
4.2 KiB
Haxe
import Compat;
|
|
import types.Types.MalType;
|
|
import types.Types.*;
|
|
import types.MalException;
|
|
import reader.*;
|
|
import printer.*;
|
|
import env.*;
|
|
import core.*;
|
|
|
|
class Step5_tco {
|
|
// READ
|
|
static function READ(str:String):MalType {
|
|
return Reader.read_str(str);
|
|
}
|
|
|
|
// EVAL
|
|
static function EVAL(ast:MalType, env:Env):MalType {
|
|
while (true) {
|
|
var dbgeval = env.get("DEBUG-EVAL");
|
|
if (dbgeval != null && dbgeval != MalFalse && dbgeval != MalNil)
|
|
Compat.println("EVAL: " + PRINT(ast));
|
|
var alst;
|
|
switch (ast) {
|
|
case MalSymbol(s):
|
|
var res = env.get(s);
|
|
if (res == null) throw "'" + s + "' not found";
|
|
return res;
|
|
case MalList(l):
|
|
alst = l;
|
|
case MalVector(l):
|
|
return MalVector(l.map(function(x) { return EVAL(x, env); }));
|
|
case MalHashMap(m):
|
|
var new_map = new Map<String,MalType>();
|
|
for (k in m.keys()) {
|
|
new_map[k] = EVAL(m[k], env);
|
|
}
|
|
return MalHashMap(new_map);
|
|
case _: return ast;
|
|
}
|
|
// apply
|
|
if (alst.length == 0) { return ast; }
|
|
switch (alst[0]) {
|
|
case MalSymbol("def!"):
|
|
return env.set(alst[1], EVAL(alst[2], env));
|
|
case MalSymbol("let*"):
|
|
var let_env = new Env(env);
|
|
switch (alst[1]) {
|
|
case MalList(l) | MalVector(l):
|
|
for (i in 0...l.length) {
|
|
if ((i%2) > 0) { continue; }
|
|
let_env.set(l[i], EVAL(l[i+1], let_env));
|
|
}
|
|
case _: throw "Invalid let*";
|
|
}
|
|
ast = alst[2];
|
|
env = let_env;
|
|
continue; // TCO
|
|
case MalSymbol("do"):
|
|
for (i in 1...alst.length-1)
|
|
EVAL(alst[i], env);
|
|
ast = alst[alst.length-1];
|
|
continue; // TCO
|
|
case MalSymbol("if"):
|
|
var cond = EVAL(alst[1], env);
|
|
if (cond != MalFalse && cond != MalNil) {
|
|
ast = alst[2];
|
|
} else if (alst.length > 3) {
|
|
ast = alst[3];
|
|
} else {
|
|
return MalNil;
|
|
}
|
|
continue; // TCO
|
|
case MalSymbol("fn*"):
|
|
return MalFunc(function (args) {
|
|
return EVAL(alst[2], new Env(env, _list(alst[1]), args));
|
|
},alst[2],env,alst[1],false,nil);
|
|
case _:
|
|
switch ( EVAL(alst[0], env)) {
|
|
case MalFunc(f,a,e,params,_,_):
|
|
var args = alst.slice(1).map(function(x) { return EVAL(x, env); });
|
|
if (a != null) {
|
|
ast = a;
|
|
env = new Env(e, _list(params), args);
|
|
continue; // TCO
|
|
} else {
|
|
return f(args);
|
|
}
|
|
case _: throw "Call of non-function";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// PRINT
|
|
static function PRINT(exp:MalType):String {
|
|
return Printer.pr_str(exp, true);
|
|
}
|
|
|
|
// repl
|
|
static var repl_env = new Env(null);
|
|
|
|
static function rep(line:String):String {
|
|
return PRINT(EVAL(READ(line), repl_env));
|
|
}
|
|
|
|
public static function main() {
|
|
// core.EXT: defined using Haxe
|
|
for (k in Core.ns.keys()) {
|
|
repl_env.set(MalSymbol(k), MalFunc(Core.ns[k],null,null,null,false,nil));
|
|
}
|
|
|
|
// core.mal: defined using the language itself
|
|
rep("(def! not (fn* (a) (if a false true)))");
|
|
|
|
while (true) {
|
|
try {
|
|
var line = Compat.readline("user> ");
|
|
if (line == "") { continue; }
|
|
Compat.println(rep(line));
|
|
} catch (exc:BlankLine) {
|
|
continue;
|
|
} catch (exc:haxe.io.Eof) {
|
|
Compat.exit(0);
|
|
} catch (exc:Dynamic) {
|
|
if (Type.getClass(exc) == MalException) {
|
|
Compat.println("Error: " + Printer.pr_str(exc.obj, true));
|
|
} else {
|
|
Compat.println("Error: " + exc);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|