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