1.1 --- a/test.py Sun May 24 20:28:56 2009 +0200
1.2 +++ b/test.py Mon May 25 00:45:53 2009 +0200
1.3 @@ -6,19 +6,6 @@
1.4 import sys
1.5 import os
1.6
1.7 -code = None
1.8 -
1.9 -def show(program, with_builtins=0):
1.10 - make(program, with_builtins)
1.11 - show_code(code)
1.12 -
1.13 -def make(program, with_builtins=0):
1.14 - global code
1.15 - code = program.get_image(with_builtins)
1.16 -
1.17 -def raw(program, with_builtins=0):
1.18 - return program.get_raw_image()
1.19 -
1.20 def show_code(code):
1.21 for i, x in enumerate(code):
1.22 print i, x
1.23 @@ -29,37 +16,15 @@
1.24 print "%6d" % (len(table_slice) - table_slice.count(None)), \
1.25 "".join(entry and "#" or "_" for entry in table_slice)
1.26
1.27 -def machine(program, with_builtins=0, debug=0):
1.28 - print "Making the image..."
1.29 - make(program, with_builtins)
1.30 - print "Getting raw structures..."
1.31 - ot = program.get_object_table()
1.32 - pt = program.get_parameter_table()
1.33 - objlist = ot.as_list()
1.34 - paramlist = pt.as_list()
1.35 - print "Getting raw image..."
1.36 - rc = program.get_raw_image()
1.37 - print "Initialising the machine..."
1.38 - importer = program.get_importer()
1.39 - true_constant = importer.get_constant(True).location
1.40 - false_constant = importer.get_constant(False).location
1.41 - rm = rsvp.RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug)
1.42 - rm.pc = program.code_location
1.43 - return rm
1.44 -
1.45 def attrs(obj):
1.46 for name, attr in obj.items():
1.47 print name, attr
1.48
1.49 -if __name__ == "__main__":
1.50 - args = sys.argv[2:]
1.51 - path = sys.path[:]
1.52 +# Command option processing.
1.53
1.54 - if len(sys.argv) > 1:
1.55 - program = os.path.abspath(sys.argv[1])
1.56 - path.append(os.path.split(program)[0])
1.57 - else:
1.58 - program = None
1.59 +def parse_optimisations(args):
1.60 +
1.61 + "Parse 'args' for optimisation flags."
1.62
1.63 if "-omax" in args:
1.64 requested_optimisations = micropython.Program.supported_optimisations
1.65 @@ -70,26 +35,49 @@
1.66 for arg_part in arg[2:].split(","):
1.67 requested_optimisations.append(arg_part)
1.68
1.69 - i = micropython.Importer(path, "-v" in args, requested_optimisations)
1.70 - p = micropython.Program(i, requested_optimisations)
1.71 + return requested_optimisations
1.72 +
1.73 +# Main program.
1.74 +
1.75 +if __name__ == "__main__":
1.76 + args = sys.argv[2:]
1.77 + path = sys.path[:]
1.78 +
1.79 + if len(sys.argv) > 1:
1.80 + filename = os.path.abspath(sys.argv[1])
1.81 + path.append(os.path.split(filename)[0])
1.82 + else:
1.83 + filename = None
1.84 +
1.85 + requested_optimisations = parse_optimisations(args)
1.86 +
1.87 + # Make the program.
1.88
1.89 try:
1.90 - builtins = i.load_from_file("lib/builtins.py", "__builtins__")
1.91 - if program is None:
1.92 + p = micropython.program(filename, path, requested_optimisations, "-v" in args)
1.93 + if filename is None:
1.94 print "Loading module micropython ..."
1.95 - m = i.load("micropython")
1.96 + m = p.get_importer().load("micropython")
1.97 else:
1.98 - print "Loading program", program, "..."
1.99 - m = i.load_from_file(program)
1.100 + m = p.get_importer().get_module("__main__")
1.101 +
1.102 + # Report any errors.
1.103 +
1.104 except micropython.ProcessingError, exc:
1.105 print repr(exc)
1.106 if "-tb" in args:
1.107 raise
1.108 +
1.109 + # Provide information to any interactive session.
1.110 +
1.111 else:
1.112 ot = p.get_object_table()
1.113 pt = p.get_parameter_table()
1.114
1.115 - if "-m" in args:
1.116 - rm = machine(p)
1.117 + if "-m" in args or "-t" in args:
1.118 + rm = rsvp.machine(p)
1.119 + if "-t" in args:
1.120 + success = rm.test(m)
1.121 + print "Test successful?", success
1.122
1.123 # vim: tabstop=4 expandtab shiftwidth=4