Skip to content

Commit

Permalink
Merge eval-ast and macro expansion into EVAL
Browse files Browse the repository at this point in the history
See issue kanaka#587.
 * Merge eval-ast and eval into a single conditional.
 * Print "EVAL: $ast" at the top of EVAL.
 * 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).
 * Remove macroexpand and quasiquoteexpand special forms.

These tests fail:
 * objpascal: I have no such error with a more recent compiler
 * groovy: stack overflow during test of recursive functions.
   Did work after first push.
 * elm: most probably unrelated, this directory is unchanged

While reformatting process/step*.txt, use a pseudo-syntax inspired by
pattern matching.

The changes have not been propagated to:
 * ada 2-9
 * awk 2-9
 * bash 2-9
 * basic 2-A
 * bbc-basic 2-9
 * chuck 2-A
 * clojure 2-9
 * coffee 2-9
 * common-lisp 2-9
 * cpp 2-9
 * crystal 2-A
 * cs 2-9
 * d 2-9
 * dart 2-9
 * elixir 2-9
 * elm 2-9
 * erlang 2-9
 * es6 2-9
 * factor 2-9
 * fantom 2-9
 * fennel 2-9
 * forth 2-A
 * fsharp 2-9
 * gnu-smalltalk 2-9
 * go 2-9
 * groovy 2-9
 * guile 2-9
 * haxe 2-9
 * hy 2-A
 * io 2-A
 * janet 2-A
 * java 2-9
 * java-truffle 2-A
 * jq 2-A
 * js 2-9
 * julia 2-9
 * kotlin 2-9
 * livescript 2-A
 * logo 2-9
 * lua 2-A
 * make 2-9
 * matlab 2-A
 * miniMAL 2-9
 * nasm 2-9
 * nim 2-9
 * objc 2-A
 * objpascal 2-9
 * perl6 2-9
 * php 2-9
 * picolisp 2-9
 * pike 2-9
 * plpgsql 2-A
 * plsql 2-A
 * powershell 2-A
 * ps 2-9
 * python.2 2-9
 * r 2-9
 * racket 2-9
 * rexx 2-A
 * rpython 2-9
 * rust 2-9
 * scala 2-A
 * scheme 2-9
 * skew 2-9
 * sml 2-9
 * swift 2-A
 * swift3 2-A
 * swift4 2-A
 * swift5 2-A
 * tcl 2-9
 * ts 2-9
 * vala 2-A
 * vb 2-9
 * vhdl 2-9
 * vimscript 2-9
 * wasm 2-A
 * wren 2-9
 * xslt 2-A
 * yorick 2-9
 * zig 2-A
  • Loading branch information
