2015-10-16 08:30:24 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Usage: sixtypical [OPTIONS] FILES
|
|
|
|
|
2018-02-05 13:17:23 +00:00
|
|
|
Analyzes and compiles a Sixtypical program.
|
2015-10-16 08:30:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
from os.path import realpath, dirname, join
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, join(dirname(realpath(sys.argv[0])), '..', 'src'))
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------- #
|
|
|
|
|
|
|
|
import codecs
|
2018-03-06 16:28:34 +00:00
|
|
|
from argparse import ArgumentParser
|
2015-10-22 14:45:16 +00:00
|
|
|
from pprint import pprint
|
2015-10-16 08:30:24 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from sixtypical.parser import Parser
|
2015-10-21 14:45:14 +00:00
|
|
|
from sixtypical.analyzer import Analyzer
|
2015-10-17 11:28:39 +00:00
|
|
|
from sixtypical.emitter import Emitter, Byte, Word
|
2015-10-17 14:06:50 +00:00
|
|
|
from sixtypical.compiler import Compiler
|
2015-10-16 08:30:24 +00:00
|
|
|
|
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
def process_input_files(filenames, options):
|
|
|
|
programs = []
|
|
|
|
|
|
|
|
for filename in options.filenames:
|
|
|
|
text = open(filename).read()
|
|
|
|
parser = Parser(text, filename)
|
|
|
|
program = parser.program()
|
|
|
|
programs.append(program)
|
|
|
|
|
|
|
|
if options.parse_only:
|
|
|
|
return
|
|
|
|
|
|
|
|
#program = merge_programs(programs)
|
|
|
|
program = programs[0]
|
|
|
|
|
|
|
|
analyzer = Analyzer(debug=options.debug)
|
|
|
|
analyzer.analyze_program(program)
|
|
|
|
|
|
|
|
if options.analyze_only:
|
|
|
|
return
|
|
|
|
|
|
|
|
fh = sys.stdout
|
|
|
|
|
|
|
|
if options.origin.startswith('0x'):
|
|
|
|
start_addr = int(options.origin, 16)
|
|
|
|
else:
|
|
|
|
start_addr = int(options.origin, 10)
|
|
|
|
|
|
|
|
output_format = options.output_format
|
|
|
|
|
|
|
|
prelude = []
|
|
|
|
if options.prelude == 'c64':
|
|
|
|
output_format = 'prg'
|
|
|
|
start_addr = 0x0801
|
|
|
|
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
|
|
|
|
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
|
|
|
|
elif options.prelude == 'vic20':
|
|
|
|
output_format = 'prg'
|
|
|
|
start_addr = 0x1001
|
|
|
|
prelude = [0x0b, 0x10, 0xc9, 0x07, 0x9e, 0x34,
|
|
|
|
0x31, 0x30, 0x39, 0x00, 0x00, 0x00]
|
|
|
|
elif options.prelude:
|
|
|
|
raise NotImplementedError("Unknown prelude: {}".format(options.prelude))
|
|
|
|
|
|
|
|
# If we are outputting a .PRG, we output the load address first.
|
|
|
|
# We don't use the Emitter for this b/c not part of addr space.
|
|
|
|
if output_format == 'prg':
|
|
|
|
fh.write(Word(start_addr).serialize(0))
|
|
|
|
|
|
|
|
emitter = Emitter(start_addr)
|
|
|
|
for byte in prelude:
|
|
|
|
emitter.emit(Byte(byte))
|
|
|
|
compiler = Compiler(emitter)
|
|
|
|
compiler.compile_program(program)
|
|
|
|
if options.debug:
|
|
|
|
pprint(emitter.accum)
|
|
|
|
else:
|
|
|
|
emitter.serialize(fh)
|
|
|
|
|
|
|
|
|
2015-10-16 08:30:24 +00:00
|
|
|
if __name__ == '__main__':
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser = ArgumentParser(__doc__.strip())
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
'filenames', metavar='FILENAME', type=str, nargs='+',
|
|
|
|
help="The SixtyPical source files to compile."
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--analyze-only",
|
|
|
|
action="store_true",
|
|
|
|
help="Only parse and analyze the program; do not compile it."
|
|
|
|
)
|
2018-03-13 13:33:01 +00:00
|
|
|
argparser.add_argument(
|
|
|
|
"--origin", type=str, default='0xc000',
|
|
|
|
help="Location in memory where the `main` routine will be "
|
|
|
|
"located. Default: 0xc000."
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--output-format", type=str, default='prg',
|
|
|
|
help="Executable format to produce. Options are: prg (.PRG file "
|
|
|
|
"for Commodore 8-bit). Default: prg."
|
|
|
|
)
|
2018-03-06 16:28:34 +00:00
|
|
|
argparser.add_argument(
|
2018-03-06 17:00:39 +00:00
|
|
|
"--prelude", type=str,
|
|
|
|
help="Insert a snippet before the compiled program "
|
|
|
|
"so that it can be LOADed and RUN on a certain platforms. "
|
2018-03-13 13:33:01 +00:00
|
|
|
"Also sets the origin and format. "
|
2018-03-06 17:00:39 +00:00
|
|
|
"Options are: c64 or vic20."
|
2018-03-06 16:28:34 +00:00
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--debug",
|
|
|
|
action="store_true",
|
|
|
|
help="Display debugging information when analyzing and compiling."
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--parse-only",
|
|
|
|
action="store_true",
|
|
|
|
help="Only parse the program; do not analyze or compile it."
|
|
|
|
)
|
|
|
|
argparser.add_argument(
|
|
|
|
"--traceback",
|
|
|
|
action="store_true",
|
|
|
|
help="When an error occurs, display a full Python traceback."
|
|
|
|
)
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-06 16:28:34 +00:00
|
|
|
options, unknown = argparser.parse_known_args(sys.argv[1:])
|
|
|
|
remainder = ' '.join(unknown)
|
2015-10-16 08:30:24 +00:00
|
|
|
|
2018-03-27 14:55:29 +00:00
|
|
|
try:
|
|
|
|
process_input_files(options.filenames, options)
|
|
|
|
except Exception as e:
|
|
|
|
if options.traceback:
|
|
|
|
raise
|
2018-02-06 16:14:44 +00:00
|
|
|
else:
|
2018-03-27 14:55:29 +00:00
|
|
|
traceback.print_exception(e.__class__, e, None)
|
|
|
|
sys.exit(1)
|