2015-10-16 08:30:24 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Usage: sixtypical [OPTIONS] FILES
|
|
|
|
|
|
|
|
Analyzes and/or executes and/or compiles a Sixtypical program.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from os.path import realpath, dirname, join
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.insert(0, join(dirname(realpath(sys.argv[0])), '..', 'src'))
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------- #
|
|
|
|
|
|
|
|
import codecs
|
|
|
|
from optparse import OptionParser
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from sixtypical.parser import Parser
|
|
|
|
from sixtypical.evaluator import eval_program
|
|
|
|
from sixtypical.analyzer import analyze_program
|
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
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
optparser = OptionParser(__doc__.strip())
|
|
|
|
|
|
|
|
optparser.add_option("--analyze",
|
2015-10-17 14:23:00 +00:00
|
|
|
action="store_true",
|
2015-10-16 08:30:24 +00:00
|
|
|
help="")
|
2015-10-17 14:23:00 +00:00
|
|
|
optparser.add_option("--basic-prelude",
|
|
|
|
action="store_true",
|
2015-10-17 11:28:39 +00:00
|
|
|
help="")
|
2015-10-16 08:30:24 +00:00
|
|
|
optparser.add_option("--compile",
|
2015-10-17 14:23:00 +00:00
|
|
|
action="store_true",
|
2015-10-16 08:30:24 +00:00
|
|
|
help="")
|
2015-10-17 10:08:25 +00:00
|
|
|
optparser.add_option("--debug",
|
2015-10-17 14:23:00 +00:00
|
|
|
action="store_true",
|
2015-10-17 10:08:25 +00:00
|
|
|
help="")
|
2015-10-16 08:30:24 +00:00
|
|
|
optparser.add_option("--traceback",
|
2015-10-17 14:23:00 +00:00
|
|
|
action="store_true",
|
2015-10-16 08:30:24 +00:00
|
|
|
help="")
|
|
|
|
optparser.add_option("--execute",
|
2015-10-17 14:23:00 +00:00
|
|
|
action="store_true",
|
2015-10-16 08:30:24 +00:00
|
|
|
help="")
|
|
|
|
|
|
|
|
(options, args) = optparser.parse_args(sys.argv[1:])
|
|
|
|
|
|
|
|
for filename in args:
|
|
|
|
text = open(filename).read()
|
|
|
|
p = Parser(text)
|
|
|
|
program = p.program()
|
|
|
|
|
|
|
|
if options.analyze:
|
|
|
|
try:
|
|
|
|
analyze_program(program)
|
|
|
|
except Exception as e:
|
|
|
|
if options.traceback:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
traceback.print_exception(e.__class__, e, None)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-10-17 09:17:44 +00:00
|
|
|
if options.compile:
|
2015-10-17 11:28:39 +00:00
|
|
|
start_addr = 0xc000
|
2015-10-17 14:23:00 +00:00
|
|
|
prelude = []
|
|
|
|
if options.basic_prelude:
|
2015-10-17 11:28:39 +00:00
|
|
|
start_addr = 0x0801
|
2015-10-17 14:23:00 +00:00
|
|
|
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
|
|
|
|
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
|
2015-10-17 11:28:39 +00:00
|
|
|
emitter = Emitter(start_addr)
|
|
|
|
# we are outputting a .PRG, so output the load address first
|
2015-10-17 14:23:00 +00:00
|
|
|
emitter.emit_header(Word(start_addr))
|
|
|
|
for byte in prelude:
|
2015-10-17 11:28:39 +00:00
|
|
|
emitter.emit(Byte(byte))
|
2015-10-17 14:06:50 +00:00
|
|
|
compiler = Compiler(emitter)
|
|
|
|
compiler.compile_program(program)
|
2015-10-17 10:08:25 +00:00
|
|
|
if options.debug:
|
|
|
|
print repr(emitter.accum)
|
|
|
|
else:
|
|
|
|
emitter.serialize(sys.stdout)
|
2015-10-17 09:17:44 +00:00
|
|
|
|
2015-10-16 08:30:24 +00:00
|
|
|
if options.execute:
|
|
|
|
context = eval_program(program)
|
2015-10-16 18:32:18 +00:00
|
|
|
print str(context)
|