From f41acc46750fe101941235b407aafc6ed39052c3 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Wed, 24 Apr 2013 10:42:16 +0200 Subject: [PATCH] dynamic method lookup works --- interp.c | 36 +++++++++++++++++++++++++++++++++--- interp.h | 2 ++ lexer.l | 2 +- number.c | 2 +- object.h | 1 + 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/interp.c b/interp.c index 70630bb..b966e15 100644 --- a/interp.c +++ b/interp.c @@ -1,6 +1,6 @@ +#include "object.h" #include "interp.h" #include "tokens.h" -#include "object.h" extern int yylex(); extern char *yytext; @@ -20,8 +20,11 @@ struct Object *apply_block(struct Object *o) int compile_image(FILE *fp) { struct Object* ast = clone_object(list_object); - expression(ast); - display_ast(ast); + struct Object* first_leaf = clone_object(list_object); + list_append(ast, first_leaf); + + expression(first_leaf); + execute(ast); } int expression(struct Object *ast) { @@ -31,6 +34,7 @@ int expression(struct Object *ast) { switch(r) { case NUMBER: obj = clone_object(number_object); + obj->value.i_value = atoi(yytext); list_append(ast, obj); return message(ast); break; @@ -53,6 +57,10 @@ int message(struct Object *ast) { break; case SEMICOLON: + obj = clone_object(base_object); + obj->type = T_SEPARATOR; + obj->name = "Separator"; + list_append(ast, obj); return 0; break; @@ -75,6 +83,28 @@ void display_ast(struct Object *ast) { } } +void display_object(struct Object *obj) { + printf("type: %d name: %s\n", obj->type, obj->name); +} + void execute(struct Object *ast) { + int length = mojo_list_length(ast->value.l_value); + + int i = 0; + for(i = 0; i < length; i++) { + struct Object *statement_list = (struct Object*) list_nth(ast, i); + + int length = mojo_list_length(statement_list->value.l_value); + struct Object *destObject = (struct Object*) list_nth(statement_list, 0); + struct Object *message = (struct Object*) list_nth(statement_list, 1); + struct Object *arg1 = (struct Object*) list_nth(statement_list, 2); + + struct Object *method = (struct Object*) lookup_method(destObject, message->name); + if(method == nil_object) { + fail("error : method %s not found in object %s", message->name, destObject->name); + } + struct Object *ret = method->value.c_method(destObject, arg1, NULL, NULL, NULL); + printf("value %d", ret->value.i_value); + } } diff --git a/interp.h b/interp.h index 79dd220..c5b6f8d 100644 --- a/interp.h +++ b/interp.h @@ -4,5 +4,7 @@ #include int compile_image(FILE *fp); +void display_object(struct Object *obj); +void execute(struct Object *ast); #endif diff --git a/lexer.l b/lexer.l index ea93643..b8d746d 100644 --- a/lexer.l +++ b/lexer.l @@ -12,7 +12,7 @@ ; { return SEMICOLON; } := { return AFFECTATION; } \"[:alnum:]+\" { return STRING; } -[a-zA-Z][a-zA-Z0-9]* { return SYMBOL; } +[a-zA-Z\+\-][a-zA-Z0-9]* { return SYMBOL; } <> { return; } \%\{ { BEGIN(raw); } \%\} { BEGIN(INITIAL); } diff --git a/number.c b/number.c index d39e024..5ce2dc0 100644 --- a/number.c +++ b/number.c @@ -40,7 +40,7 @@ void create_number_object(void) number_object->value.i_value = 0; struct Object *plus_met = clone_object(builtin_object); - plus_met->name = "+"; + plus_met->name = "plus"; plus_met->value.c_method = (struct Object * (*)(struct Object *parent, struct Object *arg1, struct Object *arg2))num_plus; diff --git a/object.h b/object.h index f43450b..eef7eb7 100644 --- a/object.h +++ b/object.h @@ -20,6 +20,7 @@ enum o_types { T_BUILTIN, T_BLOCK, T_MESSAGE, + T_SEPARATOR, /* ugly - used by the interpretor to separate statements in the AST */ }; /* the base types */ -- 2.43.0