1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 02:27:10 +03:00
mal/ada.2/types-fns.ads
Nicolas Boulenguez 00c3a3c33d ada.2: spring cleaning before final pull request.
Two changes require approval.

* The 'do' special becomes a built-in function similar to first. This
  small change reduces the complexity of eval.  The last evaluation
  cannot benefit from TCO, but the performance change seems invisible.

* read/eval/print acts on each item found in the input string, as if
  they were enclosed with (do ..). The guide does not specify what
  should happen to text following the first AST, and this change actually
  simplifies some things (like dealing with zero AST).
  The read-string built-in function only returns the first AST,
  as changing this would be much more intrusive.

Other changes seem straightforward.

Global:
* Ada 2020 target assignments (like +=, but more general).
* Use Constant_Indexing aspect for sequences, so that they can be
  indexed in source code like native arrays.
* consistency renamings.
  'fn' does not include built-in functions, 'function' does.
  'list' does not include vectors, 'sequence' does.

Move error handling to a separate package.
* This simplifies code everywhere else.
* Uncaught expressions now report a stack trace.

Types:
* Count allocations and deallocations, check that counts match.
* Share more code between functions and macros.

Core:
* Replace the Core.Ns function returning an array with a procedure
  (The intermediate object was preventing the reference counting code
  from deallocating some unused objects).
* Implement Prn with Pr_Str.

Printer:
* Change the profile so that the caller spares some allocations.

Reader:
* Share a single buffer of mal values between all recursions.
  This significantly reduces the stack footprint.

Steps:
* Fix implementation name (ada.2) in the startup script.
* Let environment variables trigger debugging information.
2019-03-17 14:03:38 +01:00

53 lines
1.6 KiB
Ada

private with Ada.Finalization;
limited with Envs;
limited with Types.Mal;
limited with Types.Sequences;
limited with Types.Symbols;
package Types.Fns is
type Ptr is tagged private;
-- A pointer to an user-defined function or macro.
function New_Function (Params : in Sequences.Ptr;
Ast : in Mal.T;
Env : in Envs.Closure_Ptr) return Mal.T
with Inline;
-- Raise an exception if Params contains something else than symbols.
function New_Macro (Item : in Ptr) return Mal.T with Inline;
function Params (Item : in Ptr) return Symbols.Symbol_Array with Inline;
function Ast (Item : in Ptr) return Mal.T with Inline;
-- Useful to print.
function Apply (Item : in Ptr;
Args : in Mal.T_Array) return Mal.T with Inline;
-- Fails for macros.
function Env (Item : in Ptr) return Envs.Closure_Ptr with Inline;
-- Fails for macros. Required for TCO, instead of Apply.
function Meta (Item : in Ptr) return Mal.T with Inline;
-- Fails for macros.
function With_Meta (Item : in Ptr;
Metadata : in Mal.T) return Mal.T with Inline;
-- Fails for macros.
procedure Check_Allocations;
private
type Rec;
type Acc is access Rec;
type Ptr is new Ada.Finalization.Controlled with record
Ref : Acc := null;
end record
with Invariant => Ptr.Ref /= null;
overriding procedure Adjust (Object : in out Ptr) with Inline;
overriding procedure Finalize (Object : in out Ptr) with Inline;
pragma Finalize_Storage_Only (Ptr);
end Types.Fns;