1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/impls/ada.2/envs.ads
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

61 lines
2.1 KiB
Ada

private with Ada.Containers.Hashed_Maps;
with Garbage_Collected;
with Types.Strings;
package Envs is
-- This package should be named Env, but Ada does not allow formal
-- parameters to be named like a package dependency, and it seems
-- that readability inside Eval is more important.
type Instance (<>) is abstract new Garbage_Collected.Instance with private;
type Link is access Instance;
subtype Ptr is not null Link;
function New_Env (Outer : in Link := null) return Ptr with Inline;
-- Set_Binds is provided as distinct subprograms because we some
-- time spare the creation of a subenvironment.
procedure Set_Binds (Env : in out Instance;
Binds : in Types.T_Array;
Exprs : in Types.T_Array);
-- Equivalent to successive calls to Set, except that if Binds
-- ends with "&" followed by a symbol, the trailing symbol
-- receives all remaining values as a list.
function Get (Env : in Instance;
Key : in Types.String_Ptr) return Types.T;
function Get_Or_Nil (Env : Instance;
Key : Types.String_Ptr) return Types.T;
procedure Set (Env : in out Instance;
Key : in Types.T;
New_Item : in Types.T) with Inline;
-- Raises an exception if Key is not a symbol.
-- Debug.
procedure Dump_Stack (Env : in Instance);
private
package HM is new Ada.Containers.Hashed_Maps
(Key_Type => Types.String_Ptr,
Element_Type => Types.T,
Hash => Types.Strings.Hash,
Equivalent_Keys => Types.Strings.Same_Contents,
"=" => Types."=");
-- It may be tempting to subclass Types.Map, but this would not
-- simplify the code much. And adding metadata to a structure that
-- is allocated very often has a cost.
type Instance is new Garbage_Collected.Instance with record
Outer : Link;
Data : HM.Map;
end record;
overriding procedure Keep_References (Object : in out Instance) with Inline;
end Envs;