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