]> khamidou — GitHub mirrors - mojo.git/commitdiff
fixed a bug in the definition of mojo_list_length
authorkhamidou <khamidou@gmail.com>
Sat, 26 Dec 2009 14:45:19 +0000 (15:45 +0100)
committerkhamidou <khamidou@gmail.com>
Sat, 26 Dec 2009 14:45:19 +0000 (15:45 +0100)
Makefile
list.c
number.c [new file with mode: 0644]
number.h [new file with mode: 0644]
object.c
object.h

index 15a78fdc25c9fc7d5091677cf910a9d52b159592..cf2fb33ccf256be31929a4952e0da2b7cb0d3a0a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -17,3 +17,6 @@ error.o: error.h error.c
 clean:
        rm *.o
        rm *~
+
+count:
+       wc -l `ls *.[chl] -1 | grep -v lex.yy.c | grep -v queue`
\ No newline at end of file
diff --git a/list.c b/list.c
index d3e53429289bc99bca3d1a20ef3721332c6dca12..8f7ee3239c658e43331172bd0a79d05438b661f5 100644 (file)
--- a/list.c
+++ b/list.c
@@ -60,11 +60,10 @@ int mojo_list_length(struct mojo_list *list)
        if (list == NULL)
                return -1;
 
-       struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
+       struct mojo_list_elem *e;
        int i = 0;
-       
-       for (i = 0; TAILQ_NEXT(e, mojo_lists) != NULL; i++) {
-               e = TAILQ_NEXT(e, mojo_lists);
+       TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) {
+               i++;
        }
 
        return i;
@@ -94,15 +93,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));
 
-       if (list_object == NULL)
-               error("Unable to create list object");
+       if (l1 == 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 */
+
+
        struct Object *met = zalloc(sizeof(struct Object));
+
        if (met == NULL)
                fail("Unable to create a method %d %d", __FILE__, __LINE__);
 
@@ -112,6 +114,7 @@ struct Object* create_list_object(void)
        list_append(l1, met);
 
        met = zalloc(sizeof(struct Object));
+
        if (met == NULL)
                fail("Unable to create a method %d %d", __FILE__, __LINE__);
 
@@ -121,6 +124,7 @@ struct Object* create_list_object(void)
        list_append(l1, met);
        
        met = zalloc(sizeof(struct Object));
+
        if (met == NULL)
                fail("Unable to create a method %d %d", __FILE__, __LINE__);
 
@@ -130,6 +134,7 @@ struct Object* create_list_object(void)
        list_append(l1, met);
 
        met = zalloc(sizeof(struct Object));
+
        if (met == NULL)
                fail("Unable to create a method %d %d", __FILE__, __LINE__);
 
@@ -138,7 +143,7 @@ struct Object* create_list_object(void)
                                                   struct Object *arg2))list_last;
        list_append(l1, met);
 
-       /* TODO: add the builtin methods to the list object */
+       list_object->methods = l1;
 }
 
 struct Object* list_append(struct Object *list, struct Object *o)
@@ -198,10 +203,9 @@ struct Object* list_length(struct Object *list)
        if (list == NULL || list->type != T_LIST || list->value.l_value == NULL)
                return nil_object;
 
-       
        struct Object *o = clone_object(number_object);
        if (o == NULL)
-               fail("Unable to clone number_object : %d %d", __FILE__, __LINE__);
+               fail("Unable to clone number_object in %s at line %d", __FILE__, __LINE__);
 
        o->value.i_value =  mojo_list_length(list->value.l_value);
 
diff --git a/number.c b/number.c
new file mode 100644 (file)
index 0000000..b68d4ee
--- /dev/null
+++ b/number.c
@@ -0,0 +1,32 @@
+#include "number.h"
+
+
+struct Object *num_plus(struct Object *parent, struct Object *n)
+{
+       if (parent == NULL || n == NULL || 
+           parent->type != T_NUMBER || n->type != T_NUMBER)
+               return NULL;
+
+       struct Object* ret = clone_object(parent);
+       if (ret == NULL)
+               fail("Unable to clone object at %s, %d", __FILE__, __LINE__);
+
+       ret->value.i_value += n->value.i_value;
+
+       return ret;
+}
+
+struct Object *num_minus(struct Object *parent, struct Object *n)
+{
+       if (parent == NULL || n == NULL || 
+           parent->type != T_NUMBER || n->type != T_NUMBER)
+               return NULL;
+
+       struct Object* ret = clone_object(parent);
+       if (ret == NULL)
+               fail("Unable to clone object at %s, %d", __FILE__, __LINE__);
+
+       ret->value.i_value -= n->value.i_value;
+       
+       return ret;
+}
diff --git a/number.h b/number.h
new file mode 100644 (file)
index 0000000..2817f34
--- /dev/null
+++ b/number.h
@@ -0,0 +1,9 @@
+#ifndef __NUMBER_H__
+#define __NUMBER_H__
+
+#include "object.h"
+
+struct Object *num_plus(struct Object *parent, struct Object *n);
+struct Object *num_minus(struct Object *parent, struct Object *n);
+
+#endif
index 5f3530cd12b2ceb00497aebffa8ecf5b1493c55e..9ac326bccf522cc673298a5e6a192d162e7652fc 100644 (file)
--- a/object.c
+++ b/object.c
@@ -41,7 +41,8 @@ struct Object* clone_object(struct Object *o)
        struct Object *met;
 
        int i;
-       for (i = 0; i < list_length(o->methods); i++) {
+       
+       for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
                met = (struct Object *) list_nth(o->methods, i);
                /* FIXME : Copy the method if it's a block,
                           to be able to free() later.
@@ -56,7 +57,7 @@ struct Object* clone_object(struct Object *o)
                break;
 
        case T_LIST:
-               for (i = 0; i < list_length(o); i++) {
+               for (i = 0; i < mojo_list_length(o->value.l_value); i++) {
                        struct Object *t = clone_object((struct Object *) list_nth(o, i));
                        
                        if (t == NULL)
@@ -108,5 +109,4 @@ void init_object_system(void)
        number_object->type = T_NUMBER;
        number_object->name = "Number";
        number_object->value.i_value = 0;
-
 }
index f9b541dbb4492f485b80e9f29edd9c7080bf5a0b..2aec958f13371f2cfa131eec294bf5d7d911d5c4 100644 (file)
--- a/object.h
+++ b/object.h
@@ -26,7 +26,7 @@ struct Object {
        enum o_types type;
 
        union {
-               int i_value;
+               signed int i_value;
                char c_value;
                struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2); 
                struct mojo_list *l_value;