]> khamidou — GitHub mirrors - mojo.git/commitdiff
implemented a list structure
authorkhamidou <khamidou@gmail.com>
Mon, 21 Dec 2009 17:47:58 +0000 (18:47 +0100)
committerkhamidou <khamidou@gmail.com>
Mon, 21 Dec 2009 17:47:58 +0000 (18:47 +0100)
Makefile
README
list.c [new file with mode: 0644]
list.h [new file with mode: 0644]
object.c
object.h
stack.c [deleted file]
stack.h [deleted file]

index 2f35ebe2508d5fa98775dd83db58c953aabbfe16..46db2e35200bd3b863e0275f28e6c858804861bf 100644 (file)
--- 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 814b0a65464eb6afe9762b68a9533d95bc7393b4..52d147757acd1af8c67a4e8d3b8f701f108b8aa9 100644 (file)
--- 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 (file)
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 (file)
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
index d0e2313838e1bb5d620b0ca224a9a64e0c74c9e5..8cbf920d3cc03185e81a9f2c0bc384ed7d2e1879 100644 (file)
--- 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";
+
 }
index b9835504afe59a2a3d0f8cc29422d1cdab1844e8..0170e1548a372dbde7843e09c80b37c0fefe6b71 100644 (file)
--- 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 (file)
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 (file)
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