Commit d1d0b370 by Andrew Dahl

Added some input checking and removed non-verbose mode outputting

parent 2b5ed2bf
......@@ -30,6 +30,7 @@ public:
Symbol* FindConstant(const int value);
Symbol* FindConstant(const float value);
Symbol* Find(const string lexeme);
bool ExistsInCurrentLevel(const string lexeme);
void setParentTable(int parent_table_num, SymbolTable* parent_table);
int getParentTable() const;
......
......@@ -174,7 +174,7 @@ string InstructionList::toString()
for(itr = it->second.begin(); itr != it->second.end(); itr++)
{
temp = itr->second;
std::cerr << temp->toGenericString() << endl;
//std::cerr << temp->toGenericString() << endl;
//TODO: Some seriously awesome stuff
switch(temp->getOpcode())
......@@ -419,7 +419,7 @@ string InstructionList::FunctionToString(Instruction* inst, int varAddrSpace) {
for(itr = ++(it->second.begin()); itr != it->second.end(); itr++)
{
temp = itr->second;
std::cerr << temp->toGenericString() << endl;
//std::cerr << temp->toGenericString() << endl;
//TODO: Some seriously awesome stuff
switch(temp->getOpcode())
......
......@@ -52,19 +52,27 @@ external_decl: NEWLINE
;
function_define: FUNCTION ID LPAREN {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($2, Symbol::FUNCTION, Symbol::INTEGER));
symbol_table->push_back(new SymbolTable(level, symbol_table->at(level)));
level = symbol_table->size()-1;
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
if(symbol_table->at(level)->ExistsInCurrentLevel($2)) {
yyerror(symbol_table,instructions,"Redeclaration of function");
} else {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($2, Symbol::FUNCTION, Symbol::INTEGER));
if(temp->getType() == Symbol::VARIABLE) {
yyerror(symbol_table,instructions,"Function cannot have the same name as a previously declared variable!")
} else {
symbol_table->push_back(new SymbolTable(level, symbol_table->at(level)));
level = symbol_table->size()-1;
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
}
}
} argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
Symbol* temp = symbol_table->at(level)->Find($2);
temp->setFunctionValue($5);
level = symbol_table->at(level)->getParentTable();
temp->setReturnType($8->getReturnType());
instructions->levelPop();
$$ = temp;
Symbol* temp = symbol_table->at(level)->Find($2);
temp->setFunctionValue($5);
level = symbol_table->at(level)->getParentTable();
temp->setReturnType($8->getReturnType());
instructions->levelPop();
$$ = temp;
}
;
......@@ -81,9 +89,13 @@ argument_exp_list: { $$ = NULL;}
assignment_exp: additive_exp
| ID EQUALS assignment_exp {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($1, Symbol::VARIABLE, $3->getReturnType()));
instructions->addInstruction(new Instruction(Instruction::VARDECL, temp));
instructions->addInstruction(new Instruction(Instruction::MOV, temp));
instructions->addInstruction(new Instruction(Instruction::POP));
if(temp->getType() == Symbol::FUNCTION) {
yyerror(symbol_table, instructions, "Function used like variable!");
} else {
instructions->addInstruction(new Instruction(Instruction::VARDECL, temp));
instructions->addInstruction(new Instruction(Instruction::MOV, temp));
instructions->addInstruction(new Instruction(Instruction::POP));
}
}
;
......@@ -92,9 +104,13 @@ stat: OUTPUT additive_exp NEWLINE {
}
| ID EQUALS assignment_exp NEWLINE {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($1, Symbol::VARIABLE, $3->getReturnType()));
instructions->addInstruction(new Instruction(Instruction::VARDECL, temp));
instructions->addInstruction(new Instruction(Instruction::MOV, temp));
instructions->addInstruction(new Instruction(Instruction::POP));
if(temp->getType() == Symbol::FUNCTION) {
yyerror(symbol_table, instructions, "Function used like variable!");
} else {
instructions->addInstruction(new Instruction(Instruction::VARDECL, temp));
instructions->addInstruction(new Instruction(Instruction::MOV, temp));
instructions->addInstruction(new Instruction(Instruction::POP));
}
}
;
......
......@@ -73,6 +73,16 @@ Symbol* SymbolTable::Find(const string lexeme)
return it->second;
}
bool SymbolTable::ExistsInCurrentLevel(const string lexeme)
{
map<string,Symbol*>::iterator it;
it = this->table.find(lexeme);
if(it == this->table.end())
return false;
return true;
}
void SymbolTable::setParentTable(int parent_table_num, SymbolTable* parent_table)
{
this->parent_table_num = parent_table_num;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment