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
from rply import LexerGenerator | |
class Lexer(): | |
def __init__(self): | |
self.lexer = LexerGenerator() | |
def _add_tokens(self): | |
self.lexer.add('PRINT', r'print') |
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
from lexer import Lexer | |
text_input = """ | |
print(4 + 4 - 2); | |
""" | |
lexer = Lexer().get_lexer() | |
tokens = lexer.lex(text_input) | |
for token in tokens: |
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
class Number(): | |
def __init__(self, value): | |
self.value = value | |
def eval(self): | |
return int(self.value) | |
class BinaryOp(): | |
def __init__(self, left, right): |
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
from rply import ParserGenerator | |
from ast import Number, Sum, Sub, Print | |
class Parser(): | |
def __init__(self): | |
self.pg = ParserGenerator( | |
# A list of all token names accepted by the parser. | |
['NUMBER', 'PRINT', 'OPEN_PAREN', 'CLOSE_PAREN', | |
'SEMI_COLON', 'SUM', 'SUB'] |
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
from lexer import Lexer | |
from parser import Parser | |
text_input = """ | |
print(4 + 4 - 2); | |
""" | |
lexer = Lexer().get_lexer() | |
tokens = lexer.lex(text_input) |
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
from llvmlite import ir, binding | |
class CodeGen(): | |
def __init__(self): | |
self.binding = binding | |
self.binding.initialize() | |
self.binding.initialize_native_target() | |
self.binding.initialize_native_asmprinter() | |
self._config_llvm() |
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
from lexer import Lexer | |
from parser import Parser | |
from codegen import CodeGen | |
fname = "input.toy" | |
with open(fname) as f: | |
text_input = f.read() | |
lexer = Lexer().get_lexer() | |
tokens = lexer.lex(text_input) |
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
from rply import ParserGenerator | |
from ast import Number, Sum, Sub, Print | |
class Parser(): | |
def __init__(self, module, builder, printf): | |
self.pg = ParserGenerator( | |
# A list of all token names accepted by the parser. | |
['NUMBER', 'PRINT', 'OPEN_PAREN', 'CLOSE_PAREN', | |
'SEMI_COLON', 'SUM', 'SUB'] |
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
from llvmlite import ir | |
class Number(): | |
def __init__(self, builder, module, value): | |
self.builder = builder | |
self.module = module | |
self.value = value | |
def eval(self): |