def analyze(s): name = traceback.extract_stack()[-2][2] ast = Parser().parse(s, lexer=Lexer()) table, blocks, functions = il_gen.generate(ast, debug=True) dot('ast.%s'%name, ast.dotty(), str(ast)) dot('blks.%s'%name, functions['f2'].entry.dotty()) cf.analyze(table, blocks, functions, debug=True) dot('cf.%s'%name, functions['f2'].tree.dotty()) return functions
def run(s): #out = cStringIO.StringIO() try: code = x86_gen.generate(*il_gen.generate(Parser().parse(s, lexer=Lexer()), debug=True)) print code gas = subprocess.Popen(['as', '--32', '-o', 'exe.o'], stdin=subprocess.PIPE, stderr=subprocess.PIPE) print gas.communicate(code) ld = subprocess.Popen(['ld', '-o', 'exe', '-melf_i386', '-dynamic-linker', conf.dynlinker, conf.libc32, 'exe.o'], stdin=subprocess.PIPE, stderr=subprocess.PIPE) print ld.communicate() exe = subprocess.Popen(['./exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) ret, err = exe.communicate() print '>', (ret, err) except Exception, e: print e traceback.print_exc(file=sys.stdout) ret = 'fail' err = e
code = self.emit(x.pushl, i.a) code += [x.pushl("$printf_msg"), x.call("printf"), x.addl(x.cint(8), x.esp)] return code if __name__ == "__main__": code = generate( *il_gen.generate( Parser().parse( """ _add = func(a, b) { reflect = func(x) { print x y = x + 5 return y - 5 } return reflect(a) + reflect(b) } add = func(f, a, b) { return f(a, b) } print add(_add, 18, 37) """, lexer=Lexer(), ) ) ) # print code vm.run(code)
def run(s): out = cStringIO.StringIO() vm.run(vm_gen.generate(*il_gen.generate(Parser().parse(s, lexer=Lexer()))), out) r = out.getvalue() del out return r