CS 603: Programming Language Organization Lecture 7 Spring 2003 Department of Computer Science University of Alabama Joel Jones
©2003 Joel Jones Outline Questions Impcore Interpreter Reading for next time
©2003 Joel Jones Impcore Interpreter: ASTs Type of abstract syntax tree is a sum type, also known as a discriminated-union type –Not directly supported in C –Use tag and undiscriminated union Pair Up: So how are they represented? Pair Up: Write structure definition for top-level and be prepared to explain. Toplevel = EXP (Exp*) | VAL (Name *name, Exp *exp) | DEFINE (Name *name, Userfun userfun) | USE (Name*)
©2003 Joel Jones Impcore Interpreter: Environments Environments map names to values (or types), therefore interpreter needs to represent names, values, and environments Names are an abstract data type, Name, which support comparison and conversion to and from char* –nametostr converts a Name to char* –strtoname converts a char* to a Name and acts as a constructor
©2003 Joel Jones Impcore Interpreter: Environments (cont.) Uses one C type for Value and another for Fun Constructors –mkValenv –mkFunenv Accessors –fetchval, isvalfound, bindval –Fetchfun, isfunbound, bindfun
©2003 Joel Jones Impcore Interpreter: Evaluation Heart of the interpreter— topeval and eval Arguments follow form of operational semantics Pair Up: What was the form of the {downarrow} relation? What did it signify? Value eval(Exp *e, Valenv *globals, Funenv *functions, Valenv *formals)
©2003 Joel Jones Impcore Interpreter: Evaluation (cont.) Evaluation of tree structure –Dispatch on type of node at root –Recursively execute children –Execute tree Pair Up: What field of the AST representation do we dispatch on? How do you dispatch on type in a non-OO language like C?
©2003 Joel Jones Impcore Interpreter: Evaluation (cont.) For each AST type, there may be multiple applicable judgments in the operational semantics –VAR has F ORMAL V AR and G LOBAL V AR Pair Up: Write the mappings from tags to judgments for the rest of the Exp productions
©2003 Joel Jones Impcore Interpreter: Evaluation (cont.) The form of eval is then: switch (e->typ) { case TAG: evaluate rhs if shared premise exists chose and execute appropriate judgment... }
©2003 Joel Jones Impcore Interpreter: Evaluation (cont.) What is done in implementing each of the productions?
©2003 Joel Jones Reading & Questions for Next Class Chapter –You might also want to look at the online version of the book “Structure and Interpretation of Computer Programs” at: What most differentiates Scheme from C or C++?