Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] add $!type and $!result parameters to DNST::If, so the resul…
…t temporary can optionally be ignored/left out,

and if not, the type can be set (other than the default RakudoObject.
  • Loading branch information
diakopter committed Nov 24, 2010
1 parent b34f13f commit ecb0d09
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 15 additions & 1 deletion dotnet/compiler/DNST.pm
Expand Up @@ -262,17 +262,31 @@ class DNST::Throw is DNST::Node {
}

class DNST::If is DNST::Node {
has $!type;
has $!bool;
has $!result;

method bool($set?) {
if $set { $!bool := $set }
$!bool
}

method new(:$bool, *@children) {
method type($set?) {
if $set { $!type := $set }
$!type
}

method result($set?) {
if $set { $!result := $set }
$!result
}

method new(:$bool, :$type, :$result, *@children) {
my $obj := self.CREATE;
$obj.set_children(@children);
$obj.bool($bool);
$obj.type(pir::defined($type) ?? $type !! 'RakudoObject');
$obj.result(pir::defined($result) ?? $result !! 1);
$obj;
}
}
Expand Down
15 changes: 8 additions & 7 deletions dotnet/compiler/DNST2CSharp.pm
Expand Up @@ -191,28 +191,29 @@ our multi sub cs_for(DNST::If $if) {
unless +@($if) >= 2 { pir::die('A DNST::If node must have at least 2 children') }

# Need a variable to put the final result in.
my $if_result := get_unique_id('if_result');
my $if_result := get_unique_id('if_result') if $if.result;

# Get the conditional and emit if.
my $code := cs_for((@($if))[0]);
$code := $code ~
" RakudoObject $if_result = null;\n" ~
" " ~ $if.type ~ " $if_result = null;\n" if $if.result;
$code := $code ~
" if ($*LAST_TEMP" ~ ($if.bool ?? "" !! " != 0") ~ ") \{\n";

# Compile branch(es).
$*LAST_TEMP := 'null';
$code := $code ~ cs_for((@($if))[1]);
$code := $code ~ " $if_result = $*LAST_TEMP;\n" ~
" }\n";
$code := $code ~ " $if_result = $*LAST_TEMP;\n" if $if.result;
$code := $code ~ " }\n";
if +@($if) == 3 {
$*LAST_TEMP := 'null';
$code := $code ~ " else \{\n";
$code := $code ~ cs_for((@($if))[2]);
$code := $code ~ " $if_result = $*LAST_TEMP;\n" ~
" }\n";
$code := $code ~ " $if_result = $*LAST_TEMP;\n" if $if.result;
$code := $code ~ " }\n";
}

$*LAST_TEMP := $if_result;
$*LAST_TEMP := $if_result if $if.result;
return $code;
}

Expand Down

0 comments on commit ecb0d09

Please sign in to comment.