Implement set

This commit is contained in:
madmaurice 2021-04-04 23:13:17 +02:00
parent 0ec88feeb6
commit 61df548e00
2 changed files with 31 additions and 0 deletions

View file

@ -743,6 +743,25 @@ sub macro_unless {
} }
$macros{unless} = \&macro_unless; $macros{unless} = \&macro_unless;
sub macro_set {
my $ts = shift;
my $ident = parser_identifier($ts);
my $expr = parser_expr($ts);
return sub {
my $ctx = shift;
my $value = $expr->($ctx);
ctx_assign($ctx, $ident, $value);
return $value;
}
}
$macros{set} = \&macro_set;
sub compile { sub compile {
my ($term) = @_; my ($term) = @_;
my @tokens = tokenize($term); my @tokens = tokenize($term);

12
t/context.t Normal file
View file

@ -0,0 +1,12 @@
(let ((a 2))
(let ((a 10))
(expect "Inner variable shadows outer" (= a 10)))
(expect "Outer is not overriden" (= a 2)))
(let ((pings 0))
(defun ping ()
(set pings (+ pings 1)))
(ping) (ping) (ping)
(expect "setting variables in outside context works" (= pings 3)))