Skip to content

Commit

Permalink
Introduce RakuAST::Literal class
Browse files Browse the repository at this point in the history
To keep all of the common stuff in the xxxLiteral classes in one place
and to allow for easier checking for *any* RakuAST literal

Also: normalise RakuAST class specifications
  • Loading branch information
lizmat committed Feb 5, 2023
1 parent 0d9f70a commit 33c2b28
Showing 1 changed file with 37 additions and 59 deletions.
96 changes: 37 additions & 59 deletions src/Raku/ast/literals.rakumod
@@ -1,4 +1,17 @@
class RakuAST::IntLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
# Marker for all compile-time literals
class RakuAST::Literal
is RakuAST::Term
is RakuAST::CompileTimeValue
{
method type() { self.value.WHAT }
method compile-time-value() { self.value }
method IMPL-CAN-INTERPRET() { True }
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { self.value }
}

class RakuAST::IntLiteral
is RakuAST::Literal
{
has Int $.value;

method new(Int $value) {
Expand All @@ -7,10 +20,6 @@ class RakuAST::IntLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$obj
}

method type {
$!value.WHAT
}

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier('Int'))
}
Expand All @@ -23,15 +32,11 @@ class RakuAST::IntLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
?? $wval
!! QAST::Want.new( $wval, 'Ii', QAST::IVal.new( :value(nqp::unbox_i($value)) ) )
}

method compile-time-value() { $!value }

method IMPL-CAN-INTERPRET() { True }

method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
}

class RakuAST::NumLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
class RakuAST::NumLiteral
is RakuAST::Literal
{
has Num $.value;

method new(Num $value) {
Expand All @@ -40,10 +45,6 @@ class RakuAST::NumLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$obj
}

method type {
$!value.WHAT
}

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier('Num'))
}
Expand All @@ -54,15 +55,11 @@ class RakuAST::NumLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
my $wval := QAST::WVal.new( :$value );
QAST::Want.new( $wval, 'Nn', QAST::NVal.new( :value(nqp::unbox_n($value)) ) )
}

method compile-time-value() { $!value }

method IMPL-CAN-INTERPRET() { True }

method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
}

class RakuAST::RatLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
class RakuAST::RatLiteral
is RakuAST::Literal
{
has Rat $.value;

method new(Rat $value) {
Expand All @@ -71,10 +68,6 @@ class RakuAST::RatLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$obj
}

method type {
$!value.WHAT
}

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier('Rat'))
}
Expand All @@ -84,15 +77,11 @@ class RakuAST::RatLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}

method compile-time-value() { $!value }

method IMPL-CAN-INTERPRET() { True }

method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
}

class RakuAST::VersionLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
class RakuAST::VersionLiteral
is RakuAST::Literal
{
has Any $.value;

method new($value) {
Expand All @@ -101,10 +90,6 @@ class RakuAST::VersionLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$obj
}

method type {
$!value.WHAT
}

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier('Version'))
}
Expand All @@ -114,18 +99,14 @@ class RakuAST::VersionLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}

method compile-time-value() { $!value }

method IMPL-CAN-INTERPRET() { True }

method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
}

# A StrLiteral is a basic string literal without any kind of interpolation
# taking place. It may be placed in the tree directly, but a compiler will
# typically emit it in a quoted string wrapper.
class RakuAST::StrLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
class RakuAST::StrLiteral
is RakuAST::Literal
{
has Str $.value;

method new(Str $value) {
Expand All @@ -134,10 +115,6 @@ class RakuAST::StrLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
$obj
}

method type {
$!value.WHAT
}

method ast-type {
RakuAST::Type::Simple.new(RakuAST::Name.from-identifier('Str'))
}
Expand All @@ -152,20 +129,17 @@ class RakuAST::StrLiteral is RakuAST::Term is RakuAST::CompileTimeValue {
my $wval := QAST::WVal.new( :$value );
QAST::Want.new( $wval, 'Ss', QAST::SVal.new( :value(nqp::unbox_s($value)) ) )
}

method compile-time-value() { $!value }

method IMPL-CAN-INTERPRET() { True }

method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) { $!value }
}

# A quoted string consists of a sequence of segments that should be evaluated
# (if needed) and concatenated. Processing may be applied to the result (these
# are "words", "quotewords", "val", and "exec", and are applied in the order
# that they are specified here).
class RakuAST::QuotedString is RakuAST::ColonPairish is RakuAST::Term
is RakuAST::ImplicitLookups {
class RakuAST::QuotedString
is RakuAST::ColonPairish
is RakuAST::Term
is RakuAST::ImplicitLookups
{
has Mu $!segments;
has Mu $!processors;

Expand Down Expand Up @@ -469,7 +443,9 @@ class RakuAST::QuotedString is RakuAST::ColonPairish is RakuAST::Term
}
}

class RakuAST::Heredoc is RakuAST::QuotedString {
class RakuAST::Heredoc
is RakuAST::QuotedString
{
has Str $!stop;
has int $!indent;

Expand Down Expand Up @@ -550,7 +526,9 @@ class RakuAST::Heredoc is RakuAST::QuotedString {

# An atom in a quote words construct. By wrapping something in this, it is
# considered indivisible in words processing.
class RakuAST::QuoteWordsAtom is RakuAST::Node {
class RakuAST::QuoteWordsAtom
is RakuAST::Node
{
has RakuAST::Node $.atom;

method new(RakuAST::Node $atom) {
Expand Down

0 comments on commit 33c2b28

Please sign in to comment.