1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-11-29 18:49:22 +00:00
SixtyPical/bin/sixtypical

95 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/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
from pprint import pprint
import sys
import traceback
from sixtypical.parser import Parser
from sixtypical.evaluator import Evaluator
from sixtypical.analyzer import Analyzer
from sixtypical.emitter import Emitter, Byte, Word
from sixtypical.compiler import Compiler
if __name__ == '__main__':
optparser = OptionParser(__doc__.strip())
optparser.add_option("--analyze",
2015-10-17 14:23:00 +00:00
action="store_true",
help="")
2015-10-17 14:23:00 +00:00
optparser.add_option("--basic-prelude",
action="store_true",
help="")
optparser.add_option("--compile",
2015-10-17 14:23:00 +00:00
action="store_true",
help="")
optparser.add_option("--debug",
2015-10-17 14:23:00 +00:00
action="store_true",
help="")
optparser.add_option("--traceback",
2015-10-17 14:23:00 +00:00
action="store_true",
help="")
optparser.add_option("--execute",
2015-10-17 14:23:00 +00:00
action="store_true",
help="")
(options, args) = optparser.parse_args(sys.argv[1:])
for filename in args:
text = open(filename).read()
parser = Parser(text)
program = parser.program()
if options.analyze:
try:
analyzer = Analyzer(debug=options.debug)
analyzer.analyze_program(program)
except Exception as e:
if options.traceback:
raise
else:
traceback.print_exception(e.__class__, e, None)
sys.exit(1)
if options.compile:
fh = sys.stdout
start_addr = 0xc000
2015-10-17 14:23:00 +00:00
prelude = []
if options.basic_prelude:
start_addr = 0x0801
2015-10-17 14:23:00 +00:00
prelude = [0x10, 0x08, 0xc9, 0x07, 0x9e, 0x32,
0x30, 0x36, 0x31, 0x00, 0x00, 0x00]
# we are outputting a .PRG, so we output the load address first
# we don't use the Emitter for this b/c not part of addr space
2015-10-18 12:55:40 +00:00
if not options.debug:
fh.write(Word(start_addr).serialize(0))
emitter = Emitter(start_addr)
2015-10-17 14:23:00 +00:00
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)
if options.execute:
context = Evaluator().eval_program(program)
print str(context)