From: khamidou Date: Fri, 25 Dec 2009 18:15:23 +0000 (+0100) Subject: a bunch of fixes - implementation of the list object, and creation of an error routine X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=961da5eba917a32aba71621cba6cf9e2319f2776;p=mojo.git a bunch of fixes - implementation of the list object, and creation of an error routine --- diff --git a/Makefile b/Makefile index 46db2e3..15a78fd 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS=-c -g -OBJS=interp.o lex.yy.o main.o object.o list.o +OBJS=interp.o lex.yy.o main.o object.o list.o error.o mojo: $(OBJS) cc -o mojo $(OBJS) -lfl @@ -12,6 +12,7 @@ object.o: object.h object.c interp.o: interp.c interp.h list.o: list.c main.o: main.c +error.o: error.h error.c clean: rm *.o diff --git a/interp.c b/interp.c index 52bb625..8fd0ef9 100644 --- a/interp.c +++ b/interp.c @@ -12,20 +12,6 @@ int eval_image(FILE *fp) switch(r) { case NUMBER: { - struct Object *o = clone_object(number_object); - o->value.i_value = atoi(yytext); - struct Object *no; - - if (yylex() == NUMBER) { - no = clone_object(number_object); - no->value.i_value = atoi(yytext); - } - - struct Method *met = lookup_method(o, "+"); - struct Object *reto = met->c_method(o, no, NULL); - - printf("%d\n", reto->value.i_value); - break; } default: break; diff --git a/list.c b/list.c index 5f499cb..d3e5342 100644 --- a/list.c +++ b/list.c @@ -1,7 +1,8 @@ #include "list.h" +#include "error.h" #include "zalloc.h" -struct mojo_list* list_init(void) +struct mojo_list* mojo_list_init(void) { struct mojo_list *l = zalloc(sizeof(struct mojo_list)); TAILQ_INIT(&l->mojo_list_head); @@ -9,20 +10,20 @@ struct mojo_list* list_init(void) return l; } -void list_append(struct Object *list, struct Object *o) +void mojo_list_append(struct mojo_list *list, struct Object *o) { - if (list == NULL || o == NULL || list->type != T_LIST) + if (list == NULL || o == NULL) return; - struct list_elem *e = zalloc(sizeof(struct list_elem)); + struct mojo_list_elem *e = zalloc(sizeof(struct mojo_list_elem)); e->obj = o; TAILQ_INSERT_TAIL(&list->mojo_list_head, e, mojo_lists); } -void list_remove(struct Object * list, struct Object *o) +void mojo_list_remove(struct mojo_list* list) { - struct list_elem *e = TAILQ_LAST(&list->mojo_list_head, mojo_lists); + struct mojo_list_elem *e = TAILQ_LAST(&list->mojo_list_head, Lists_head); TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists); /* Also free the associated object : */ @@ -30,39 +31,179 @@ void list_remove(struct Object * list, struct Object *o) free(e); } -struct Object* list_nth(struct Object *list, int n) +struct Object* mojo_list_nth(struct mojo_list *list, int n) { if (list == NULL) return NULL; - struct Object *o = TAILQ_FIRST(&list->mojo_list_head); + struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head); int i = 0; - for (i = 0; i < n && o != NULL; i++) { - o = TAILQ_NEXT(o); + for (i = 0; i < n && TAILQ_NEXT(e, mojo_lists) != NULL; i++) { + e = TAILQ_NEXT(e, mojo_lists); } - return o; + return e->obj; } -struct Object* list_last(struct Object *list) +struct Object* mojo_list_last(struct mojo_list *list) { - struct list_elem *e = TAILQ_LAST(&mojo_list_head, lists); + if (list == NULL) + return NULL; + + struct mojo_list_elem *e = TAILQ_LAST(&list->mojo_list_head, Lists_head); return e->obj; } +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); + int i = 0; + + for (i = 0; TAILQ_NEXT(e, mojo_lists) != NULL; i++) { + e = TAILQ_NEXT(e, mojo_lists); + } + + return i; + +} /* Here begins the public facing list api. */ -struct Object* create_list(void) +struct Object* create_list_object(void) +{ + /* init base object - this needs to be called only once*/ + /* 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)); + + if (list_object == NULL) + error("Unable to create list object"); + + list_object->super = nil_object; + list_object->type = T_LIST; + list_object->name = "List"; + list_object->value.l_value = mojo_list_init(); + + /* 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"); + + l1->super = nil_object; + l1->type = T_LIST; + l1->name = "List"; + l1->value.l_value = mojo_list_init(); + + struct Object *met = zalloc(sizeof(struct Object)); + if (met == NULL) + fail("Unable to create a method %d %d", __FILE__, __LINE__); + + met->type = T_BUILTIN; + met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, + struct Object *arg2))list_append; + list_append(l1, met); + + met = zalloc(sizeof(struct Object)); + if (met == NULL) + fail("Unable to create a method %d %d", __FILE__, __LINE__); + + met->type = T_BUILTIN; + met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, + struct Object *arg2))list_remove; + list_append(l1, met); + + met = zalloc(sizeof(struct Object)); + if (met == NULL) + fail("Unable to create a method %d %d", __FILE__, __LINE__); + + met->type = T_BUILTIN; + met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, + struct Object *arg2))list_nth; + list_append(l1, met); + + met = zalloc(sizeof(struct Object)); + if (met == NULL) + fail("Unable to create a method %d %d", __FILE__, __LINE__); + + met->type = T_BUILTIN; + met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, + struct Object *arg2))list_last; + list_append(l1, met); + + /* TODO: add the builtin methods to the list object */ +} + +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; + + if (o == NULL) + return NULL; + + mojo_list_append(list->value.l_value, o); + + return o; +} + +struct Object* list_remove(struct Object *list) { - /* init base object */ - struct Object *list_object - base_object = new_object(); - base_object->super = nil_object; - base_object->type = T_OBJECT; - base_object->name = "Object"; + if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + return nil_object; + + mojo_list_remove(list->value.l_value); + + return nil_object; +} + +struct Object* list_nth(struct Object *list, int n) +{ + if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + return nil_object; + + struct Object *o = mojo_list_nth(list->value.l_value, n); + + if (o != NULL) + return o; + else + return nil_object; } + + +struct Object* list_last(struct Object *list) +{ + if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + return nil_object; + + struct Object *o = mojo_list_last(list->value.l_value); + + if (o != NULL) + return o; + else + return nil_object; + +} + +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__); + + o->value.i_value = mojo_list_length(list->value.l_value); + + return o; +} diff --git a/list.h b/list.h index 24f89f4..fc6eb1a 100644 --- a/list.h +++ b/list.h @@ -5,7 +5,7 @@ #include "queue.h" struct mojo_list_elem { - Object *obj; + struct Object *obj; TAILQ_ENTRY(mojo_list_elem) mojo_lists; }; @@ -14,9 +14,10 @@ struct mojo_list { }; struct mojo_list* list_init(void); -void list_append(struct Object *list, struct Object *o); -void list_remove(struct Object *list, struct Object *o); +struct Object* list_append(struct Object *list, struct Object *o); +struct Object* list_remove(struct Object *list); struct Object* list_nth(struct Object *list, int n); struct Object* list_last(struct Object *list); +struct Object* create_list_object(void); #endif diff --git a/object.c b/object.c index 8cbf920..dddae1c 100644 --- a/object.c +++ b/object.c @@ -2,32 +2,33 @@ #include #include "object.h" -struct Method *lookup_method(struct Object *o, char *name) +struct Object* lookup_method(struct Object *o, char *name) { if (name == NULL || o == NULL) return NULL; - struct Method *m; - - TAILQ_FOREACH(m, &o->methods_head, methods) { - if (strcmp(name, m->name) == 0) - return m; + struct Object *met; + + int i; + for (i = 0; met != nil_object; i++) { + met = (struct Object *) list_nth(o->methods, i); + if (strcmp(name, met->name) == 0) + return met; } - return NULL; + return nil_object; } -struct Object *new_object(void) +struct Object* new_object(void) { struct Object *new = (struct Object *) zalloc(sizeof(struct Object)); - - TAILQ_INIT(&new->methods_head); + new->methods = clone_object(list_object); return new; } -struct Object *clone_object(struct Object *o) +struct Object* clone_object(struct Object *o) { if (o == NULL) return NULL; @@ -37,91 +38,47 @@ struct Object *clone_object(struct Object *o) new->type = o->type; new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */ - struct Method *m; - struct Method *new_method; - TAILQ_INIT(&new->methods_head); - TAILQ_FOREACH(m, &o->methods_head, methods) { - new_method = clone_method(m); - TAILQ_INSERT_TAIL(&new->methods_head, new_method, methods); - } + struct Object *met; - return new; -} + int i; + for (i = 0; i < list_length(o->methods); i++) { + met = (struct Object *) list_nth(o->methods, i); + /* FIXME : Copy the method if it's a block, + to be able to free() later. + */ -void free_object(struct Object *o) -{ - if (o == NULL) - return; - - free(o->name); - - struct Method *m; - TAILQ_FOREACH(m, &o->methods_head, methods) { - free_method(m); + list_append(new->methods, met); } -} - -struct Method *add_method(struct Object *o, struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2), - char *name) -{ - if (o == NULL) - return NULL; - - struct Method *new = (struct Method *) zalloc(sizeof(struct Method)); - new->name = strndup(name, 255); - new->c_method = c_method; - new->parent = o; - TAILQ_INSERT_TAIL(&o->methods_head, new, methods); return new; } -struct Method *clone_method(struct Method *m) -{ - if (m == NULL) - return NULL; - - struct Method *new = (struct Method *) zalloc(sizeof(struct Method)); - - new->name = strndup(m->name, 255); - new->c_method = m->c_method; - new->parent = m->parent; - - return new; -} - -void free_method(struct Method *m) +void free_object(struct Object *o) { - if (m == NULL) + if (o == NULL) return; - TAILQ_REMOVE(&m->parent->methods_head, m, methods); - free(m->name); - free(m); -} - -struct Object* Plus_method(struct Object *parent, struct Object *arg1, struct Object *arg2) -{ - if (parent == NULL || arg1 == NULL) - return NULL; - - if (arg1->type != T_NUMBER) - return nil_object; - struct Object *ret = clone_object(number_object); - ret->value.i_value = parent->value.i_value + arg1->value.i_value; + free(o->name); - return ret; + /* FIXME : free : the methods list, the methods and finally the object */ + free(o); } 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(); + /* init nil object */ nil_object = new_object(); nil_object->super = NULL; nil_object->type = T_NIL; nil_object->name = "nil"; + /* init base object */ base_object = new_object(); base_object->super = nil_object; @@ -134,12 +91,5 @@ void init_object_system(void) number_object->type = T_NUMBER; number_object->name = "Number"; number_object->value.i_value = 0; - add_method(number_object, &Plus_method, "+"); - - /* init list object */ - list_object = new_object(); - list_object->super = base_object; - list_object->type = T_LIST; - list_object->name = "List"; } diff --git a/object.h b/object.h index 0170e15..f9b541d 100644 --- a/object.h +++ b/object.h @@ -4,7 +4,10 @@ #include "queue.h" #include "zalloc.h" - +/* + The types : + Note that a block is actually a list that is gonna be interpreted in a different environment. +*/ enum o_types { T_NIL, T_BOOL, @@ -12,25 +15,10 @@ enum o_types { T_LIST, T_STRING, T_OBJECT, + T_BUILTIN, T_BLOCK, -}; /* the base types */ -/* - A method is actually a function pointer. - What about methods defined in mojo ? - for instance, 3 square ? - where square is defined as : : square -> self * self; - On a un handler par défaut qui appelle les sous-methodes et retourne le résultat. -*/ - -struct Method { - char *name; - struct Object *parent; - struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2); - - TAILQ_ENTRY(Method) methods; - -}; +}; /* the base types */ struct Object { struct Object *super; @@ -40,13 +28,15 @@ struct Object { union { int i_value; char c_value; + struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2); struct mojo_list *l_value; + struct Object *m_block; } value; - TAILQ_HEAD(Methods_head, Method) methods_head; + struct Object *methods; /* A list object */ }; -struct Method *lookup_method(struct Object *o, char *name); +struct Object *lookup_method(struct Object *o, char *name); struct Object *new_object(void); struct Object *clone_object(struct Object *o); void free_object(struct Object *o);