14479Sbinkertn@umich.eduInspired by a September 14, 2006 Salon article "Why Johnny Can't Code" by 24479Sbinkertn@umich.eduDavid Brin (http://www.salon.com/tech/feature/2006/09/14/basic/index.html), 34479Sbinkertn@umich.eduI thought that a fully working BASIC interpreter might be an interesting, 44479Sbinkertn@umich.eduif not questionable, PLY example. Uh, okay, so maybe it's just a bad idea, 54479Sbinkertn@umich.edubut in any case, here it is. 64479Sbinkertn@umich.edu 74479Sbinkertn@umich.eduIn this example, you'll find a rough implementation of 1964 Dartmouth BASIC 84479Sbinkertn@umich.eduas described in the manual at: 94479Sbinkertn@umich.edu 104479Sbinkertn@umich.edu http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf 114479Sbinkertn@umich.edu 124479Sbinkertn@umich.eduSee also: 134479Sbinkertn@umich.edu 144479Sbinkertn@umich.edu http://en.wikipedia.org/wiki/Dartmouth_BASIC 154479Sbinkertn@umich.edu 164479Sbinkertn@umich.eduThis dialect is downright primitive---there are no string variables 174479Sbinkertn@umich.eduand no facilities for interactive input. Moreover, subroutines and functions 184479Sbinkertn@umich.eduare brain-dead even more than they usually are for BASIC. Of course, 194479Sbinkertn@umich.eduthe GOTO statement is provided. 204479Sbinkertn@umich.edu 214479Sbinkertn@umich.eduNevertheless, there are a few interesting aspects of this example: 224479Sbinkertn@umich.edu 234479Sbinkertn@umich.edu - It illustrates a fully working interpreter including lexing, parsing, 244479Sbinkertn@umich.edu and interpretation of instructions. 254479Sbinkertn@umich.edu 264479Sbinkertn@umich.edu - The parser shows how to catch and report various kinds of parsing 274479Sbinkertn@umich.edu errors in a more graceful way. 284479Sbinkertn@umich.edu 294479Sbinkertn@umich.edu - The example both parses files (supplied on command line) and 304479Sbinkertn@umich.edu interactive input entered line by line. 314479Sbinkertn@umich.edu 324479Sbinkertn@umich.edu - It shows how you might represent parsed information. In this case, 334479Sbinkertn@umich.edu each BASIC statement is encoded into a Python tuple containing the 344479Sbinkertn@umich.edu statement type and parameters. These tuples are then stored in 354479Sbinkertn@umich.edu a dictionary indexed by program line numbers. 364479Sbinkertn@umich.edu 374479Sbinkertn@umich.edu - Even though it's just BASIC, the parser contains more than 80 384479Sbinkertn@umich.edu rules and 150 parsing states. Thus, it's a little more meaty than 394479Sbinkertn@umich.edu the calculator example. 404479Sbinkertn@umich.edu 414479Sbinkertn@umich.eduTo use the example, run it as follows: 424479Sbinkertn@umich.edu 434479Sbinkertn@umich.edu % python basic.py hello.bas 444479Sbinkertn@umich.edu HELLO WORLD 454479Sbinkertn@umich.edu % 464479Sbinkertn@umich.edu 474479Sbinkertn@umich.eduor use it interactively: 484479Sbinkertn@umich.edu 494479Sbinkertn@umich.edu % python basic.py 504479Sbinkertn@umich.edu [BASIC] 10 PRINT "HELLO WORLD" 514479Sbinkertn@umich.edu [BASIC] 20 END 524479Sbinkertn@umich.edu [BASIC] RUN 534479Sbinkertn@umich.edu HELLO WORLD 544479Sbinkertn@umich.edu [BASIC] 554479Sbinkertn@umich.edu 564479Sbinkertn@umich.eduThe following files are defined: 574479Sbinkertn@umich.edu 584479Sbinkertn@umich.edu basic.py - High level script that controls everything 594479Sbinkertn@umich.edu basiclex.py - BASIC tokenizer 604479Sbinkertn@umich.edu basparse.py - BASIC parser 614479Sbinkertn@umich.edu basinterp.py - BASIC interpreter that runs parsed programs. 624479Sbinkertn@umich.edu 634479Sbinkertn@umich.eduIn addition, a number of sample BASIC programs (.bas suffix) are 644479Sbinkertn@umich.eduprovided. These were taken out of the Dartmouth manual. 654479Sbinkertn@umich.edu 664479Sbinkertn@umich.eduDisclaimer: I haven't spent a ton of time testing this and it's likely that 674479Sbinkertn@umich.eduI've skimped here and there on a few finer details (e.g., strictly enforcing 684479Sbinkertn@umich.eduvariable naming rules). However, the interpreter seems to be able to run 694479Sbinkertn@umich.eduthe examples in the BASIC manual. 704479Sbinkertn@umich.edu 714479Sbinkertn@umich.eduHave fun! 724479Sbinkertn@umich.edu 734479Sbinkertn@umich.edu-Dave 744479Sbinkertn@umich.edu 754479Sbinkertn@umich.edu 764479Sbinkertn@umich.edu 774479Sbinkertn@umich.edu 784479Sbinkertn@umich.edu 794479Sbinkertn@umich.edu 80