README revision 4479
113481Sgiacomo.travaglini@arm.comInspired by a September 14, 2006 Salon article "Why Johnny Can't Code" by
213481Sgiacomo.travaglini@arm.comDavid Brin (http://www.salon.com/tech/feature/2006/09/14/basic/index.html),
313481Sgiacomo.travaglini@arm.comI thought that a fully working BASIC interpreter might be an interesting,
413481Sgiacomo.travaglini@arm.comif not questionable, PLY example.  Uh, okay, so maybe it's just a bad idea,
513481Sgiacomo.travaglini@arm.combut in any case, here it is.
613481Sgiacomo.travaglini@arm.com
713481Sgiacomo.travaglini@arm.comIn this example, you'll find a rough implementation of 1964 Dartmouth BASIC
813481Sgiacomo.travaglini@arm.comas described in the manual at:
913481Sgiacomo.travaglini@arm.com
1013481Sgiacomo.travaglini@arm.com   http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf
1113481Sgiacomo.travaglini@arm.com
1213481Sgiacomo.travaglini@arm.comSee also:
1313481Sgiacomo.travaglini@arm.com
1413481Sgiacomo.travaglini@arm.com  http://en.wikipedia.org/wiki/Dartmouth_BASIC
1513481Sgiacomo.travaglini@arm.com
1613481Sgiacomo.travaglini@arm.comThis dialect is downright primitive---there are no string variables
1713481Sgiacomo.travaglini@arm.comand no facilities for interactive input. Moreover, subroutines and functions
1813481Sgiacomo.travaglini@arm.comare brain-dead even more than they usually are for BASIC. Of course,
1913481Sgiacomo.travaglini@arm.comthe GOTO statement is provided.
2013481Sgiacomo.travaglini@arm.com
2113481Sgiacomo.travaglini@arm.comNevertheless, there are a few interesting aspects of this example:
2213481Sgiacomo.travaglini@arm.com
2313481Sgiacomo.travaglini@arm.com  - It illustrates a fully working interpreter including lexing, parsing,
2413481Sgiacomo.travaglini@arm.com    and interpretation of instructions.
2513481Sgiacomo.travaglini@arm.com 
2613481Sgiacomo.travaglini@arm.com  - The parser shows how to catch and report various kinds of parsing
2713481Sgiacomo.travaglini@arm.com    errors in a more graceful way.
2813481Sgiacomo.travaglini@arm.com
2913481Sgiacomo.travaglini@arm.com  - The example both parses files (supplied on command line) and
3013481Sgiacomo.travaglini@arm.com    interactive input entered line by line.
3113481Sgiacomo.travaglini@arm.com
3213481Sgiacomo.travaglini@arm.com  - It shows how you might represent parsed information.  In this case,
3313481Sgiacomo.travaglini@arm.com    each BASIC statement is encoded into a Python tuple containing the
3413481Sgiacomo.travaglini@arm.com    statement type and parameters.  These tuples are then stored in
3513481Sgiacomo.travaglini@arm.com    a dictionary indexed by program line numbers.
3613481Sgiacomo.travaglini@arm.com
3713481Sgiacomo.travaglini@arm.com  - Even though it's just BASIC, the parser contains more than 80
3813481Sgiacomo.travaglini@arm.com    rules and 150 parsing states. Thus, it's a little more meaty than
3913481Sgiacomo.travaglini@arm.com    the calculator example.
4013481Sgiacomo.travaglini@arm.com
41To use the example, run it as follows:
42
43   % python basic.py hello.bas
44   HELLO WORLD
45   %
46
47or use it interactively:
48
49   % python basic.py
50   [BASIC] 10 PRINT "HELLO WORLD"
51   [BASIC] 20 END
52   [BASIC] RUN
53   HELLO WORLD
54   [BASIC]
55
56The following files are defined:
57
58   basic.py         - High level script that controls everything
59   basiclex.py      - BASIC tokenizer
60   basparse.py      - BASIC parser
61   basinterp.py     - BASIC interpreter that runs parsed programs.
62
63In addition, a number of sample BASIC programs (.bas suffix) are
64provided.  These were taken out of the Dartmouth manual.
65
66Disclaimer: I haven't spent a ton of time testing this and it's likely that
67I've skimped here and there on a few finer details (e.g., strictly enforcing
68variable naming rules).  However, the interpreter seems to be able to run
69the examples in the BASIC manual.
70
71Have fun!
72
73-Dave
74
75
76
77
78
79
80