Skip to content

Commit

Permalink
RakuAST: add deparse/raku/test for RakuAST::MetaInfix::Zip
Browse files Browse the repository at this point in the history
Also add a RakuAST::MetaInfix marker base class to allow typechecking
on any RakuAST::MetaInfix::xxx meta op (as ApplyListInfix deparsing
needs to be able to differentiate on that)
  • Loading branch information
lizmat committed Mar 23, 2023
1 parent 81f2ea2 commit fb30afa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/Raku/ast/expressions.rakumod
Expand Up @@ -365,9 +365,13 @@ class RakuAST::Infix
}
}

# Meta infixes base class, mostly for type checking
class RakuAST::MetaInfix
is RakuAST::Infixish { }

# An assign meta-operator, operator on another infix.
class RakuAST::MetaInfix::Assign
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;

Expand Down Expand Up @@ -494,7 +498,7 @@ class RakuAST::FunctionInfix

# A negate meta-operator.
class RakuAST::MetaInfix::Negate
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;

Expand Down Expand Up @@ -530,7 +534,7 @@ class RakuAST::MetaInfix::Negate

# A reverse meta-operator.
class RakuAST::MetaInfix::Reverse
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;

Expand All @@ -557,7 +561,7 @@ class RakuAST::MetaInfix::Reverse

# A cross meta-operator.
class RakuAST::MetaInfix::Cross
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;

Expand Down Expand Up @@ -589,7 +593,7 @@ class RakuAST::MetaInfix::Cross

# A zip meta-operator.
class RakuAST::MetaInfix::Zip
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;

Expand Down Expand Up @@ -621,7 +625,7 @@ class RakuAST::MetaInfix::Zip

# An infix hyper operator.
class RakuAST::MetaInfix::Hyper
is RakuAST::Infixish
is RakuAST::MetaInfix
{
has RakuAST::Infixish $.infix;
has Bool $.dwim-left;
Expand Down
11 changes: 9 additions & 2 deletions src/core.c/RakuAST/Deparse.pm6
Expand Up @@ -329,9 +329,12 @@ class RakuAST::Deparse {
}

multi method deparse(RakuAST::ApplyListInfix:D $ast --> Str:D) {
my str $operator = $ast.infix.operator;
my str @parts = $ast.operands.map({ self.deparse($_) });
my $infix := $ast.infix;
my str $operator = nqp::istype($infix,RakuAST::MetaInfix)
?? (' ' ~ self.deparse($infix))
!! $infix.operator;

my str @parts = $ast.operands.map({ self.deparse($_) });
@parts
?? $operator eq ','
?? @parts == 1
Expand Down Expand Up @@ -584,6 +587,10 @@ class RakuAST::Deparse {
'R' ~ self.deparse($ast.infix)
}

multi method deparse(RakuAST::MetaInfix::Zip:D $ast --> Str:D) {
'Z' ~ self.deparse($ast.infix)
}

multi method deparse(RakuAST::Method:D $ast --> Str:D) {
self!method($ast, 'method')
}
Expand Down
4 changes: 4 additions & 0 deletions src/core.c/RakuAST/Raku.pm6
Expand Up @@ -379,6 +379,10 @@ augment class RakuAST::Node {
self!positional(self.infix)
}

multi method raku(RakuAST::MetaInfix::Zip:D: --> Str:D) {
self!positional(self.infix)
}

multi method raku(RakuAST::Method:D: --> Str:D) {
my str @nameds = 'name';
@nameds.unshift("multiness") if self.multiness;
Expand Down
43 changes: 42 additions & 1 deletion t/12-rakuast/meta-operators.rakutest
@@ -1,7 +1,7 @@
use v6.e.PREVIEW;
use Test;

plan 6;
plan 7;

my $ast;
my $deparsed;
Expand Down Expand Up @@ -49,6 +49,47 @@ subtest 'Reverse meta-op evaluates to expected value' => {
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}

subtest 'Zip meta-op evaluates to expected value' => {
# (1, 2) Z== (1, 3)
ast RakuAST::ApplyListInfix.new(
infix => RakuAST::MetaInfix::Zip.new(
RakuAST::Infix.new("==")
),
operands => (
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(2),
)
)
)
)
),
RakuAST::Circumfix::Parentheses.new(
RakuAST::SemiList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyListInfix.new(
infix => RakuAST::Infix.new(","),
operands => (
RakuAST::IntLiteral.new(1),
RakuAST::IntLiteral.new(3),
)
)
)
)
),
)
);

is-deeply $deparsed, '(1, 2) Z== (1, 3)', 'deparse';
is-deeply $_, (True, False), @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}

subtest 'Assignment meta-op with short-circuit || evaluates to true LHS' => {
my $test = 10;
my $update = 2;
Expand Down

0 comments on commit fb30afa

Please sign in to comment.