From deed6d8f9f4b93637c2dadb28ff27c9b026dcde8 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Thu, 25 Apr 2013 16:00:53 +0200 Subject: [PATCH] extending the interpreter to handle expressions --- interp.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/interp.c b/interp.c index b966e15..2fee6d0 100644 --- a/interp.c +++ b/interp.c @@ -79,7 +79,7 @@ void display_ast(struct Object *ast) { int i = 0; for(i = 0; i < length; i++) { struct Object *o = list_nth(ast, i); - printf("type: %d name: %s\n", o->type, o->name); + display_object(o); } } @@ -94,17 +94,31 @@ void execute(struct Object *ast) { 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); + int statement_length = mojo_list_length(statement_list->value.l_value); + int j = 0; - 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); - } + // returnValue holds the result of the evaluation of an expression + struct Object *returnValue = (struct Object*) list_nth(statement_list, statement_length - 1); + struct Object *destObject, *method; + for(j = statement_length - 2; j >= 0; j--) { + struct Object *obj = (struct Object*) list_nth(statement_list, j); + switch(obj->type) { + case T_MESSAGE: + if (j - 1 > 0) { // guard + destObject = (struct Object*) list_nth(statement_list, j - 1); + method = (struct Object*) lookup_method(destObject, obj->name); + if(method == nil_object) { + fail("error : method %s not found in object %s", obj->name, destObject->name); + } + + returnValue = method->value.c_method(destObject, returnValue, NULL, NULL, NULL); + } + + break; + } - struct Object *ret = method->value.c_method(destObject, arg1, NULL, NULL, NULL); - printf("value %d", ret->value.i_value); + + } + printf("value %d", returnValue->value.i_value); } } -- 2.43.0