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

93 lines
3.3 KiB
Ada

limited with Types.Atoms;
limited with Types.Builtins;
limited with Types.Fns;
limited with Types.Maps;
limited with Types.Sequences;
limited with Types.Strings;
package Types is
-- A type with a default value for the discriminant is the Ada
-- equivalent of a C union. It uses a fixed size, and allows
-- efficient arrays. A class hierarchy would make this impossible,
-- for little gain.
-- Native types may seem to consume too much memory, but
-- 1/ they require no allocation/deallocation.
-- 2/ the overhead would actually be higher with an intermediate
-- reference (the size of the pointer plus the size of the native
-- type, while an union uses the minimum of both and a single
-- memory area ).
-- The idea is inspired from the Haskell and OCaml interpreters,
-- which use a bit to distinguish pointers from integers. Ada
-- allows to specify the bit position of each component, but
-- generating such architecture-dependent definitions seems a lot
-- of work for MAL.
-- The Ada tradition is to give explicit names to types, but this
-- one will be used very often.
type Kind_Type is
(Kind_Nil,
Kind_Atom,
Kind_Boolean,
Kind_Number,
Kind_Symbol,
Kind_Keyword, Kind_String,
Kind_List, Kind_Vector,
Kind_Map,
Kind_Macro, Kind_Fn, Kind_Builtin_With_Meta, Kind_Builtin);
subtype Kind_Key is Kind_Type range Kind_Keyword .. Kind_String;
subtype Kind_Sequence is Kind_Type range Kind_List .. Kind_Vector;
subtype Kind_Function is Kind_Type range Kind_Fn .. Kind_Builtin;
type T;
type T_Array;
type Atom_Ptr is not null access Atoms.Instance;
type Builtin_Ptr is not null access function (Args : in T_Array) return T;
type Builtin_With_Meta_Ptr is not null access Builtins.Instance;
type Fn_Ptr is not null access Fns.Instance;
type Map_Ptr is not null access Maps.Instance;
type Sequence_Ptr is not null access Sequences.Instance;
type String_Ptr is not null access Strings.Instance;
type T (Kind : Kind_Type := Kind_Nil) is record
case Kind is
when Kind_Nil =>
null;
when Kind_Boolean =>
Ada_Boolean : Boolean;
when Kind_Number =>
Number : Integer;
when Kind_Atom =>
Atom : Atom_Ptr;
when Kind_Key | Kind_Symbol =>
Str : String_Ptr;
when Kind_Sequence =>
Sequence : Sequence_Ptr;
when Kind_Map =>
Map : Map_Ptr;
when Kind_Builtin =>
Builtin : Builtin_Ptr;
when Kind_Builtin_With_Meta =>
Builtin_With_Meta : Builtin_With_Meta_Ptr;
when Kind_Fn | Kind_Macro =>
Fn : Fn_Ptr;
end case;
end record;
-- Useful for recursive automatic definition of equality for
-- composite types like the array type below.
function "=" (Left, Right : in T) return Boolean with Inline;
Nil : constant T := (Kind => Kind_Nil);
function To_Boolean (Form : T) return Boolean with Inline;
procedure Keep (Object : in T) with Inline;
type T_Array is array (Positive range <>) of T;
end Types;