Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 1 | /*===-- Lexer.l - Scanner for Stacker language -----------------*- C++ -*--===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This file implements the flex scanner for Stacker languages files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===*/ |
| 14 | |
| 15 | %option prefix="Stacker" |
| 16 | %option yylineno |
| 17 | %option nostdinit |
| 18 | %option never-interactive |
| 19 | %option batch |
| 20 | %option noyywrap |
| 21 | %option nodefault |
| 22 | %option 8bit |
| 23 | %option outfile="Lexer.cpp" |
| 24 | %option ecs |
| 25 | %option noreject |
| 26 | %option noyymore |
| 27 | |
| 28 | %{ |
| 29 | |
| 30 | #include "StackerCompiler.h" |
| 31 | #include "StackerParser.h" |
| 32 | |
| 33 | /* Conversion of text ints to binary */ |
Reid Spencer | dc8e6b5 | 2004-05-09 23:20:19 +0000 | [diff] [blame^] | 34 | static int64_t IntToVal(const char *Buffer) { |
| 35 | int64_t Result = 0; |
Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 36 | for (; *Buffer; Buffer++) { |
Reid Spencer | dc8e6b5 | 2004-05-09 23:20:19 +0000 | [diff] [blame^] | 37 | int64_t OldRes = Result; |
Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 38 | Result *= 10; |
| 39 | Result += *Buffer-'0'; |
| 40 | if (Result < OldRes) // Uh, oh, overflow detected!!! |
| 41 | StackerCompiler::ThrowException("constant bigger than 64 bits detected!"); |
| 42 | } |
| 43 | return Result; |
| 44 | } |
| 45 | |
| 46 | /* Conversion of text hexadecimal ints to binary */ |
Reid Spencer | dc8e6b5 | 2004-05-09 23:20:19 +0000 | [diff] [blame^] | 47 | static int64_t HexIntToVal(const char *Buffer) { |
| 48 | int64_t Result = 0; |
Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 49 | for (; *Buffer; ++Buffer) { |
Reid Spencer | dc8e6b5 | 2004-05-09 23:20:19 +0000 | [diff] [blame^] | 50 | int64_t OldRes = Result; |
Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 51 | Result *= 16; |
| 52 | char C = *Buffer; |
| 53 | if (C >= '0' && C <= '9') |
| 54 | Result += C-'0'; |
| 55 | else if (C >= 'A' && C <= 'F') |
| 56 | Result += C-'A'+10; |
| 57 | else if (C >= 'a' && C <= 'f') |
| 58 | Result += C-'a'+10; |
| 59 | |
| 60 | if (Result < OldRes) // Uh, oh, overflow detected!!! |
| 61 | StackerCompiler::ThrowException("constant bigger than 64 bits detected!"); |
| 62 | } |
| 63 | return Result; |
| 64 | } |
| 65 | |
| 66 | #define YY_NEVER_INTERACTIVE 1 |
| 67 | %} |
| 68 | |
| 69 | /* Comments start with a ; and go till end of line */ |
| 70 | Comment1 [#].*$ |
| 71 | /* You can also embed them in ( ... ) */ |
| 72 | Comment2 \(.*\) |
| 73 | /* We ignore white space */ |
| 74 | White [ \t\n] |
| 75 | |
| 76 | /* jdentifiers start with a % sign */ |
| 77 | Identifier [A-Za-z][-A-Za-z0-9_]* |
| 78 | |
| 79 | /* Strings can contain any character except " and \ */ |
| 80 | String \"[^\"]*\" |
| 81 | |
| 82 | /* Positive and negative integer constants*/ |
| 83 | PInteger [+]?[0-9]+ |
| 84 | NInteger -[0-9]+ |
| 85 | HexInteger 0x[0-9A-Fa-f]+ |
| 86 | |
| 87 | /* Special Characters - name them to avoid flex confusion */ |
| 88 | Semi [;] |
| 89 | Colon [:] |
| 90 | Less \< |
| 91 | More \> |
| 92 | LessEq \<\= |
| 93 | MoreEq \>\= |
| 94 | NotEq \<\> |
| 95 | Equal \= |
| 96 | Plus \+ |
| 97 | Minus \- |
| 98 | Incr \+\+ |
| 99 | Decr \-\- |
| 100 | Mult \* |
| 101 | Div \/ |
| 102 | StarSlash \*\/ |
| 103 | LShift \<\< |
| 104 | RShift \>\> |
| 105 | InStr \<s |
| 106 | InNum \<d |
| 107 | InChar \<c |
| 108 | OutStr \>s |
| 109 | OutNum \>d |
| 110 | OutChar \>c |
| 111 | |
| 112 | %% |
| 113 | |
| 114 | {Comment1} { /* Ignore comments */ } |
| 115 | {Comment2} { /* Ignore comments */ } |
| 116 | |
| 117 | {Colon} { return COLON; } |
| 118 | {Semi} { return SEMI; } |
| 119 | |
Chris Lattner | 91ef460 | 2004-03-31 03:49:47 +0000 | [diff] [blame] | 120 | TRUE { return TRUETOK; } |
| 121 | FALSE { return FALSETOK; } |
| 122 | ON { return TRUETOK; } |
| 123 | OFF { return FALSETOK; } |
Chris Lattner | f608e85 | 2003-11-23 17:52:55 +0000 | [diff] [blame] | 124 | {Less} { return LESS; } |
| 125 | LT { return LESS; } |
| 126 | {More} { return MORE; } |
| 127 | GT { 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; } |
| 135 | EQ { return EQUAL; } |
| 136 | |
| 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; } |
| 145 | ABS { return ABS; } |
| 146 | MIN { return MIN; } |
| 147 | MAX { return MAX; } |
| 148 | {StarSlash} { return STAR_SLASH; } |
| 149 | |
| 150 | AND { return AND; } |
| 151 | OR { return OR; } |
| 152 | XOR { return XOR; } |
| 153 | {LShift} { return LSHIFT; } |
| 154 | {RShift} { return RSHIFT; } |
| 155 | |
| 156 | DROP { return DROP; } |
| 157 | NIP { return NIP; } |
| 158 | DUP { return DUP; } |
| 159 | SWAP { return SWAP; } |
| 160 | OVER { return OVER; } |
| 161 | PICK { return PICK; } |
| 162 | SELECT { return SELECT; } |
| 163 | ROT { return ROT; } |
| 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; } |
| 175 | |
| 176 | MALLOC { return MALLOC; } |
| 177 | FREE { return FREE; } |
| 178 | GET { return GET; } |
| 179 | PUT { return PUT; } |
| 180 | |
| 181 | IF { return IF; } |
| 182 | ELSE { return ELSE; } |
| 183 | ENDIF { return ENDIF; } |
| 184 | WHILE { return WHILE; } |
| 185 | END { return END; } |
| 186 | |
| 187 | RECURSE { return RECURSE; } |
| 188 | RETURN { return RETURN; } |
| 189 | EXIT { return EXIT; } |
| 190 | FORWARD { return FORWARD; } |
| 191 | TAB { return TAB; } |
| 192 | SPACE { return SPACE; } |
| 193 | CR { return CR; } |
| 194 | |
| 195 | {InStr} { return IN_STR; } |
| 196 | {InNum} { return IN_NUM; } |
| 197 | {InChar} { return IN_CHAR; } |
| 198 | |
| 199 | {OutStr} { return OUT_STR; } |
| 200 | {OutNum} { return OUT_NUM; } |
| 201 | {OutChar} { return OUT_CHAR; } |
| 202 | |
| 203 | MAIN { return MAIN; } |
| 204 | |
| 205 | DUMP { return DUMP; } |
| 206 | |
| 207 | != { StackerCompiler::ThrowException( |
| 208 | "You probably meant to use a <> instead of !=" ); } |
| 209 | |
| 210 | == { StackerCompiler::ThrowException( |
| 211 | "You probably meant to use a single = .. this isn't C"); } |
| 212 | |
| 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; |
| 220 | return INTEGER; |
| 221 | } |
| 222 | {HexInteger} { Stackerlval.IntegerVal = HexIntToVal(yytext+3); |
| 223 | return INTEGER; |
| 224 | } |
| 225 | |
| 226 | {String} { yytext[strlen(yytext)-1] = 0; // nuke end quote |
| 227 | Stackerlval.StringVal = strdup(yytext+1); // Nuke start quote |
| 228 | return STRING; |
| 229 | } |
| 230 | |
| 231 | {Identifier} { Stackerlval.StringVal = strdup(yytext); return IDENTIFIER; } |
| 232 | |
| 233 | {White} { /* Ignore whitespace */ } |
| 234 | %% |