mirror of
https://github.com/kanaka/mal.git
synced 2024-09-21 02:27:10 +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) .
129 lines
4.1 KiB
Ada
129 lines
4.1 KiB
Ada
with Ada.Text_IO.Unbounded_IO;
|
|
|
|
with Err;
|
|
with Printer;
|
|
with Types.Sequences;
|
|
|
|
package body Envs is
|
|
|
|
use all type Types.Kind_Type;
|
|
use type Types.Strings.Instance;
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
procedure Dump_Stack (Env : in Instance) is
|
|
use Ada.Text_IO;
|
|
begin
|
|
Put ("environment:");
|
|
for P in Env.Data.Iterate loop
|
|
-- Do not print builtins for repl.
|
|
if HM.Element (P).Kind /= Kind_Builtin or Env.Outer /= null then
|
|
Put (" ");
|
|
HM.Key (P).all.Query_Element (Put'Access);
|
|
Put (':');
|
|
Unbounded_IO.Put (Printer.Pr_Str (HM.Element (P)));
|
|
New_Line;
|
|
end if;
|
|
end loop;
|
|
if Env.Outer /= null then
|
|
Put ("outer is ");
|
|
Env.Outer.all.Dump_Stack;
|
|
end if;
|
|
end Dump_Stack;
|
|
|
|
function Get (Env : in Instance;
|
|
Key : in Types.String_Ptr) return Types.T
|
|
is
|
|
Position : HM.Cursor := Env.Data.Find (Key);
|
|
Ref : Link;
|
|
begin
|
|
if not HM.Has_Element (Position) then
|
|
Ref := Env.Outer;
|
|
loop
|
|
if Ref = null then
|
|
-- Not using Err.Check, which would compute the
|
|
-- argument even if the assertion holds...
|
|
Err.Raise_With ("'" & Key.To_String & "' not found");
|
|
end if;
|
|
Position := Ref.all.Data.Find (Key);
|
|
exit when HM.Has_Element (Position);
|
|
Ref := Ref.all.Outer;
|
|
end loop;
|
|
end if;
|
|
return HM.Element (Position);
|
|
end Get;
|
|
|
|
function Get_Or_Nil (Env : Instance;
|
|
Key : Types.String_Ptr) return Types.T is
|
|
Position : HM.Cursor := Env.Data.Find (Key);
|
|
Ref : Link;
|
|
begin
|
|
if not HM.Has_Element (Position) then
|
|
Ref := Env.Outer;
|
|
loop
|
|
if Ref = null then
|
|
return Types.Nil;
|
|
end if;
|
|
Position := Ref.all.Data.Find (Key);
|
|
exit when HM.Has_Element (Position);
|
|
Ref := Ref.all.Outer;
|
|
end loop;
|
|
end if;
|
|
return HM.Element (Position);
|
|
end Get_Or_Nil;
|
|
|
|
procedure Keep_References (Object : in out Instance) is
|
|
begin
|
|
for Position in Object.Data.Iterate loop
|
|
HM.Key (Position).all.Keep;
|
|
Types.Keep (HM.Element (Position));
|
|
end loop;
|
|
if Object.Outer /= null then
|
|
Object.Outer.all.Keep;
|
|
end if;
|
|
end Keep_References;
|
|
|
|
function New_Env (Outer : in Link := null) return Ptr is
|
|
Ref : constant Ptr := new Instance;
|
|
begin
|
|
Garbage_Collected.Register (Garbage_Collected.Pointer (Ref));
|
|
Ref.all.Outer := Outer;
|
|
return Ref;
|
|
end New_Env;
|
|
|
|
procedure Set_Binds (Env : in out Instance;
|
|
Binds : in Types.T_Array;
|
|
Exprs : in Types.T_Array)
|
|
is
|
|
begin
|
|
if 2 <= Binds'Length and then Binds (Binds'Last - 1).Str.all = "&" then
|
|
Err.Check (Binds'Length - 2 <= Exprs'Length,
|
|
"not enough actual parameters for vararg function");
|
|
for I in 0 .. Binds'Length - 3 loop
|
|
Env.Data.Include (Key => Binds (Binds'First + I).Str,
|
|
New_Item => Exprs (Exprs'First + I));
|
|
end loop;
|
|
Env.Data.Include (Key => Binds (Binds'Last).Str,
|
|
New_Item => Types.Sequences.List
|
|
(Exprs (Exprs'First + Binds'Length - 2 .. Exprs'Last)));
|
|
else
|
|
Err.Check (Binds'Length = Exprs'Length,
|
|
"wrong parameter count for (not vararg) function");
|
|
for I in 0 .. Binds'Length - 1 loop
|
|
Env.Data.Include (Key => Binds (Binds'First + I).Str,
|
|
New_Item => Exprs (Exprs'First + I));
|
|
end loop;
|
|
end if;
|
|
end Set_Binds;
|
|
|
|
procedure Set (Env : in out Instance;
|
|
Key : in Types.T;
|
|
New_Item : in Types.T)
|
|
is
|
|
begin
|
|
Err.Check (Key.Kind = Kind_Symbol, "environment keys must be symbols");
|
|
Env.Data.Include (Key.Str, New_Item);
|
|
end Set;
|
|
|
|
end Envs;
|