micropython

test.py

137:f660fe1aac5c
2008-09-01 Paul Boddie Added notes about calling initialisers and instantiators, adopting a strategy where instantiation detected at compile-time is performed using an initialiser directly, whereas that detected at run-time is done using an instantiator whose code is now generated in the image. Added a finalise method to the Importer in order to set attribute locations before code generation, since some code (use of initialisers) requires details of a different program unit's locals (although this is actually unnecessary, but done because Attr instances are employed in the generated code). Changed class invocation at compile-time to acquire the new object reference from the frame of an already invoked initialiser just before dropping the frame. Added some support for raw image encoding of classes and functions. Changed JumpWithFrame usage to involve the current callable, not the current value. Added RecoverFrame and AdjustFrame instructions. Improved the tests around instantiation.
     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 show_table_usage(raw_table, slice_size=100):    25     for x in xrange(0, len(raw_table), slice_size):    26         table_slice = raw_table[x:x+slice_size]    27         print "%6d" % (len(table_slice) - table_slice.count(None)), \    28             "".join(entry and "#" or "_" for entry in table_slice)    29     30 def machine(importer):    31     print "Making the image..."    32     make(importer)    33     rc = raw(importer.get_image())    34     print "Getting raw tables..."    35     objtable = importer.get_object_table().as_raw()    36     paramtable = importer.get_parameter_table().as_raw()    37     print "Initialising the machine..."    38     rm = rsvp.RSVPMachine(rc, objtable, paramtable)    39     rm.pc = importer.code_location    40     return rm    41     42 def attrs(obj):    43     for name, attr in obj.items():    44         print name, attr    45     46 if __name__ == "__main__":    47     args = sys.argv[2:]    48     path = sys.path[:]    49     50     if len(sys.argv) > 1:    51         program = os.path.abspath(sys.argv[1])    52         path.append(os.path.split(program)[0])    53     else:    54         program = None    55     56     i = micropython.Importer(path, "-v" in args)    57     58     if "-omax" in args:    59         requested_optimisations = i.supported_optimisations    60     else:    61         requested_optimisations = []    62         for arg in args:    63             if arg.startswith("-o"):    64                 for arg_part in arg[2:].split(","):    65                     requested_optimisations.append(arg_part)    66     67     try:    68         builtins = i.load_from_file("lib/builtins.py", "__builtins__")    69         if program is None:    70             print "Loading module micropython ..."    71             m = i.load("micropython")    72         else:    73             print "Loading program", program, "..."    74             m = i.load_from_file(program)    75     except micropython.ProcessingError, exc:    76         print repr(exc)    77         if "-tb" in args:    78             raise    79     else:    80         i.vacuum()    81         ot = i.get_object_table()    82         pt = i.get_parameter_table()    83     84 # vim: tabstop=4 expandtab shiftwidth=4