1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/d/step3_env.d
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

150 lines
3.7 KiB
D

module main;
import std.algorithm;
import std.array;
import std.range;
import std.stdio;
import std.string;
import env;
import readline;
import reader;
import printer;
import types;
MalType READ(string str)
{
return read_str(str);
}
MalType EVAL(MalType ast, Env env)
{
if (auto dbgeval = env.get("DEBUG-EVAL"))
if (dbgeval.is_truthy())
writeln("EVAL: ", pr_str(ast));
if (auto sym = cast(MalSymbol)ast)
{
if (auto val = env.get(sym.name))
return val;
else
throw new Exception("'" ~ sym.name ~ "' not found");
}
else if (auto lst = cast(MalVector)ast)
{
auto el = array(lst.elements.map!(e => EVAL(e, env)));
return new MalVector(el);
}
else if (auto hm = cast(MalHashmap)ast)
{
typeof(hm.data) new_data;
foreach (string k, MalType v; hm.data)
{
new_data[k] = EVAL(v, env);
}
return new MalHashmap(new_data);
}
// todo: indent right
else if (auto ast_list = cast(MalList)ast)
{
if (ast_list.elements.length == 0)
{
return ast;
}
auto a0_sym = verify_cast!MalSymbol(ast_list.elements[0]);
switch (a0_sym.name)
{
case "def!":
auto a1 = verify_cast!MalSymbol(ast_list.elements[1]);
return env.set(a1.name, EVAL(ast_list.elements[2], env));
case "let*":
auto a1 = verify_cast!MalSequential(ast_list.elements[1]);
auto let_env = new Env(env);
foreach (kv; chunks(a1.elements, 2))
{
if (kv.length < 2) throw new Exception("let* requires even number of elements");
auto var_name = verify_cast!MalSymbol(kv[0]);
let_env.set(var_name.name, EVAL(kv[1], let_env));
}
return EVAL(ast_list.elements[2], let_env);
default:
auto fobj = verify_cast!MalBuiltinFunc(EVAL(ast_list.elements[0], env));
auto args = array(ast_list.elements[1..$].map!(e => EVAL(e, env)));
return fobj.fn(args);
}
}
else
{
return ast;
}
}
string PRINT(MalType ast)
{
return pr_str(ast);
}
string rep(string str, Env env)
{
return PRINT(EVAL(READ(str), env));
}
static MalType mal_add(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val + i1.val);
}
static MalType mal_sub(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val - i1.val);
}
static MalType mal_mul(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val * i1.val);
}
static MalType mal_div(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val / i1.val);
}
void main()
{
auto repl_env = new Env(null);
repl_env.set("+", new MalBuiltinFunc(&mal_add, "+"));
repl_env.set("-", new MalBuiltinFunc(&mal_sub, "-"));
repl_env.set("*", new MalBuiltinFunc(&mal_mul, "*"));
repl_env.set("/", new MalBuiltinFunc(&mal_div, "/"));
for (;;)
{
string line = _readline("user> ");
if (line is null) break;
if (line.length == 0) continue;
try
{
writeln(rep(line, repl_env));
}
catch (Exception e)
{
writeln("Error: ", e.msg);
}
}
writeln("");
}