]> khamidou — GitHub mirrors - mojo.git/commitdiff
fixed an infinite-recursion bug
authorkhamidou <khamidou@gmail.com>
Sat, 26 Dec 2009 15:31:55 +0000 (16:31 +0100)
committerkhamidou <khamidou@gmail.com>
Sat, 26 Dec 2009 15:31:55 +0000 (16:31 +0100)
Makefile
interp.c
list.c
main.c
number.c
number.h
object.c
object.h

index cf2fb33ccf256be31929a4952e0da2b7cb0d3a0a..e296a01097f662625f16fff09fb1278b32c3516b 100644 (file)
--- 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
 
index 8fd0ef96b4bcb3707da799ff7a68f0f7e6f1b7ce..a18ee1e3117166a28001d6b52ac7a1da4205fe0e 100644 (file)
--- 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 8f7ee3239c658e43331172bd0a79d05438b661f5..3b3319830487970762912093f2b9cb4bc6deefab 100644 (file)
--- 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 42c61d27c5c1b532da2a0f4ecfd259f52c98b46d..fe26e474cf5da6f1cf3890d86537d7669650bd0a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -3,6 +3,8 @@
 #include "object.h"
 
 main() {
+
        init_object_system();
        eval_image(stdin);
+
 }
index b68d4ee982865e75bf3fbd412039e9f068e3b5db..d39e02423dc7ab6bcf161eec61b8b4d286ce7a5c 100644 (file)
--- 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);
+}
index 2817f3445a998fc849cc223f98c981e2155b9cd7..4a6f72e2b569af44d667186803a0cba250eb24de 100644 (file)
--- 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
index 9ac326bccf522cc673298a5e6a192d162e7652fc..55cd489c0664ee75fcdaca7e4cfbd7b7dc21f4c3 100644 (file)
--- 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();
+
 }
index 2aec958f13371f2cfa131eec294bf5d7d911d5c4..b128a6330d029d19352dcd54a5790c3fe46e09dc 100644 (file)
--- 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;