Skip to content

Commit

Permalink
Merge pull request #4 from JackCloudman/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JackCloudman authored May 15, 2019
2 parents 5de0e05 + f549809 commit 5fa1611
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 23 deletions.
1 change: 1 addition & 0 deletions Objects/LLang.l
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ EXIT exit[\(][\)]
{op} |
\n {return *yytext;}
{ws} { /* Do nothing */ }
[\[|\]|,:] {return *yytext;}
. { /* Do nothing */ }
{STRING} {
yylval.sym = install("",object,(LLObject*)LLString_Make(yytext));
Expand Down
19 changes: 17 additions & 2 deletions Objects/LLang.y
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void warning(char *s, char *t);
extern void init();
extern void execute();
extern void initcode();
extern Inst *progp;
extern FILE *yyin;
int readfile = 0;
%}
Expand All @@ -22,6 +23,7 @@ int readfile = 0;
Inst *inst;
}
%token <sym> object VAR BLTIN INDEF EXIT
%type<inst> arraylist initarray

%right '='
%left '+' '-'
Expand All @@ -34,8 +36,15 @@ list:
| list exp '\n' { code2(print,STOP);return 1;}
| list error '\n' {initcode();printf(">>> ");yyerrok;}
;
initarray: {code(makeArray);}
;
arraylist: arraylist','arraylist {}
| exp {}
| {$$=progp;}
;
asgn: VAR '=' exp {code3(varpush,(Inst)$1,assign);}
;
| exp '[' exp ']' '=' exp {code(ChangeValue);}
;
exp: object { code2(constpush,(Inst)$1);}
| VAR {code3(varpush,(Inst)$1,eval);}
| asgn
Expand All @@ -47,7 +56,13 @@ exp: object { code2(constpush,(Inst)$1);}
|BLTIN '(' exp ')' {code2(bltin,(Inst)$1->u.ptr);}
|'-' exp %prec UNARYMINUS {code(negate);}
| EXIT {exit(0);}
| initarray '['arraylist']' {code(STOP);}
| exp '[' exp ']' {code(aArray);}
| exp '[' index ':' index ']' {code(getSubArray);}
;
index: exp {}
| {code(emptypush);}
;
%%
#include <stdio.h>
#include <ctype.h>
Expand All @@ -60,7 +75,7 @@ int main (int argc, char *argv[]){
progname=argv[0];
init();
if(argc==1){
printf("Lala Lang v1.2 \n[GCC 8.2.1 20181127]\n");
printf("Lala Lang v1.3 \n[GCC 8.2.1 20181127]\n");
setjmp(begin);
printf(">>> ");
for(initcode(); yyparse (); initcode()){
Expand Down
84 changes: 69 additions & 15 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ extern void execerror(char *s, char *t);
// 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);
}
}
}
if((a->ob_type == LLListTypeObject) && (b->ob_type == LLListTypeObject)){
result = (LLObject*) LLList_merge((LLEntryListObject*)b,(LLEntryListObject*)a);
}else
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);
}
}
}
if(result==LL_NOT_IMPLEMENTED)
execerror("Operador '+' no esta implementado", NULL);
return result;
Expand Down Expand Up @@ -55,13 +58,64 @@ LLObject* LL_FUNC_DIV(LLObject* a,LLObject* b){
}
LLObject* LL_FUNC_NEGATE(LLObject* a){
LLObject* result = 0;
if ((a->ob_type == LLIntTypeObject)) {
if (a->ob_type == LLIntTypeObject) {
result = (LLObject*)LLInt_Make(-((LLIntObject*)a)->o_val);
}
if(result==LL_NOT_IMPLEMENTED)
execerror("Operador 'unary -' no esta implementado", NULL);
return result;
}
LLObject* LL_FUNC_AACCESS(LLObject* l,LLObject* i){
LLObject* result = 0;
if((l->ob_type == LLListTypeObject)&&(i->ob_type==LLIntTypeObject)){
result = LLList_getElement((LLEntryListObject*)l,((LLIntObject*)i)->o_val);
if(!result)
execerror("IndexError: list index out of range", NULL);
}
if(result==LL_NOT_IMPLEMENTED)
execerror("Operador 'Access array [ ]' no esta implementado", NULL);
return result;
}
LLObject* LL_FUNC_AREPLACE(LLObject* l,LLObject* i,LLObject*o){
LLListObject* result = 0;
if((l->ob_type == LLListTypeObject)&&(i->ob_type==LLIntTypeObject)){
result = LLEntryList_getElement((LLEntryListObject*)l,((LLIntObject*)i)->o_val);
if(!result)
execerror("IndexError: list index out of range", NULL);
result->o = o;
}
if(result==LL_NOT_IMPLEMENTED)
execerror("Operador 'Access array [ ]' no esta implementado", NULL);
return o;
}

LLObject* LL_FUNC_GETSUBARRAY(LLObject* l,LLObject* start,LLObject* end){
LLObject* result = 0;
int* s = 0;
int* e = 0;
if(l->ob_type == LLListTypeObject){
if(start){
if(start->ob_type==LLIntTypeObject){
s = &(((LLIntObject*)start)->o_val);
}else{
execerror("Error Index Access ' [ ] start'", NULL);
}
}
if(end){
if(end->ob_type==LLIntTypeObject){
e = &(((LLIntObject*)end)->o_val);
}else{
execerror("Error Index Access ' [ ] end'", NULL);
}
}
result = (LLObject*)LLList_getrange((LLEntryListObject*)l,s,e);
if(!result)
return (LLObject*)LLEntryList_Make();
}
if(result==LL_NOT_IMPLEMENTED)
execerror("Operador 'Access array [ ]' no esta implementado", NULL);
return result;
}

int LL_FUNC_PRINT(LLObject* o,char* end){
if(o==0){
Expand All @@ -72,4 +126,4 @@ int LL_FUNC_PRINT(LLObject* o,char* end){
printf("%s",end);
}
return 0;
}
}
6 changes: 3 additions & 3 deletions Objects/listObject.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,15 @@ LLEntryListObject* LLList_merge(LLEntryListObject* e1,LLEntryListObject* e2){
LLListObject *aux;
LLEntryListObject *n;
n = 0;
if(e1!=0){
if(e1->l_val!=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){
if(e2->l_val!=0){
aux = e2->l_val;
while(aux->next!=(e2->l_val)){
n = LLList_append(n,aux->o);
Expand All @@ -196,7 +196,7 @@ LLEntryListObject* LLList_merge(LLEntryListObject* e1,LLEntryListObject* e2){
int LLList_print(LLObject* o){
LLEntryListObject* e = (LLEntryListObject*)o;
if(e==0||e->l_val==0){
printf("[]\n");
printf("[]");
return 0;
}
LLListObject *aux = e->l_val;
Expand Down
47 changes: 46 additions & 1 deletion Objects/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,53 @@ void bltin( )/* evaluar un predefinido en el tope de la pila */
d.val = (*(LLObject* (*)())(*pc++))(d.val);
push(d);
}
void makeArray(){
Datum d;
LLEntryListObject* l = 0;
Datum* savestackp = stackp;
execute(pc);
while(stackp > savestackp){
d = pop();
l = LLList_insert(l,d.val);
}
if(l == 0){
l = LLEntryList_Make();
}
d.val = (LLObject*)l;
push(d);
*pc++;
}

void aArray(){
Datum d1,d2;
d2 = pop();
d1 = pop();
d1.val = LL_FUNC_AACCESS(d1.val,d2.val);
push(d1);
}
void ChangeValue(){
Datum nd,index,a;//New dato,index, array
nd = pop();
index = pop();
a = pop(); //Save array
LL_FUNC_AREPLACE(a.val,index.val,nd.val);
push(nd);
}
void getSubArray(){
Datum start,end,lista;

end = pop();
start = pop();
lista = pop();

lista.val = LL_FUNC_GETSUBARRAY(lista.val,start.val,end.val);
push(lista);
}
void emptypush(){
Datum d;
d.val = 0;
push(d);
}
Inst *code(Inst f) /* instalar una instrucción u operando */
{
Inst *oprogp = progp;
Expand All @@ -134,4 +179,4 @@ void execute(Inst* p) /* ejecución con la máquina */
{
for (pc = p; *pc != STOP; )
(*(*pc++))();
}
}
3 changes: 2 additions & 1 deletion include/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef void (*Inst)(); /* instrucción de máquina */
extern Inst prog[];

extern void eval(), addo(), subo(), mulo(), divo(),negate();
extern void assign(), bltin(), varpush(), constpush(), print();
extern void assign(), bltin(), varpush(), constpush(), print(),makeArray(),execute(Inst* p);
extern void aArray(),ChangeValue(),getSubArray(),emptypush();

#endif
5 changes: 4 additions & 1 deletion include/abstract.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ 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_AACCESS(LLObject*,LLObject*);
int LL_FUNC_PRINT(LLObject*,char*);
LLObject* LL_FUNC_AREPLACE(LLObject*,LLObject*,LLObject*);
LLObject* LL_FUNC_GETSUBARRAY(LLObject*,LLObject*,LLObject*);
#define LL_NOT_IMPLEMENTED 0
#endif
#endif

0 comments on commit 5fa1611

Please sign in to comment.