]> khamidou — GitHub mirrors - mojo.git/commitdiff
debugging memory corruption errors
authorkarim hamidou <khamidou@gmail.com>
Tue, 30 Apr 2013 09:24:18 +0000 (11:24 +0200)
committerkarim hamidou <khamidou@gmail.com>
Tue, 30 Apr 2013 09:24:18 +0000 (11:24 +0200)
Makefile
interp.c
interp.h
lexer.l
list.c
list.h
main.c
object.c
object.h

index 9cb32f92c561694ea4e23e431d29b7812039361d..4564f90c95a2b9c6b2ed2865d806aaa1d63742ea 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-CFLAGS=-c -g -Wparentheses
+CFLAGS=-c -g -Wall
 OBJS=lex.yy.o object.o list.o error.o number.o main.o interp.o
 
 mojo: $(OBJS)
index 3c2812bd538fecc6d64fb828df2967bf4b6a34b2..12eb65d64dc89b32a30853d465e43c95457c7df2 100644 (file)
--- a/interp.c
+++ b/interp.c
  *              (: setPosx 3)))
  */
 
+
 #include "object.h"
 #include "interp.h"
 #include "tokens.h"
+#include "list.h"
+#include "error.h"
 
 extern int yylex();
+extern int yyleng;
+extern FILE *yyin;
 extern char *yytext;
 
 int compile_image(FILE *fp)
 {
-       struct Object* ast = clone_object(list_object);
-    
-    expression(ast);
-    display_ast(ast, 0);
-    puts(">>> AST <<<");
+   
+    while(!feof(fp)) {
+        struct Object* ast = clone_object(list_object);
+        struct Object* scope = clone_object(list_object);
+     
+        expression(ast);
+        display_ast(ast, 0);
+        execute_branch(ast, scope);
+        free_object(ast);
+    }
+
+    return 0;
+}
 
-    struct Object *returnValue = execute_branch(ast);
-    printf("Value : %d", returnValue->value.i_value);
+/*
+void unput_token(void) {
+   int i;
+   for (i = yyleng - 1; i >= 0; --i )
+           unput(yytext[i]); 
 }
+*/
 
 int expression(struct Object *ast) {
 
@@ -41,11 +58,22 @@ int expression(struct Object *ast) {
             params = clone_object(list_object);
             list_append(params, obj);
             list_append(ast, params);
-
             return message(params);
             break;
+
+        case SYMBOL:
+            obj = clone_object(base_object);
+            obj->type = T_SYMBOL;
+            obj->name = strdup(yytext);
+            params = clone_object(list_object);
+            list_append(params, obj);
+            list_append(ast, params);
+            return message(params);
+
+            break; 
         default:
             fail("Unexpected : %s", yytext);
+            return -1;
             break;
     }
 }
