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