Skip to content

Instantly share code, notes, and snippets.

@2colours
Created February 24, 2023 13:25
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 2colours/0b6f4611360746f33ff82462ff79787e to your computer and use it in GitHub Desktop.
Save 2colours/0b6f4611360746f33ff82462ff79787e to your computer and use it in GitHub Desktop.
#!/usr/bin/env raku
grammar Calculator {
token TOP { <calc-op> }
proto rule calc-op {*}
rule calc-op:sym<add> { <num> '+' <num> }
rule calc-op:sym<sub> { <num> '-' <num> }
token num { \d+ }
}
class Calculations {
method TOP ($/) { make $<calc-op>.made; }
proto method calc-op($/) { say 'ASD'; {*} }
method calc-op:sym<add> ($/) { make [+] $<num>; }
method calc-op:sym<sub> ($/) { make [-] $<num>; }
}
say Calculator.parse('2 + 3', actions => Calculations).made;
# OUTPUT: «5␤»
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment