My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
Interpreter  
A brief summary of the Interpreter.
Updated Dec 18, 2008 by zach.bus...@gmail.com

Setting Up

Details on downloading Kitsch and compiling the interpreter are available here. Please see that page for instruction on setting up and running the Kitsch interpreter.

Interpreter Design

The interpreter source code is available here. The design follows the standard Visitor pattern; pj2, which generates the parser for the language, is also kind enough to generate tree node classes and implement a visit() method for each of them.

So then the only code that needs to be written is the "tree walker" side of the visitor pattern, which comprises Evaluator.java. We simply step walk down the tree in a generally postfix manner (slight exceptions for if statements), evaluating the children to get the result of the parent, and then bubble that up the tree (if necessary) by using return statements.

Scope

Scope is implemented using the Scope object, an inner class of Evaluate. A Scope object contains a Map from identifiers to their values, and an outerscope that defines the parent Scope object (this is null for the global scope). Name lookup works by checking the particular scope's map for the identifier, and, if it is not found, asking the parent scope to find it. In this way, recursive calls get bubbled up the scope chain until the identifier is found (if it isn't found, it is undeclared and hence an error).

The evaluator maintains two Scope objects; the global Scope, which is the overarching Scope that contains all global variables and has no parent, and the current Scope, which is the current scope as the program is being interpreted.

Powered by Google Project Hosting