]> khamidou — GitHub mirrors - mojo.git/commitdiff
dynamic method lookup works
authorkarim hamidou <khamidou@gmail.com>
Wed, 24 Apr 2013 08:42:16 +0000 (10:42 +0200)
committerkarim hamidou <khamidou@gmail.com>
Wed, 24 Apr 2013 08:42:16 +0000 (10:42 +0200)
interp.c
interp.h
lexer.l
number.c
object.h

index 70630bbe1ee42a6a2e58d4799a561e53b5002189..b966e158285ce046188d5e3160a7796e5d1e742d 100644 (file)
--- 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);
+    }
 }
index 79dd220d07e822ec5c50380be1e4520f8468c707..c5b6f8dee966f48d814843da9daafb9da266f3ad 100644 (file)
--- a/interp.h
+++ b/interp.h
@@ -4,5 +4,7 @@
 #include <stdio.h>
 
 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 ea93643d86cc7017ac949b086effabaec3a3b949..b8d746d776c967932788a583121e96fa3f4dd714 100644 (file)
--- 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;        }
 <<EOF>>                { return; }
 \%\{           { BEGIN(raw); }
 <raw>\%\}              { BEGIN(INITIAL); }
index d39e02423dc7ab6bcf161eec61b8b4d286ce7a5c..5ce2dc0e4cdb60e2457c5926faa32f2a24a823d0 100644 (file)
--- 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;
 
index f43450b1c260ccdb167cd36b3b464b2d4ded1e70..eef7eb71ffed1eb9a5f33ff29d85e847ebabed4f 100644 (file)
--- 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 */