From: khamidou Date: Sat, 26 Dec 2009 15:31:55 +0000 (+0100) Subject: fixed an infinite-recursion bug X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=bee252c12f0802d01216b5b9d4f4c3cd6d8e9d35;p=mojo.git fixed an infinite-recursion bug --- diff --git a/Makefile b/Makefile index cf2fb33..e296a01 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS=-c -g -OBJS=interp.o lex.yy.o main.o object.o list.o error.o +OBJS=interp.o lex.yy.o main.o object.o list.o error.o number.o mojo: $(OBJS) cc -o mojo $(OBJS) -lfl @@ -11,6 +11,7 @@ lex.yy.o: lexer.l object.o: object.h object.c interp.o: interp.c interp.h list.o: list.c +number.o: number.c number.h main.o: main.c error.o: error.h error.c diff --git a/interp.c b/interp.c index 8fd0ef9..a18ee1e 100644 --- a/interp.c +++ b/interp.c @@ -7,11 +7,48 @@ extern char *yytext; int eval_image(FILE *fp) { + struct Object *o1 = clone_object(number_object); + struct Object *o2 = clone_object(number_object); + + if (o1 == NULL || o2 == NULL) + fail("Unable to clone both objects"); + + o1->value.i_value = 2; + o2->value.i_value = 2; + + struct Object *met = lookup_method(o1, "+"); + + if (met == NULL) + fail("Unable to lookup method"); + + struct Object *ret = met->value.c_method(o1, o2, NULL); + + fail("valeur : %d", ret->value.i_value); + int r; while((r = yylex()) != T_EOF) { switch(r) { case NUMBER: { + puts("ho"); + struct Object *o1 = clone_object(number_object); + struct Object *o2 = clone_object(number_object); + + if (o1 == NULL || o2 == NULL) + fail("Unable to clone both objects"); + + printf("%s\n", yytext); + o1->value.i_value = atoi(yytext); + o2->value.i_value = 3; + + struct Object *met = lookup_method(o1, "+"); + printf("%s", met->name); + if (met == NULL) + fail("Unable to lookup method"); + + struct Object *ret = met->value.c_method(o1, o2, NULL); + + printf("valeur : %d\n", ret->value.i_value); } default: break; diff --git a/list.c b/list.c index 8f7ee32..3b33198 100644 --- a/list.c +++ b/list.c @@ -80,7 +80,7 @@ struct Object* create_list_object(void) /* the rest of the time, this object is cloned */ /* We can't use new_object because it calls create_list recursively */ - list_object = (struct Object *) zalloc(sizeof(struct Object)); + struct Object * list_object = (struct Object *) zalloc(sizeof(struct Object)); if (list_object == NULL) error("Unable to create list object"); @@ -92,15 +92,18 @@ struct Object* create_list_object(void) /* Create a second list object that contains the methods of the list object */ struct Object *l1 = (struct Object *) zalloc(sizeof(struct Object)); + struct Object *l2 = (struct Object *) zalloc(sizeof(struct Object)); - if (l1 == NULL) + if (l1 == NULL || l2 == NULL) error("Unable to create list object at %s %d", __FILE__, __LINE__); l1->super = nil_object; l1->type = T_LIST; l1->name = "List"; l1->value.l_value = mojo_list_init(); - l1->methods = l1; /* the methods of l1 are l1 itself - this way we can have infinite recursion */ + l1->methods = nil_object; /* the methods of l1 are in l2, which is an empty list. This way, we don't fall into + an infinite loop in clone_object() + */ struct Object *met = zalloc(sizeof(struct Object)); @@ -144,6 +147,8 @@ struct Object* create_list_object(void) list_append(l1, met); list_object->methods = l1; + + return list_object; } struct Object* list_append(struct Object *list, struct Object *o) diff --git a/main.c b/main.c index 42c61d2..fe26e47 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,8 @@ #include "object.h" main() { + init_object_system(); eval_image(stdin); + } diff --git a/number.c b/number.c index b68d4ee..d39e024 100644 --- a/number.c +++ b/number.c @@ -30,3 +30,19 @@ struct Object *num_minus(struct Object *parent, struct Object *n) return ret; } + +void create_number_object(void) +{ + /* init number object */ + number_object = clone_object(base_object); + number_object->type = T_NUMBER; + number_object->name = "Number"; + number_object->value.i_value = 0; + + struct Object *plus_met = clone_object(builtin_object); + plus_met->name = "+"; + plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, + struct Object *arg2))num_plus; + + list_append(number_object->methods, plus_met); +} diff --git a/number.h b/number.h index 2817f34..4a6f72e 100644 --- a/number.h +++ b/number.h @@ -5,5 +5,6 @@ struct Object *num_plus(struct Object *parent, struct Object *n); struct Object *num_minus(struct Object *parent, struct Object *n); +void create_number_object(void); #endif diff --git a/object.c b/object.c index 9ac326b..55cd489 100644 --- a/object.c +++ b/object.c @@ -10,7 +10,7 @@ struct Object* lookup_method(struct Object *o, char *name) struct Object *met; int i; - for (i = 0; met != nil_object; i++) { + for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) { met = (struct Object *) list_nth(o->methods, i); if (strcmp(name, met->name) == 0) return met; @@ -37,6 +37,7 @@ struct Object* clone_object(struct Object *o) new->super = o->super; new->type = o->type; new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */ + new->methods = create_list_object(); struct Object *met; @@ -88,7 +89,7 @@ void init_object_system(void) /* We need to create the list object before everything else because it is needed for the method dictionnaries. */ - create_list_object(); + list_object = create_list_object(); /* init nil object */ nil_object = new_object(); @@ -96,17 +97,18 @@ void init_object_system(void) nil_object->type = T_NIL; nil_object->name = "nil"; - /* init base object */ base_object = new_object(); base_object->super = nil_object; base_object->type = T_OBJECT; base_object->name = "Object"; - - /* init number object */ - number_object = new_object(); - number_object->super = base_object; - number_object->type = T_NUMBER; - number_object->name = "Number"; - number_object->value.i_value = 0; + + /* init builtin object */ + builtin_object = new_object(); + builtin_object->super = nil_object; + builtin_object->type = T_BUILTIN; + builtin_object->name = "Builtin"; + + create_number_object(); + } diff --git a/object.h b/object.h index 2aec958..b128a63 100644 --- a/object.h +++ b/object.h @@ -2,6 +2,7 @@ #define __MOJO_OBJECT__ #include "queue.h" +#include "number.h" #include "zalloc.h" /* @@ -50,6 +51,7 @@ void init_object_system(void); struct Object *nil_object; struct Object *base_object; +struct Object *builtin_object; struct Object *number_object; struct Object *list_object;