Skip to content

Instantly share code, notes, and snippets.

@isidentical
Last active July 17, 2019 06:47
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 isidentical/9005b50d8f0c64469dc1b7b29487ee12 to your computer and use it in GitHub Desktop.
Save isidentical/9005b50d8f0c64469dc1b7b29487ee12 to your computer and use it in GitHub Desktop.
Valf implementation spec (valf is not written, yet!)
import valf
class Grammar(valf.Grammar):
class Terminals:
AUTO_HANDLE = ("int",)
END_OF_STMT = valf.Const(";")
class Compiler(valf.Visitor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._numbers = []
def visit_int(self, node):
self._numbers.append(node.value)
def compile(self, tree):
self._numbers = []
self.visit(tree)
return sum(self._numbers)
if __name__ == '__main__':
parser = valf.Parser.from_grammar(Grammar)
compiler = Compiler()
code = "1;2;3;4;5;6"
tree = parser.parse(code)
print(compiler.compile(code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment