class Compiler(object): def __init__(self, filename, options): self.errors = Errors(filename) self.options = options def compile(self, data): machine = self.find_machine(self.options) ast = Parser(data, errors=self.errors).run() VarCheck(ast, machine.builtins, errors=self.errors).run() Flatten(ast, errors=self.errors).run() Reduce(ast, errors=self.errors).run() TailRecursion(ast, errors=self.errors).run() Inline(ast, errors=self.errors).run() for f in ast.symbol_table.symbols.values(): cfg = f.cfg RegisterAllocation(f.cfg, errors=self.errors).run() lines = Linearise(ast, errors=self.errors).run() output = Render(lines, machine, errors=self.errors).run() return output def find_machine(self, options): if options.target == 'E1': import compiler.e1 return compiler.e1.Machine(options) self.errors.error("Unknown target type: '%s'" % options.target)
def assertSuccess(self, input_lines): errors = Errors() output = Render(input_lines, self.machine, indent=False, errors=errors).run() self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) return output
def testConflict(self): st = SymbolTable() errors = Errors() st.add('x', self.x, errors) st.add('x', self.x2, errors) self.assertEquals(errors.num_errors, 1) self.assertEquals(st.get_names(), set(['x'])) self.assertEquals(st.lookup('x'), self.x)
def assertSuccess(self, input): errors = Errors() Flatten(input, errors=errors).run() self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) Inline(input, errors=errors).run() self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0)
def __init__(self, **kwargs): if 'name' in kwargs: self.name = kwargs['name'] else: self.name = self.__class__.__name__ if 'errors' in kwargs: self.errors = kwargs['errors'] else: self.errors = Errors()
def testShadow(self): parent = SymbolTable() parent.add('x', self.x, None) st = SymbolTable(parent) errors = Errors() st.add('x', self.x2, errors) self.assertEquals(errors.num_warnings, 1) self.assertEquals(st.get_names(), set(['x'])) self.assertEquals(st.get_all_names(), set(['x'])) self.assertEquals(st.lookup('x'), self.x2)
def add(self, name, decl, errors=Errors()): if name in self.symbols: prev_decl = self.symbols[name] errors.error(decl.get_location(), """'%s' conflicts with previous declaration at '%s'""" % (name, prev_decl.get_location())) return if self.parent is not None: prev_decl = self.parent.lookup(name) if prev_decl is not None: errors.warn(decl.get_location(), """'%s' shadows previous declaration at '%s'""" % (name, prev_decl.get_location())) self.symbols[name] = decl
def assertSuccess(self, input): errors = Errors() lines = Linearise(input, errors=errors).run() self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0) return lines
def assertSuccess(self, input, expected_errors=0, expected_warnings=0): errors = Errors() Flatten(input, errors=errors).run() self.assertEquals(errors.num_errors, expected_errors) self.assertEquals(errors.num_warnings, expected_warnings)
def assertFailure(self, input): errors = Errors() fl = Flatten(input, errors) self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0)
def __init__(self, filename, options): self.errors = Errors(filename) self.options = options
def assertSuccess(self, input, expected_errors=0, expected_warnings=0): errors = Errors() VarCheck(input, self.builtins, errors=errors).run() self.assertEquals(errors.num_errors, expected_errors) self.assertEquals(errors.num_warnings, expected_warnings)
def assertSuccess(self, input): errors = Errors() Reduce(input, errors=errors).run() self.assertEquals(errors.num_errors, 0) self.assertEquals(errors.num_warnings, 0)