Skip to content

Commit

Permalink
Make sure a ternary with heredocs deparses ok
Browse files Browse the repository at this point in the history
This is a bit tricky, as the ternary should first list all of the
headers of any heredocs, before generating the bodies.  Sadly, this
does **NOT** yet handle multiple nested ternaries yet
  • Loading branch information
lizmat committed Jan 3, 2023
1 parent b5df6e2 commit 5f7ff01
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/core.c/RakuAST/Deparse.pm6
Expand Up @@ -1454,13 +1454,31 @@ class RakuAST::Deparse {
}

multi method deparse(RakuAST::Ternary:D $ast --> str) {
nqp::join('',nqp::list_s(
self.deparse($ast.condition),
$.ternary1,
self.deparse($ast.then),
$.ternary2,
self.deparse($ast.else)
))
my $parts := nqp::list_s(self.deparse($ast.condition),$.ternary1);
my str $heredoc;

if nqp::istype((my $then := $ast.then),RakuAST::Heredoc) {
my str ($header,$rest) = self.deparse($then).split("\n",2);
nqp::push_s($parts,$header);
$heredoc = nqp::concat("\n",$rest);
}
else {
nqp::push_s($parts,self.deparse($then));
}

nqp::push_s($parts,$.ternary2);

if nqp::istype((my $else := $ast.else),RakuAST::Heredoc) {
my str ($header,$rest) = self.deparse($else).split("\n",2);
nqp::push_s($parts,$header);
nqp::push_s($parts,nqp::concat($heredoc ?? $heredoc !! "\n",$rest));
}
else {
nqp::push_s($parts,self.deparse($else));
nqp::push_s($parts,$heredoc);
}

nqp::join('',$parts)
}

multi method deparse(RakuAST::Trait::Is:D $ast --> str) {
Expand Down

0 comments on commit 5f7ff01

Please sign in to comment.