Commit 3045417b by Andrew Dahl

Fixes #12 - Stage 9 - Potentially Recursive Functions works

parent 2b438a82
Showing with 12 additions and 80 deletions
......@@ -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 */
......@@ -58,7 +58,7 @@ function_define: FUNCTION ID LPAREN {
symbol_table->at(level)->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
} argument_exp_list RPAREN EQUALS additive_exp_func NEWLINE {
} 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();
......@@ -189,84 +189,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");
} else if($3->getType() == Symbol::FLOAT) {
if($3->getValue().f == 0)
yyerror(symbol_table, instructions, "Cannot divide by 0");
}
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;
}
| 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!");
} else if (temp->getType() == Symbol::FUNCTION) {
yyerror(symbol_table, instructions, "Function used like variable!");
} 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!");
}
;
func_call_args: { $$ = NULL; }
| additive_exp {
......
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
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