From: khamidou Date: Sat, 26 Dec 2009 14:45:19 +0000 (+0100) Subject: fixed a bug in the definition of mojo_list_length X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=af85c2279927abb514b62c775459066acb90bf5e;p=mojo.git fixed a bug in the definition of mojo_list_length --- diff --git a/Makefile b/Makefile index 15a78fd..cf2fb33 100644 --- 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 d3e5342..8f7ee32 100644 --- 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 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 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 diff --git a/object.c b/object.c index 5f3530c..9ac326b 100644 --- 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; - } diff --git a/object.h b/object.h index f9b541d..2aec958 100644 --- 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;