def emitCode(self, gen): log.write('emit', 'emitCode class ', self, self.name) subGen = CodeGenerator(gen) subGen.typeBuilder = self.builder for decl in self.body.decls: decl.emitCode(subGen) self.builder.CreateType()
def emitDeclaration(self, gen): from nine.codegenerator import CodeGenerator if self.__emitDecls: return self.__emitDecls = True log.write('emit', 'emitDeclaration iface ', self, self.name) subGen = CodeGenerator(gen) subGen.typeBuilder = self.builder for decl in self.body.decls: decl.emitDeclaration(subGen)
def emitCode(self, gen): from nine.codegenerator import CodeGenerator log.write('emit', 'emitCode func', self, self.name) assert self.builder is not None, (self, self.name, self.klass) subGen = CodeGenerator(gen) subGen.ilGen = self.builder.GetILGenerator() subGen.methodBuilder = self.builder if not self.flags.abstract: self.body.emitCode(subGen) subGen.ilGen.Emit(subGen.opCodes.Ret)
def compileProgram(self, ast, outputName): globalNs = self.scanAssemblies(self.references) self.fixPrimitives() # Fill globalNs with declared symbols for decl in ast: if hasattr(decl, 'name'): globalNs.symbols[decl.name] = decl globalScope = semantic.makeGlobalScope(globalNs.symbols) st = semantic.semantic(ast, globalScope) gen = CodeGenerator() gen.createProgram(outputName, st)
def emitCode(self, gen): from CLR import System from nine import util from nine.codegenerator import CodeGenerator subGen = CodeGenerator(gen) subGen.ilGen = self.builder.GetILGenerator() subGen.methodBuilder = self.builder subGen.ilGen.Emit(gen.opCodes.Ldarg_0) #FIXME: remove when super() is implemented #auto-call super ctor base = self.klass.bases[0] assert base is not None, "ALL Classes have a base!" baseCtor = base.getCtor([]) subGen.ilGen.Emit(gen.opCodes.Call, baseCtor.builder) self.body.emitCode(subGen) subGen.ilGen.Emit(subGen.opCodes.Ret)
def emitCode(self, gen): from nine.codegenerator import CodeGenerator from ast.breakstatement import BreakStatement from ast.continuestatement import ContinueStatement startLabel = gen.ilGen.DefineLabel() loopLabel = gen.ilGen.DefineLabel() endLabel = gen.ilGen.DefineLabel() gen.ilGen.Emit(gen.opCodes.Br, startLabel) gen.ilGen.MarkLabel(loopLabel) miniGen = CodeGenerator(gen) miniGen.breakLabel = endLabel miniGen.continueLabel = startLabel self.block.emitCode(miniGen) gen.ilGen.MarkLabel(startLabel) self.arg.emitLoad(gen) gen.ilGen.Emit(gen.opCodes.Brtrue, loopLabel) gen.ilGen.MarkLabel(endLabel)
def emitDeclaration(self, gen): from CLR.System.Reflection.Emit import CustomAttributeBuilder if self.__declsBuilt: return self.__declsBuilt = True log.write('emit', 'emitDeclaration class ', self, self.name) for attr in self.attributes: ctorInfo = attr.className.builder.GetConstructor([]) attrBuilder = CustomAttributeBuilder(ctorInfo, []) self.builder.SetCustomAttribute(attrBuilder) # Be sure base classes get their stuff done before us. self.baseClass.emitDeclaration(gen) for iface in self.baseInterfaces: iface.emitDeclaration(gen) subGen = CodeGenerator(gen) subGen.typeBuilder = self.builder for decl in self.body.decls: decl.emitDeclaration(subGen)
class CodeGeneratorTest(unittest.TestCase): def setUp(self): self.gen = CodeGenerator() def testBuildEmptyProgram(self): name = os.path.join('bin', 'codegen_empty_test') program = [] # it is a complicated program self.gen.createProgram(name, program) result = os.system('%s >> stdout.txt' % name) self.failUnless(result == 0) def testBuildHelloWorld(self): '''FIXME: this test no longer works if run in isolation. ''' program = [ PrintStatement(StringLiteral((0, '<fakesource>'), '\'Hello, code generator test!\'')) ] name = os.path.join('bin', 'codegen_hello_test') self.gen.createProgram(name, program) result = os.system('%s >> stdout.txt' % name) self.failUnless(result == 0) def testBuildLibrary(self): program = util.source(''' class LibraryClass: def SayHi(): print 'Hi!' ''') util.buildProgram('build_library_test.dll', program) self.assertTrue(os.path.exists('bin/build_library_test.dll'))
def setUp(self): self.gen = CodeGenerator()