I am trying to make an interpreter. This stack function is supposed to represent the scope of the program, meaning scope of the variables. The START represents a new scope, and FINISH represents the current scope closing. I am trying to do a recursive function in which the stack is updated with each recursive call, which represent a new scope for each call.
Here's whats wrong:
After i enter a new scope and the scope ends, my program prematurely terminates. What do you think is wrong?
void stack(ifstream& file,Hash& Table)
{
string line;
getline(file,line);
int i=0;
for(i=0;isspace(line.at(i))&&i<line.length();i++)
{}
while(line.substr(i,6)!="FINISH")
{
if(line.substr(i,5)=="START")
{
stack(file,Table);
}
if(line.substr(i,3)=="VAR")
{
int j=i+4;
for(j=i+4;line.at(j)!='=';j++)
{}
int number=stoi(line.substr(j+1,line.length()));
Table.add(line.substr(i+4,j-(i+4)),number);
}
getline(file,line);
int i=0;
for(i=0;isspace(line.at(i))&&i<line.length();i++)
{}
}
}
Heres my source program i am trying to interpret:
COM HERE IS OUR FIRST BORG PROGRAM
COM WHAT A ROBUST LANGUAGE IT IS
START
VAR BORAMIR=25
VAR LEGOLAS=101
PRINT BORAMIR
BORAMIR++
PRINT LEGOLAS
PRINT GANDALF
PRINT BORAMIR*2
COM
COM NESTED BLOCK
COM
START
VAR GANDALF=49
PRINT GANDALF
PRINT BORAMIR
FINISH
PRINT GANDALF
START
LEGOLAS=1000
PRINT LEGOLAS
FINISH
PRINT LEGOLAS
LEGOLAS--
PRINT LEGOLAS
FINISH