Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created May 19, 2022 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/2a0e66ab826b46bb3f9b343a77e5209e to your computer and use it in GitHub Desktop.
Save Xliff/2a0e66ab826b46bb3f9b343a77e5209e to your computer and use it in GitHub Desktop.
Some weird RakuAST!

Here is some RakuAST. It behaves a bit differently than you'd expect:

use MONKEY-SEE-NO-EVAL;

my $ast = RakuAST::ApplyInfix.new(
      left => RakuAST::IntLiteral.new(44),
      infix => RakuAST::Infix.new('+'),
      right => RakuAST::IntLiteral.new(22)
);


$ast.left.value.say;
$ast.infix.operator.say;
$ast.right.value.say;

my $r = EVAL $ast;
$r.say;

$ast.infix.^attributes[0].set_value($ast.infix, '*');

$ast.left.value.say;
$ast.infix.operator.say;
$ast.right.value.say;

$r = EVAL $ast;
$r.say;
say '----';

my $new-ast = RakuAST::ApplyInfix.new(
        left => $ast.left,
        infix => RakuAST::Infix.new('*'),
        right => $ast.right
);

$r = EVAL $new-ast;
$r.say;

It outputs:

44
+
22
66
44
*
22
66
----
968

Note that the 968 should replace the 66 above the '---'. It didn't. I had to recreate the node.

Was this intentional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment