]> khamidou — GitHub mirrors - mojo.git/commitdiff
implementing support for blocks
authorkhamidou <khamidou@gmail.com>
Tue, 29 Dec 2009 15:52:03 +0000 (16:52 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 29 Dec 2009 15:52:03 +0000 (16:52 +0100)
README
interp.c
lexer.l
list.c
object.c
object.h
tokens.h

diff --git a/README b/README
index 26efecaf7edd24b652bf7d20494bcfad299b556f..673aafdefaae8983ac1b8aeeb747acd0159fdb1d 100644 (file)
--- 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
 =======
index a18ee1e3117166a28001d6b52ac7a1da4205fe0e..3f30b1a54c4dca7ee49e12eb9e82074358ac2ffa 100644 (file)
--- 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 bb039646e8ffc9236f5ac0da3f186b949266bf3e..90b6ed6393cba000c50c95e007f033bf529ca142 100644 (file)
--- 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;        }
 <<EOF>>                { return T_EOF;         }
diff --git a/list.c b/list.c
index 3b3319830487970762912093f2b9cb4bc6deefab..d44264117b19aef0a8f33aff2948c22e46189338 100644 (file)
--- 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);
index 55cd489c0664ee75fcdaca7e4cfbd7b7dc21f4c3..b96b50fbdfde3388f415db57b2743e72f35f12e3 100644 (file)
--- 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();
index b128a6330d029d19352dcd54a5790c3fe46e09dc..34909358a6e878da70bacfb4ff042a949ef7d818 100644 (file)
--- 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);
index 6830e00b40082d32f8d10825bd92b67e93d9ef4c..380a10fd54ac56e357632190ad2f5505c9874983 100644 (file)
--- 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