Implement set
This commit is contained in:
parent
0ec88feeb6
commit
61df548e00
2 changed files with 31 additions and 0 deletions
|
@ -743,6 +743,25 @@ sub macro_unless {
|
||||||
}
|
}
|
||||||
$macros{unless} = \¯o_unless;
|
$macros{unless} = \¯o_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} = \¯o_set;
|
||||||
|
|
||||||
sub compile {
|
sub compile {
|
||||||
my ($term) = @_;
|
my ($term) = @_;
|
||||||
my @tokens = tokenize($term);
|
my @tokens = tokenize($term);
|
||||||
|
|
12
t/context.t
Normal file
12
t/context.t
Normal 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)))
|
Loading…
Reference in a new issue