Commit c6e1681f by Andrew Dahl

Checking for correct number of arguments works now

parent 0736eb76
......@@ -40,11 +40,13 @@ class Symbol {
string getLexeme() const;
void setValue(int value);
void setValue(float value);
void setValue(Symbol* value);
value_t getValue() const;
void setType(enum Type valueType);
enum Type getType() const;
void setReturnType(enum Type returnType);
enum Type getReturnType() const;
int getArguments();
bool operator==(const Symbol *rhs) const;
string toString() const;
......
......@@ -119,8 +119,10 @@ string InstructionList::toString()
* variables at the start of functions... so, when we run into a functio or a block, we'll need
* to call this function so that it allocates all the required stuff...
*/
int e = 0;
for(it = this->instructions.begin(); it != this->instructions.end(); it++)
{
this->Print();
varAddrSpace = this->findVarStackSpace(it->second);
if(it->second[0]->getOpcode() != Instruction::FUNCTION_START)
{
......@@ -131,6 +133,8 @@ string InstructionList::toString()
isFunction = true;
for(itr = it->second.begin(); itr != it->second.end(); itr++)
{
std::cerr << e << std::endl;
e++;
temp = itr->second;
//TODO: Some seriously awesome stuff
......
......@@ -34,7 +34,7 @@
%token <fnum> FLOAT
%token <name> ID
%type <sym> stat additive_exp mult_exp primary_exp assignment_exp function_define argument_exp_list
%type <sym> stat additive_exp mult_exp primary_exp assignment_exp function_define argument_exp_list func_call_args
%% /* Grammer Rules and Actions */
......@@ -68,11 +68,14 @@ function_define: FUNCTION ID LPAREN argument_exp_list RPAREN EQUALS {
}
;
argument_exp_list: { $$ = new Symbol();}
argument_exp_list: { $$ = NULL;}
| ID {
$$ = symbol_table[level]->Insert(new Symbol($1, Symbol::VARIABLE, Symbol::INTEGER));
}
| argument_exp_list COMMA ID { }
| argument_exp_list COMMA ID {
Symbol::value_t symbol_value; symbol_value.s = $1;
$$ = symbol_table[level]->Insert(new Symbol($3, symbol_value, Symbol::VARIABLE));
}
;
assignment_exp: additive_exp
......@@ -168,18 +171,31 @@ primary_exp: LPAREN additive_exp RPAREN {
}
| ID LPAREN func_call_args RPAREN {
Symbol* temp = symbol_table[level]->Find($1);
Symbol* func = new Symbol(temp);
func->setValue($3);
fprintf(stderr, "%d %d", temp->getArguments(), func->getArguments());
if(temp == NULL) {
yyerror(symbol_table, instructions, "Function not declared yet!");
} else if(temp->getArguments() != func->getArguments()) {
yyerror(symbol_table, instructions, "Incorrect number of arguments given!");
} else {
instructions->addInstruction(new Instruction(Instruction::CALL, temp));
$$ = temp;
instructions->addInstruction(new Instruction(Instruction::CALL, func));
$$ = func;
}
}
;
func_call_args: { }
| additive_exp { }
| func_call_args COMMA additive_exp { }
func_call_args: { $$ = NULL; }
| additive_exp {
$1->setType(Symbol::VARIABLE);
$$ = $1;
}
| func_call_args COMMA additive_exp {
$3->setType(Symbol::VARIABLE);
$3->setValue($1);
$$ = $3;
}
;
%%
......
......@@ -47,7 +47,7 @@ equals [=]
lparen [(]
rparen [)]
letter [a-zA-Z]
identifier {letter}({letter}|{digit})+
identifier {letter}({letter}|{digit})*
comma [,]
%%
......
......@@ -78,6 +78,11 @@ void Symbol::setValue(float value)
this->setType(Symbol::FLOAT);
}
void Symbol::setValue(Symbol* value)
{
this->value.s = value;
}
Symbol::value_t Symbol::getValue() const
{
return this->value;
......@@ -100,6 +105,14 @@ void Symbol::setReturnType(enum Symbol::Type returnType)
this->returnType = returnType;
}
int Symbol::getArguments()
{
if((this->getType() == Symbol::FUNCTION || this->getType() == Symbol::VARIABLE) && this->getValue().s != NULL)
return this->getValue().s->getArguments() + 1;
return 0;
}
enum Symbol::Type Symbol::getReturnType() const
{
return this->returnType;
......
......@@ -26,10 +26,12 @@ int main(int argc, char *argv[]) {
return 1;
}
#else
vector<SymbolTable*> tables;
tables.push_back(new SymbolTable());
InstructionList* list = new InstructionList();
yyparse(tables, list);
InstructionList* instructions = new InstructionList();
vector<SymbolTable*> symbol_table;
symbol_table.push_back(new SymbolTable());
symbol_table[0]->Insert(new Symbol("readInput", Symbol::INPUT, Symbol::INTEGER));
symbol_table.push_back(new SymbolTable(0, symbol_table[0]));
yyparse(symbol_table, instructions);
#endif
return 0;
}
var1 = 1.0
var2=1.1
var4=2
function func()=1
output var2 + func()
function func(a,b,c)=a+b+c
output var2 + func(1,2,3)
output 1 + 2 + var2 + readInput
output var2
output 1.0 + 2.0 + 3.0 + 4.1
......
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