Skip to content

Commit

Permalink
RakuAST: move Literal.new functionality to Literal.from-value
Browse files Browse the repository at this point in the history
After consultation with jnthn, it was felt that the auto-determining
the type by default, would be overhead that wouldn't be needed coming
from the grammar.  So make .new less smart, and introduce a .from-value
method with the old semantics of .new
  • Loading branch information
lizmat committed Jun 6, 2023
1 parent dd475ed commit 9b207ab
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Raku/ast/literals.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ class RakuAST::Literal
is RakuAST::Term
is RakuAST::CompileTimeValue
{
has str $!typename;
has Str $!typename;
has Mu $.value;

method new(Mu $value) {
nqp::die("Please use RakuAST::Literal.from-value()")
if nqp::eqaddr(self,RakuAST::Literal);

my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Literal, '$!value', $value);
nqp::bindattr($obj, RakuAST::Literal, '$!typename', nqp::null);
$obj
}

method from-value(Mu $value) {
my $typename := $value.HOW.name($value);
my $obj := nqp::create(nqp::eqaddr(self,RakuAST::Literal)
?? RakuAST.WHO{$typename ~ 'Literal'}
!! self
);
nqp::bindattr( $obj, RakuAST::Literal, '$!value', $value);
nqp::bindattr_s($obj, RakuAST::Literal, '$!typename', $typename);
my $obj := nqp::create(RakuAST.WHO{$typename ~ 'Literal'});
nqp::bindattr($obj, RakuAST::Literal, '$!value', $value);
nqp::bindattr($obj, RakuAST::Literal, '$!typename', $typename);
$obj
}

Expand All @@ -28,7 +35,15 @@ class RakuAST::Literal
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier($!typename))
RakuAST::Type::Simple.new(
RakuAST::Name.from-identifier(
nqp::ifnull(
$!typename,
nqp::bindattr(self,RakuAST::Literal,'$!typename',
$!value.HOW.name($!value))
)
)
)
}

# default for non int/str/num literals
Expand Down

0 comments on commit 9b207ab

Please sign in to comment.