1 #!/usr/bin/env python 2 3 import micropython 4 from micropython.rsvp import raw 5 import rsvp 6 import sys 7 import os 8 9 code = None 10 11 def show(importer, with_builtins=0, optimisations=None): 12 make(importer, with_builtins, optimisations) 13 show_code(code) 14 15 def make(importer, with_builtins=0, optimisations=None): 16 optimisations = optimisations or requested_optimisations 17 global code 18 code = importer.get_image(with_builtins, optimisations) 19 20 def show_code(code): 21 for i, x in enumerate(code): 22 print i, x 23 24 def machine(importer): 25 rc = raw(importer.code) 26 rm = rsvp.RSVPMachine(rc) 27 rm.pc = importer.code_location 28 return rm 29 30 def attrs(obj): 31 for name, attr in obj.items(): 32 print name, attr 33 34 if __name__ == "__main__": 35 args = sys.argv[2:] 36 path = sys.path[:] 37 38 if len(sys.argv) > 1: 39 program = os.path.abspath(sys.argv[1]) 40 path.append(os.path.split(program)[0]) 41 else: 42 program = None 43 44 i = micropython.Importer(path, "-v" in args) 45 46 if "-omax" in args: 47 requested_optimisations = i.supported_optimisations 48 else: 49 requested_optimisations = [] 50 for arg in args: 51 if arg.startswith("-o"): 52 for arg_part in arg[2:].split(","): 53 requested_optimisations.append(arg_part) 54 55 try: 56 builtins = i.load_from_file("lib/builtins.py", "__builtins__") 57 if program is None: 58 print "Loading module micropython ..." 59 m = i.load("micropython") 60 else: 61 print "Loading program", program, "..." 62 m = i.load_from_file(program) 63 except micropython.ProcessingError, exc: 64 print repr(exc) 65 else: 66 i.vacuum() 67 ot = i.get_object_table() 68 pt = i.get_parameter_table() 69 70 # vim: tabstop=4 expandtab shiftwidth=4