From 5df004c0a4fdbd071c6c993ac95de2b6cd9e7f2c Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Tue, 23 Apr 2013 13:35:05 +0200 Subject: [PATCH] refactoring code around an interpreter --- Makefile | 7 ++++--- interp.c | 62 +++++++++++++++++++++++++++++++++++++++++++++----------- interp.h | 2 +- lexer.l | 2 +- main.c | 2 +- object.c | 1 + object.h | 2 ++ tokens.h | 2 +- 8 files changed, 61 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 9fedc70..22788de 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS=-c -g -OBJS=lex.yy.o object.o list.o error.o number.o compiler.o +OBJS=lex.yy.o object.o list.o error.o number.o main.o interp.o mojo: $(OBJS) cc -o mojo $(OBJS) -lfl @@ -9,8 +9,9 @@ lex.yy.o: lexer.l cc $(CFLAGS) lex.yy.c object.o: object.h object.c -compiler.o: compiler.c compiler.h +main.o: main.c list.o: list.c +interp.o: interp.c number.o: number.c number.h error.o: error.h error.c @@ -19,4 +20,4 @@ clean: rm *~ count: - wc -l `ls *.[chl] -1 | grep -v lex.yy.c | grep -v queue` \ No newline at end of file + wc -l `ls *.[chl] -1 | grep -v lex.yy.c | grep -v queue` diff --git a/interp.c b/interp.c index 3f30b1a..94906c3 100644 --- a/interp.c +++ b/interp.c @@ -17,17 +17,55 @@ struct Object *apply_block(struct Object *o) } } -int eval_image(FILE *fp) +int compile_image(FILE *fp) { - struct Object* stack = clone_object(list_object); - int r; - while((r = yylex()) != T_EOF) { - switch(r) { - case NUMBER: - { - } - default: - break; - } - } + struct Object* ast = clone_object(list_object); + expression(ast); + display_ast(ast); +} + +int expression(struct Object *ast) { + + int r = yylex(); + switch(r) { + case NUMBER: + puts("number here bro"); + struct Object* obj = clone_object(number_object); + list_append(ast, obj); + return message(ast); + break; + default: + fail("Unexpected : %s", yytext); + break; + } +} + +int message(struct Object *ast) { + int r = yylex(); + switch(r) { + case SYMBOL: + printf("message : %s\n", yytext); + struct Object* obj = clone_object(base_object); + obj->type = T_MESSAGE; + obj->name = strdup(yytext); + list_append(ast, obj); + expression(ast); + break; + + case SEMICOLON: + return 0; + break; + + default: + fail("Expected symbol, got : %s", yytext); + break; + } + +} + +void display_ast(struct Object *ast) { + + int length = mojo_list_length(ast->value.l_value); + + printf("ast->value.lv: %x, ast len: %d\n", ast->value.l_value, length); } diff --git a/interp.h b/interp.h index d08aebd..79dd220 100644 --- a/interp.h +++ b/interp.h @@ -3,6 +3,6 @@ #include -int eval_image(FILE *fp); +int compile_image(FILE *fp); #endif diff --git a/lexer.l b/lexer.l index e346996..ea93643 100644 --- a/lexer.l +++ b/lexer.l @@ -9,7 +9,7 @@ [0-9]+ { return NUMBER; } -; { return COMMA; } +; { return SEMICOLON; } := { return AFFECTATION; } \"[:alnum:]+\" { return STRING; } [a-zA-Z][a-zA-Z0-9]* { return SYMBOL; } diff --git a/main.c b/main.c index fe26e47..5972ccf 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,6 @@ main() { init_object_system(); - eval_image(stdin); + compile_image(stdin); } diff --git a/object.c b/object.c index b96b50f..adc6e5e 100644 --- a/object.c +++ b/object.c @@ -58,6 +58,7 @@ struct Object* clone_object(struct Object *o) break; case T_LIST: + new->value.l_value = mojo_list_init(); for (i = 0; i < mojo_list_length(o->value.l_value); i++) { struct Object *t = clone_object((struct Object *) list_nth(o, i)); diff --git a/object.h b/object.h index 3490935..f43450b 100644 --- a/object.h +++ b/object.h @@ -1,6 +1,7 @@ #ifndef __MOJO_OBJECT__ #define __MOJO_OBJECT__ +#include #include "queue.h" #include "number.h" #include "zalloc.h" @@ -18,6 +19,7 @@ enum o_types { T_OBJECT, T_BUILTIN, T_BLOCK, + T_MESSAGE, }; /* the base types */ diff --git a/tokens.h b/tokens.h index 80ad8c9..e6bfefa 100644 --- a/tokens.h +++ b/tokens.h @@ -4,7 +4,7 @@ #define SYMBOL 0 #define NUMBER 1 #define STRING 2 -#define COMMA 3 +#define SEMICOLON 3 #define AFFECTATION 4 #define T_EOF 99 -- 2.43.0