Skip to content

Instantly share code, notes, and snippets.

@saulfield
Created December 20, 2018 04:57
Show Gist options
  • Save saulfield/adee4e358bd413a2e3458695fc994dde to your computer and use it in GitHub Desktop.
Save saulfield/adee4e358bd413a2e3458695fc994dde to your computer and use it in GitHub Desktop.
optimize_grammar = r"""
inc_chain = ['INC':op anything:n]+:xs -> (op, sum([x[1] for x in xs]))
dec_chain = ['DEC':op anything:n]+:xs -> (op, sum([x[1] for x in xs]))
add_chain = ['ADD':op anything:n]+:xs -> (op, sum([x[1] for x in xs]))
sub_chain = ['SUB':op anything:n]+:xs -> (op, sum([x[1] for x in xs]))
slice = inc_chain | dec_chain | add_chain | sub_chain | [anything:op anything:arg]
bytecode = slice*
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment