Skip to content

Commit

Permalink
Add deparsing / tests for RakuAST::Postfix::Power
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jan 12, 2023
1 parent 6974696 commit 1b5b575
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/core.c/RakuAST/Deparse.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,10 @@ class RakuAST::Deparse {
$ast.operator
}

multi method deparse(RakuAST::Postfix::Power:D $ast --> Str:D) {
$ast.power.trans('-0123456789' => '⁻⁰¹²³⁴⁵⁶⁷⁸⁹')
}

multi method deparse(RakuAST::Prefix:D $ast --> Str:D) {
$ast.operator
}
Expand Down
1 change: 0 additions & 1 deletion t/12-rakuast/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ RakuAST::Origin
RakuAST::Origin::Match
RakuAST::Origin::Source
RakuAST::ParameterDefaultThunk
RakuAST::Postfix::Power
RakuAST::QuoteWordsAtom
RakuAST::QuotedMatchConstruct
RakuAST::Regex::Anchor::BeginningOfLine
Expand Down
26 changes: 25 additions & 1 deletion t/12-rakuast/operators.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use MONKEY-SEE-NO-EVAL;
use experimental :rakuast;
use Test;

plan 29;
plan 31;

my $ast;
my $deparsed;
Expand Down Expand Up @@ -154,6 +154,30 @@ subtest 'Application of a prefix operator to a literal (2)' => {
for EVAL($ast), EVAL($deparsed);
}

subtest 'Application of a positive power postfix operator' => {
# 3²²
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::IntLiteral.new(3),
postfix => RakuAST::Postfix::Power.new(22)
);

is-deeply $deparsed, '3²²', 'deparse';
is-deeply $_, 31381059609, @type[$++]
for EVAL($ast), EVAL($deparsed);
}

subtest 'Application of a negative power postfix operator' => {
# 4⁻²
ast RakuAST::ApplyPostfix.new(
operand => RakuAST::IntLiteral.new(4),
postfix => RakuAST::Postfix::Power.new(-2)
);

is-deeply $deparsed, '4⁻²', 'deparse';
is-deeply $_, 0.0625, @type[$++]
for EVAL($ast), EVAL($deparsed);
}

subtest 'Application of a (user-defined) postfix operator to a literal' => {
sub postfix:<!>($n) {
[*] 1..$n
Expand Down

0 comments on commit 1b5b575

Please sign in to comment.