CFLAGS=-c -g
-OBJS=interp.o lex.yy.o main.o object.o list.o error.o
+OBJS=interp.o lex.yy.o main.o object.o list.o error.o number.o
mojo: $(OBJS)
cc -o mojo $(OBJS) -lfl
object.o: object.h object.c
interp.o: interp.c interp.h
list.o: list.c
+number.o: number.c number.h
main.o: main.c
error.o: error.h error.c
int eval_image(FILE *fp)
{
+ struct Object *o1 = clone_object(number_object);
+ struct Object *o2 = clone_object(number_object);
+
+ if (o1 == NULL || o2 == NULL)
+ fail("Unable to clone both objects");
+
+ o1->value.i_value = 2;
+ o2->value.i_value = 2;
+
+ struct Object *met = lookup_method(o1, "+");
+
+ if (met == NULL)
+ fail("Unable to lookup method");
+
+ struct Object *ret = met->value.c_method(o1, o2, NULL);
+
+ fail("valeur : %d", ret->value.i_value);
+
int r;
while((r = yylex()) != T_EOF) {
switch(r) {
case NUMBER:
{
+ puts("ho");
+ struct Object *o1 = clone_object(number_object);
+ struct Object *o2 = clone_object(number_object);
+
+ if (o1 == NULL || o2 == NULL)
+ fail("Unable to clone both objects");
+
+ printf("%s\n", yytext);
+ o1->value.i_value = atoi(yytext);
+ o2->value.i_value = 3;
+
+ struct Object *met = lookup_method(o1, "+");
+ printf("%s", met->name);
+ if (met == NULL)
+ fail("Unable to lookup method");
+
+ struct Object *ret = met->value.c_method(o1, o2, NULL);
+
+ printf("valeur : %d\n", ret->value.i_value);
}
default:
break;
/* 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));
+ struct Object * list_object = (struct Object *) zalloc(sizeof(struct Object));
if (list_object == NULL)
error("Unable to create list object");
/* Create a second list object that contains the methods of the list object */
struct Object *l1 = (struct Object *) zalloc(sizeof(struct Object));
+ struct Object *l2 = (struct Object *) zalloc(sizeof(struct Object));
- if (l1 == NULL)
+ if (l1 == NULL || l2 == 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 */
+ l1->methods = nil_object; /* the methods of l1 are in l2, which is an empty list. This way, we don't fall into
+ an infinite loop in clone_object()
+ */
struct Object *met = zalloc(sizeof(struct Object));
list_append(l1, met);
list_object->methods = l1;
+
+ return list_object;
}
struct Object* list_append(struct Object *list, struct Object *o)
#include "object.h"
main() {
+
init_object_system();
eval_image(stdin);
+
}
return ret;
}
+
+void create_number_object(void)
+{
+ /* init number object */
+ number_object = clone_object(base_object);
+ number_object->type = T_NUMBER;
+ number_object->name = "Number";
+ number_object->value.i_value = 0;
+
+ struct Object *plus_met = clone_object(builtin_object);
+ plus_met->name = "+";
+ plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1,
+ struct Object *arg2))num_plus;
+
+ list_append(number_object->methods, plus_met);
+}
struct Object *num_plus(struct Object *parent, struct Object *n);
struct Object *num_minus(struct Object *parent, struct Object *n);
+void create_number_object(void);
#endif
struct Object *met;
int i;
- for (i = 0; met != nil_object; i++) {
+ for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
met = (struct Object *) list_nth(o->methods, i);
if (strcmp(name, met->name) == 0)
return met;
new->super = o->super;
new->type = o->type;
new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */
+ new->methods = create_list_object();
struct Object *met;
/* We need to create the list object before everything else because it is
needed for the method dictionnaries.
*/
- create_list_object();
+ list_object = create_list_object();
/* init nil object */
nil_object = new_object();
nil_object->type = T_NIL;
nil_object->name = "nil";
-
/* init base object */
base_object = new_object();
base_object->super = nil_object;
base_object->type = T_OBJECT;
base_object->name = "Object";
-
- /* init number object */
- number_object = new_object();
- number_object->super = base_object;
- number_object->type = T_NUMBER;
- number_object->name = "Number";
- number_object->value.i_value = 0;
+
+ /* init builtin object */
+ builtin_object = new_object();
+ builtin_object->super = nil_object;
+ builtin_object->type = T_BUILTIN;
+ builtin_object->name = "Builtin";
+
+ create_number_object();
+
}
#define __MOJO_OBJECT__
#include "queue.h"
+#include "number.h"
#include "zalloc.h"
/*
struct Object *nil_object;
struct Object *base_object;
+struct Object *builtin_object;
struct Object *number_object;
struct Object *list_object;