Skip to content

Instantly share code, notes, and snippets.

@peschwa
Created November 21, 2015 00:39
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 peschwa/7202b9f622de517cd881 to your computer and use it in GitHub Desktop.
Save peschwa/7202b9f622de517cd881 to your computer and use it in GitHub Desktop.
use nqp;
use QAST:from<NQP>;
use NQPHLL:from<NQP>;
sub EXPORT(|) {
my role SuperscriptPotentiation {
token postfix:sym<superscript> {
<[²³]>+
}
}
my role SuperscriptPotentiationActions {
method postfix:sym<superscript>(Mu $/) {
my $exp;
my $i = 0;
my \chars = nqp::split('', $/.Str);
my $iter := nqp::iterator(chars);
while $iter {
my $val := nqp::shift($iter);
$exp += unival($val) * ($i + 1)
}
#say QAST::IVal.new(:value($exp.Int)).dump;
my $block = QAST::Op.new(
:op<call>, :name('infix:<**>'),
QAST::IVal.new(:value(2)),
QAST::IVal.new(:value($exp.Int)));
$/.'!make'($block);
}
}
my Mu $MAIN-grammar = nqp::atkey(%*LANG, 'MAIN');
my Mu $MAIN-actions = nqp::atkey(%*LANG, 'MAIN-actions');
nqp::bindkey(%*LANG, 'MAIN', $MAIN-grammar.HOW.mixin($MAIN-grammar, SuperscriptPotentiation));
nqp::bindkey(%*LANG, 'MAIN-actions', $MAIN-actions.HOW.mixin($MAIN-actions, SuperscriptPotentiationActions));
{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment