]> khamidou — GitHub mirrors - mojo.git/commitdiff
added a basic compiler
authorkhamidou <khamidou@gmail.com>
Mon, 18 Jan 2010 18:49:40 +0000 (19:49 +0100)
committerkhamidou <khamidou@gmail.com>
Mon, 18 Jan 2010 18:49:40 +0000 (19:49 +0100)
Makefile
README
compiler.c
compiler.h
lexer.l
tests/basic.mj [new file with mode: 0644]
tests/basic.mj~ [new file with mode: 0644]
tokens.h

index e296a01097f662625f16fff09fb1278b32c3516b..9fedc703f8d40b1b8fe81a45b94dc87b25bea54a 100644 (file)
--- 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 673aafdefaae8983ac1b8aeeb747acd0159fdb1d..2336db31612d57f9c1e1e62f8d857d8f42a83ccc 100644 (file)
--- 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
index fea3e04e198db135935d867d770e31a3e5ca938f..eb959625a68748662b6a8edafc2fe56a2afa24ac 100644 (file)
@@ -6,12 +6,91 @@
   
 */
 
+#include <stdio.h>
+#include <string.h>
 #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("}");
+       }
 }
index 51a48d4bb58ef92fa36e9c64a90f1db8627a9f30..eefa57ad9d7315e5fb8b479f326ccaa3e38ec517 100644 (file)
@@ -4,5 +4,13 @@
 #include "tokens.h"
 #include "object.h"
 
+#define source_prologue "#include <stdio.h>\n\
+#include <stdlib.h>\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 90b6ed6393cba000c50c95e007f033bf529ca142..67cf0ddf15fc79852ad2ed72543ca9281ef67d55 100644 (file)
--- 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;        }
-<<EOF>>                { return T_EOF;         }
+[a-zA-Z][a-zA-Z0-9]*   { return SYMBOL;        }
+<<EOF>>                { exit(0);}
diff --git a/tests/basic.mj b/tests/basic.mj
new file mode 100644 (file)
index 0000000..90b8f8d
--- /dev/null
@@ -0,0 +1 @@
+Lexer yylex;
diff --git a/tests/basic.mj~ b/tests/basic.mj~
new file mode 100644 (file)
index 0000000..e3196f6
--- /dev/null
@@ -0,0 +1,2 @@
+Lexer yylex;
+Lexer with very long parameters;
\ No newline at end of file
index 380a10fd54ac56e357632190ad2f5505c9874983..80ad8c92f9f4118a82a3fa01e1b68281f43a475a 100644 (file)
--- 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