asarhaddon committed Dec 13, 2021
1 parent 23dddd9 commit e612101
Show file tree
Hide file tree
Showing 146 changed files with 2,958 additions and 5,945 deletions.
3 changes: 0 additions & 3 deletions impls/ada.2/step7_quote.adb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ procedure Step7_Quote is
Ast => Ast.Sequence.all.Data (3),
Env => Env));
end;
elsif First.Str.all = "quasiquoteexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
return Quasiquote (Ast.Sequence.all.Data (2));
elsif First.Str.all = "quasiquote" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Ast := Quasiquote (Ast.Sequence.all.Data (2));
Expand Down
37 changes: 5 additions & 32 deletions impls/ada.2/step8_macros.adb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ procedure Step8_Macros is
-- True when the environment has been created in this recursion
-- level, and has not yet been referenced by a closure. If so,
-- we can reuse it instead of creating a subenvironment.
Macroexpanding : Boolean := False;
First : Types.T;
begin
<<Restart>>
Expand Down Expand Up @@ -183,14 +182,6 @@ procedure Step8_Macros is
Ast => Ast.Sequence.all.Data (3),
Env => Env));
end;
elsif First.Str.all = "macroexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Macroexpanding := True;
Ast := Ast.Sequence.all.Data (2);
goto Restart;
elsif First.Str.all = "quasiquoteexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
return Quasiquote (Ast.Sequence.all.Data (2));
elsif First.Str.all = "quasiquote" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Ast := Quasiquote (Ast.Sequence.all.Data (2));
Expand All @@ -217,24 +208,10 @@ procedure Step8_Macros is
case First.Kind is
when Kind_Macro =>
-- Use the unevaluated arguments.
if Macroexpanding then
-- Evaluate the macro with tail call optimization.
if not Env_Reusable then
Env := Envs.New_Env (Outer => First.Fn.all.Env);
Env_Reusable := True;
end if;
Env.all.Set_Binds
(Binds => First.Fn.all.Params.all.Data,
Exprs => Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
Ast := First.Fn.all.Ast;
goto Restart;
else
-- Evaluate the macro normally.
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
end if;
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
when Types.Kind_Function =>
null;
when others =>
Expand All @@ -260,11 +237,7 @@ procedure Step8_Macros is
end;
exception
when Err.Error =>
if Macroexpanding then
Err.Add_Trace_Line ("macroexpand", Ast);
else
Err.Add_Trace_Line ("eval", Ast);
end if;
Err.Add_Trace_Line ("eval", Ast);
raise;
end Eval;

Expand Down
37 changes: 5 additions & 32 deletions impls/ada.2/step9_try.adb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ procedure Step9_Try is
-- True when the environment has been created in this recursion
-- level, and has not yet been referenced by a closure. If so,
-- we can reuse it instead of creating a subenvironment.
Macroexpanding : Boolean := False;
First : Types.T;
begin
<<Restart>>
Expand Down Expand Up @@ -183,14 +182,6 @@ procedure Step9_Try is
Ast => Ast.Sequence.all.Data (3),
Env => Env));
end;
elsif First.Str.all = "macroexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Macroexpanding := True;
Ast := Ast.Sequence.all.Data (2);
goto Restart;
elsif First.Str.all = "quasiquoteexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
return Quasiquote (Ast.Sequence.all.Data (2));
elsif First.Str.all = "quasiquote" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Ast := Quasiquote (Ast.Sequence.all.Data (2));
Expand Down Expand Up @@ -247,24 +238,10 @@ procedure Step9_Try is
case First.Kind is
when Kind_Macro =>
-- Use the unevaluated arguments.
if Macroexpanding then
-- Evaluate the macro with tail call optimization.
if not Env_Reusable then
Env := Envs.New_Env (Outer => First.Fn.all.Env);
Env_Reusable := True;
end if;
Env.all.Set_Binds
(Binds => First.Fn.all.Params.all.Data,
Exprs => Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
Ast := First.Fn.all.Ast;
goto Restart;
else
-- Evaluate the macro normally.
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
end if;
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
when Types.Kind_Function =>
null;
when others =>
Expand All @@ -290,11 +267,7 @@ procedure Step9_Try is
end;
exception
when Err.Error =>
if Macroexpanding then
Err.Add_Trace_Line ("macroexpand", Ast);
else
Err.Add_Trace_Line ("eval", Ast);
end if;
Err.Add_Trace_Line ("eval", Ast);
raise;
end Eval;

Expand Down
37 changes: 5 additions & 32 deletions impls/ada.2/stepa_mal.adb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ procedure StepA_Mal is
-- True when the environment has been created in this recursion
-- level, and has not yet been referenced by a closure. If so,
-- we can reuse it instead of creating a subenvironment.
Macroexpanding : Boolean := False;
First : Types.T;
begin
<<Restart>>
Expand Down Expand Up @@ -184,14 +183,6 @@ procedure StepA_Mal is
Ast => Ast.Sequence.all.Data (3),
Env => Env));
end;
elsif First.Str.all = "macroexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Macroexpanding := True;
Ast := Ast.Sequence.all.Data (2);
goto Restart;
elsif First.Str.all = "quasiquoteexpand" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
return Quasiquote (Ast.Sequence.all.Data (2));
elsif First.Str.all = "quasiquote" then
Err.Check (Ast.Sequence.all.Length = 2, "expected 1 parameter");
Ast := Quasiquote (Ast.Sequence.all.Data (2));
Expand Down Expand Up @@ -248,24 +239,10 @@ procedure StepA_Mal is
case First.Kind is
when Kind_Macro =>
-- Use the unevaluated arguments.
if Macroexpanding then
-- Evaluate the macro with tail call optimization.
if not Env_Reusable then
Env := Envs.New_Env (Outer => First.Fn.all.Env);
Env_Reusable := True;
end if;
Env.all.Set_Binds
(Binds => First.Fn.all.Params.all.Data,
Exprs => Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
Ast := First.Fn.all.Ast;
goto Restart;
else
-- Evaluate the macro normally.
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
end if;
Ast := First.Fn.all.Apply
(Ast.Sequence.all.Data (2 .. Ast.Sequence.all.Length));
-- Then evaluate the result with TCO.
goto Restart;
when Types.Kind_Function =>
null;
when others =>
Expand Down Expand Up @@ -296,11 +273,7 @@ procedure StepA_Mal is
end;
exception
when Err.Error =>
if Macroexpanding then
Err.Add_Trace_Line ("macroexpand", Ast);
else
Err.Add_Trace_Line ("eval", Ast);
end if;
Err.Add_Trace_Line ("eval", Ast);
raise;
end Eval;

Expand Down

0 comments on commit e612101

Please sign in to comment.