Commit 9fa7ae35 by Andrew Dahl

Merge branch 'dev_stage9'

Conflicts:
	lib/instructionList.cpp
	lib/parser.y
parents 5aa1e682 9013645b
......@@ -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;
......
......@@ -276,7 +276,6 @@ string InstructionList::toString()
oss << "PUSH DWORD [" << temp->arg1ToString() << "]\n";
break;
case Symbol::INTEGER:
this->PrintStack();
oss << "PUSH " << temp->arg1ToString() << endl;
break;
case Symbol::VARIABLE:
......
......@@ -34,7 +34,7 @@
%token <fnum> FLOAT
%token <name> ID
%type <sym> stat additive_exp mult_exp primary_exp additive_exp_func mult_exp_func primary_exp_func assignment_exp function_define argument_exp_list func_call_args
%type <sym> stat additive_exp mult_exp primary_exp assignment_exp function_define argument_exp_list func_call_args
%% /* Grammer Rules and Actions */
......@@ -65,16 +65,16 @@ function_define: FUNCTION ID LPAREN {
level = symbol_table->size()-1;
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
}
}
} argument_exp_list RPAREN EQUALS additive_exp_func 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;
} 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;
}
;
......@@ -227,99 +227,6 @@ primary_exp: LPAREN additive_exp RPAREN {
}
}
;
additive_exp_func: additive_exp_func PLUS mult_exp_func {
instructions->addInstruction(new Instruction(Instruction::ADD));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
$$ = new Symbol("temp",static_cast<float>(1.0));
else
$$ = new Symbol("temp",1);
}
| additive_exp_func MINUS mult_exp_func {
instructions->addInstruction(new Instruction(Instruction::SUB));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
$$ = new Symbol("temp",static_cast<float>(1.0));
else
$$ = new Symbol("temp",1);
}
| mult_exp_func {
$$ = $1;
}
;
mult_exp_func: mult_exp_func ASTERISK primary_exp_func {
instructions->addInstruction(new Instruction(Instruction::MUL));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
$$ = new Symbol("temp",static_cast<float>(1.0));
else
$$ = new Symbol("temp",1);
}
| mult_exp_func SLASH primary_exp_func {
if($3->getType() == Symbol::INTEGER) {
if($3->getValue().i == 0)
yyerror(symbol_table, instructions, "Cannot divide by 0");
YYABORT;
} else if($3->getType() == Symbol::FLOAT) {
if($3->getValue().f == 0)
yyerror(symbol_table, instructions, "Cannot divide by 0");
YYABORT;
}
instructions->addInstruction(new Instruction(Instruction::DIV));
if($1->getReturnType() == Symbol::FLOAT || $3->getReturnType() == Symbol::FLOAT)
$$ = new Symbol("temp",static_cast<float>(1.0));
else
$$ = new Symbol("temp",1);
}
| primary_exp_func {
$$ = $1;
}
;
primary_exp_func: LPAREN additive_exp_func RPAREN {
$$ = $2;
}
| NUMBER {
Symbol* temp = symbol_table->at(level)->Insert($1);
instructions->addInstruction(new Instruction(Instruction::PUSH, temp));
$$ = temp;
}
| FLOAT {
Symbol* temp = symbol_table->at(level)->Insert($1);
instructions->addInstruction(new Instruction(Instruction::PUSH, temp));
$$ = temp;
}
| MINUS NUMBER {
Symbol* temp = symbol_table->at(level)->Insert((-1 * $2));
instructions->addInstruction(new Instruction(Instruction::PUSH, temp));
$$ = temp;
}
| MINUS FLOAT {
Symbol* temp = symbol_table->at(level)->Insert((-1 * $2));
instructions->addInstruction(new Instruction(Instruction::PUSH, temp));
$$ = temp;
}
| USERINPUT {
instructions->addInstruction(new Instruction(Instruction::INPUT));
$$ = symbol_table->at(level)->Find("readInput");
}
| ID {
Symbol* temp = symbol_table->at(level)->Find($1);
if(temp == NULL) {
yyerror(symbol_table, instructions, "Variable not declared yet!");
YYABORT;
} else if (temp->getType() == Symbol::FUNCTION) {
yyerror(symbol_table, instructions, "Function used like variable!");
YYABORT;
} else {
instructions->addInstruction(new Instruction(Instruction::PUSH, temp));
$$ = temp;
}
}
| ID LPAREN func_call_args RPAREN {
yyerror(symbol_table, instructions, "Cannot use function within a function!");
YYABORT;
}
;
func_call_args: { $$ = NULL; }
| additive_exp {
......
......@@ -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;
......
a=readInput
b=readInput + 1.0
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()
output readInput + func(func1(readInput,a),2,func2(),0)
output a
output b
output c
output func(1,2,3,4)
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