From deea86d0590aa7db10e37042dc8a488b1a5208ff Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Tue, 30 Apr 2013 11:24:18 +0200 Subject: [PATCH] debugging memory corruption errors --- Makefile | 2 +- interp.c | 207 +++++++++++++++++++++++++++++++++++-------------------- interp.h | 9 ++- lexer.l | 2 +- list.c | 12 +++- list.h | 9 ++- main.c | 5 +- object.c | 57 +++++++++++---- object.h | 4 +- 9 files changed, 213 insertions(+), 94 deletions(-) diff --git a/Makefile b/Makefile index 9cb32f9..4564f90 100644 --- 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) diff --git a/interp.c b/interp.c index 3c2812b..12eb65d 100644 --- a/interp.c +++ b/interp.c @@ -10,24 +10,41 @@ * (: 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; } diff --git a/interp.h b/interp.h index 7c31517..c7fe52e 100644 --- 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 b8d746d..95c1d7b 100644 --- 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; } <> { return; } diff --git a/list.c b/list.c index 68964c7..aa0f9fa 100644 --- 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 fc6eb1a..e0fe170 100644 --- 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 ec20f31..d6d451e 100644 --- a/main.c +++ b/main.c @@ -1,11 +1,12 @@ #include #include "object.h" +#include "interp.h" -main() { +int main() { init_object_system(); puts("this is mojo."); compile_image(stdin); - + return 0; } diff --git a/object.c b/object.c index d98a0d7..b47d326 100644 --- a/object.c +++ b/object.c @@ -1,6 +1,8 @@ #include #include #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(); diff --git a/object.h b/object.h index 7d897f2..8217a63 100644 --- 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); -- 2.43.0