work on type inference some more
[ccc.git] / scan.l
diff --git a/scan.l b/scan.l
index bb34a5b..664868c 100644 (file)
--- a/scan.l
+++ b/scan.l
@@ -1,25 +1,91 @@
+D [0-9]
+H [0-9a-fA-F]
+E ([0\\abtnvfr]|x{H}?{H}|0[0-3]?{O}?{O})
+I [a-zA-Z_]
+O [0-7]
+
 %option noinput
 %option nounput
 %{
 
 #include <stdio.h>
+#define YY_USER_ACTION \
+    yylloc.first_line = yylloc.last_line; \
+    yylloc.first_column = yylloc.last_column; \
+    for(int i = 0; yytext[i] != '\0'; i++) { \
+        if(yytext[i] == '\n') { \
+            yylloc.last_line++; \
+            yylloc.last_column = 0; \
+        } \
+        else { \
+            yylloc.last_column++; \
+        } \
+    }
+
 #include "ast.h"
-#define YYSTYPE struct ast *
-#include "y.tab.h"
-extern YYSTYPE yylval;
+#include "parse.h"
 
 %}
 
+%start IN_COMMENT
+
 %%
 
-[0-9]+      { yylval = ast_int(atoi(yytext)); return INTEGER; }
+<INITIAL>{
+\/\*        BEGIN(IN_COMMENT);
+[ \n\t]     ;
+\/\/.*\n?   ;
+if          return IF;
+else        return ELSE;
+while       return WHILE;
+var         return VAR;
+true        { yylval.expr = expr_bool(true); return BOOL; }
+false       { yylval.expr = expr_bool(false); return BOOL; }
+return      return RETURN;
+Int         return TINT;
+Bool        return TBOOL;
+Char        return TCHAR;
+Void        return TVOID;
+->          return ARROW;
+=           return ASSIGN;
+!           return INVERSE;
+\|\|        return BINOR;
+&&          return BINAND;
+==          return EQ;
+!=          return NEQ;
+\<=         return GEQ;
+\<          return GE;
+>=          return LEQ;
+>           return LE;
+:           return CONS;
 \+          return PLUS;
 -           return MINUS;
 \*          return TIMES;
 \/          return DIVIDE;
+%           return MODULO;
+\^          return POWER;
 \(          return BOPEN;
 \)          return BCLOSE;
+\{          return COPEN;
+\}          return CCLOSE;
 \;          return SEMICOLON;
-[ \n\t]  ;
+\[          return SOPEN;
+\]          return SCLOSE;
+\[\]        return NIL;
+\.          return DOT;
+,           return COMMA;
+\"([^\\"]|\\(\"|{E}))*\" {
+       yylval.expr = expr_string(trimquotes(yytext)); return STRING; }
+'([^\\']|\\('|{E}))' {
+       yylval.expr = expr_char(trimquotes(yytext)); return CHAR; }
+{D}+ {
+       yylval.expr = expr_int(atoi(yytext)); return INTEGER; }
+{I}({I}|{D})* {
+       yylval.ident = safe_strdup(yytext); return IDENT; }
+}
+<IN_COMMENT>{
+\*\/       BEGIN(INITIAL);
+.          ;
+}
 
 %%