1 /*===-- Lexer.l - Scanner for Stacker language -----------------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and donated to the LLVM research
6 // group and is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file implements the flex scanner for Stacker languages files.
13 //===----------------------------------------------------------------------===*/
15 %option prefix="Stacker"
18 %option never-interactive
23 %option outfile="Lexer.cpp"
30 #include "StackerCompiler.h"
31 #include "StackerParser.h"
33 /* Conversion of text ints to binary */
34 static uint64_t IntToVal(const char *Buffer) {
36 for (; *Buffer; Buffer++) {
37 uint64_t OldRes = Result;
39 Result += *Buffer-'0';
40 if (Result < OldRes) // Uh, oh, overflow detected!!!
41 StackerCompiler::ThrowException("constant bigger than 64 bits detected!");
46 /* Conversion of text hexadecimal ints to binary */
47 static uint64_t HexIntToVal(const char *Buffer) {
49 for (; *Buffer; ++Buffer) {
50 uint64_t OldRes = Result;
53 if (C >= '0' && C <= '9')
55 else if (C >= 'A' && C <= 'F')
57 else if (C >= 'a' && C <= 'f')
60 if (Result < OldRes) // Uh, oh, overflow detected!!!
61 StackerCompiler::ThrowException("constant bigger than 64 bits detected!");
66 #define YY_NEVER_INTERACTIVE 1
69 /* Comments start with a ; and go till end of line */
71 /* You can also embed them in ( ... ) */
73 /* We ignore white space */
76 /* jdentifiers start with a % sign */
77 Identifier [A-Za-z][-A-Za-z0-9_]*
79 /* Strings can contain any character except " and \ */
82 /* Positive and negative integer constants*/
85 HexInteger 0x[0-9A-Fa-f]+
87 /* Special Characters - name them to avoid flex confusion */
114 {Comment1} { /* Ignore comments */ }
115 {Comment2} { /* Ignore comments */ }
117 {Colon} { return COLON; }
118 {Semi} { return SEMI; }
120 TRUE { return TRUETOK; }
121 FALSE { return FALSETOK; }
122 ON { return TRUETOK; }
123 OFF { return FALSETOK; }
124 {Less} { return LESS; }
126 {More} { return MORE; }
128 {LessEq} { return LESS_EQUAL; }
129 LE { return LESS_EQUAL; }
130 {MoreEq} { return MORE_EQUAL; }
131 GE { return MORE_EQUAL; }
132 {NotEq} { return NOT_EQUAL; }
133 NE { return NOT_EQUAL; }
134 {Equal} { return EQUAL; }
137 {Plus} { return PLUS; }
138 {Minus} { return MINUS; }
139 {Incr} { return INCR; }
140 {Decr} { return DECR; }
141 {Mult} { return MULT; }
142 {Div} { return DIV; }
143 MOD { return MODULUS; }
144 NEG { return NEGATE; }
148 {StarSlash} { return STAR_SLASH; }
153 {LShift} { return LSHIFT; }
154 {RShift} { return RSHIFT; }
156 DROP { return DROP; }
159 SWAP { return SWAP; }
160 OVER { return OVER; }
161 PICK { return PICK; }
162 SELECT { return SELECT; }
164 RROT { return RROT; }
165 ROLL { return ROLL; }
166 TUCK { return TUCK; }
167 DROP2 { return DROP2; }
168 NIP2 { return NIP2; }
169 DUP2 { return DUP2; }
170 SWAP2 { return SWAP2; }
171 OVER2 { return OVER2; }
172 TUCK2 { return TUCK2; }
173 ROT2 { return ROT2; }
174 RROT2 { return RROT2; }
176 MALLOC { return MALLOC; }
177 FREE { return FREE; }
182 ELSE { return ELSE; }
183 ENDIF { return ENDIF; }
184 WHILE { return WHILE; }
187 RECURSE { return RECURSE; }
188 RETURN { return RETURN; }
189 EXIT { return EXIT; }
190 FORWARD { return FORWARD; }
192 SPACE { return SPACE; }
195 {InStr} { return IN_STR; }
196 {InNum} { return IN_NUM; }
197 {InChar} { return IN_CHAR; }
199 {OutStr} { return OUT_STR; }
200 {OutNum} { return OUT_NUM; }
201 {OutChar} { return OUT_CHAR; }
203 MAIN { return MAIN; }
205 DUMP { return DUMP; }
207 != { StackerCompiler::ThrowException(
208 "You probably meant to use a <> instead of !=" ); }
210 == { StackerCompiler::ThrowException(
211 "You probably meant to use a single = .. this isn't C"); }
213 {PInteger} { Stackerlval.IntegerVal = IntToVal(yytext); return INTEGER; }
214 {NInteger} { uint64_t Val = IntToVal(yytext+1);
215 // +1: we have bigger negative range
216 if (Val > (uint64_t)INT64_MAX+1)
217 StackerCompiler::ThrowException(
218 "Constant too large for signed 64 bits!");
219 Stackerlval.IntegerVal = -Val;
222 {HexInteger} { Stackerlval.IntegerVal = HexIntToVal(yytext+3);
226 {String} { yytext[strlen(yytext)-1] = 0; // nuke end quote
227 Stackerlval.StringVal = strdup(yytext+1); // Nuke start quote
231 {Identifier} { Stackerlval.StringVal = strdup(yytext); return IDENTIFIER; }
233 {White} { /* Ignore whitespace */ }