pfff forgot to strdup yytext
[ozulis.git] / src / plugins / lang / mugiwara / bison / lexer.l
blobbc646e0b7cc89fccaf381b6947e79b734c1c26b4
1 %option reentrant noyywrap batch
2 %option bison-bridge bison-locations yylineno
3 %option warn
4 %option prefix="mugiwara_"
6 %{
7 #include <stdio.h>
8 #include <stdlib.h>
10 #include <ozulis/core/assert.hh>
11 #include <ozulis/ast/ast.hh>
12 #include <ozulis/ast/node-factory.hh>
13 #include "parser.hh"
15 #define YY_USER_ACTION                                  \
16   yylloc->first_line = yylineno;                        \
17   yylloc->last_line = yylineno;                         \
18   yylloc->first_column = yylloc->last_column;           \
19   yylloc->last_column += yyleng;
21 #define MAKE_BOOL(Value)                                                \
22   do {                                                                  \
23     yylval->nbExp = new ozulis::ast::NumberExp();                       \
24     yylval->nbExp->type = ozulis::ast::NodeFactory::createBoolType();   \
25     yylval->nbExp->number = Value;                                      \
26     return NUMBER;                                                      \
27   } while (0)
29 static ozulis::ast::NumberExp *
30 makeInteger(double value, int size, bool isSigned);
32 static ozulis::ast::NumberExp *
33 makeFloat(double value);
39 \n                      /* ignore end of line */yylloc->last_column = 0;
40 [ \t]+                  /* ignore whitespace */;
41 \/\/.*$                 /* line comment */
42 #.*$                    /* line comment */
44 ;                       return ';';
45 \.                      return '.';
46 :                       return ':';
47 ,                       return ',';
49 =                       return '=';
50 ==                      return EQEQ;
51 !=                      return NEQ;
52 \<                      return '<';
53 \<=                     return LTEQ;
54 >                       return '>';
55 >=                      return GTEQ;
57 \(                      return '(';
58 \)                      return ')';
60 \[                      return '[';
61 \]                      return ']';
63 \{                      return '{';
64 \}                      return '}';
66 \+                      return '+';
67 -                       return '-';
68 \*                      return '*';
69 \/                      return '/';
70 %                       return '%';
72 \|                      return '|';
73 &                       return '&';
74 \^                      return '^';
75 \<\<                    return SHL;
76 >>                      return LSHR;
77 >>>                     return ASHR;
78 ~                       return '~';
80 !                       return '!';
81 \|\|                    return OROR;
82 &&                      return ANDAND;
84 @                       return '@';
85 \$                      return '$';
87 \"[^\"]*\"              yylval->string = strdup(yytext); return STRING;
89 const                   return CONST;
90 goto                    return GOTO;
91 cast                    return CAST;
92 if                      return IF;
93 else                    return ELSE;
94 while                   return WHILE;
95 do                      return DO;
96 for                     return FOR;
97 return                  return RETURN;
98 true                    MAKE_BOOL(1);
99 false                   MAKE_BOOL(0);
101 [a-zA-Z][a-zA-Z0-9_]*   yylval->string = strdup(yytext); return ID;
103 ([0-9]+)?\.[0-9]+    {
104   yylval->nbExp = makeFloat(strtod(yytext, 0));
105   return NUMBER;
108 [0-9]+    {
109   yylval->nbExp = makeInteger(strtod(yytext, 0), 32, true);
110   return NUMBER;
113 [0-9]+U    {
114   yylval->nbExp = makeInteger(strtod(yytext, 0), 32, false);
115   return NUMBER;
118 [0-9]+L    {
119   yylval->nbExp = makeInteger(strtod(yytext, 0), 64, true);
120   return NUMBER;
123 [0-9]+UL    {
124   yylval->nbExp = makeInteger(strtod(yytext, 0), 64, false);
125   return NUMBER;
128 .               return *yytext;
132 static ozulis::ast::NumberExp *
133 makeInteger(double value, int size, bool isSigned)
135   ozulis::ast::NumberExp * nbExp = new ozulis::ast::NumberExp();
136   ozulis::ast::IntegerType * nbType = new ozulis::ast::IntegerType();
137   nbType->size = size;
138   nbType->isSigned = isSigned;
139   nbExp->type = nbType;
140   nbExp->number = value;
141   return nbExp;
144 static ozulis::ast::NumberExp *
145 makeFloat(double value)
147   ozulis::ast::NumberExp * nbExp = new ozulis::ast::NumberExp();
148   ozulis::ast::FloatType * nbType = new ozulis::ast::FloatType();
149   nbExp->type = nbType;
150   nbExp->number = value;
151   return nbExp;