Add when, unless
This commit is contained in:
parent
0b19227810
commit
e163fdce59
3 changed files with 48 additions and 0 deletions
|
@ -640,6 +640,44 @@ sub macro_cond {
|
|||
}
|
||||
$macros{cond} = \¯o_cond;
|
||||
|
||||
sub macro_when {
|
||||
my $ts = shift;
|
||||
|
||||
my $condition = parser_expr($ts);
|
||||
my $work = parser_expr($ts);
|
||||
|
||||
return sub {
|
||||
my $ctx = shift;
|
||||
|
||||
if ($condition->($ctx))
|
||||
{
|
||||
return $work->($ctx);
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
$macros{when} = \¯o_when;
|
||||
|
||||
sub macro_unless {
|
||||
my $ts = shift;
|
||||
|
||||
my $condition = parser_expr($ts);
|
||||
my $work = parser_expr($ts);
|
||||
|
||||
return sub {
|
||||
my $ctx = shift;
|
||||
|
||||
if (!$condition->($ctx))
|
||||
{
|
||||
return $work->($ctx);
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
$macros{unless} = \¯o_unless;
|
||||
|
||||
sub compile {
|
||||
my ($term) = @_;
|
||||
my @tokens = tokenize($term);
|
||||
|
|
5
t/unless.t
Normal file
5
t/unless.t
Normal file
|
@ -0,0 +1,5 @@
|
|||
(expect "unless - true condition"
|
||||
(null (unless t 'fail)))
|
||||
|
||||
(expect "unless - false condition"
|
||||
(equal 'ok (unless f 'ok)))
|
5
t/when.t
Normal file
5
t/when.t
Normal file
|
@ -0,0 +1,5 @@
|
|||
(expect "when - true condition"
|
||||
(equal 'ok (when t 'ok)))
|
||||
|
||||
(expect "when - false condition"
|
||||
(null (when f 'fail)))
|
Loading…
Reference in a new issue