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;
/* 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__);
list_append(l1, met);
met = zalloc(sizeof(struct Object));
+
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
list_append(l1, met);
met = zalloc(sizeof(struct Object));
+
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
list_append(l1, met);
met = zalloc(sizeof(struct Object));
+
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
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)
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);
--- /dev/null
+#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;
+}
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.
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)
number_object->type = T_NUMBER;
number_object->name = "Number";
number_object->value.i_value = 0;
-
}