diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0cf8ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +Lala +lex.yy.c +y.tab.c +y.tab.h +.idea diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8e73f8d --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +Gram=Objects/y.tab.c Objetcs/y.tab.h + +all: $(Gram) Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c + @gcc -I include -o Lala Objects/y.tab.c Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c + @echo Compiled + +$(Gram): Objects/LLang.y + @yacc -d Objects/LLang.y + @mv *.tab.* Objects/ + +Objects/lex.yy.c: Objects/LLang.l + @flex Objects/LLang.l + @mv lex.yy.c Objects/ + +clean: + @rm -f Objects/lex.yy.c Objects/*.tab.* Lala + @echo Clean diff --git a/Objects/LLang.l b/Objects/LLang.l new file mode 100644 index 0000000..02d60e5 --- /dev/null +++ b/Objects/LLang.l @@ -0,0 +1,48 @@ +%option noyywrap +%{ +#include +#include +#include "Lala.h" +#define YYSTYPE APLLObject +#include "y.tab.h" +extern LLIntObject* LLInt_Make(int); +extern LLStringObject* LLString_Make(char*); +void RmWs(char* str); +extern YYSTYPE yylval; +%} +/* Add your Flex definitions here */ +/* Some definitions are already provided to you*/ +op [-+*\/()] +ws [ \t]+ +digits [0-9] +number (0|[1-9]+{digits}*)\.?{digits}* +STRING \"(\\.|[^"\\])*\" +%% +{number} { + int dato; + RmWs(yytext); + sscanf(yytext,"%d",&dato); + yylval=(LLObject*) LLInt_Make(dato); + return object;} +{op} | +\n {return *yytext;} +{ws} { /* Do nothing */ } +. { /* Do nothing */ } +{STRING} { + yylval = (LLObject*)LLString_Make(yytext); + return object; + } +%% +void RmWs(char* str) { + int i = 0, j = 0; + char temp[strlen(str) + 1]; + strcpy(temp, str); + while (temp[i] != '\0') { + while (temp[i] == ' ') + i++; + str[j] = temp[i]; + i++; + j++; + } + str[j] = '\0'; +} \ No newline at end of file diff --git a/Objects/LLang.y b/Objects/LLang.y new file mode 100644 index 0000000..66d7f98 --- /dev/null +++ b/Objects/LLang.y @@ -0,0 +1,43 @@ +%{ +#include +#include "Lala.h" +#include "abstract.h" +void yyerror (char *s); +int yylex (); +void warning(char *s, char *t); +extern void init(); +%} +%token object +%left '+' '-' +%left '*' '/' +%% +list: + | list'\n' + | list exp '\n' {LL_FUNC_PRINT($2,"\n");printf(">>> ");} + ; +exp: object { $$ = $1;} + | exp '+' exp { $$ = LL_FUNC_ADD($1,$3); } + | exp '-' exp { $$ = LL_FUNC_SUB($1,$3); } + | exp '*' exp { $$ = LL_FUNC_MUL($1,$3);} + | exp '/' exp { $$ = LL_FUNC_DIV($1,$3); } + | '(' exp ')' { $$ = $2;} + ; +%% +#include +#include +char *progname; +int main (int argc, char *argv[]){ + init(); + printf(">>> "); + progname=argv[0]; + yyparse (); + return 0; +} +void yyerror (char *s) { + warning(s, (char *) 0); +} +void warning(char *s, char *t){ + fprintf (stderr, "%s: %s", progname, s); + if(t) + fprintf (stderr, " %s", t); +} \ No newline at end of file diff --git a/Objects/Object.c b/Objects/Object.c new file mode 100644 index 0000000..e69de29 diff --git a/Objects/abstract.c b/Objects/abstract.c new file mode 100644 index 0000000..37fa5a6 --- /dev/null +++ b/Objects/abstract.c @@ -0,0 +1,58 @@ +#include "abstract.h" +#include "Lala.h" +extern LLTypeObject* LLIntTypeObject; +// Implementaciones +LLObject* LL_FUNC_ADD(LLObject* a,LLObject* b) { + LLObject *result = 0; + if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)){ + result = LLInt_ADD(a, b); + } else { + if ((a->ob_type == LLStringTypeObject)&& (b->ob_type == LLStringTypeObject)) { + result = LLString_CAT(a, b); + }else{ + if(a->ob_type==LLStringTypeObject){ + result = LLString_CAT(a,b->to_String(b)); + }else if(b->ob_type==LLStringTypeObject){ + result = LLString_CAT(a->to_String(a),b); + } + } + } + return result; +} +LLObject* LL_FUNC_SUB(LLObject* a,LLObject* b){ + LLObject* result = 0; + if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) { + result = LLInt_SUB(a, b); + } + return result; +} +LLObject* LL_FUNC_MUL(LLObject* a,LLObject* b){ + LLObject* result = 0; + if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) { + result = LLInt_MUL(a, b); + }else if ((a->ob_type == LLStringTypeObject)&&(b->ob_type == LLIntTypeObject)){ + result = LLString_MUL(a,b); + }else if((a->ob_type == LLIntTypeObject)&&(b->ob_type == LLStringTypeObject)){ + result = LLString_MUL(b,a); + } + return result; +} +LLObject* LL_FUNC_DIV(LLObject* a,LLObject* b){ + LLObject* result = 0; + if ((a->ob_type == LLIntTypeObject) && (b->ob_type == LLIntTypeObject)) { + result = LLInt_DIV(a, b); + } + return result; +} + + +int LL_FUNC_PRINT(LLObject* o,char* end){ + if(o==0){ + return 0; + } + o->print(o); + if(end!=NULL){ + printf("%s",end); + } + return 0; +} \ No newline at end of file diff --git a/Objects/init.c b/Objects/init.c new file mode 100644 index 0000000..71b8849 --- /dev/null +++ b/Objects/init.c @@ -0,0 +1,20 @@ +#include "Lala.h" +extern LLTypeObject* LLIntTypeObject; +extern LLTypeObject* LLStringTypeObject; +extern LLTypeObject* LLListTypeObject; +void init(){ + LLIntTypeObject = calloc(1,sizeof(LLTypeObject)); + LLStringTypeObject = calloc(1,sizeof(LLTypeObject)); + LLListTypeObject = calloc(1,sizeof(LLTypeObject)); + char* c = 0; + c = malloc(sizeof(char)*13); + strcpy(c,"\0"); + LLIntTypeObject->name = c; + c = malloc(sizeof(char)*13); + strcpy(c,"\0"); + LLStringTypeObject->name = c; + c = malloc(sizeof(char)*14); + strcpy(c,"\0"); + LLListTypeObject->name = c; + return; +} \ No newline at end of file diff --git a/Objects/intObject.c b/Objects/intObject.c new file mode 100644 index 0000000..073f3ff --- /dev/null +++ b/Objects/intObject.c @@ -0,0 +1,40 @@ +#include "intObject.h" + +LLObject* LLInt_String(LLObject *o){ + char* buffer = (char*)calloc(32,sizeof(char)); + int j; + j = sprintf(buffer,"\""); + if(o!=NULL) + j += sprintf(buffer+j,"%d",((LLIntObject*)o)->o_val); + j+= sprintf(buffer+j,"\""); + buffer[j] = '\0'; + return (LLObject*)LLString_Make(buffer); +} +int LLInt_print(LLObject *o){ + printf("%d",((LLIntObject*)o)->o_val); + return 1; +} +LLIntObject* LLInt_Make(int val){ + LLIntObject * result = 0; + result = calloc(1,sizeof(LLIntObject)); + result->o_val = val; + result->print = LLInt_print; + result->to_String = LLInt_String; + result->ob_type = LLIntTypeObject; + return result; +} +LLObject* LLInt_ADD(LLObject* a,LLObject* b){ + return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val+((LLIntObject*)b)->o_val); +} + +LLObject* LLInt_SUB(LLObject* a,LLObject* b){ + return (LLObject*)LLInt_Make((((LLIntObject*)a)->o_val)-(((LLIntObject*)b)->o_val)); +} +LLObject* LLInt_MUL(LLObject* a,LLObject* b){ + return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val*((LLIntObject*)b)->o_val); +} +LLObject* LLInt_DIV(LLObject* a,LLObject* b){ + return (LLObject*)LLInt_Make(((LLIntObject*)a)->o_val/((LLIntObject*)b)->o_val); +} + + diff --git a/Objects/listObject.c b/Objects/listObject.c new file mode 100644 index 0000000..cd802c6 --- /dev/null +++ b/Objects/listObject.c @@ -0,0 +1,212 @@ +#include "listObject.h" +LLObject* LLList_String(LLObject* o){ + LLEntryListObject* e = (LLEntryListObject*)o; + if(e==NULL||e->l_val==NULL) + return (LLObject*)LLString_Make("\"[]\""); + char* buffer = 0; + int j = 0; + LLStringObject *s = 0; + + LLListObject *aux = e->l_val; + buffer = (char*)calloc(5,sizeof(char)); + j += sprintf(buffer,"%c",'\"'); + j += sprintf(buffer+j,"%c",'['); + while(aux->next!=(e->l_val)){ + if(aux->o!=NULL){ + s = (LLStringObject*)(aux->o->to_String(aux->o)); + buffer = realloc(buffer,strlen(buffer)+(s->len)+2); + j+=sprintf(buffer+j,"%s, ",s->o_sval); + } + aux = aux->next; + } + s = (LLStringObject*)(aux->o->to_String(aux->o)); + buffer = realloc(buffer,strlen(buffer)+(s->len)+2); + j+=sprintf(buffer+j,"%s]",s->o_sval); + buffer[j] = '\"'; + buffer[j+1] = '\0'; + return (LLObject*)LLString_Make(buffer); +} +LLEntryListObject* LLEntryList_Make(){ + LLEntryListObject* n = (LLEntryListObject*)calloc(1,sizeof(LLEntryListObject)); + n->len = 0; + n->l_val = 0; + n->print = LLList_print; + n->ob_type = LLListTypeObject; + n->to_String = LLList_String; + return n; +} +LLListObject* LLList_Make(LLObject *o){ + LLListObject* n = (LLListObject*)malloc(sizeof(LLListObject)); + n->o = o; + n->next = n; + n->prev = n; + return n; +} +LLEntryListObject* LLList_insert(LLEntryListObject* e,LLObject* o){ + LLEntryListObject* ne = 0; + LLListObject* n = LLList_Make(o); + + if(e==0){ + ne = LLEntryList_Make(); + ne->l_val = n; + ne->len = 1; + return ne; + } + n->next = e->l_val; + e->l_val->prev->next = n; + n->prev = e->l_val->prev; + e->l_val->prev = n; + e->l_val = n; + (e->len)++; + return e; +} +LLIntObject* LLList_getsize(LLEntryListObject* e){ + return LLInt_Make(e->len); +} +LLEntryListObject* LLList_getrange(LLEntryListObject* e,int* pos1,int* pos2){ + LLListObject* start = 0; + LLListObject* end = 0; + LLEntryListObject* aux = 0; + if(e==0||e->l_val ==0) + return NULL; + if(pos1 == 0){ + start = e->l_val; + }else{ + start = LLEntryList_getElement(e,*pos1); + if(start == 0){ + if(*pos1<0) + start = e->l_val; + else + return NULL; + } + } + if(pos2 == 0){ + end = e->l_val; + }else{ + if(*pos2 == 0) + return NULL; + end = LLEntryList_getElement(e,*pos2); + if(end == 0){ + if(*pos2>0) + end = e->l_val; + else + return NULL; + } + } + if(pos1 !=0 && pos2!=0 && *pos1==*pos2) + return 0; + while(start->next!=end){ + aux = LLList_append(aux,start->o); + start = start->next; + } + if(pos2!=0 && *pos2!=0) + aux = LLList_append(aux,start->o); + else + if(pos2==0) + aux = LLList_append(aux,start->o); + return aux; +} +LLListObject* LLEntryList_getElement(LLEntryListObject* e,int pos){ + LLListObject* aux = 0; + int i; + if(e==0 || e->l_val==0) + return NULL; + if(pos>=0){ // Recorrido a la derecha + aux = e->l_val; + for(i=0;(aux->next!=(e->l_val))&&(inext; + } + if(pos>i) + return NULL; + } + else{ //Recorrido a la izquierda + aux = e->l_val->prev; + for(i=-1;(aux->prev!=(e->l_val))&&(i>pos);i--){ + aux = aux->prev; + } + if(posl_val; + } + return aux; +} +LLObject* LLList_getElement(LLEntryListObject* e,int pos){ + LLListObject* aux = 0; + int i; + if(e==0 || e->l_val==0) + return NULL; + if(pos>=0){ // Recorrido a la derecha + aux = e->l_val; + for(i=0;(aux->next!=(e->l_val))&&(inext; + } + if(pos>i) + return NULL; + } + else{ //Recorrido a la izquierda + aux = e->l_val->prev; + for(i=-1;(aux->prev!=(e->l_val))&&(i>pos);i--){ + aux = aux->prev; + } + if(posl_val->o; + } + return aux->o; +} +LLEntryListObject* LLList_append(LLEntryListObject* e,LLObject* o){ + LLListObject* n = LLList_Make(o); + if(e==0){ + LLEntryListObject* ne = LLEntryList_Make(); + ne->len = 1; + ne->l_val = n; + return ne; + } + n->next = e->l_val; + n->prev = e->l_val->prev; + e->l_val->prev->next = n; + e->l_val->prev = n; + (e->len)++; + return e; +} +LLEntryListObject* LLList_merge(LLEntryListObject* e1,LLEntryListObject* e2){ + LLListObject *aux; + LLEntryListObject *n; + n = 0; + if(e1!=0){ + aux = e1->l_val; + while(aux->next!=(e1->l_val)){ + n = LLList_append(n,aux->o); + aux = aux->next; + } + n = LLList_append(n,aux->o); + } + if(e2!=0){ + aux = e2->l_val; + while(aux->next!=(e2->l_val)){ + n = LLList_append(n,aux->o); + aux = aux->next; + } + n = LLList_append(n,aux->o); + } + return n; +} +int LLList_print(LLObject* o){ + LLEntryListObject* e = (LLEntryListObject*)o; + if(e==0||e->l_val==0){ + printf("[]\n"); + return 0; + } + LLListObject *aux = e->l_val; + printf("["); + while(aux->next!=(e->l_val)){ + aux->o->print(aux->o); + printf(", "); + aux = aux->next; + } + aux->o->print(aux->o); + printf("]"); + return 0; +} diff --git a/Objects/stringObject.c b/Objects/stringObject.c new file mode 100644 index 0000000..ebb26a8 --- /dev/null +++ b/Objects/stringObject.c @@ -0,0 +1,51 @@ +#include "stringObject.h" +#include "intObject.h" +int LLString_print(LLObject *o){ + printf("\"%s\"",((LLStringObject*)o)->o_sval); + return 1; +} +LLObject* LLString_String(LLObject* o){ + LLStringObject* s = (LLStringObject*)o; + if(s==NULL) + return (LLObject*)LLString_Make("\"\'\'\""); + char * cad = calloc((s->len)+4,sizeof(char)); + sprintf(cad,"\"\'%s\'\"",s->o_sval); + return (LLObject*)LLString_Make(cad); +} +LLStringObject* LLString_Make(char* cadena){ + int len = strlen(cadena)-1; // La cadena llega como: "hola" + LLStringObject* result = 0; + result = calloc(1,sizeof(LLStringObject)); + char* c = (char*)malloc(sizeof(char)*len); + strncpy(c, cadena+1,len); + c[len-1] = '\0'; + result->o_sval = c; + result->len = len; + result->print = LLString_print; + result->ob_type = LLStringTypeObject; + result->to_String = LLString_String; + return result; +} +LLObject* LLString_CAT(LLObject* a,LLObject* b){ + int len = (((LLStringObject*)a)->len)+(((LLStringObject*)b)->len); + char *buffer = 0; + buffer = calloc(len+3,sizeof(char)); + int j=sprintf(buffer,"\"%s%s\"",((LLStringObject*)a)->o_sval,((LLStringObject*)b)->o_sval); + buffer[j] = '\0'; + return (LLObject*)LLString_Make(buffer); + +} +LLObject* LLString_MUL(LLObject* s,LLObject* i){ + int len = ((LLStringObject*)s)->len; + int mul = ((LLIntObject*)i)->o_val; + int tam = len*mul+3; + char* cad = calloc(tam,sizeof(char)); + int k=0; + k=sprintf(cad,"\""); + for(int j=0;jo_sval); + } + cad[k]='\"'; + cad[k+1]='\0'; + return (LLObject*)LLString_Make(cad); +} diff --git a/include/Lala.h b/include/Lala.h new file mode 100644 index 0000000..dac1013 --- /dev/null +++ b/include/Lala.h @@ -0,0 +1,8 @@ +#ifndef __LALA_H__ +#define __LALA_H__ + +#include "Object.h" +#include "listObject.h" +#include "intObject.h" +#include "stringObject.h" +#endif \ No newline at end of file diff --git a/include/Object.h b/include/Object.h new file mode 100644 index 0000000..e457666 --- /dev/null +++ b/include/Object.h @@ -0,0 +1,21 @@ +#ifndef __OBJECT_H__ +#define __OBJECT_H__ + #include + #include + #include +// #define LLMEM(LLObject) (LLObject*)calloc(1,sizeof(LLObject)) + + typedef struct _typeobject{ + char *name; + }LLTypeObject; + + typedef struct _object LLObject; + #define LLObject_HEAD LLTypeObject *ob_type; \ + int (*print)(LLObject*); \ + LLObject* (*to_String)(LLObject*); + typedef struct _object{ + LLObject_HEAD + }LLObject; + typedef LLObject *APLLObject; + #define YYSTYPE APLLObject +#endif diff --git a/include/abstract.h b/include/abstract.h new file mode 100644 index 0000000..1d3a549 --- /dev/null +++ b/include/abstract.h @@ -0,0 +1,13 @@ +#ifndef __LL_ABSTRACT_H__ +#define __LL_ABSTRACT_H__ + +#include "Lala.h" + +LLObject* LL_FUNC_ADD(LLObject*,LLObject*); +LLObject* LL_FUNC_SUB(LLObject*,LLObject*); +LLObject* LL_FUNC_MUL(LLObject*,LLObject*); +LLObject* LL_FUNC_DIV(LLObject*,LLObject*); +LLObject* LL_FUNC_NEGATE(LLObject*); +int LL_FUNC_PRINT(LLObject*,char*); +#define LL_NOT_IMPLEMENTED 0 +#endif \ No newline at end of file diff --git a/include/intObject.h b/include/intObject.h new file mode 100644 index 0000000..7d5d81a --- /dev/null +++ b/include/intObject.h @@ -0,0 +1,18 @@ +#ifndef __LL_INT_OBJECT_H__ +#define __LL_INT_OBJECT_H__ + #include "Object.h" + #include "stringObject.h" + typedef struct LLIntObject{ + LLObject_HEAD + int len; + int o_val; + }LLIntObject; +LLTypeObject* LLIntTypeObject; + LLObject* LLInt_String(LLObject *); + int LLInt_print(LLObject*); + LLIntObject* LLInt_Make(int val); + LLObject* LLInt_ADD(LLObject*,LLObject*); +LLObject* LLInt_SUB(LLObject*,LLObject*); +LLObject* LLInt_MUL(LLObject*,LLObject*); +LLObject* LLInt_DIV(LLObject*,LLObject*); +#endif diff --git a/include/listObject.h b/include/listObject.h new file mode 100644 index 0000000..9bfff0e --- /dev/null +++ b/include/listObject.h @@ -0,0 +1,27 @@ +#ifndef __LL_LIST_OBJECT_H__ +#define __LL_LIST_OBJECT_H__ + #include "Object.h" + #include "intObject.h" + typedef struct LLListObject{ + struct LLListObject* next; + struct LLListObject* prev; + LLObject* o; + }LLListObject; + + typedef struct LLEntryListObject{ + LLObject_HEAD + int len; + LLListObject* l_val; + }LLEntryListObject; + LLTypeObject* LLListTypeObject; + int LLList_print(LLObject*); + LLEntryListObject* LLEntryList_Make(); + LLEntryListObject* LLList_append(LLEntryListObject*,LLObject*); + LLEntryListObject* LLList_insert(LLEntryListObject*,LLObject*); + LLEntryListObject* LLList_merge(LLEntryListObject*,LLEntryListObject*); + LLListObject* LLEntryList_getElement(LLEntryListObject*,int); + LLObject* LLList_getElement(LLEntryListObject*,int); + LLEntryListObject* LLList_getrange(LLEntryListObject*,int*,int*); + LLIntObject* LLList_getsize(LLEntryListObject*); + LLObject* LLList_String(LLObject*); +#endif diff --git a/include/stringObject.h b/include/stringObject.h new file mode 100644 index 0000000..55663da --- /dev/null +++ b/include/stringObject.h @@ -0,0 +1,18 @@ +#ifndef __STRING_OBJECT_H__ +#define __STRING_OBJECT_H__ + #include "Object.h" + #include + typedef struct LLStringObject{ + LLObject_HEAD + int len; + char *o_sval; + }LLStringObject; + + LLTypeObject* LLStringTypeObject; + + int LLString_print(LLObject *o); + LLStringObject* LLString_Make(char*); + LLObject* LLString_CAT(LLObject*,LLObject*); + LLObject* LLString_String(LLObject*); + LLObject* LLString_MUL(LLObject*,LLObject*); +#endif