From f5233162a38d59fbef92b671c8afc4e100da05d1 Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 18 Jan 2010 19:49:40 +0100 Subject: [PATCH] added a basic compiler --- Makefile | 5 ++- README | 2 +- compiler.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++- compiler.h | 8 +++++ lexer.l | 7 +++-- tests/basic.mj | 1 + tests/basic.mj~ | 2 ++ tokens.h | 1 + 8 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 tests/basic.mj create mode 100644 tests/basic.mj~ diff --git a/Makefile b/Makefile index e296a01..9fedc70 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CFLAGS=-c -g -OBJS=interp.o lex.yy.o main.o object.o list.o error.o number.o +OBJS=lex.yy.o object.o list.o error.o number.o compiler.o mojo: $(OBJS) cc -o mojo $(OBJS) -lfl @@ -9,10 +9,9 @@ lex.yy.o: lexer.l cc $(CFLAGS) lex.yy.c object.o: object.h object.c -interp.o: interp.c interp.h +compiler.o: compiler.c compiler.h list.o: list.c number.o: number.c number.h -main.o: main.c error.o: error.h error.c clean: diff --git a/README b/README index 673aafd..2336db3 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ mojo is a smalltalk without smalltalk. That is, without the cruft that smalltalk accumulated -in 30 years of existence, in the hope that it'd easier to grasp. +in 30 years of existence, in the hope that it'd be easier to grasp. Building diff --git a/compiler.c b/compiler.c index fea3e04..eb95962 100644 --- a/compiler.c +++ b/compiler.c @@ -6,12 +6,91 @@ */ +#include +#include #include "compiler.h" +#define SYMBOL_LENGTHS 255 + +extern char *yytext; +extern FILE *yyin; + +void symbol(); + int main(int argc, char **argv) { if (argc != 2) { fail("%s file", argv[0]); } - + + yyin = fopen(argv[1], "r"); + if (yyin == NULL) + fail("Unable to open %s", argv[1]); + + printf("%s", source_prologue); + + int r; + while((r = yylex()) != T_EOF) { + switch(r) { + case SYMBOL: + symbol(); + break; + + default: + break; + } + } + printf("%s", source_epilogue); +} + + +void symbol() +{ + char symname[SYMBOL_LENGTHS]; + strncpy(symname, yytext, SYMBOL_LENGTHS); + + int r = yylex(); + switch(r) { + case AFFECTATION: + printf("struct Object *%s = ", symname); + break; + + default: + /* method call, like Lexer yylex; */ + puts("{"); /* put it in a block, to not disrupt other declarations */ + + printf("struct Object *met = lookup_method(%s, %s);\n", symname, yytext); + printf("if (met != nil_object && met->type == T_BUILTIN)\n"); + printf(" met->c_method(%s,", symname); + + /* this is the most complex part of the algorithm : + A call to c_method takes four arguments - as we don't know how much arguments we're gonna get, + we add them on the fly until we get a comma, at which point we fill the remainder arguments + with NULLs. + */ + + int i = 0; + + while ((r = yylex()) && i < 4) { + switch(r) { + case COMMA: + break; + + default: + printf("%s,", yytext); + break; + } + i++; + } + + if (i < 4) { + while(i < 3) { + printf("NULL,"); + i++; + } + printf("NULL"); + } + printf(");\n"); + puts("}"); + } } diff --git a/compiler.h b/compiler.h index 51a48d4..eefa57a 100644 --- a/compiler.h +++ b/compiler.h @@ -4,5 +4,13 @@ #include "tokens.h" #include "object.h" +#define source_prologue "#include \n\ +#include \n\ +#include \"object.h\"\n\ +\n\ +int main(int argc, char **argv) {\n\ + struct Object *Lexer;\n" + +#define source_epilogue "}" #endif diff --git a/lexer.l b/lexer.l index 90b6ed6..67cf0dd 100644 --- a/lexer.l +++ b/lexer.l @@ -8,7 +8,8 @@ [0-9]+ { return NUMBER; } -";" { return COMMA; } +; { return COMMA; } +:= { return AFFECTATION; } \"[:alnum:]+\" { return STRING; } -[:alnum:]+ { return SYMBOL; } -<> { return T_EOF; } +[a-zA-Z][a-zA-Z0-9]* { return SYMBOL; } +<> { exit(0);} diff --git a/tests/basic.mj b/tests/basic.mj new file mode 100644 index 0000000..90b8f8d --- /dev/null +++ b/tests/basic.mj @@ -0,0 +1 @@ +Lexer yylex; diff --git a/tests/basic.mj~ b/tests/basic.mj~ new file mode 100644 index 0000000..e3196f6 --- /dev/null +++ b/tests/basic.mj~ @@ -0,0 +1,2 @@ +Lexer yylex; +Lexer with very long parameters; \ No newline at end of file diff --git a/tokens.h b/tokens.h index 380a10f..80ad8c9 100644 --- a/tokens.h +++ b/tokens.h @@ -5,6 +5,7 @@ #define NUMBER 1 #define STRING 2 #define COMMA 3 +#define AFFECTATION 4 #define T_EOF 99 #endif -- 2.43.0