Path: blob/main/third_party/ply/example/BASIC/basic.py
7087 views
# An implementation of Dartmouth BASIC (1964)1#23import sys4sys.path.insert(0,"../..")56if sys.version_info[0] >= 3:7raw_input = input89import basiclex10import basparse11import basinterp1213# If a filename has been specified, we try to run it.14# If a runtime error occurs, we bail out and enter15# interactive mode below16if len(sys.argv) == 2:17data = open(sys.argv[1]).read()18prog = basparse.parse(data)19if not prog: raise SystemExit20b = basinterp.BasicInterpreter(prog)21try:22b.run()23raise SystemExit24except RuntimeError:25pass2627else:28b = basinterp.BasicInterpreter({})2930# Interactive mode. This incrementally adds/deletes statements31# from the program stored in the BasicInterpreter object. In32# addition, special commands 'NEW','LIST',and 'RUN' are added.33# Specifying a line number with no code deletes that line from34# the program.3536while 1:37try:38line = raw_input("[BASIC] ")39except EOFError:40raise SystemExit41if not line: continue42line += "\n"43prog = basparse.parse(line)44if not prog: continue4546keys = list(prog)47if keys[0] > 0:48b.add_statements(prog)49else:50stat = prog[keys[0]]51if stat[0] == 'RUN':52try:53b.run()54except RuntimeError:55pass56elif stat[0] == 'LIST':57b.list()58elif stat[0] == 'BLANK':59b.del_line(stat[1])60elif stat[0] == 'NEW':61b.new()626364656667686970717273