blob: e45cdee87f26a351f3d7a491cd8f50fd8520aaf7 [file] [log] [blame]
Chris Lattnerf608e852003-11-23 17:52:55 +00001/*===-- 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 Spencerdc8e6b52004-05-09 23:20:19 +000034static int64_t IntToVal(const char *Buffer) {
35 int64_t Result = 0;
Chris Lattnerf608e852003-11-23 17:52:55 +000036 for (; *Buffer; Buffer++) {
Reid Spencerdc8e6b52004-05-09 23:20:19 +000037 int64_t OldRes = Result;
Chris Lattnerf608e852003-11-23 17:52:55 +000038 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 Spencerdc8e6b52004-05-09 23:20:19 +000047static int64_t HexIntToVal(const char *Buffer) {
48 int64_t Result = 0;
Chris Lattnerf608e852003-11-23 17:52:55 +000049 for (; *Buffer; ++Buffer) {
Reid Spencerdc8e6b52004-05-09 23:20:19 +000050 int64_t OldRes = Result;
Chris Lattnerf608e852003-11-23 17:52:55 +000051 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 */
70Comment1 [#].*$
71/* You can also embed them in ( ... ) */
72Comment2 \(.*\)
73/* We ignore white space */
74White [ \t\n]
75
76/* jdentifiers start with a % sign */
77Identifier [A-Za-z][-A-Za-z0-9_]*
78
79/* Strings can contain any character except " and \ */
80String \"[^\"]*\"
81
82/* Positive and negative integer constants*/
83PInteger [+]?[0-9]+
84NInteger -[0-9]+
85HexInteger 0x[0-9A-Fa-f]+
86
87/* Special Characters - name them to avoid flex confusion */
88Semi [;]
89Colon [:]
90Less \<
91More \>
92LessEq \<\=
93MoreEq \>\=
94NotEq \<\>
95Equal \=
96Plus \+
97Minus \-
98Incr \+\+
99Decr \-\-
100Mult \*
101Div \/
102StarSlash \*\/
103LShift \<\<
104RShift \>\>
105InStr \<s
106InNum \<d
107InChar \<c
108OutStr \>s
109OutNum \>d
110OutChar \>c
111
112%%
113
114{Comment1} { /* Ignore comments */ }
115{Comment2} { /* Ignore comments */ }
116
117{Colon} { return COLON; }
118{Semi} { return SEMI; }
119
Chris Lattner91ef4602004-03-31 03:49:47 +0000120TRUE { return TRUETOK; }
121FALSE { return FALSETOK; }
122ON { return TRUETOK; }
123OFF { return FALSETOK; }
Chris Lattnerf608e852003-11-23 17:52:55 +0000124{Less} { return LESS; }
125LT { return LESS; }
126{More} { return MORE; }
127GT { return MORE; }
128{LessEq} { return LESS_EQUAL; }
129LE { return LESS_EQUAL; }
130{MoreEq} { return MORE_EQUAL; }
131GE { return MORE_EQUAL; }
132{NotEq} { return NOT_EQUAL; }
133NE { return NOT_EQUAL; }
134{Equal} { return EQUAL; }
135EQ { 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; }
143MOD { return MODULUS; }
144NEG { return NEGATE; }
145ABS { return ABS; }
146MIN { return MIN; }
147MAX { return MAX; }
148{StarSlash} { return STAR_SLASH; }
149
150AND { return AND; }
151OR { return OR; }
152XOR { return XOR; }
153{LShift} { return LSHIFT; }
154{RShift} { return RSHIFT; }
155
156DROP { return DROP; }
157NIP { return NIP; }
158DUP { return DUP; }
159SWAP { return SWAP; }
160OVER { return OVER; }
161PICK { return PICK; }
162SELECT { return SELECT; }
163ROT { return ROT; }
164RROT { return RROT; }
165ROLL { return ROLL; }
166TUCK { return TUCK; }
167DROP2 { return DROP2; }
168NIP2 { return NIP2; }
169DUP2 { return DUP2; }
170SWAP2 { return SWAP2; }
171OVER2 { return OVER2; }
172TUCK2 { return TUCK2; }
173ROT2 { return ROT2; }
174RROT2 { return RROT2; }
175
176MALLOC { return MALLOC; }
177FREE { return FREE; }
178GET { return GET; }
179PUT { return PUT; }
180
181IF { return IF; }
182ELSE { return ELSE; }
183ENDIF { return ENDIF; }
184WHILE { return WHILE; }
185END { return END; }
186
187RECURSE { return RECURSE; }
188RETURN { return RETURN; }
189EXIT { return EXIT; }
190FORWARD { return FORWARD; }
191TAB { return TAB; }
192SPACE { return SPACE; }
193CR { 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
203MAIN { return MAIN; }
204
205DUMP { 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%%