Commit d3eeb271 by Andrew Dahl

Added some input checking and removed non-verbose mode outputting

Conflicts:

	lib/parser.y
parent 29be2982
......@@ -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())
......
......@@ -51,14 +51,22 @@ external_decl: NEWLINE
| error { yyerrok;}
;
function_decl: 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));
$$ = temp;
function_decl: FUNCTION ID LPAREN {
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));
$$ = temp;
}
}
}
;
......@@ -90,9 +98,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));
}
}
;
......@@ -105,9 +117,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;
......
......@@ -4,25 +4,7 @@ c=readInput + 2.5
function func(a,b,c,d)=a+b+c
function func2()=10
function func1(a,b)=func(a,b,1,readInput) - func2()
function outFunc(a,b,c)
{
output a
output b
output c
return a+b+c
}
output readInput + func(func1(readInput,a),2,func2(),0)
d=outFunc(1,2,3)
output a
output b
output c
output d
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