Add loop macro and a test

This commit is contained in:
madmaurice 2021-04-07 23:31:28 +02:00
parent 2e784310fb
commit 8bdc8232f9
2 changed files with 25 additions and 0 deletions

View file

@ -977,6 +977,25 @@ sub macro_return {
} }
$macros{'return'} = \&macro_return; $macros{'return'} = \&macro_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} = \&macro_loop;
sub compile { sub compile {
my ($term) = @_; my ($term) = @_;
my @tokens = tokenize($term); my @tokens = tokenize($term);

6
t/loop.t Normal file
View file

@ -0,0 +1,6 @@
(expect "loop"
(equal (let ((i 0))
(loop (set i (+ i 1))
(when (> i 3)
(return i))))
4))