First cut at a --run option for sixtypical, replacing loadngo.sh.
Chris Pressey
6 years ago
10 | 10 | import codecs |
11 | 11 | import json |
12 | 12 | from pprint import pprint |
13 | from subprocess import check_call | |
13 | 14 | import sys |
15 | from tempfile import NamedTemporaryFile | |
14 | 16 | import traceback |
15 | 17 | |
16 | 18 | from sixtypical.parser import Parser, SymbolTable, merge_programs |
63 | 65 | compilation_roster = fa.serialize() |
64 | 66 | dump(compilation_roster) |
65 | 67 | |
66 | if options.analyze_only or options.output is None: | |
68 | if options.analyze_only or (options.output is None and not options.run): | |
67 | 69 | return |
68 | 70 | |
69 | 71 | start_addr = None |
73 | 75 | else: |
74 | 76 | start_addr = int(options.origin, 10) |
75 | 77 | |
76 | with open(options.output, 'wb') as fh: | |
77 | outputter = outputter_class_for(options.output_format)(fh, start_addr=start_addr) | |
78 | outputter.write_prelude() | |
79 | compiler = Compiler(symtab, outputter.emitter) | |
80 | compiler.compile_program(program, compilation_roster=compilation_roster) | |
81 | outputter.write_postlude() | |
82 | if options.debug: | |
83 | pprint(outputter.emitter) | |
84 | else: | |
85 | outputter.emitter.serialize_to(fh) | |
78 | if options.run: | |
79 | fh = NamedTemporaryFile(delete=False) | |
80 | output_filename = fh.name | |
81 | else: | |
82 | fh = open(options.output, 'wb') | |
83 | output_filename = options.output | |
84 | ||
85 | outputter = outputter_class_for(options.output_format)(fh, start_addr=start_addr) | |
86 | outputter.write_prelude() | |
87 | compiler = Compiler(symtab, outputter.emitter) | |
88 | compiler.compile_program(program, compilation_roster=compilation_roster) | |
89 | outputter.write_postlude() | |
90 | if options.debug: | |
91 | pprint(outputter.emitter) | |
92 | else: | |
93 | outputter.emitter.serialize_to(fh) | |
94 | ||
95 | fh.close() | |
96 | ||
97 | if options.run: | |
98 | emu = { | |
99 | 'c64-basic-prg': "x64 -config vicerc", | |
100 | 'vic20-basic-prg': "xvic -config vicerc", | |
101 | 'atari2600-cart': "stella" | |
102 | }.get(options.output_format) | |
103 | if not emu: | |
104 | raise ValueError("No emulator configured for selected --output-format '{}'".format(options.output_format)) | |
105 | ||
106 | command = "{} {}".format(emu, output_filename) | |
107 | check_call(command, shell=True) | |
86 | 108 | |
87 | 109 | |
88 | 110 | if __name__ == '__main__': |
142 | 164 | help="Display debugging information when analyzing and compiling." |
143 | 165 | ) |
144 | 166 | argparser.add_argument( |
167 | "--run", | |
168 | action="store_true", | |
169 | help="Engage 'load-and-go' operation: write the output to a temporary filename, " | |
170 | "infer an emulator from the given --output-format, and boot the emulator." | |
171 | ) | |
172 | argparser.add_argument( | |
145 | 173 | "--traceback", |
146 | 174 | action="store_true", |
147 | 175 | help="When an error occurs, display a full Python traceback." |