Path: blob/main/third_party/ply/example/BASIC/basiclog.py
7087 views
# An implementation of Dartmouth BASIC (1964)1#23import sys4sys.path.insert(0,"../..")56if sys.version_info[0] >= 3:7raw_input = input89import logging10logging.basicConfig(11level = logging.INFO,12filename = "parselog.txt",13filemode = "w"14)15log = logging.getLogger()1617import basiclex18import basparse19import basinterp2021# If a filename has been specified, we try to run it.22# If a runtime error occurs, we bail out and enter23# interactive mode below24if len(sys.argv) == 2:25data = open(sys.argv[1]).read()26prog = basparse.parse(data,debug=log)27if not prog: raise SystemExit28b = basinterp.BasicInterpreter(prog)29try:30b.run()31raise SystemExit32except RuntimeError:33pass3435else:36b = basinterp.BasicInterpreter({})3738# Interactive mode. This incrementally adds/deletes statements39# from the program stored in the BasicInterpreter object. In40# addition, special commands 'NEW','LIST',and 'RUN' are added.41# Specifying a line number with no code deletes that line from42# the program.4344while 1:45try:46line = raw_input("[BASIC] ")47except EOFError:48raise SystemExit49if not line: continue50line += "\n"51prog = basparse.parse(line,debug=log)52if not prog: continue5354keys = list(prog)55if keys[0] > 0:56b.add_statements(prog)57else:58stat = prog[keys[0]]59if stat[0] == 'RUN':60try:61b.run()62except RuntimeError:63pass64elif stat[0] == 'LIST':65b.list()66elif stat[0] == 'BLANK':67b.del_line(stat[1])68elif stat[0] == 'NEW':69b.new()707172737475767778798081