diff --git a/tokenize.l b/tokenize.l index 28b20de..69a278a 100644 --- a/tokenize.l +++ b/tokenize.l @@ -3,6 +3,19 @@ * cc lex.yy.c -lfl */ %{ +#include +#include +#include +struct token; +struct token { + int type; + union { + char *str; + int num; + } value; + struct token *next; +}; + enum yytokentype { NUMBER = 258, LPAR = 259, @@ -12,6 +25,7 @@ enum yytokentype { EOL = 263 }; +struct token *make_token(int, int, char*); int yylval; char *yystr; %} @@ -20,13 +34,13 @@ char *yystr; "(" { return LPAR; } ")" { return RPAR; } [0-9]+ { yylval = atoi(yytext); return NUMBER; } +\n { return EOL; } [ \t\n] { /* ignore white space */ } -\".*\" { printf("str: %s\n", yytext); } -[a-zA-Z][a-zA-Z0-9]* { printf("sym: %s\n", yytext); } -. { printf("error: %s\n", yytext); } +\".*\" { return STR; } +[a-zA-Z][a-zA-Z0-9]* { return SYM; } +. { err(1, "invalid symbol: %s\n", yytext); } %% - /* * Goal: write LISP read-eval-print Loop and support * @ Function @@ -42,13 +56,59 @@ char *yystr; * (if ) */ +struct token* +make_token(int type, int num, char* str) +{ + struct token *t = (struct token*)calloc(1, sizeof(struct token)); + if (t == NULL) + err(1, "malloc failed"); + t->type = type; + t->next = NULL; + if (type == NUMBER) + t->value.num = num; + else if (type == SYM || type == STR) + t->value.str = strdup(str); + return t; +} + int main(void) { int tok; + struct token *t, *u = NULL, *start = NULL; while(tok = yylex()) { - printf("token: %d\n", tok); - if(tok == NUMBER) printf(" = %d\n", yylval); - //else printf("\n"); + //printf("token: %d, %d, %s\n", tok, yylval, yytext); + //printf("t=%p\n", t); + //if(t->type == NUMBER) printf(" = %d\n", t->value.num); + + t = make_token(tok, yylval, yytext); + + if (start == NULL) start = t; + if (u == NULL) u = t; + else u->next = t; + + if (t->type == EOL) { + /* eval(start) */ + struct token *x = start; + printf("eval this: %p\n", x); + while (x != NULL) { + printf("%d\n", x->type); + x = x->next; + } + start = NULL; + } } } + + +/* + * Local Variables: + * mode: c; + * eval: (message "main()") + * fill-column: 80 + * comment-column: 40 + * indent-tabs-mode: nil + * tab-width: 2 + * c-basic-offset: 2 + * End: + */