Question: ICSI 3 1 1 Assignment 7 Start the Interpreter Introduction Let s talk about interpreters. Remember that our AST is read only while our BASIC
ICSI Assignment Start the Interpreter
Introduction
Lets talk about interpreters. Remember that our AST is read only while our BASIC code is interpreting. It is a definition of our program in memory. But, of course, we know in computer science, our program needs two things to run: storage and inputoutput The AST is the program; to build the interpreter, we will build facilities for storage of variables, inputoutput functionality and finally some code that will look at each Node and execute it
First, though, lets think about efficiency a little. Consider a program:
READ count
FOR I to count
READ F
GOSUB Convert
NEXT I
END
DATA
Convert:
CF
PRINT FDEG F CDEG C
RETURN
When we encounter that first READ how can we get that value? We have to walk through the entire program looking for a DATA Once, that wouldnt be bad, but not look down to READ F We again have to walk the entire program to find the DATA And we have to keep track of what has been read already. Thats going to be really slow. What if before we started running, we did that walk once, and pulled all of the DATA items into a Java LinkedListQueue Once we do that, READ becomes pop Fast and easy!
Now, lets look at the GOSUB. In theory, the subroutine could be above or below the call. So similar to READ we would have to start at the top and read all the way through the AST to find it Thats slow! So we will modify our AST we will look through the AST before we start running, find all of the labels and make a HashMap. This will let us run our code quickly.
We will need to have storage for our variables. In other languages, we must be very flexible. BASIC lets us take a couple of shortcuts because there are only data types int float, string and every variable is global. That means no variables ever disappear go out of scope We dont have to track what is in scope and what is not; once a variable is created, it is forever We will turn, again, to HashMap. We will make one for each variable type.
The last bit of prep work is to implement the builtin functions. There are of them versions of NUM$ Implement these as static methods on your interpreter, using Java data types for example:
public static String leftString data, int characters implement me
Details
Create a new class Interpreterjava
Since it is possible and in fact, good form to put the DATA at the end of your program, we need to preprocess these. Walk the AST, searching for the DATA statements. Insert their contents into a Java collection that we can use for READ.
We also need to know where the labels are. Create a hash map string LabeledStatementNode. Use a visitor pattern to walk the statementsnode searching for labeledstatements.
We will need storage for our variables. Create HashMaps that map name string to data type for our variables. We will need three hash maps: StringInteger, StringFloat and StringString.
Finally, implement all of the builtin functions.
Testing is of course still important. You can test the data optimization and the label hashmap as well as the builtin functions.
Rubric
Poor OK Good Great
Code Style Few comments, bad names Some good naming, some necessary comments Mostly good naming, most necessary comments Good naming, nontrivial methods well commented, static only when necessary, private members
Unit Tests Dont exist At least one Missing tests All functionality tested
Variable Storage None Correct
Label optimization None Significantly Attempted Correct
Read optimization None Significantly Attempted Correct
RANDOM None Correct
LEFT$ None Correct
RIGHT$ None Correct
MID$ None Correct
NUM$ None Works for int OR float Correct for int and float
VAL None Correct
VAL None Correct
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
