diff --git a/Arbol.h b/Arbol.h index 4a904d9..f41b165 100644 --- a/Arbol.h +++ b/Arbol.h @@ -1,214 +1,214 @@ -#ifndef ARBOL_H_INCLUDED -#define ARBOL_H_INCLUDED - -#include "Nodos.h" - -class ArbolSemanticoException : public exception { - private: - string msg; - - public: - explicit ArbolSemanticoException(const char* message) : msg(message) { } - - explicit ArbolSemanticoException(const string& message) : msg(message) { } - - virtual ~ArbolSemanticoException() throw () { } - - virtual const char* what() const throw () { - return msg.c_str(); - } - }; - -class ArbolS -{ -private: - pSentencia root; - pSentencia fin; - -public: - - ArbolS() - { - //root->siguiente = NULL; - root = NULL; - //fin->siguiente = NULL; - fin = NULL; - } - - bool vacio() - { - return (root == NULL); - } - - void insertaNodo(pSentencia nuevo) - { - nuevo->siguiente = NULL; - if(vacio()) - { - root = nuevo; - fin = nuevo; - } - else - { - fin->siguiente = nuevo; - fin = nuevo; - } - } - - void imprime() - { - //limpiarPantalla(); - ofstream writeFile; - +#ifndef ARBOL_H_INCLUDED +#define ARBOL_H_INCLUDED + +#include "Nodos.h" + +class ArbolSemanticoException : public exception { + private: + string msg; + + public: + explicit ArbolSemanticoException(const char* message) : msg(message) { } + + explicit ArbolSemanticoException(const string& message) : msg(message) { } + + virtual ~ArbolSemanticoException() throw () { } + + virtual const char* what() const throw () { + return msg.c_str(); + } + }; + +class ArbolS +{ +private: + pSentencia root; + pSentencia fin; + +public: + + ArbolS() + { + //root->siguiente = NULL; + root = NULL; + //fin->siguiente = NULL; + fin = NULL; + } + + bool vacio() + { + return (root == NULL); + } + + void insertaNodo(pSentencia nuevo) + { + nuevo->siguiente = NULL; + if(vacio()) + { + root = nuevo; + fin = nuevo; + } + else + { + fin->siguiente = nuevo; + fin = nuevo; + } + } + + void imprime() + { + //limpiarPantalla(); + ofstream writeFile; + remove("salida.txt"); writeFile.open("salida.txt"); - - if(writeFile.is_open()) { - writeFile << "" << endl; - pSentencia temp = root; - while(temp != NULL) - { - writeFile << temp->imprime(); - temp = temp->siguiente; - } + + if(writeFile.is_open()) { + writeFile << "" << endl; + pSentencia temp = root; + while(temp != NULL) + { + writeFile << temp->imprime(); + temp = temp->siguiente; + } writeFile<< "" << endl; - } - - writeFile.close(); - - } - - void throwException (const string& messageToThrow) { - throw ArbolSemanticoException(messageToThrow); - } - - void analyze() - { - try - { - cout << "Esto es un analisis" << endl << endl; - pSentencia temp = root; - - recorre(temp); - - //lista.imprime(); - - generaCodigo(); - } - catch(NodeException e) - { - throwException(e.what()); - } - - } - - void recorre(pSentencia temp) - { - char t; - while(temp != NULL) - { - t = temp->checkType(); - if(t == 'e') - { - string errorMessage = "Tipo de Sentencia invalida: \n"; - errorMessage += temp->imprime(); - throw NodeException(errorMessage); - } - else if(t == 'a') - { - //Obtiene el tipo de dato de la expresion - temp->semantico(); - if(temp->getTipo() == 'e') - { - string errorMessage = "Asignacion Invalida (Semanticamente): \n"; - errorMessage += temp->imprime(); - throw NodeException(errorMessage); - } - //inserta ID de la asignacion a la lista de ID's - lista.insertaNodo(temp->getID(), temp->getTipo()); - } - else if(t == 'p') - { - //Obtiene tipo de dato de la expresion - temp->semantico(); - if(temp->getTipo() == 'e') - { - string errorMessage = "Print Invalido (Semanticamente): \n"; - errorMessage += temp->imprime(); - throw NodeException(errorMessage); - } - } - else if(t == 'i') - { - //Obtiene el tipo de dato de la condicion - temp->semantico(); - if(temp->getTipo() == 'e') - { - string errorMessage = "Condicion Invalida en IF (Semanticamente): \n"; - errorMessage += temp->imprime(); - throw NodeException(errorMessage); - } - //recorre las sentencias dentro del bloque - recorre(temp->getBloque()); - recorre(temp->getOtro()); - } - else if(t == 'w') - { - //Obtiene el tipo de dato de la condicion - temp->semantico(); - if(temp->getTipo() == 'e') - { - string errorMessage = "Condicion Invalida en WHILE (Semanticamente): \n"; - errorMessage += temp->imprime(); - throw NodeException(errorMessage); - } - //recorre las sentencias dentro del bloque - recorre(temp->getBloque()); - } - - temp = temp->siguiente; - } - } - - void generaCodigo() - { - ofstream writeFile; - + } + + writeFile.close(); + + } + + void throwException (const string& messageToThrow) { + throw ArbolSemanticoException(messageToThrow); + } + + void analyze() + { + try + { + cout << "Esto es un analisis" << endl << endl; + pSentencia temp = root; + + recorre(temp); + + //lista.imprime(); + + generaCodigo(); + } + catch(NodeException e) + { + throwException(e.what()); + } + + } + + void recorre(pSentencia temp) + { + char t; + while(temp != NULL) + { + t = temp->checkType(); + if(t == 'e') + { + string errorMessage = "Tipo de Sentencia invalida: \n"; + errorMessage += temp->imprime(); + throw NodeException(errorMessage); + } + else if(t == 'a') + { + //Obtiene el tipo de dato de la expresion + temp->semantico(); + if(temp->getTipo() == 'e') + { + string errorMessage = "Asignacion Invalida (Semanticamente): \n"; + errorMessage += temp->imprime(); + throw NodeException(errorMessage); + } + //inserta ID de la asignacion a la lista de ID's + lista.insertaNodo(temp->getID(), temp->getTipo()); + } + else if(t == 'p') + { + //Obtiene tipo de dato de la expresion + temp->semantico(); + if(temp->getTipo() == 'e') + { + string errorMessage = "Print Invalido (Semanticamente): \n"; + errorMessage += temp->imprime(); + throw NodeException(errorMessage); + } + } + else if(t == 'i') + { + //Obtiene el tipo de dato de la condicion + temp->semantico(); + if(temp->getTipo() == 'e') + { + string errorMessage = "Condicion Invalida en IF (Semanticamente): \n"; + errorMessage += temp->imprime(); + throw NodeException(errorMessage); + } + //recorre las sentencias dentro del bloque + recorre(temp->getBloque()); + recorre(temp->getOtro()); + } + else if(t == 'w') + { + //Obtiene el tipo de dato de la condicion + temp->semantico(); + if(temp->getTipo() == 'e') + { + string errorMessage = "Condicion Invalida en WHILE (Semanticamente): \n"; + errorMessage += temp->imprime(); + throw NodeException(errorMessage); + } + //recorre las sentencias dentro del bloque + recorre(temp->getBloque()); + } + + temp = temp->siguiente; + } + } + + void generaCodigo() + { + ofstream writeFile; + remove("salida.txt"); writeFile.open("salida.txt"); - - if(writeFile.is_open()) { - writeFile << ".386" << endl ; - writeFile << ".model flat, stdcall" << endl; - writeFile << "option casemap:none" << endl; - writeFile << "\tinclude \\masm32\\macros\\macros.asm" << endl; - writeFile << "\tinclude \\masm32\\include\\masm32.inc" << endl; - writeFile << "\tinclude \\masm32\\include\\kernel32.inc" << endl; - writeFile << "\tincludelib \\masm32\\lib\\masm32.lib" << endl; - writeFile << "\tincludelib \\masm32\\lib\\kernel32.lib" << endl << endl; - - - writeFile << ".data" << endl; - // - writeFile << ".data?" << endl; - - writeFile << ".code" << endl; - writeFile << "inicio:" << endl << endl; - - pSentencia temp = root; - while(temp != NULL) - { - writeFile << temp->generaCodigo(); - temp = temp->siguiente; - } - - writeFile << "exit" << endl << endl; + + if(writeFile.is_open()) { + writeFile << ".386" << endl ; + writeFile << ".model flat, stdcall" << endl; + writeFile << "option casemap:none" << endl; + writeFile << "\tinclude \\masm32\\macros\\macros.asm" << endl; + writeFile << "\tinclude \\masm32\\include\\masm32.inc" << endl; + writeFile << "\tinclude \\masm32\\include\\kernel32.inc" << endl; + writeFile << "\tincludelib \\masm32\\lib\\masm32.lib" << endl; + writeFile << "\tincludelib \\masm32\\lib\\kernel32.lib" << endl << endl; + + + writeFile << ".data" << endl; + // + writeFile << ".data?" << endl; + writeFile << lista.listaEnteros(); + writeFile << ".code" << endl; + writeFile << "inicio:" << endl << endl; + + pSentencia temp = root; + while(temp != NULL) + { + writeFile << temp->generaCodigo(); + temp = temp->siguiente; + } + + writeFile << "exit" << endl << endl; writeFile << "end inicio" << endl; - } - - writeFile.close(); - } - -}; - -#endif // ARBOL_H_INCLUDED + } + + writeFile.close(); + } + +}; + +#endif // ARBOL_H_INCLUDED diff --git a/Nodos.h b/Nodos.h index 0a0dd91..503db22 100644 --- a/Nodos.h +++ b/Nodos.h @@ -1,821 +1,847 @@ -#ifndef NODOS_H_INCLUDED -#define NODOS_H_INCLUDED - -class NodeException : public exception { - private: - string msg; - - public: - explicit NodeException(const char* message) : msg(message) { } - - explicit NodeException(const string& message) : msg(message) { } - - virtual ~NodeException() throw () { } - - virtual const char* what() const throw () { - return msg.c_str(); - } -}; - - -class Sentencia -{ -public: - Sentencia *siguiente = NULL; - char tipo = 'e'; - - Sentencia(){} - - virtual string imprime() - { - stringstream s; - return s.str(); - } - - virtual string generaCodigo() - { - stringstream s; - return s.str(); - } - - virtual char checkType() - { - return 'e'; - } - - virtual void semantico() - { - - } - - virtual char getTipo() - { - return tipo; - } - - virtual string getID() - { - return "\\"; - } - - virtual Sentencia* getBloque() - { - return NULL; - } - - virtual Sentencia* getOtro() - { - return NULL; - } - -}; -typedef Sentencia *pSentencia; - -class Expresion -{ -public: - string simbolo; - char tipo = 'e'; - - Expresion *izq = NULL, *der = NULL; - - Expresion(){} - - virtual string imprime() - { - stringstream s; - s << "" << endl; - s << "" << endl; - return s.str(); - } - - virtual string generaCodigo() - { - stringstream s; - return s.str(); - } - - virtual string getSimbolo() - { - return simbolo; - } - - virtual void semantico() - { - - } - - virtual char getTipo() - { - return tipo; - } - -}; -typedef Expresion *pExpresion; - -class Condicion -{ -public: - char tipo = 'e'; - string simbolo; - - Condicion(){} - - virtual string imprime() - { - stringstream s; - s << "" << endl; - s << "" << endl; - return s.str(); - } - - virtual string generaCodigo() - { - stringstream s; - return s.str(); - } - - virtual void semantico() - { - - } - - virtual char getTipo() - { - return tipo; - } - -}; -typedef Condicion *pCondicion; - -//TIPOS DE DATOS - -class Identificador : public Expresion -{ -public: - Identificador(string sim) - { - simbolo=sim; - } - - string imprime() - { - stringstream s; - s << "" << simbolo << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << "push "<< simbolo << ";" << endl; - return s.str(); - } - - void semantico() - { - if(lista.buscaID(simbolo)) - { - tipo = lista.tipoID(simbolo); - } - else - { - tipo = 'e'; - } - } - - char getTipo() - { - return tipo; - } - -}; -typedef Identificador *pID; - -class Entero : public Expresion -{ -public: - Entero(string sim) - { - simbolo=sim; - } - - string imprime() - { - stringstream s; - s << "" << simbolo << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << "push "<< simbolo << ";" << endl; - return s.str(); - } - - void semantico() - { - tipo = 'i'; - } - - char getTipo() - { - return tipo; - } - -}; -typedef Entero *pEntero; - -class Real : public Expresion -{ -public: - Real(string sim) - { - simbolo=sim; - } - - string imprime() - { - stringstream s; - s << "" << simbolo << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << "push "<< simbolo << ";" << endl; - return s.str(); - } - - void semantico() - { - tipo = 'r'; - } - - char getTipo() - { - return tipo; - } - -}; -typedef Real *pReal; - -class Cadena : public Expresion -{ -public: - Cadena(string sim) - { - simbolo=sim; - } - - string imprime() - { - stringstream s; - s << "" << simbolo << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << "push "<< simbolo << ";" << endl; - return s.str(); - } - - void semantico() - { - tipo = 'c'; - } - - char getTipo() - { - return tipo; - } - -}; -typedef Cadena *pCadena; - -//FIN TIPOS DE DATOS -//TIPOS DE OPERACIONES ARITMETICAS - -class Suma : public Expresion -{ -public: - Suma(Expresion *i, Expresion *d, string sim) - { - izq = i; - der = d; - simbolo = sim; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << izq->imprime(); - s << der->imprime(); - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << izq->generaCodigo(); - s << der->generaCodigo(); - s << "pop ebx;" << endl; - s << "pop eax;" << endl; - s << "add eax, ebx;" << endl; - s << "push eax;" << endl; - return s.str(); - } - - void semantico() - { - izq->semantico(); - der->semantico(); - - if(izq->getTipo() == 'i' && der->getTipo() == 'i') - { - tipo = 'i'; - } - else if(izq->getTipo() == 'r' && der->getTipo() == 'r') - { - tipo = 'r'; - } - else - { - tipo = 'e'; - } - } - - char getTipo() - { - return tipo; - } - -}; -typedef Suma *pSuma; - -class Signo : public Expresion -{ -public: - Signo(pExpresion d, string sim) - { - der = d; - simbolo = sim; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << der->imprime(); - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << "push " << simbolo << der->getSimbolo() << ";" << endl; - return s.str(); - } - - void semantico() - { - der->semantico(); - - if(der->getTipo() == 'i') - { - tipo = 'i'; - } - else if(der->getTipo() == 'r') - { - tipo = 'r'; - } - else - { - tipo = 'e'; - } - } - - char getTipo() - { - return tipo; - } - -}; -typedef Signo *pSigno; - -class Multiplicacion : public Expresion -{ -public: - Multiplicacion(Expresion *i, Expresion *d, string sim) - { - izq = i; - der = d; - simbolo = sim; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << izq->imprime(); - s << der->imprime(); - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << izq->generaCodigo(); - s << der->generaCodigo(); - s << "pop ebx;" << endl; - s << "pop eax;" << endl; - s << "mul ebx;" << endl; //Check - s << "push eax;" << endl; - return s.str(); - } - - void semantico() - { - izq->semantico(); - der->semantico(); - - if(izq->getTipo() == 'i' && der->getTipo() == 'i') - { - tipo = 'i'; - } - else if(izq->getTipo() == 'r' && der->getTipo() == 'r') - { - tipo = 'r'; - } - else - { - tipo = 'e'; - } - } - - char getTipo() - { - return tipo; - } - -}; -typedef Multiplicacion *pMultiplicacion; - - -//FIN OPERACIONES ARITMETICAS -//OPERACION RELACIONAL - -class Relacional : public Condicion -{ - Expresion *izq = NULL, *der = NULL; -public: - Relacional(Expresion *i, Expresion *d, string sim) - { - izq = i; - der = d; - simbolo = sim; - } - - string getSimbolo() - { - if (simbolo == ">") - return ">"; - else if(simbolo == "<") - return "<"; - else if(simbolo == "==") - return simbolo; - else if(simbolo == "!=") - return simbolo; - else if(simbolo == "<=") - return "<="; - else if(simbolo == ">=") - return ">="; - else - return "ERROR"; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << izq->imprime(); - s << der->imprime(); - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << izq->generaCodigo(); - s << der->generaCodigo(); - s << "pop eax;" << endl; - s << "pop ebx;" << endl; - s << "cmp eax, ebx;" << endl;//Deja banderas de comparación - return s.str(); - } - - - void semantico() - { - izq->semantico(); - der->semantico(); - - if(izq->getTipo() == 'i' && der->getTipo() == 'i') - { - tipo = 'i'; - } - else if(izq->getTipo() == 'r' && der->getTipo() == 'r') - { - tipo = 'r'; - } - else - { - tipo = 'e'; - } - } - - char getTipo() - { - return tipo; - } - -}; -typedef Relacional *pRelacional; - -class Logico : public Condicion -{ - Condicion *izq = NULL, *der = NULL; -public: - Logico(Condicion*i, Condicion*d, string sim) - { - izq = i; - der = d; - simbolo = sim; - } - -}; -typedef Logico *pLogico; - -class LogicoNOT : public Condicion -{ - Condicion *sig = NULL; -public: - LogicoNOT(Condicion*s) - { - sig = s; - } - -}; -typedef LogicoNOT *pLogicoNOT; - -//SENTENCIAS - -class Asignacion : public Sentencia -{ - Expresion *der = NULL; - Identificador *izq = NULL; -public: - - Asignacion(Identificador *i, Expresion *d) - { - izq = i; - der = d; - } - - Asignacion(){} - - char checkType() - { - return 'a'; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << izq->imprime(); - s << der->imprime(); - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << der->generaCodigo(); - s << "pop " << izq->getSimbolo() << ";" << endl; - return s.str(); - } - - void semantico() - { - der->semantico(); - tipo = der->getTipo(); - } - - char getTipo() - { - return tipo; - } - - string getID() - { - return izq->simbolo; - } -}; -typedef Asignacion *pAsignacion; - -class Print : public Sentencia -{ - Expresion *contenido = NULL; -public: - - Print(Expresion *c) - { - contenido = c; - } - - Print(){} - - char checkType() - { - return 'p'; - } - - string imprime() - { - stringstream s; - s << "" << endl; - s << "" << endl; - s << contenido->imprime(); - s << "" << endl; - s << "" << endl; - return s.str(); - } - - string generaCodigo() - { - stringstream s; - s << contenido->generaCodigo(); - s << "pop eax" << endl; - s << "print str$(eax),10;" << endl;//Esto funciona si no es una cadena - return s.str(); - } - - void semantico() - { - contenido->semantico(); - tipo = contenido->getTipo(); - } - - char getTipo() - { - return tipo; - } - -}; -typedef Print *pPrint; - -class SI : public Sentencia -{ - Condicion *cond = NULL; - Sentencia *bloque = NULL, *otro = NULL; -public: - - SI(Condicion *c, Sentencia *b, Sentencia *o) - { - cond = c; - bloque = b; - otro = o; - } - - SI(){} - - char checkType() - { - return 'i'; - } - - string imprime() - { - pSentencia temp; - stringstream s; - - s << "" << endl; - s << cond->imprime(); - s << "" << endl; - temp = bloque; - while(temp != NULL) - { - s << temp->imprime(); - temp = temp->siguiente; - } - s << "" << endl; - s << "" << endl; - temp = otro; - while(temp != NULL) - { - s << temp->imprime(); - temp = temp->siguiente; - } - s << "" << endl; - s << "" << endl; - - return s.str(); - } - - void semantico() - { - cond->semantico(); - tipo = cond->getTipo(); - } - - pSentencia getBloque() - { - return bloque; - } - - pSentencia getOtro() - { - return otro; - } - - char getTipo() - { - return tipo; - } - -}; -typedef SI *pSI; - -class Mientras : public Sentencia -{ - Condicion *cond = NULL; - Sentencia *bloque = NULL; -public: - - Mientras(Condicion *c, Sentencia *b) - { - cond = c; - bloque = b; - } - - Mientras(){} - - char checkType() - { - return 'w'; - } - - string imprime() - { - pSentencia temp; - stringstream s; - - s << "" << endl; - s << cond->imprime(); - s << "" << endl; - temp = bloque; - while(temp != NULL) - { - s << temp->imprime(); - temp = temp->siguiente; - } - s << "" << endl; - s << "" << endl; - - return s.str(); - } - - - - void semantico() - { - cond->semantico(); - tipo = cond->getTipo(); - } - - pSentencia getBloque() - { - return bloque; - } - - char getTipo() - { - return tipo; - } - -}; -typedef Mientras *pMientras; - - -#endif // NODOS_H_INCLUDED +#ifndef NODOS_H_INCLUDED +#define NODOS_H_INCLUDED + +class NodeException : public exception { + private: + string msg; + + public: + explicit NodeException(const char* message) : msg(message) { } + + explicit NodeException(const string& message) : msg(message) { } + + virtual ~NodeException() throw () { } + + virtual const char* what() const throw () { + return msg.c_str(); + } +}; + + +class Sentencia +{ +public: + Sentencia *siguiente = NULL; + char tipo = 'e'; + + Sentencia(){} + + virtual string imprime() + { + stringstream s; + return s.str(); + } + + virtual string generaCodigo() + { + stringstream s; + return s.str(); + } + + virtual char checkType() + { + return 'e'; + } + + virtual void semantico() + { + + } + + virtual char getTipo() + { + return tipo; + } + + virtual string getID() + { + return "\\"; + } + + virtual Sentencia* getBloque() + { + return NULL; + } + + virtual Sentencia* getOtro() + { + return NULL; + } + +}; +typedef Sentencia *pSentencia; + +class Expresion +{ +public: + string simbolo; + char tipo = 'e'; + + Expresion *izq = NULL, *der = NULL; + + Expresion(){} + + virtual string imprime() + { + stringstream s; + s << "" << endl; + s << "" << endl; + return s.str(); + } + + virtual string generaCodigo() + { + stringstream s; + return s.str(); + } + + virtual string getSimbolo() + { + return simbolo; + } + + virtual void semantico() + { + + } + + virtual char getTipo() + { + return tipo; + } + +}; +typedef Expresion *pExpresion; + +class Condicion +{ +public: + char tipo = 'e'; + string simbolo; + + Condicion(){} + + virtual string imprime() + { + stringstream s; + s << "" << endl; + s << "" << endl; + return s.str(); + } + + virtual string generaCodigo() + { + stringstream s; + return s.str(); + } + + virtual void semantico() + { + + } + + virtual char getTipo() + { + return tipo; + } + +}; +typedef Condicion *pCondicion; + +//TIPOS DE DATOS + +class Identificador : public Expresion +{ +public: + Identificador(string sim) + { + simbolo=sim; + } + + string imprime() + { + stringstream s; + s << "" << simbolo << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + s << "push _"<< simbolo << ";" << endl; + return s.str(); + } + + void semantico() + { + if(lista.buscaID(simbolo)) + { + tipo = lista.tipoID(simbolo); + } + else + { + tipo = 'e'; + } + } + + char getTipo() + { + return tipo; + } + +}; +typedef Identificador *pID; + +class Entero : public Expresion +{ +public: + Entero(string sim) + { + simbolo=sim; + } + + string imprime() + { + stringstream s; + s << "" << simbolo << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + s << "push "<< simbolo << ";" << endl; + return s.str(); + } + + void semantico() + { + tipo = 'i'; + } + + char getTipo() + { + return tipo; + } + +}; +typedef Entero *pEntero; + +class Real : public Expresion +{ +public: + Real(string sim) + { + simbolo=sim; + } + + string imprime() + { + stringstream s; + s << "" << simbolo << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + s << "push "<< simbolo << ";" << endl; + return s.str(); + } + + void semantico() + { + tipo = 'r'; + } + + char getTipo() + { + return tipo; + } + +}; +typedef Real *pReal; + +class Cadena : public Expresion +{ +public: + Cadena(string sim) + { + simbolo=sim; + } + + string imprime() + { + stringstream s; + s << "" << simbolo << "" << endl; + return s.str(); + } + + string generaCodigo()//Use getSimbolo() instead + { + stringstream s; + //s << "push "<< simbolo << ";" << endl; + return s.str(); + } + + void semantico() + { + tipo = 'c'; + } + + char getTipo() + { + return tipo; + } + +}; +typedef Cadena *pCadena; + +//FIN TIPOS DE DATOS +//TIPOS DE OPERACIONES ARITMETICAS + +class Suma : public Expresion +{ +public: + Suma(Expresion *i, Expresion *d, string sim) + { + izq = i; + der = d; + simbolo = sim; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << izq->imprime(); + s << der->imprime(); + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + if(tipo == 'i') + { + s << izq->generaCodigo(); + s << der->generaCodigo(); + s << "pop ebx;" << endl; + s << "pop eax;" << endl; + s << "add eax, ebx;" << endl; + s << "push eax;" << endl; + } + else + s << ";Solo acepta enteros" << endl; + + return s.str(); + } + + void semantico() + { + izq->semantico(); + der->semantico(); + + if(izq->getTipo() == 'i' && der->getTipo() == 'i') + { + tipo = 'i'; + } + else if(izq->getTipo() == 'r' && der->getTipo() == 'r') + { + tipo = 'r'; + } + else + { + tipo = 'e'; + } + } + + char getTipo() + { + return tipo; + } + +}; +typedef Suma *pSuma; + +class Signo : public Expresion +{ +public: + Signo(pExpresion d, string sim) + { + der = d; + simbolo = sim; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << der->imprime(); + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + s << "push " << simbolo << der->getSimbolo() << ";" << endl; + return s.str(); + } + + void semantico() + { + der->semantico(); + + if(der->getTipo() == 'i') + { + tipo = 'i'; + } + else if(der->getTipo() == 'r') + { + tipo = 'r'; + } + else + { + tipo = 'e'; + } + } + + char getTipo() + { + return tipo; + } + +}; +typedef Signo *pSigno; + +class Multiplicacion : public Expresion +{ +public: + Multiplicacion(Expresion *i, Expresion *d, string sim) + { + izq = i; + der = d; + simbolo = sim; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << izq->imprime(); + s << der->imprime(); + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + if(tipo == 'i') + { + s << izq->generaCodigo(); + s << der->generaCodigo(); + s << "pop ebx;" << endl; + s << "pop eax;" << endl; + s << "mul ebx;" << endl; //Check + s << "push eax;" << endl; + } + else + s << ";Solo acepta enteros" << endl; + + return s.str(); + } + + void semantico() + { + izq->semantico(); + der->semantico(); + + if(izq->getTipo() == 'i' && der->getTipo() == 'i') + { + tipo = 'i'; + } + else if(izq->getTipo() == 'r' && der->getTipo() == 'r') + { + tipo = 'r'; + } + else + { + tipo = 'e'; + } + } + + char getTipo() + { + return tipo; + } + +}; +typedef Multiplicacion *pMultiplicacion; + + +//FIN OPERACIONES ARITMETICAS +//OPERACION RELACIONAL + +class Relacional : public Condicion +{ + Expresion *izq = NULL, *der = NULL; +public: + Relacional(Expresion *i, Expresion *d, string sim) + { + izq = i; + der = d; + simbolo = sim; + } + + string getSimbolo() + { + if (simbolo == ">") + return ">"; + else if(simbolo == "<") + return "<"; + else if(simbolo == "==") + return simbolo; + else if(simbolo == "!=") + return simbolo; + else if(simbolo == "<=") + return "<="; + else if(simbolo == ">=") + return ">="; + else + return "ERROR"; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << izq->imprime(); + s << der->imprime(); + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + s << izq->generaCodigo(); + s << der->generaCodigo(); + s << "pop eax;" << endl; + s << "pop ebx;" << endl; + s << "cmp eax, ebx;" << endl;//Deja banderas de comparación + return s.str(); + } + + + void semantico() + { + izq->semantico(); + der->semantico(); + + if(izq->getTipo() == 'i' && der->getTipo() == 'i') + { + tipo = 'i'; + } + else if(izq->getTipo() == 'r' && der->getTipo() == 'r') + { + tipo = 'r'; + } + else + { + tipo = 'e'; + } + } + + char getTipo() + { + return tipo; + } + +}; +typedef Relacional *pRelacional; + +class Logico : public Condicion +{ + Condicion *izq = NULL, *der = NULL; +public: + Logico(Condicion*i, Condicion*d, string sim) + { + izq = i; + der = d; + simbolo = sim; + } + +}; +typedef Logico *pLogico; + +class LogicoNOT : public Condicion +{ + Condicion *sig = NULL; +public: + LogicoNOT(Condicion*s) + { + sig = s; + } + +}; +typedef LogicoNOT *pLogicoNOT; + +//SENTENCIAS + +class Asignacion : public Sentencia +{ + Expresion *der = NULL; + Identificador *izq = NULL; +public: + + Asignacion(Identificador *i, Expresion *d) + { + izq = i; + der = d; + } + + Asignacion(){} + + char checkType() + { + return 'a'; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << izq->imprime(); + s << der->imprime(); + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + if(tipo == 'i')//Asignación para enteros + { + s << der->generaCodigo(); + s << "pop _" << izq->getSimbolo() << ";" << endl; + } + else //Empezar por aqui si se quieren integrar numeros reales o cadenas + s << ";Solo asigna enteros" << endl; + + return s.str(); + } + + void semantico() + { + der->semantico(); + tipo = der->getTipo(); + } + + char getTipo() + { + return tipo; + } + + string getID() + { + return izq->simbolo; + } +}; +typedef Asignacion *pAsignacion; + +class Print : public Sentencia +{ + Expresion *contenido = NULL; +public: + + Print(Expresion *c) + { + contenido = c; + } + + Print(){} + + char checkType() + { + return 'p'; + } + + string imprime() + { + stringstream s; + s << "" << endl; + s << "" << endl; + s << contenido->imprime(); + s << "" << endl; + s << "" << endl; + return s.str(); + } + + string generaCodigo() + { + stringstream s; + if(tipo == 'i') + { + s << contenido->generaCodigo(); + s << "pop eax" << endl; + s << "print str$(eax),10;" << endl; + } + else if(tipo == 'c') + { + s << "print chr$(" << contenido->getSimbolo() << "),10;" << endl; + } + + return s.str(); + } + + void semantico() + { + contenido->semantico(); + tipo = contenido->getTipo(); + } + + char getTipo() + { + return tipo; + } + +}; +typedef Print *pPrint; + +class SI : public Sentencia +{ + Condicion *cond = NULL; + Sentencia *bloque = NULL, *otro = NULL; +public: + + SI(Condicion *c, Sentencia *b, Sentencia *o) + { + cond = c; + bloque = b; + otro = o; + } + + SI(){} + + char checkType() + { + return 'i'; + } + + string imprime() + { + pSentencia temp; + stringstream s; + + s << "" << endl; + s << cond->imprime(); + s << "" << endl; + temp = bloque; + while(temp != NULL) + { + s << temp->imprime(); + temp = temp->siguiente; + } + s << "" << endl; + s << "" << endl; + temp = otro; + while(temp != NULL) + { + s << temp->imprime(); + temp = temp->siguiente; + } + s << "" << endl; + s << "" << endl; + + return s.str(); + } + + void semantico() + { + cond->semantico(); + tipo = cond->getTipo(); + } + + pSentencia getBloque() + { + return bloque; + } + + pSentencia getOtro() + { + return otro; + } + + char getTipo() + { + return tipo; + } + +}; +typedef SI *pSI; + +class Mientras : public Sentencia +{ + Condicion *cond = NULL; + Sentencia *bloque = NULL; +public: + + Mientras(Condicion *c, Sentencia *b) + { + cond = c; + bloque = b; + } + + Mientras(){} + + char checkType() + { + return 'w'; + } + + string imprime() + { + pSentencia temp; + stringstream s; + + s << "" << endl; + s << cond->imprime(); + s << "" << endl; + temp = bloque; + while(temp != NULL) + { + s << temp->imprime(); + temp = temp->siguiente; + } + s << "" << endl; + s << "" << endl; + + return s.str(); + } + + + + void semantico() + { + cond->semantico(); + tipo = cond->getTipo(); + } + + pSentencia getBloque() + { + return bloque; + } + + char getTipo() + { + return tipo; + } + +}; +typedef Mientras *pMientras; + + +#endif // NODOS_H_INCLUDED diff --git a/entrada.txt b/entrada.txt index 2cef30b..44da876 100644 --- a/entrada.txt +++ b/entrada.txt @@ -1,6 +1,7 @@ -a = 5; -b = a - 3; -print (a+2); -a = 34; -c = 7+3; -print (a+3); \ No newline at end of file +a = 5; +b = 2.3 - 3.22; +print (a+2); +a = 34; +c = 7+3; +print (c*3); +print("Kappa"); \ No newline at end of file diff --git a/lista.h b/lista.h index e4e4e08..628a757 100644 --- a/lista.h +++ b/lista.h @@ -1,130 +1,146 @@ -#ifndef LISTA_H_INCLUDED -#define LISTA_H_INCLUDED - -class ActualID -{ -public: - ActualID *siguiente = NULL; - string simbolo; - char tipo; - - ActualID(string sim, char t) - { - simbolo = sim; - tipo = t; - } - - void setTipo(char t) - { - tipo = t; - } - - char getTipo() - { - return tipo; - } - - string getID() - { - return simbolo; - } -}; -typedef ActualID *pActualID; - - -class Lista -{ -private: - pActualID root; - pActualID fin; -public: - - Lista() - { - root = NULL; - fin = NULL; - } - - bool vacio() - { - return (root == NULL); - } - - void insertaNodo(string nuevo, char t) - { - if(vacio()) - { - root = new ActualID(nuevo,t); - fin = root; - } - else - { - if(!buscaID(nuevo)) - { - fin->siguiente = new ActualID(nuevo, t); - fin = fin->siguiente; - } - else - { - updateType(nuevo, t); - } - } - } - - bool buscaID(string busca) - { - pActualID temp = root; - while(temp != NULL) - { - if(temp->getID() == busca) - { - return true; - } - temp = temp->siguiente; - } - return false; - } - - char tipoID(string busca) - { - pActualID temp = root; - while(temp != NULL) - { - if(temp->getID() == busca) - { - return temp->getTipo(); - } - temp = temp->siguiente; - } - return 'e'; - } - - void updateType(string busca, char t) - { - pActualID temp = root; - while(temp != NULL) - { - if(temp->getID() == busca) - { - temp->setTipo(t); - break; - } - temp = temp->siguiente; - } - } - - void imprime() - { - pActualID temp = root; - while(temp != NULL) - { - cout << temp->getID() << "\t" << temp->getTipo() << endl; - temp = temp->siguiente; - } - } - - -}; - -#endif // LISTA_H_INCLUDED - +#ifndef LISTA_H_INCLUDED +#define LISTA_H_INCLUDED + +class ActualID +{ +public: + ActualID *siguiente = NULL; + string simbolo; + char tipo; + + ActualID(string sim, char t) + { + simbolo = sim; + tipo = t; + } + + void setTipo(char t) + { + tipo = t; + } + + char getTipo() + { + return tipo; + } + + string getID() + { + return simbolo; + } +}; +typedef ActualID *pActualID; + + +class Lista +{ +private: + pActualID root; + pActualID fin; +public: + + Lista() + { + root = NULL; + fin = NULL; + } + + bool vacio() + { + return (root == NULL); + } + + void insertaNodo(string nuevo, char t) + { + if(vacio()) + { + root = new ActualID(nuevo,t); + fin = root; + } + else + { + if(!buscaID(nuevo)) + { + fin->siguiente = new ActualID(nuevo, t); + fin = fin->siguiente; + } + else + { + updateType(nuevo, t); + } + } + } + + bool buscaID(string busca) + { + pActualID temp = root; + while(temp != NULL) + { + if(temp->getID() == busca) + { + return true; + } + temp = temp->siguiente; + } + return false; + } + + char tipoID(string busca) + { + pActualID temp = root; + while(temp != NULL) + { + if(temp->getID() == busca) + { + return temp->getTipo(); + } + temp = temp->siguiente; + } + return 'e'; + } + + void updateType(string busca, char t) + { + pActualID temp = root; + while(temp != NULL) + { + if(temp->getID() == busca) + { + temp->setTipo(t); + break; + } + temp = temp->siguiente; + } + } + + void imprime() + { + pActualID temp = root; + while(temp != NULL) + { + cout << temp->getID() << "\t" << temp->getTipo() << endl; + temp = temp->siguiente; + } + } + + string listaEnteros() + { + stringstream s; + pActualID temp = root; + while(temp != NULL) + { + if(temp->getTipo() == 'i') + { + s << "_" << temp->getID() << " SDWORD ?" << endl; + } + temp = temp->siguiente; + } + + return s.str(); + } + + +}; + +#endif // LISTA_H_INCLUDED + diff --git a/salida.txt b/salida.txt index 1ba91b2..3a3980f 100644 --- a/salida.txt +++ b/salida.txt @@ -1,51 +1,48 @@ -.386 -.model flat, stdcall -option casemap:none - include \masm32\macros\macros.asm - include \masm32\include\masm32.inc - include \masm32\include\kernel32.inc - includelib \masm32\lib\masm32.lib - includelib \masm32\lib\kernel32.lib - -.data -.data? -.code -inicio: - -push 5; -pop a; -push a; -push 3; -pop ebx; -pop eax; -add eax, ebx; -push eax; -pop b; -push a; -push 2; -pop ebx; -pop eax; -add eax, ebx; -push eax; -pop eax -print str$(eax),10; -push 34; -pop a; -push 7; -push 3; -pop ebx; -pop eax; -add eax, ebx; -push eax; -pop c; -push a; -push 3; -pop ebx; -pop eax; -add eax, ebx; -push eax; -pop eax -print str$(eax),10; -exit - -end inicio +.386 +.model flat, stdcall +option casemap:none + include \masm32\macros\macros.asm + include \masm32\include\masm32.inc + include \masm32\include\kernel32.inc + includelib \masm32\lib\masm32.lib + includelib \masm32\lib\kernel32.lib + +.data +.data? +_a SDWORD ? +_c SDWORD ? +.code +inicio: + +push 5; +pop _a; +;Solo asigna enteros +push _a; +push 2; +pop ebx; +pop eax; +add eax, ebx; +push eax; +pop eax +print str$(eax),10; +push 34; +pop _a; +push 7; +push 3; +pop ebx; +pop eax; +add eax, ebx; +push eax; +pop _c; +push _c; +push 3; +pop ebx; +pop eax; +mul ebx; +push eax; +pop eax +print str$(eax),10; +print chr$("Kappa"),10; +exit + +end inicio