From 0b28d767785117cf57c2324fddda60aaff7016d0 Mon Sep 17 00:00:00 2001 From: khamidou Date: Tue, 29 Dec 2009 16:52:03 +0100 Subject: [PATCH] implementing support for blocks --- README | 4 ++-- interp.c | 48 ++++++++++++------------------------------------ lexer.l | 3 ++- list.c | 26 +++++++++++++------------- object.c | 4 ++-- object.h | 8 +++++++- tokens.h | 1 + 7 files changed, 39 insertions(+), 55 deletions(-) diff --git a/README b/README index 26efeca..673aafd 100644 --- a/README +++ b/README @@ -24,8 +24,8 @@ x := 3; x + 3; # Test the value of x, and execute a block it is true - -x = 3 ifTrue: [ x y z | # doStuff... ] +# Note that a block doesn't get parameters, only +x = 3 ifTrue: [ # doStuff... ] Hacking ======= diff --git a/interp.c b/interp.c index a18ee1e..3f30b1a 100644 --- a/interp.c +++ b/interp.c @@ -5,50 +5,26 @@ extern int yylex(); extern char *yytext; -int eval_image(FILE *fp) +struct Object *apply_block(struct Object *o) { - 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 (o == NULL || o->type != T_BLOCK) + return NULL; - 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 i = 0; + for (i = 0; i < list_length(o); i++) { + + + } +} +int eval_image(FILE *fp) +{ + struct Object* stack = clone_object(list_object); 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; diff --git a/lexer.l b/lexer.l index bb03964..90b6ed6 100644 --- a/lexer.l +++ b/lexer.l @@ -7,7 +7,8 @@ %% -[0-9]+ { return NUMBER; } +[0-9]+ { return NUMBER; } +";" { return COMMA; } \"[:alnum:]+\" { return STRING; } [:alnum:]+ { return SYMBOL; } <> { return T_EOF; } diff --git a/list.c b/list.c index 3b33198..d442641 100644 --- a/list.c +++ b/list.c @@ -112,8 +112,8 @@ struct Object* create_list_object(void) 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; + met->name = "+"; + met->value.c_method = BUILTIN_CAST list_append; list_append(l1, met); met = zalloc(sizeof(struct Object)); @@ -122,8 +122,8 @@ struct Object* create_list_object(void) 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; + met->name = "remove"; + met->value.c_method = BUILTIN_CAST list_remove; list_append(l1, met); met = zalloc(sizeof(struct Object)); @@ -132,8 +132,8 @@ struct Object* create_list_object(void) 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; + met->name = "nth:"; + met->value.c_method = BUILTIN_CAST list_nth; list_append(l1, met); met = zalloc(sizeof(struct Object)); @@ -142,8 +142,8 @@ struct Object* create_list_object(void) 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; + met->name = "last"; + met->value.c_method = BUILTIN_CAST list_last; list_append(l1, met); list_object->methods = l1; @@ -153,7 +153,7 @@ struct Object* create_list_object(void) struct Object* list_append(struct Object *list, struct Object *o) { - if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + if (list == NULL || list->type != T_LIST || list->type != T_BLOCK || list->value.l_value == NULL) return nil_object; if (o == NULL) @@ -166,7 +166,7 @@ struct Object* list_append(struct Object *list, struct Object *o) struct Object* list_remove(struct Object *list) { - if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + if (list == NULL || list->type != T_LIST || list->type != T_BLOCK || list->value.l_value == NULL) return nil_object; mojo_list_remove(list->value.l_value); @@ -176,7 +176,7 @@ struct Object* list_remove(struct Object *list) struct Object* list_nth(struct Object *list, int n) { - if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + if (list == NULL || list->type != T_LIST || list->type != T_BLOCK || list->value.l_value == NULL) return nil_object; struct Object *o = mojo_list_nth(list->value.l_value, n); @@ -191,7 +191,7 @@ struct Object* list_nth(struct Object *list, int n) struct Object* list_last(struct Object *list) { - if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + if (list == NULL || list->type != T_LIST || list->type != T_BLOCK || list->value.l_value == NULL) return nil_object; struct Object *o = mojo_list_last(list->value.l_value); @@ -205,7 +205,7 @@ struct Object* list_last(struct Object *list) struct Object* list_length(struct Object *list) { - if (list == NULL || list->type != T_LIST || list->value.l_value == NULL) + if (list == NULL || list->type != T_LIST || list->type != T_BLOCK || list->value.l_value == NULL) return nil_object; struct Object *o = clone_object(number_object); diff --git a/object.c b/object.c index 55cd489..b96b50f 100644 --- a/object.c +++ b/object.c @@ -37,7 +37,7 @@ struct Object* clone_object(struct Object *o) 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(); + new->methods = (struct Object *) create_list_object(); struct Object *met; @@ -89,7 +89,7 @@ void init_object_system(void) /* We need to create the list object before everything else because it is needed for the method dictionnaries. */ - list_object = create_list_object(); + list_object = (struct Object *) create_list_object(); /* init nil object */ nil_object = new_object(); diff --git a/object.h b/object.h index b128a63..3490935 100644 --- a/object.h +++ b/object.h @@ -21,6 +21,10 @@ enum o_types { }; /* the base types */ +/* this cast is to silence some warnings sent by the compiler */ +#define BUILTIN_CAST (struct Object * (*)(struct Object *parent, struct Object *arg1,\ + struct Object *arg2, struct Object *arg3, struct Object *arg4)) + struct Object { struct Object *super; char *name; @@ -29,12 +33,14 @@ struct Object { union { signed int i_value; char c_value; - struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2); + struct Object * (*c_method)(struct Object *parent, struct Object *arg1, struct Object *arg2, + struct Object *arg3, struct Object *arg4); struct mojo_list *l_value; struct Object *m_block; } value; struct Object *methods; /* A list object */ + struct Object *symtab; /* The list of symbols defined at this point */ }; struct Object *lookup_method(struct Object *o, char *name); diff --git a/tokens.h b/tokens.h index 6830e00..380a10f 100644 --- a/tokens.h +++ b/tokens.h @@ -4,6 +4,7 @@ #define SYMBOL 0 #define NUMBER 1 #define STRING 2 +#define COMMA 3 #define T_EOF 99 #endif -- 2.43.0