Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement Complex literals
This generalizes the serialization for 'rational'
to any setting type that can be instantiated as
$type_object.new(|@primitives).
  • Loading branch information
moritz committed Jul 1, 2011
1 parent cb31488 commit ae04dda
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/Perl6/Actions.pm
Expand Up @@ -2672,13 +2672,9 @@ class Perl6::Actions is HLL::Actions {
}

method number:sym<complex>($/) {
# XXX Work out at compile time, then...
# make $*ST.add_constant('Complex', 'complex', [$re, $im]);
make PAST::Op.new(
:pasttype('callmethod'), :name('new'),
PAST::Var.new( :name('Complex'), :namespace(''), :scope('package') ),
($<re> ?? $<re>.ast !! 0), $<im>.ast
);
my $re := $*ST.add_constant('Num', 'num', 0e0);
my $im := $*ST.add_constant('Num', 'num', +~$<im>);
make $*ST.add_constant('Complex', 'type_new', $re<compile_time_value>, $im<compile_time_value>);
}

method number:sym<numish>($/) {
Expand Down Expand Up @@ -2715,7 +2711,7 @@ class Perl6::Actions is HLL::Actions {
# TODO: strip trailing zeros from $frac
my $nu := $*ST.add_constant('Int', 'int', +($int ~ $frac));
my $de := $*ST.add_constant('Int', 'int', pir::set__In(nqp::pow_n(10, nqp::chars($frac))));
make $*ST.add_constant('Rat', 'rational', $nu<compile_time_value>, $de<compile_time_value>);
make $*ST.add_constant('Rat', 'type_new', $nu<compile_time_value>, $de<compile_time_value>);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/Perl6/SymbolTable.pm
Expand Up @@ -713,13 +713,14 @@ class Perl6::SymbolTable is HLL::Compiler::SerializationContextBuilder {
$constant := pir::repr_box_num__PnP(@value[0], $type_obj);
$des := PAST::Op.new( :pirop('repr_box_num PnP'), @value[0], $type_obj_lookup );
}
elsif $primitive eq 'rational' {
$constant := $type_obj.new(@value[0], @value[1]);
elsif $primitive eq 'type_new' {
$constant := $type_obj.new(|@value);
$des := PAST::Op.new(
:pasttype('callmethod'), :name('new'),
$type_obj_lookup,
self.get_object_sc_ref_past(@value[0]),
self.get_object_sc_ref_past(@value[1]));
$type_obj_lookup
);
nqp::push($des, self.get_object_sc_ref_past(nqp::shift(@value)))
while @value;
}
else {
pir::die("Don't know how to build a $primitive constant");
Expand Down
2 changes: 1 addition & 1 deletion src/core/Complex.pm
Expand Up @@ -113,6 +113,6 @@ multi sub infix:</>(Real \$a, Complex \$b) {
proto postfix:<i>(|$) { * }
multi postfix:<i>(Real \$a) { Complex.new(0e0, $a); }
multi postfix:<i>(Complex \$a) { Complex.new(-$a.re, $a.im) }
multi postfix:<i>(Numeric \$a) { $a * 1i }
multi postfix:<i>(Numeric \$a) { $a * Complex.new(0e0, 1e0) }

# vim: ft=perl6

0 comments on commit ae04dda

Please sign in to comment.