1# An implementation of Dartmouth BASIC (1964) 2# 3 4import sys 5sys.path.insert(0,"../..") 6 7if sys.version_info[0] >= 3: 8 raw_input = input 9 10import logging 11logging.basicConfig( 12 level = logging.INFO, 13 filename = "parselog.txt", 14 filemode = "w" 15) 16log = logging.getLogger() 17 18import basiclex 19import basparse 20import basinterp 21 22# If a filename has been specified, we try to run it. 23# If a runtime error occurs, we bail out and enter 24# interactive mode below 25if len(sys.argv) == 2: 26 data = open(sys.argv[1]).read() 27 prog = basparse.parse(data,debug=log) 28 if not prog: raise SystemExit 29 b = basinterp.BasicInterpreter(prog) 30 try: 31 b.run() 32 raise SystemExit 33 except RuntimeError: 34 pass 35 36else: 37 b = basinterp.BasicInterpreter({}) 38 39# Interactive mode. This incrementally adds/deletes statements 40# from the program stored in the BasicInterpreter object. In 41# addition, special commands 'NEW','LIST',and 'RUN' are added. 42# Specifying a line number with no code deletes that line from 43# the program. 44 45while 1: 46 try: 47 line = raw_input("[BASIC] ") 48 except EOFError: 49 raise SystemExit 50 if not line: continue 51 line += "\n" 52 prog = basparse.parse(line,debug=log) 53 if not prog: continue 54 55 keys = list(prog) 56 if keys[0] > 0: 57 b.add_statements(prog) 58 else: 59 stat = prog[keys[0]] 60 if stat[0] == 'RUN': 61 try: 62 b.run() 63 except RuntimeError: 64 pass 65 elif stat[0] == 'LIST': 66 b.list() 67 elif stat[0] == 'BLANK': 68 b.del_line(stat[1]) 69 elif stat[0] == 'NEW': 70 b.new() 71 72 73 74 75 76 77 78 79 80