Skip to content

Commit

Permalink
agregado offset para tipos primitivos u estructuras anidadas, y arreg…
Browse files Browse the repository at this point in the history
…lado un peo de recorrido que no diferenciaba entre una definicion de struct y lo que si era una variable
  • Loading branch information
wilmer05 committed May 25, 2014
1 parent 0e7a233 commit c97642c
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 280 deletions.
53 changes: 45 additions & 8 deletions MadafakaTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ using namespace std;

//campos estaticos de IntegerType
IntegerType *IntegerType::singleton_instance = NULL;
int IntegerType::tam = 4;
//int IntegerType::tam = 4;

//Campos estaticos de FloatType
FloatType *FloatType::singleton_instance = NULL;
int FloatType::tam = 8;
//int FloatType::tam = 8;

//Campos estaticos de StringType
StringType *StringType::singleton_instance = NULL;

//Same as before
CharType *CharType::singleton_instance = NULL;
int CharType::tam = 1;
//int CharType::tam = 1;

//Same
BoolType *BoolType::singleton_instance = NULL;
int BoolType::tam = 1;
//int BoolType::tam = 1;


//" "
Expand Down Expand Up @@ -70,6 +70,7 @@ MadafakaType::operator string() const{

IntegerType::IntegerType(){
name = "Idafak";
tam = 4;
};

IntegerType* IntegerType::instance(){
Expand All @@ -80,6 +81,7 @@ IntegerType* IntegerType::instance(){

FloatType::FloatType(){
name = "Fdafak";
tam =8;
};
FloatType* FloatType::instance(){
if (!singleton_instance)
Expand All @@ -89,6 +91,7 @@ FloatType* FloatType::instance(){

StringType::StringType(){
name = "Sdafak";
tam =4;
};

StringType* StringType::instance(){
Expand All @@ -99,6 +102,7 @@ StringType* StringType::instance(){

CharType::CharType(){
name = "Cdafak";
tam = 1;
};
CharType* CharType::instance(){
if (!singleton_instance)
Expand All @@ -109,6 +113,7 @@ CharType* CharType::instance(){

BoolType::BoolType(){
name = "Bdafak";
tam = 1;
};
BoolType* BoolType::instance(){
if (!singleton_instance)
Expand All @@ -119,6 +124,7 @@ BoolType* BoolType::instance(){

VoidType::VoidType(){
name = "Void";
tam = 4;
};
VoidType* VoidType::instance(){
if (!singleton_instance)
Expand All @@ -130,6 +136,7 @@ RecordType::RecordType(string *symname, arbol *newfields){
name = "Struct";
symbolName = symname;
SymTable = newfields;
tam=0;
};

// Equivalencia por nombre en los records y unions
Expand All @@ -146,6 +153,7 @@ UnionType::UnionType(string *symname, arbol *newfields){
symbolName = symname;
SymTable = newfields;
SymTable = new arbol();
tam =0;
};

bool UnionType::operator==(UnionType &rhs){
Expand All @@ -160,6 +168,7 @@ ArrayType::ArrayType(int s, MadafakaType *t){
name = "Array";
size = s;
type = t;
tam =1;
};

FunctionType::FunctionType(arbol *arguments,MadafakaType *returntype){
Expand All @@ -179,7 +188,7 @@ TypeError* TypeError::instance(){

Undeclared::Undeclared(){
name = "Undeclared";
tam =0;
tam =0;
};
Undeclared* Undeclared::instance(){
if (!singleton_instance)
Expand Down Expand Up @@ -274,6 +283,27 @@ string arbol::getTipoArray(string &var){
return "";
}

int arbol::getTam(string &n1){
return contenido[n1]->tam;
}


int arbol::getBase(){
return base;
}

void arbol::addBase(int val){
base+=val;
}

void arbol::setOffset(string &v, int val){
offset[v]=val;
}

int arbol::getOffset(string &var){
if(!offset.count(var))return -1;
return offset[var];
}


MadafakaType *buscarVariable(string var, arbol *actual){
Expand Down Expand Up @@ -319,11 +349,18 @@ void recorrer(arbol *a, int nivel){
string s = "";
for(int i=0;i<nivel;i++) s+='\t';

cout << s << "Abriendo anidamiento"<<endl<<endl;
cout << s << "Abriendo anidamiento de base " << (*a).getBase();
cout <<endl<<endl;


for(map<string,MadafakaType*>::iterator it = vars.begin();it!=vars.end();it++){
cout << s+"\t" << it->first << " es de tipo " << it->second->name << endl << endl;
string s2 = it->first;
if(it->second->name!="Function" && (*a).getOffset(s2)<0) continue;
cout << s+"\t" << it->first << " es de tipo " << it->second->name;

cout << " y su offset es: " << (*a).getOffset(s2);
cout << " y tam: " << it->second->tam << endl;
cout << endl << endl;
pair<int,int> p = ubic[it->first];
int t1 = p.first;
int t2 = p.second;
Expand All @@ -344,7 +381,7 @@ void recorrer(arbol *a, int nivel){
}

for(int i =0;i<h.size();i++){
if(!ya.count(i))
if(!ya.count(i) && h[i]->var)
recorrer(h[i],nivel+1);
}
cout << s << "Cerrando anidamiendo" << endl<<endl;
Expand Down
95 changes: 84 additions & 11 deletions MadafakaTypes.cpp~
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,33 @@ using namespace std;
* @author Wilmer Bandres <wilmer0593@gmail.com>
*/


//campos estaticos de IntegerType
IntegerType *IntegerType::singleton_instance = NULL;
//int IntegerType::tam = 4;

//Campos estaticos de FloatType
FloatType *FloatType::singleton_instance = NULL;
//int FloatType::tam = 8;

//Campos estaticos de StringType
StringType *StringType::singleton_instance = NULL;

//Same as before
CharType *CharType::singleton_instance = NULL;
//int CharType::tam = 1;

//Same
BoolType *BoolType::singleton_instance = NULL;
//int BoolType::tam = 1;


//" "
VoidType *VoidType::singleton_instance = NULL;

//" "
TypeError *TypeError::singleton_instance = NULL;

//" "
Undeclared *Undeclared::singleton_instance = NULL;


Expand All @@ -37,12 +56,21 @@ bool MadafakaType::operator==(const char* word){
return name==string(word);
}

std::ostream& MadafakaType::operator<<(std::ostream &os) {
return os << name;
bool MadafakaType::operator!=(const char* word){
return name!=string(word);
}

std::ostream& operator<<(std::ostream &os,MadafakaType const &var) {
return os << var.name;
}

MadafakaType::operator string() const{
return name;
}

IntegerType::IntegerType(){
name = "Idafak";
tam = 4;
};

IntegerType* IntegerType::instance(){
Expand All @@ -53,6 +81,7 @@ IntegerType* IntegerType::instance(){

FloatType::FloatType(){
name = "Fdafak";
tam =8;
};
FloatType* FloatType::instance(){
if (!singleton_instance)
Expand All @@ -62,6 +91,7 @@ FloatType* FloatType::instance(){

StringType::StringType(){
name = "Sdafak";
tam =4;
};

StringType* StringType::instance(){
Expand All @@ -72,6 +102,7 @@ StringType* StringType::instance(){

CharType::CharType(){
name = "Cdafak";
tam = 1;
};
CharType* CharType::instance(){
if (!singleton_instance)
Expand All @@ -81,7 +112,8 @@ CharType* CharType::instance(){


BoolType::BoolType(){
name = "Cdafak";
name = "Bdafak";
tam = 1;
};
BoolType* BoolType::instance(){
if (!singleton_instance)
Expand All @@ -92,6 +124,7 @@ BoolType* BoolType::instance(){

VoidType::VoidType(){
name = "Void";
tam = 4;
};
VoidType* VoidType::instance(){
if (!singleton_instance)
Expand All @@ -103,6 +136,7 @@ RecordType::RecordType(string *symname, arbol *newfields){
name = "Struct";
symbolName = symname;
SymTable = newfields;
tam=0;
};

// Equivalencia por nombre en los records y unions
Expand All @@ -119,6 +153,7 @@ UnionType::UnionType(string *symname, arbol *newfields){
symbolName = symname;
SymTable = newfields;
SymTable = new arbol();
tam =0;
};

bool UnionType::operator==(UnionType &rhs){
Expand All @@ -129,11 +164,11 @@ arbol* UnionType::getSymTable(){
return SymTable;
};

ArrayType::ArrayType(int lo, int up, MadafakaType *t){
ArrayType::ArrayType(int s, MadafakaType *t){
name = "Array";
lower = lo;
upper = up;
size = s;
type = t;
tam =1;
};

FunctionType::FunctionType(arbol *arguments,MadafakaType *returntype){
Expand All @@ -143,7 +178,7 @@ FunctionType::FunctionType(arbol *arguments,MadafakaType *returntype){
};

TypeError::TypeError(){
name = "Type Error";
name = "TypeError";
};
TypeError* TypeError::instance(){
if (!singleton_instance)
Expand All @@ -153,13 +188,23 @@ TypeError* TypeError::instance(){

Undeclared::Undeclared(){
name = "Undeclared";
tam =0;
};
Undeclared* Undeclared::instance(){
if (!singleton_instance)
singleton_instance = new Undeclared();
return singleton_instance;
};

// Checkeo para las expresiones aritméticas
MadafakaType* check_and_widen(MadafakaType *left, MadafakaType *right){
if ((*left != "Fdafak" && *left != "Idafak") ||
(*right != "Fdafak" && *right != "Idafak")){
return new TypeError();
}
else if ((*left == "Fdafak") || (*right == "Fdafak")) return new FloatType();
else return new IntegerType();
}

// Cosas de estructuras.cpp

Expand Down Expand Up @@ -195,7 +240,7 @@ void arbol::insertar(string s,MadafakaType *tipo,int fila, int col, int si){
if(si)
bloque[s] = hijos.size()-1;
}
cout << (*tipo).name << " " << s << endl;
//cout << (*tipo).name << " " << s << endl;
}

MadafakaType* arbol::tipoVar(string &var){
Expand Down Expand Up @@ -238,6 +283,27 @@ string arbol::getTipoArray(string &var){
return "";
}

int arbol::getTam(string &n1){
return contenido[n1]->tam;
}


int arbol::getBase(){
return base;
}

void arbol::addBase(int val){
base+=val;
}

void arbol::setOffset(string &v, int val){
offset[v]=val;
}

int arbol::getOffset(string &var){
if(!offset.count(var))return -1;
return offset[var];
}


MadafakaType *buscarVariable(string var, arbol *actual){
Expand Down Expand Up @@ -283,11 +349,18 @@ void recorrer(arbol *a, int nivel){
string s = "";
for(int i=0;i<nivel;i++) s+='\t';

cout << s << "Abriendo anidamiento"<<endl<<endl;
cout << s << "Abriendo anidamiento de base " << (*a).getBase();
cout <<endl<<endl;


for(map<string,MadafakaType*>::iterator it = vars.begin();it!=vars.end();it++){
cout << s+"\t" << it->first << " es de tipo " << it->second << endl << endl;
string s2 = it->first;
if(it->second->name!="Function" && (*a).getOffset(s2)<0) continue;
cout << s+"\t" << it->first << " es de tipo " << it->second->name;

cout << " y su offset es: " << (*a).getOffset(s2);
cout << " y tam: " << it->second->tam << endl;
cout << endl << endl;
pair<int,int> p = ubic[it->first];
int t1 = p.first;
int t2 = p.second;
Expand Down
Loading

0 comments on commit c97642c

Please sign in to comment.