CS 603: Programming Language Organization Lecture 3 Spring 2002 Department of Computer Science University of Alabama Joel Jones
©2001 Joel Jones Outline Questions Interpreter Basics Reading for next time
©2001 Joel Jones Questions Any questions on chapter 1 language?
©2001 Joel Jones Interpreters Input: program text (or some intermediate form) Execution: runs program without producing machine code
©2001 Joel Jones Common Features of Language Processsors Readers –convert external representation into internal representation –Also called lexers/parsers Environments –mapping from names to values
©2001 Joel Jones Common Features of “Kamin” Interpreters (via Budd) Readers— class Reader Environments— class Environment Expressions— class Expression and its subclasses Read-eval-print loop—in main()
©2001 Joel Jones Read-eval-print Loop (figure 1.1) Initialization of data structures Then: while(true) { read expression if “quit” then break (and exit interpreter) evaluate expression print expression }
©2001 Joel Jones Reader (figure 1.2, 1.3, 1.4) Superclass with some general functions –e.g. printPrimaryPrompt, skipSpaces, etc. Subclassed (potentially) for every language –virtual Expression * readExpression(); Public interface just a single function: –Expression * promptAndRead()
©2001 Joel Jones Environment (figures 1.5, 1.6, 1.7) Several public methods –Constructor: Environment(ListNode *, ListNode *, Environment *); –Testing: virtual Environment * isEnvironment(); –Accessing: Expression * lookup(Symbol *); void add(Symbol *, Expression *); void set(Symbol *, Expression *); –Memory Management: virtual void free(); class Env : public Expr
©2001 Joel Jones Environment (figure 1.6, 1.7) Uses parallel lists –nameit—list of Symbols –valueit—list of Expressions
©2001 Joel Jones Expressions (figure 1.8) Superclass at top of extensive hierarchy –Expression Integer Symbol List Function –DefineStatement »IfStatement »… »BinaryFunction »UserFunction
©2001 Joel Jones Expression (figures 1.9–1.11) To determine specific subclass –uses isFoo functions e.g. isInteger(), isSymbol(), etc. To evaluate a function –virtual void eval(Expr &, Environment *, Environment) Expr uses “smart pointers” for memory management –overload “=“ –reference counting
©2001 Joel Jones IntegerExpression Constructor takes int Overloads isInteger() to return the current object Pair Up: Write IntegerExpression * isInteger();
©2001 Joel Jones Reading & Questions for Next Class Rest of Interpreter Handout