Commit 9013645b by Andrew Dahl

Added error for calling self within function

parent 20a1c943
Showing with 10 additions and 1 deletions
......@@ -5,6 +5,8 @@
#include <stdio.h>
#include <math.h>
#include <vector>
#include <string>
#include <string.h>
#include "../include/symbol.h"
#include "../include/symbolTable.h"
#include "../include/instruction.h"
......@@ -17,6 +19,7 @@
#define yyerror(symbol_table,instructions,msg) hack_yyerror(msg);
void hack_yyerror(char const *);
int level = 1;
string lastFunc = "";
%}
%parse-param { vector<SymbolTable*>* symbol_table }
......@@ -57,6 +60,7 @@ function_define: FUNCTION ID LPAREN {
YYABORT;
} else {
Symbol* temp = symbol_table->at(level)->Insert(new Symbol($2, Symbol::FUNCTION, Symbol::INTEGER));
lastFunc = $2;
if(temp->getType() == Symbol::VARIABLE) {
yyerror(symbol_table,instructions,"Function cannot have the same name as a previously declared variable!")
YYABORT;
......@@ -65,7 +69,8 @@ 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));
lastFunc = "";
}
}
} argument_exp_list RPAREN EQUALS additive_exp NEWLINE {
......@@ -213,6 +218,9 @@ primary_exp: LPAREN additive_exp RPAREN {
} else if(temp->getType() != Symbol::FUNCTION) {
yyerror(symbol_table, instructions, "Variable used like function!");
YYABORT;
} else if(strcmp(lastFunc.c_str(),$1)) {
yyerror(symbol_table, instructions, "Cannot use a function within itself!");
YYABORT;
} else {
Symbol* func = new Symbol(temp);
func->setFunctionValue($3);
......
......@@ -8,3 +8,4 @@ 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