Add cond macro
This commit is contained in:
parent
ed59a6a04e
commit
0b19227810
|
@ -603,6 +603,43 @@ sub macro_defun {
|
|||
}
|
||||
$macros{defun} = \¯o_defun;
|
||||
|
||||
sub macro_cond {
|
||||
my $ts = shift;
|
||||
|
||||
my @cases;
|
||||
while ($ts->[0]->{type} != RPAREN)
|
||||
{
|
||||
die "Expected ( before case in cond"
|
||||
unless (shift @$ts)->{type} == LPAREN;
|
||||
|
||||
my $condition = parser_expr($ts);
|
||||
my $work = parser_expr($ts);
|
||||
|
||||
die "Expected ) after case in cond"
|
||||
unless (shift @$ts)->{type} == RPAREN;
|
||||
|
||||
push @cases, {
|
||||
condition => $condition,
|
||||
work => $work,
|
||||
};
|
||||
}
|
||||
|
||||
return sub {
|
||||
my $ctx = shift;
|
||||
|
||||
foreach my $case (@cases)
|
||||
{
|
||||
if($case->{condition}->($ctx))
|
||||
{
|
||||
return $case->{work}->($ctx);
|
||||
}
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
}
|
||||
$macros{cond} = \¯o_cond;
|
||||
|
||||
sub compile {
|
||||
my ($term) = @_;
|
||||
my @tokens = tokenize($term);
|
||||
|
|
Loading…
Reference in a new issue