work on structure
This commit is contained in:
parent
8b790839df
commit
dab413018e
74
tokenize.l
74
tokenize.l
|
@ -3,6 +3,19 @@
|
||||||
* cc lex.yy.c -lfl
|
* cc lex.yy.c -lfl
|
||||||
*/
|
*/
|
||||||
%{
|
%{
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <err.h>
|
||||||
|
struct token;
|
||||||
|
struct token {
|
||||||
|
int type;
|
||||||
|
union {
|
||||||
|
char *str;
|
||||||
|
int num;
|
||||||
|
} value;
|
||||||
|
struct token *next;
|
||||||
|
};
|
||||||
|
|
||||||
enum yytokentype {
|
enum yytokentype {
|
||||||
NUMBER = 258,
|
NUMBER = 258,
|
||||||
LPAR = 259,
|
LPAR = 259,
|
||||||
|
@ -12,6 +25,7 @@ enum yytokentype {
|
||||||
EOL = 263
|
EOL = 263
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct token *make_token(int, int, char*);
|
||||||
int yylval;
|
int yylval;
|
||||||
char *yystr;
|
char *yystr;
|
||||||
%}
|
%}
|
||||||
|
@ -20,13 +34,13 @@ char *yystr;
|
||||||
"(" { return LPAR; }
|
"(" { return LPAR; }
|
||||||
")" { return RPAR; }
|
")" { return RPAR; }
|
||||||
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
|
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
|
||||||
|
\n { return EOL; }
|
||||||
[ \t\n] { /* ignore white space */ }
|
[ \t\n] { /* ignore white space */ }
|
||||||
\".*\" { printf("str: %s\n", yytext); }
|
\".*\" { return STR; }
|
||||||
[a-zA-Z][a-zA-Z0-9]* { printf("sym: %s\n", yytext); }
|
[a-zA-Z][a-zA-Z0-9]* { return SYM; }
|
||||||
. { printf("error: %s\n", yytext); }
|
. { err(1, "invalid symbol: %s\n", yytext); }
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Goal: write LISP read-eval-print Loop and support
|
* Goal: write LISP read-eval-print Loop and support
|
||||||
* @ Function
|
* @ Function
|
||||||
|
@ -42,13 +56,59 @@ char *yystr;
|
||||||
* (if <bool> <body> <else>)
|
* (if <bool> <body> <else>)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
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 main(void)
|
||||||
{
|
{
|
||||||
int tok;
|
int tok;
|
||||||
|
struct token *t, *u = NULL, *start = NULL;
|
||||||
|
|
||||||
while(tok = yylex()) {
|
while(tok = yylex()) {
|
||||||
printf("token: %d\n", tok);
|
//printf("token: %d, %d, %s\n", tok, yylval, yytext);
|
||||||
if(tok == NUMBER) printf(" = %d\n", yylval);
|
//printf("t=%p\n", t);
|
||||||
//else printf("\n");
|
//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:
|
||||||
|
*/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user