diff --git a/src/parse.y b/src/parse.y index b51c175e..1837fee3 100644 --- a/src/parse.y +++ b/src/parse.y @@ -31,12 +31,17 @@ void yylex_destroy(); %lex-param {struct parser_control *p} %union { + int i; + double f; + char *cstr; pic_value datum; } %token tLPAREN tRPAREN tDOT %token tQUOTE tQUASIQUOTE tUNQUOTE tUNQUOTE_SPLICING -%token tSYMBOL tNUMBER tBOOLEAN tSTRING +%token tINT tBOOLEAN +%token tFLOAT +%token tSYMBOL tSTRING %type program_data %type datum simple_datum compound_datum abbrev @@ -86,9 +91,27 @@ datum simple_datum : tSYMBOL - | tNUMBER - | tBOOLEAN + { + $$ = pic_intern_cstr(p->pic, $1); + free($1); + } | tSTRING + { + $$ = pic_str_new_cstr(p->pic, $1); + free($1); + } + | tINT + { + $$ = pic_int_value($1); + } + | tFLOAT + { + $$ = pic_float_value($1); + } + | tBOOLEAN + { + $$ = pic_bool_value($1); + } ; compound_datum diff --git a/src/scan.l b/src/scan.l index 00e2e6b1..c312aeff 100644 --- a/src/scan.l +++ b/src/scan.l @@ -47,6 +47,7 @@ infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0" [ \t\n\r] /* skip whitespace */ {comment} /* skip comment */ + "." return tDOT; "(" return tLPAREN; ")" return tRPAREN; @@ -54,17 +55,34 @@ infnan "+inf.0"|"-inf.0"|"+nan.0"|"-nan.0" "`" return tQUASIQUOTE; "," return tUNQUOTE; ",@" return tUNQUOTE_SPLICING; -{boolean} { yylvalp->datum = pic_bool_value(strcmp(yytext, "#t") == 0 || strcmp(yytext, "#true") == 0); return tBOOLEAN; } -{integer} { yylvalp->datum = pic_int_value(atoi(yytext)); return tNUMBER; } -{real} { yylvalp->datum = pic_float_value(atof(yytext)); return tNUMBER; } -{identifier} { yylvalp->datum = pic_intern_cstr(p->pic, yytext); return tSYMBOL; } + +{boolean} { + yylvalp->i = (yytext[1] == 't'); + return tBOOLEAN; +} + +{integer} { + yylvalp->i = atoi(yytext); + return tINT; +} + +{real} { + yylvalp->f = atof(yytext); + return tFLOAT; +} + +{identifier} { + yylvalp->cstr = strdup(yytext); + return tSYMBOL; +} + "\"" BEGIN(STRING); { [^\\"]* yymore(); <> { yyerror(p, "eof in string"); BEGIN(INITIAL); } "\"" { yytext[yyleng-1] = '\0'; - yylvalp->datum = pic_str_new_cstr(p->pic, yytext); + yylvalp->cstr = strdup(yytext); BEGIN(INITIAL); return tSTRING; }