Skip to content

Commit

Permalink
Add MUL,DIV,SUB operations and fix errors with strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
JackCloudman committed May 14, 2019
1 parent 473c67a commit 20f822b
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 17 deletions.
12 changes: 7 additions & 5 deletions Objects/LLang.y
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@ extern void init();
%%
list:
| list'\n'
| list exp '\n' {LL_FUNC_PRINT($2,"\n");}
| list exp '\n' {LL_FUNC_PRINT($2,"\n");printf(">>> ");}
;
exp: object { $$ = $1;}
| exp '+' exp { $$ = LL_FUNC_ADD($1,$3); }
| exp '-' exp { /*$$ = Complejo_sub($1,$3);*/ }
| exp '*' exp { /*$$ = Complejo_mul($1,$3); */ }
| exp '/' exp { /*$$ = Complejo_div($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 <stdio.h>
#include <ctype.h>
char *progname;
void main (int argc, char *argv[]){
int main (int argc, char *argv[]){
init();
printf(">>> ");
progname=argv[0];
yyparse ();
return 0;
}
void yyerror (char *s) {
warning(s, (char *) 0);
Expand Down
32 changes: 25 additions & 7 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,33 @@ LLObject* LL_FUNC_ADD(LLObject* a,LLObject* b) {
}
}
}
if (result == LL_NOT_IMPLEMENTED)
return 0;
return result;
}
/*
LLObject* LL_FUNC_SUB(LLObject*,LLObject*);
LLObject* LL_FUNC_MUL(LLObject*,LLObject*);
LLObject* LL_FUNC_DIV(LLObject*,LLObject*);
LLObject* LL_FUNC_NEGATE(LLObject*);*/
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){
Expand Down
10 changes: 10 additions & 0 deletions Objects/intObject.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ 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);
}


3 changes: 2 additions & 1 deletion Objects/listObject.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ LLObject* LLList_String(LLObject* o){
s = (LLStringObject*)(aux->o->to_String(aux->o));
buffer = realloc(buffer,strlen(buffer)+(s->len)+2);
j+=sprintf(buffer+j,"%s]",s->o_sval);
j+=sprintf(buffer+j,"\"\0");
buffer[j] = '\"';
buffer[j+1] = '\0';
return (LLObject*)LLString_Make(buffer);
}
LLEntryListObject* LLEntryList_Make(){
Expand Down
19 changes: 17 additions & 2 deletions Objects/stringObject.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "stringObject.h"

#include "intObject.h"
int LLString_print(LLObject *o){
printf("\"%s\"",((LLStringObject*)o)->o_sval);
return 1;
Expand Down Expand Up @@ -30,7 +30,22 @@ LLObject* LLString_CAT(LLObject* a,LLObject* b){
int len = (((LLStringObject*)a)->len)+(((LLStringObject*)b)->len);
char *buffer = 0;
buffer = calloc(len+3,sizeof(char));
sprintf(buffer,"\"%s%s\"\0",((LLStringObject*)a)->o_sval,((LLStringObject*)b)->o_sval);
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;j<mul;j++){
k+=sprintf(cad+k,"%s",((LLStringObject*)s)->o_sval);
}
cad[k]='\"';
cad[k+1]='\0';
return (LLObject*)LLString_Make(cad);
}
4 changes: 2 additions & 2 deletions include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "Lala.h"

LLObject* LL_FUNC_ADD(LLObject*,LLObject*);
/*LLObject* LL_FUNC_SUB(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*);*/
LLObject* LL_FUNC_NEGATE(LLObject*);
int LL_FUNC_PRINT(LLObject*,char*);
#define LL_NOT_IMPLEMENTED 0
#endif
3 changes: 3 additions & 0 deletions include/intObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ LLTypeObject* LLIntTypeObject;
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
1 change: 1 addition & 0 deletions include/stringObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
LLStringObject* LLString_Make(char*);
LLObject* LLString_CAT(LLObject*,LLObject*);
LLObject* LLString_String(LLObject*);
LLObject* LLString_MUL(LLObject*,LLObject*);
#endif

0 comments on commit 20f822b

Please sign in to comment.