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
interp.o: interp.c interp.h
list.o: list.c
main.o: main.c
+error.o: error.h error.c
clean:
rm *.o
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;
#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);
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 : */
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;
+}
#include "queue.h"
struct mojo_list_elem {
- Object *obj;
+ struct Object *obj;
TAILQ_ENTRY(mojo_list_elem) mojo_lists;
};
};
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
#include <string.h>
#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;
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;
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";
}
#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,
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;
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);