@@ -54,24 +82,20 @@ int message(struct Object *ast) {
     int r = yylex();
     struct Object *obj;
 
-    switch(r) {
-        case SYMBOL:
+    if (r == SYMBOL) {
             obj = clone_object(base_object);
             obj->type = T_MESSAGE;
             obj->name = strdup(yytext);
             list_append(ast, obj);
             expression(ast);
-            break;
-
-        case SEMICOLON:
+    } else if (r == SEMICOLON) {
             return 0;
-            break;
-
-        default:
+    } else {
             fail("Expected symbol, got : %s", yytext);
-            break;
+            return -1;
     }
 
+    return -1;
 }
 
 void display_ast(struct Object *ast, int nindents) {
@@ -92,65 +116,82 @@ void display_ast(struct Object *ast, int nindents) {
     }
 }
 
+const char *type_to_string(enum o_types e)
+{
+    switch(e) {
+        case T_NIL:
+            return "Nil";
+            break;
+
+        case T_BOOL:
+            return "Boolean";
+            break;
+
+        case T_NUMBER:
+            return "Number";
+            break;
+
+        case T_SYMBOL:
+            return "Symbol";
+            break;
+
+        case T_LIST:
+            return "List";
+            break;
+
+        case T_STRING:
+            return "String";
+            break;
+
+        case T_OBJECT:
+            return "Object";
+            break;
+
+        case T_BUILTIN:
+            return "Builtin function";
+            break;
+
+        case T_BLOCK:
+            return "Block";
+            break;
+
+        case T_MESSAGE:
+            return "Message";
+            break;
+
+        case T_SEPARATOR:
+            return "Separator";
+            break;
+
+        default:
+            return "Unknkown object";
+            break;
+    }
+}
+
 void display_object(struct Object *obj, int nindents) {
 
         int i = 0;
         for (i = 0; i < nindents; i++)
             putc(' ', stdout);
 
-        printf("class: %s\n", obj->name);
+        printf("class: %s, type: %s\n", obj->name, type_to_string(obj->type));
 
         if(obj->type == T_LIST) {
             display_ast(obj, nindents + 4);
         }
 }
 
-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 statement_length = mojo_list_length(statement_list->value.l_value);
-        int j = 0;
-        
-        // 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;
-            }
-
-
-        }
-       printf("value %d", returnValue->value.i_value);
-    }
-}
-
-struct Object* execute_branch(struct Object *ast) {
+struct Object* execute_branch(struct Object *ast, struct Object *scope) {
     if (ast == NULL || ast->type != T_LIST) 
-        return;
+        return NULL;
 
 
        struct Object* stack = clone_object(list_object);
 
     int length = mojo_list_length(ast->value.l_value);
     if (length == 0)
-        return;
+        return NULL;
 
 
     // first, execute all the sub-expressions
@@ -161,7 +202,7 @@ struct Object* execute_branch(struct Object *ast) {
                 continue;
 
             if(obj->type == T_LIST) {
-                list_append(stack, execute_branch(obj));
+                list_append(stack, execute_branch(obj, scope));
             } else {
                 list_append(stack, obj);
             }
@@ -177,26 +218,46 @@ struct Object* execute_branch(struct Object *ast) {
     } else if (length > 1) {
         struct Object *destObject = list_nth(stack, 0);
         struct Object *methodName = list_nth(stack, 1);
-
-        struct Object *method = (struct Object*) lookup_method(destObject, methodName->name);
-        if(method == nil_object) {
-                        fail("error : method %s not found in object %s", methodName->name, destObject->name);
-        }
+    
+        
+        if(destObject->type == T_SYMBOL && strcmp(methodName->name, ":=") == 0) {
+            // this is an assignment
+            puts("assignment");
+            struct Object *value = list_nth(stack, 2);
+            struct Object* var = clone_object(base_object);
+            var->type = T_SYMBOL;
+            var->name = strdup(destObject->name);
+            var->value.v_value = clone_object(value);
+            list_append(scope->symtab, var); 
+
+        } else {
+            // this is a regular method call
+            struct Object *method = (struct Object*) lookup_method(destObject, methodName->name);
+            if(method == nil_object) {
+                            fail("error : method %s not found in object %s", methodName->name, destObject->name);
+            }
 
 
-        /* we use the fact that if list_nth doesn't find an
-         * object because it doesn't exists at the index, it returns nil
-         */ 
-        struct Object *param1 = list_nth(stack, 2), 
-                      *param2 = list_nth(stack, 3),
-                      *param3 = list_nth(stack, 4), 
-                      *param4 = list_nth(stack, 5);
+            /* we use the fact that if list_nth doesn't find an
+             * object because it doesn't exists at the index, it returns nil
+             */ 
+            struct Object *param1 = list_nth(stack, 2), 
+                          *param2 = list_nth(stack, 3),
+                          *param3 = list_nth(stack, 4), 
+                          *param4 = list_nth(stack, 5);
+            if (param1->type == T_SYMBOL) {
+                struct Object *o = lookup_variable(scope, param1->name);
+                if (o != nil_object)
+                    param1 = o;
+            }
+                
 
-        destObject = method->value.c_method(destObject, param1, param2, param3, param4);
+            destObject = method->value.c_method(destObject, param1, param2, param3, param4);
+        }
 
-        // free the stack and the ast
-        free_object(ast);
+        puts("freeing stuff");
         free_object(stack);
+
         return destObject;
     }
 
index 7c31517c78621b4e6db0dbcda4d9a6f75ed1b6d4..c7fe52e9aa2b64f1ad0e6c48f731d1f08b02cf20 100644 (file)
--- a/interp.h
+++ b/interp.h
@@ -6,6 +6,13 @@
 int compile_image(FILE *fp);
 void display_object(struct Object *obj, int nindents);
 void execute(struct Object *ast);
-struct Object* execute_branch(struct Object *ast);
+struct Object* execute_branch(struct Object *ast, struct Object *scope);
+
+
+int expression(struct Object *ast);
+int message(struct Object *ast);
+
+void display_ast(struct Object *ast, int nindents);
+    
 
 #endif
diff --git a/lexer.l b/lexer.l
index b8d746d776c967932788a583121e96fa3f4dd714..95c1d7b1013f9a1ef6219a2a82fd6661e332df9a 100644 (file)
--- a/lexer.l
+++ b/lexer.l
@@ -10,7 +10,7 @@
 
 [0-9]+         { return NUMBER;        }
 ;              { return SEMICOLON;             }
-:=             { return AFFECTATION;   }
+:=             { return SYMBOL;   }
 \"[:alnum:]+\"  { return STRING;       }
 [a-zA-Z\+\-][a-zA-Z0-9]*       { return SYMBOL;        }
 <<EOF>>                { return; }
diff --git a/list.c b/list.c
index 68964c7c9691973f6eafbc5fc29de3d5cceadbed..aa0f9fab2bd7bd8e9c278c8ab42417c0ab444747 100644 (file)
--- a/list.c
+++ b/list.c
@@ -76,10 +76,18 @@ void mojo_list_free(struct mojo_list *list) {
        if (list == NULL)
                return;
 
+    //printf("len : %d\n", mojo_list_length(list));
+    /*
+    while(mojo_list_length(list) > 0)
+        mojo_list_remove(list);
+
+    free(list);
+    */
     struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
 
        TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) {
            TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists);
+        //display_object(e->obj, 0);
         free_object(e->obj);
         free(e);
        }
@@ -99,7 +107,7 @@ struct Object* create_list_object(void)
        struct Object * list_object = (struct Object *) zalloc(sizeof(struct Object));
 
        if (list_object == NULL)
-               error("Unable to create list object");
+               fail("Unable to create list object");
 
        list_object->super = nil_object;
        list_object->type = T_LIST;
@@ -111,7 +119,7 @@ struct Object* create_list_object(void)
        struct Object *l2 = (struct Object *) zalloc(sizeof(struct Object));
 
        if (l1 == NULL || l2 == NULL)
-               error("Unable to create list object at %s %d", __FILE__, __LINE__);
+               fail("Unable to create list object at %s %d", __FILE__, __LINE__);
 
        l1->super = nil_object;
        l1->type = T_LIST;
diff --git a/list.h b/list.h
index fc6eb1afc747074d41127e05d1c7b20c8299f105..e0fe170e7d9e77390c9de57110f0db9ee486687b 100644 (file)
--- a/list.h
+++ b/list.h
@@ -13,7 +13,14 @@ struct mojo_list {
        TAILQ_HEAD(Lists_head, mojo_list_elem) mojo_list_head;
 };
 
-struct mojo_list* list_init(void);
+struct mojo_list* mojo_list_init(void);
+int mojo_list_length(struct mojo_list *list);
+void mojo_list_append(struct mojo_list *list, struct Object *o);
+void mojo_list_remove(struct mojo_list* list);
+struct Object* mojo_list_nth(struct mojo_list *list, int n);
+struct Object* mojo_list_last(struct mojo_list *list);
+void mojo_list_free(struct mojo_list *list);
+
 struct Object* list_append(struct Object *list, struct Object *o);
 struct Object* list_remove(struct Object *list);
 struct Object* list_nth(struct Object *list, int n);
diff --git a/main.c b/main.c
index ec20f31d19b1ae5fd2bc0d2bb734a6c8d6b324dd..d6d451eead74205706438a2098d4ee108410b8ab 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,11 +1,12 @@
 #include <stdio.h>
 
 #include "object.h"
+#include "interp.h"
 
-main() {
+int main() {
 
        init_object_system();
     puts("this is mojo.");
        compile_image(stdin);
-
+    return 0;
 }
index d98a0d7a488a217519c75675f821196a2ae2b941..b47d326cbf905d0a2c461e6324f2c1b525580aa1 100644 (file)
--- a/object.c
+++ b/object.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include <string.h>
 #include "object.h"
+#include "list.h"
+#include "error.h"
 
 struct Object* lookup_method(struct Object *o, char *name)
 {
@@ -10,10 +12,28 @@ struct Object* lookup_method(struct Object *o, char *name)
        struct Object *met;
        
        int i;
-       for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
-               met = (struct Object *) list_nth(o->methods, i);
-               if (strcmp(name, met->name) == 0)
-                       return met;
+    if (o->methods != NULL && o->methods->value.l_value != NULL)
+        for (i = 0; i < mojo_list_length(o->methods->value.l_value); i++) {
+            met = (struct Object *) list_nth(o->methods, i);
+            if (strcmp(name, met->name) == 0)
+                return met;
+        }
+
+       return nil_object;
+}
+
+struct Object* lookup_variable(struct Object *scope, char *name)
+{
+       if (name == NULL || scope == NULL)
+               return NULL;
+
+       struct Object *var;
+       
+       int i;
+       for (i = 0; i < mojo_list_length(scope->symtab->value.l_value); i++) {
+               var = (struct Object *) list_nth(scope->symtab, i);
+               if (strcmp(name, var->name) == 0)
+                       return var;
        }
 
        return nil_object;
@@ -38,6 +58,7 @@ struct Object* clone_object(struct Object *o)
        new->type = o->type;
        new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */
        new->methods = (struct Object *) create_list_object();
+       new->symtab = (struct Object *) create_list_object();
 
        struct Object *met;
 
@@ -69,21 +90,29 @@ struct Object* clone_object(struct Object *o)
                }
                break;
 
-               
+       default:
+        break; 
        }
        return new;
 }
 
 void free_object(struct Object *o)
 {
-       if (o == NULL || o == nil_object || o == base_object || o == number_object)
+       if (o == NULL || o == nil_object || o == base_object || o == number_object || o == list_object)
                return;
 
+    /*
+    if (o->type == T_BUILTIN)
+        // don't free builtin methods
+        return;
+    */
     /*
      * FIXME: use our own lib for strings and don't use
      * BSS strings which can't be freed.
-       free(o->name);
     */
+
+       free(o->name);
+
     if (o->methods != NULL)
         free_object(o->methods);
 
@@ -97,6 +126,14 @@ void free_object(struct Object *o)
        free(o);
 }
 
+void create_base_object(void) {
+       /* init base object */
+       base_object = new_object();
+       base_object->super = nil_object;
+       base_object->type = T_OBJECT;
+       base_object->name = "Object";
+}
+
 void init_object_system(void)
 {
        /* We need to create the list object before everything else because it is
@@ -110,11 +147,7 @@ void init_object_system(void)
        nil_object->type = T_NIL;
        nil_object->name = "nil";
 
-       /* init base object */
-       base_object = new_object();
-       base_object->super = nil_object;
-       base_object->type = T_OBJECT;
-       base_object->name = "Object";
+    create_base_object();
 
        /* init builtin object */
        builtin_object = new_object();
index 7d897f2d323e0e56d44a3746371c508d82f9d320..8217a63cd3e3e5819f045ab142344d2aa35f0083 100644 (file)
--- a/object.h
+++ b/object.h
@@ -14,6 +14,7 @@ enum o_types {
        T_NIL,
        T_BOOL,
        T_NUMBER,
+    T_SYMBOL,
        T_LIST,
        T_STRING,
        T_OBJECT, 
@@ -41,7 +42,7 @@ struct Object {
                struct mojo_list *l_value;
         struct mojo_string *s_value;
                struct Object *m_block;
-        struct Object *v_value; /* the value pointed to by a variable */
+        struct Object *v_value; /* the value pointed to by a symbol */
        } value;
 
        struct Object *methods; /* A list object */
@@ -49,6 +50,7 @@ struct Object {
 };
 
 struct Object *lookup_method(struct Object *o, char *name);
+struct Object* lookup_variable(struct Object *scope, char *name);
 struct Object *new_object(void);
 struct Object *clone_object(struct Object *o);
 void free_object(struct Object *o);