Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Goal: To develop a COOL program interpreter by evaluating the abstract syntax tree, which represents a COOL program. Description: You need to implement interp methods

Goal: To develop a COOL program interpreter by evaluating the abstract syntax tree, which represents a COOL program. Description: You need to implement interp methods for each type of expressions. Thefollowing changes has been made: class ValueEntry: classes to hold different types of values class Environment o SymbolTable var_table; Var_table is now defined to hold value, instead of type information for each variable o ValueP var_value_lookup(Symbol s); look up the value of a given variable s o ValueP var_value_probe(Symbol s); check the value of a given variable s in latest scope o void var_value_change(Symbol s, ValueP v); Modify the value for the given variable s o EnvironmentP instantiation(); Duplicate the environment deep copy of var_table, but shallow copy for all other member data class InheritanceNode o EnvironmentP instantiation(); Create an environment for an object from environment of the class o EnvironmentP getEnvironment() class ClassTable o ValueP interp(); For each class, using provided initial value to initialize attribute in Environment. Find Main class, then find main method and interp main method. Interpreter_student.cpp: All your implementation should go in this file. Interpreter.cpp: You can modify this file to decide which version: student or teacher version should be used to test your solution. To debug your solution, you can come up with your own COOL program to contain features you want to test. You can also test your interpreter against COOL sample programs provided in class repository. Also provides the solution in a library. During the development, if you want to use teacherssolution in some methods so that you can focus on one specific method, you can use teacher_version instead of student_version in typechecking.cpp file. Set up environment: In addition to Visual Studio solution for Windows platform, the project also provides one for Linux platform. --work on TypeChecking_Student.cpp file make --compile your project using make command ./main example.cl --run your program against example.cl If you want to use MacOS for the project, you can download the Linux version. You may need to change g++ at line 9 of makefile to the compiler you want to use. The compilation and execution should be similar to the Linux version. Tips for the project: How to create an object from a class? First, get the InheritanceNodeP from the class name by looking up class symbol table, and call InheritanceNode::instantiation() to create an object from it. Please be noted that an object is represented by an Environment object. Instructor provided files in the class repository The following files are provided by the instructor: Skeleton source files you need to know: o Absyn.h: contains class definition for all AST node types. o AbsynExtension.h: contains extensions to nodes defined in AST o StringTab.h and StringTab.cpp: contains definition of string table o Interpreter.h: the file defining values of variables o Interpreter.cpp: the file of deciding which version is used: teachers or students o Interpreter_student.cpp: This is the file you should work on. o Semant.h: Contains class definitions you may need for this project o good.cl: provided test file Description7.pdf: this file #include #include #include #include #include "Absyn.h" #include "Semant.h" #include "lib.h" using namespace std; using namespace absyn; /////////////////////////////////////////////////////////////////////////////// // // Interpreter Features // // For each class of expression, there is an interpreter method to evaluate it. // The interpreter methods make use of the environments previously constructred // for each class. This code parallels the structure of the type // inference rules in the CoolAid very closely. // /////////////////////////////////////////////////////////////////////////////// ValueP IntExp::interp_student(EnvironmentP env) { return new ValueEntry(stoi(getValue()->getContent())); } ValueP BoolExp::interp_student(EnvironmentP env) { return new ValueEntry(getValue()); } ValueP StringExp::interp_student(EnvironmentP) { return new ValueEntry(getValue()->getContent()); } ValueP OpExp::interp_student(EnvironmentP env) { //put your implementation here // Algorithm: // interpret left operand and save its value to a local variable t1 // interpret right operand and save its value to a local variable t2 // // if operator is PLUS/MINUS/MUL/DIV/LT/LE, return the result of t1 and t2 based on the type of operator // // if operator is EQ // if t1 is an integer, compare t1 and t2 as integers and return result // else if t1 is a string, compare t1 and t2 as strings and return result // else if t1 is an object, compare t1 and t2 as objects and return result // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP NotExp::interp_student(EnvironmentP env) { //put your implementation here // //Algorithm: // interpret expr and save its return value to a local variable v // return a value based on the result of v. // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP ObjectExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // if name is equal to self // return ValueEntry based on the self type and env. // else // return the value of the variable "name". (Where to get the value of a variable??) // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP NewExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // retrieve the class information from the "type_name" and save it to a local variable. // return a pointer to ValueEntry based on the type_name and its envrionment // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP IsvoidExp::interp_student(EnvironmentP env) { //put your implementation here // Algorithm: // interpret "expr" and save its return value to a local variable v; // return value based on the type of value in v (void or not) // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP LetExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // enter scope for variable symbol table // add the variable "identifier" to the variable symbol table // // if init is provided // interpret the init and save its return value to a local variable v // use v to update the value of variable "identifier" // interpret the body and save its return value to v // exit scope for variable symbol table // return v; // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP BlockExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // for each expressionin the "body" // interpret the expression and save its value to a local variable v // // return v; // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP AssignExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // interpret the "expr" and save its return value to a local variable v; // use v to update the value of variable "name" in the variable symbol table // return the value of variable "name" // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP IfExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // interpret the "pred" and save its return value to a local variable "cond" // if cond represents true // interpret "then_exp" and return its result // else // interpret "else_exp" and return its result //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP WhileExp::interp_student(EnvironmentP env) { //put your implementation here //Algorithm: // interpret the "pred" and save its return value to a local variable "cond" // while "cond" represents true // interpret the expression "body" // re-interpret the "pred" and save its return value to a local variable "cond" // //The return statement below is to avoid compile errors by returning VOID. //Your implementation should return the result of the expression being evaluated. return new ValueEntry(); } ValueP CallExp::interp_student(EnvironmentP env) { //No need to implement this one. //Teacher's solution will be used. return new ValueEntry(); } ValueP StaticCallExp::interp_student(EnvironmentP env) { //No need to implement this one. //Teacher's solution will be used. return new ValueEntry(); } ValueP Branch_class::interp_student(EnvironmentP env) { //No need to implement this one. //Teacher's solution will be used. return new ValueEntry(); } ValueP CaseExp::interp_student(EnvironmentP env) { //No need to implement this one. //Teacher's solution will be used. return new ValueEntry(); } ValueP NoExp::interp_student(EnvironmentP) { //No need to implement this one. //Teacher's solution will be used. return new ValueEntry(); } Only Implementing methods in Interpreter_student.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Recommended Textbook for

Cost Management Accounting And Control

Authors: Don R. Hansen, Maryanne M. Mowen, Liming Guan

6th Edition

324559674, 978-0324559675

More Books

Students also viewed these Programming questions

Question

understand the selection bias in contemporary work psychology;

Answered: 1 week ago