Debian Codegraph @

third_party/python/ply/example/BASIC/basic.py

raw
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:
21 raise SystemExit
22 b = basinterp.BasicInterpreter(prog)
23 try:
24 b.run()
25 raise SystemExit
26 except RuntimeError:
27 pass
28
29 else:
30 b = basinterp.BasicInterpreter({})
31
32 # Interactive mode. This incrementally adds/deletes statements
33 # from the program stored in the BasicInterpreter object. In
34 # addition, special commands 'NEW','LIST',and 'RUN' are added.
35 # Specifying a line number with no code deletes that line from
36 # the program.
37
38 while 1:
39 try:
40 line = raw_input("[BASIC] ")
41 except EOFError:
42 raise SystemExit
43 if not line:
44 continue
45 line += "\n"
46 prog = basparse.parse(line)
47 if not prog:
48 continue
49
50 keys = list(prog)
51 if keys[0] > 0:
52 b.add_statements(prog)
53 else:
54 stat = prog[keys[0]]
55 if stat[0] == 'RUN':
56 try:
57 b.run()
58 except RuntimeError:
59 pass
60 elif stat[0] == 'LIST':
61 b.list()
62 elif stat[0] == 'BLANK':
63 b.del_line(stat[1])
64 elif stat[0] == 'NEW':
65 b.new()