From 004586d65bd20f877b033e518623a82666d8796e Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 21 Dec 2009 18:47:58 +0100 Subject: [PATCH] implemented a list structure --- Makefile | 3 ++- README | 7 +++++- list.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ list.h | 22 ++++++++++++++++++ object.c | 7 ++++++ object.h | 3 +++ stack.c | 28 ----------------------- stack.h | 18 --------------- 8 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 list.c create mode 100644 list.h delete mode 100644 stack.c delete mode 100644 stack.h diff --git a/Makefile b/Makefile index 2f35ebe..46db2e3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS=-c -g -OBJS=interp.o lex.yy.o main.o object.o +OBJS=interp.o lex.yy.o main.o object.o list.o mojo: $(OBJS) cc -o mojo $(OBJS) -lfl @@ -10,6 +10,7 @@ lex.yy.o: lexer.l object.o: object.h object.c interp.o: interp.c interp.h +list.o: list.c main.o: main.c clean: diff --git a/README b/README index 814b0a6..52d1477 100644 --- a/README +++ b/README @@ -1 +1,6 @@ -mojo is a smalltalk without smalltalk. \ No newline at end of file +mojo is a smalltalk without smalltalk. + +Coding conventions +================== + +All the names beginning with mojo_ are internal. \ No newline at end of file diff --git a/list.c b/list.c new file mode 100644 index 0000000..5f499cb --- /dev/null +++ b/list.c @@ -0,0 +1,68 @@ +#include "list.h" +#include "zalloc.h" + +struct mojo_list* list_init(void) +{ + struct mojo_list *l = zalloc(sizeof(struct mojo_list)); + TAILQ_INIT(&l->mojo_list_head); + + return l; +} + +void list_append(struct Object *list, struct Object *o) +{ + if (list == NULL || o == NULL || list->type != T_LIST) + return; + + struct list_elem *e = zalloc(sizeof(struct list_elem)); + e->obj = o; + + TAILQ_INSERT_TAIL(&list->mojo_list_head, e, mojo_lists); +} + +void list_remove(struct Object * list, struct Object *o) +{ + struct list_elem *e = TAILQ_LAST(&list->mojo_list_head, mojo_lists); + TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists); + + /* Also free the associated object : */ + free(e->obj); + free(e); +} + +struct Object* list_nth(struct Object *list, int n) +{ + if (list == NULL) + return NULL; + + struct Object *o = TAILQ_FIRST(&list->mojo_list_head); + int i = 0; + + for (i = 0; i < n && o != NULL; i++) { + o = TAILQ_NEXT(o); + } + + return o; +} + +struct Object* list_last(struct Object *list) +{ + struct list_elem *e = TAILQ_LAST(&mojo_list_head, lists); + return e->obj; +} + +/* + Here begins the public facing list api. + + */ + +struct Object* create_list(void) +{ + /* 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"; + +} diff --git a/list.h b/list.h new file mode 100644 index 0000000..24f89f4 --- /dev/null +++ b/list.h @@ -0,0 +1,22 @@ +#ifndef __LIST_H__ +#define __LIST_H__ + +#include "object.h" +#include "queue.h" + +struct mojo_list_elem { + Object *obj; + TAILQ_ENTRY(mojo_list_elem) mojo_lists; +}; + +struct mojo_list { + TAILQ_HEAD(Lists_head, mojo_list_elem) mojo_list_head; +}; + +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_nth(struct Object *list, int n); +struct Object* list_last(struct Object *list); + +#endif diff --git a/object.c b/object.c index d0e2313..8cbf920 100644 --- a/object.c +++ b/object.c @@ -135,4 +135,11 @@ void init_object_system(void) 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 b983550..0170e15 100644 --- a/object.h +++ b/object.h @@ -40,6 +40,7 @@ struct Object { union { int i_value; char c_value; + struct mojo_list *l_value; } value; TAILQ_HEAD(Methods_head, Method) methods_head; @@ -60,5 +61,7 @@ void init_object_system(void); struct Object *nil_object; struct Object *base_object; struct Object *number_object; +struct Object *list_object; + #endif diff --git a/stack.c b/stack.c deleted file mode 100644 index 903daf6..0000000 --- a/stack.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "stack.h" -#include "zalloc.h" - -void stack_push(struct Object *o) -{ - if (o == NULL) - return; - - struct stack_elem *e = zalloc(sizeof(struct stack_elem)); - e->obj = o; - - TAILQ_INSERT_TAIL(&stack_head, e, stacks); -} - -struct Object *stack_pop(void) -{ - struct stack_elem *e = TAILQ_LAST(&stack_head, stacks); - TAILQ_REMOVE(&stack_head, e, stacks); - - /* Also free the associated object : */ - free(e->obj); - free(e); -} -struct Object* stack_top(void) -{ - struct stack_elem *e = TAILQ_LAST(&stack_head, stacks); - return e->obj; -} diff --git a/stack.h b/stack.h deleted file mode 100644 index b7296fe..0000000 --- a/stack.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef __STACK_H__ -#define __STACK_H__ - -#include "object.h" -#include "queue.h" - -struct stack_elem { - Object *obj; - TAILQ_ENTRY(stack_elem) stacks; -}; - -TAILQ_HEAD(Stacks_head, stack_elem) stack_head; - -void stack_push(struct Object *o); -void stack_pop(struct Object *o); -struct Object* stack_top(void); - -#endif -- 2.43.0