Commit 2e4c7732 by Andrew Dahl

ref #14 Types for functions work -- Need printed warnings yet

parent 1d372730
......@@ -32,7 +32,8 @@ class Instruction {
MOV, /* Not sure if this matters on type... MOV Dest Src */
CALL, /* CALL Arg */
FUNCTION_START, /* Used to differentiate blocks from functions */
VARDECL /* Used to determine variable declarations in blocks */
VARDECL,/* Used to determine variable declarations in blocks */
FUNCTION_RETURN
};
enum arg_type {
......
......@@ -50,9 +50,12 @@ class Symbol {
void setReturnType(enum Type returnType);
enum Type getReturnType() const;
int getArguments(bool isFunction=false);
int getArgumentsValue(bool isFunction=false);
bool operator==(const Symbol *rhs) const;
void setStackAddress(int address);
int getStackAddress() const;
void setGlobal(bool global);
bool getGlobal() const;
string toString() const;
......@@ -63,6 +66,7 @@ private:
Type returnType;
Symbol* f; // These will be variables for the function
int stack_address;
bool is_global;
};
#endif /* SYMBOL_H_ */
......@@ -14,6 +14,7 @@
#include <vector>
const int FLOAT_PRECISION = 6;
const int GLOBAL_PARENT_TABLE = 0;
using std::map;
using std::string;
......
......@@ -77,7 +77,8 @@ function_decl: FUNCTION type_spec ID LPAREN {
;
function_define: function_decl argument_exp_list RPAREN LCURLY statlist RETURN additive_exp RCURLY {
$1->setFunctionValue($2);
$1->setFunctionValue($2);
instructions->addInstruction(new Instruction(Instruction::FUNCTION_RETURN, $1));
level = symbol_table->at(level)->getParentTable();
instructions->levelPop();
$$ = $1;
......
......@@ -134,6 +134,31 @@ int Symbol::getArguments(bool isFunction)
return 0;
}
int Symbol::getArgumentsValue(bool isFunction)
{
if(this->getType() == Symbol::FUNCTION && this->f != NULL && isFunction)
return this->f->getArgumentsValue(false);
else if(((this->getType() == Symbol::FUNCTION && !isFunction) || this->getType() == Symbol::VARIABLE))
{
if(this->getValue().s != NULL)
{
if(this->getReturnType() == Symbol::FLOAT)
return this->getValue().s->getArgumentsValue() + 8;
else
return this->getValue().s->getArgumentsValue() + 4;
}
else
{
if(this->getReturnType() == Symbol::FLOAT)
return 8;
else
return 4;
}
}
return 0;
}
enum Symbol::Type Symbol::getReturnType() const
{
return this->returnType;
......@@ -209,6 +234,16 @@ int Symbol::getStackAddress() const
return this->stack_address;
}
void Symbol::setGlobal(bool global)
{
this->is_global = global;
}
bool Symbol::getGlobal() const
{
return this->is_global;
}
string Symbol::toString() const
{
stringstream oss;
......
......@@ -32,6 +32,8 @@ Symbol* SymbolTable::Insert(Symbol *entry)
if(it == this->table.end())
{
if(this->parent_table_num == GLOBAL_PARENT_TABLE)
entry->setGlobal(true);
this->table.insert( std::pair<string,Symbol*>(entry->getLexeme(),entry));
if(entry->getType() == Symbol::VARIABLE)
{
......
int a=readInput
float b=readInput + 1.0
int c=readInput + 2.49999
output readInput
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()
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
function float func(int a,int b,int c,float d){
int e = d
int f = 20
output a
output b
output c
output d
output e
output f
return a+b+c+d+e+f }
function int func2(int a){
output a
return a
}
output func(0,1,1,1.2)
output func2(0)
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