From 8bdc8232f91dafa52bb97f4a3cbd02bbeabbe8b8 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Wed, 7 Apr 2021 23:31:28 +0200 Subject: [PATCH] Add loop macro and a test --- lib/Minilisp.pm | 19 +++++++++++++++++++ t/loop.t | 6 ++++++ 2 files changed, 25 insertions(+) create mode 100644 t/loop.t diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 99d5f05..dec835d 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -977,6 +977,25 @@ sub macro_return { } $macros{'return'} = \¯o_return; +sub macro_loop { + my $ts = shift; + + my $inner = macro_progn($ts); + + return create_block( + "nil", + sub { + my $ctx = shift; + + while (1) + { + $inner->($ctx); + } + } + ); +} +$macros{loop} = \¯o_loop; + sub compile { my ($term) = @_; my @tokens = tokenize($term); diff --git a/t/loop.t b/t/loop.t new file mode 100644 index 0000000..76149bd --- /dev/null +++ b/t/loop.t @@ -0,0 +1,6 @@ +(expect "loop" + (equal (let ((i 0)) + (loop (set i (+ i 1)) + (when (> i 3) + (return i)))) + 4))