Last active
July 17, 2019 06:47
-
-
Save isidentical/9005b50d8f0c64469dc1b7b29487ee12 to your computer and use it in GitHub Desktop.
Valf implementation spec (valf is not written, yet!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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