| 1 | package Psh::Builtins::If; | ||
| 2 | |||
| 3 | require Psh::Parser; | ||
| 4 | require Psh; | ||
| 5 | require Psh::Util; | ||
| 6 | |||
| 7 | sub bi_if { | ||
| 8 | my @words=@{$_[1]}; | ||
| 9 | my $success=0; | ||
| 10 | my @result=(0,undef); | ||
| 11 | my @cond; | ||
| 12 | |||
| 13 | TRY: while (@words>0) { | ||
| 14 | @cond=(); | ||
| 15 | while (@words>0) { | ||
| 16 | last if substr($words[0],0,1) eq '{'; | ||
| 17 | push @cond, shift @words; | ||
| 18 | } | ||
| 19 | unless (@words) { | ||
| 20 | Psh::Util::print_error("Missing action for if\n"); | ||
| 21 | return (0,undef); | ||
| 22 | } | ||
| 23 | my $cond= join(' ',@cond); | ||
| 24 | ($success)= Psh::evl(Psh::Parser::ungroup($cond)); | ||
| 25 | $Psh::Builtins::If::last_success= $success; | ||
| 26 | |||
| 27 | if ($success) { | ||
| 28 | return Psh::evl(Psh::Parser::ungroup(shift @words)); | ||
| 29 | } else { | ||
| 30 | shift @words; # ignore the if-block | ||
| 31 | my $next= shift @words; | ||
| 32 | next TRY if $next eq 'elsif'; | ||
| 33 | if ($next eq 'else') { | ||
| 34 | return Psh::evl(Psh::Parser::ungroup(shift @words)); | ||
| 35 | } | ||
| 36 | return (0,undef); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | return (1,undef); | ||
| 40 | } | ||
| 41 | |||
| 42 | 1; |