108 lines
2.5 KiB
Python
Executable File
108 lines
2.5 KiB
Python
Executable File
#! /usr/bin/python3
|
|
|
|
import sys
|
|
import getopt
|
|
|
|
from parser import Parser
|
|
from lexer import UrlLexer, TextLexer
|
|
from printer import print_ast
|
|
from drawing import Window
|
|
from interpreter import Context
|
|
|
|
#---
|
|
# Main program
|
|
#---
|
|
|
|
usage_string = f"""
|
|
usage: {sys.argv[0]} [-s|-u] <file> [options...]
|
|
|
|
If "-" is specified as input, stdin is read.
|
|
|
|
Input mode (default is -s):
|
|
-s Input file is an ASCII script ("GOTO 12, 35")
|
|
-u Input file is a wes.casio.com URL ("https://...F908313200333500")
|
|
|
|
Output options:
|
|
--quiet Do not show the SDL window
|
|
--save=<file> Save a copy of the screen output in a bitmap file
|
|
--scale=<n> Window scale up (default 4, max 16)
|
|
""".strip()
|
|
|
|
def usage(exitcode=None):
|
|
print(usage_string, file=sys.stderr)
|
|
|
|
if exitcode is not None:
|
|
sys.exit(exitcode)
|
|
|
|
def main():
|
|
# Read command-line arguments
|
|
try:
|
|
opts, args = getopt.gnu_getopt(sys.argv[1:], "hus",
|
|
["help", "quiet", "save=", "scale=", "debug="])
|
|
opts = dict(opts)
|
|
|
|
if len(sys.argv) == 1 or "-h" in opts or "--help" in opts:
|
|
usage(0)
|
|
|
|
if "-u" in opts and "-s" in opts:
|
|
raise getopt.GetoptError("-s (script input) and -u (URL input) "
|
|
"are exclusive")
|
|
|
|
if len(args) < 1:
|
|
usage(1)
|
|
if len(args) > 1:
|
|
raise getopt.GetoptError("only one input file can be specified")
|
|
|
|
except getopt.GetoptError as e:
|
|
print("error:", e, file=sys.stderr)
|
|
print(f"Try '{sys.argv[0]} --help' for details.", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
# Other parameters
|
|
quiet = "--quiet" in opts
|
|
out = opts.get("--save", None)
|
|
debug = opts.get("--debug", None)
|
|
|
|
scale = int(opts.get("--scale", "4"))
|
|
if scale < 1:
|
|
scale = 1
|
|
if scale > 16:
|
|
scale = 16
|
|
|
|
# Read the input program
|
|
file = args[0]
|
|
if file == "-":
|
|
program = sys.stdin.read()
|
|
else:
|
|
with open(file, "r") as fp:
|
|
program = fp.read()
|
|
|
|
# URL mode
|
|
if "-u" in opts:
|
|
lexer = UrlLexer(program)
|
|
# Default, script mode
|
|
else:
|
|
lexer = TextLexer(program)
|
|
|
|
# Lexer debug mode, just print the token stream
|
|
if debug == "lexer":
|
|
lexer.dump()
|
|
return 0
|
|
|
|
parser = Parser(lexer)
|
|
ast = parser.parse_program()
|
|
ast = ast.simplify()
|
|
|
|
print_ast(ast, lang="ast")
|
|
|
|
with Window(width=192, height=47, scale=scale, quiet=quiet) as w:
|
|
ctx = Context(w)
|
|
ctx.run(ast)
|
|
|
|
w.wait()
|
|
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|