-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscan.l
More file actions
218 lines (184 loc) · 4.92 KB
/
scan.l
File metadata and controls
218 lines (184 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright (C) 2012 Sean "rioki" Farrell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
%{
#include <assert.h>
#include <stdio.h>
#include "scan.h"
%}
comment ;[^\r\n]*
ws [ \t]+
nl (\r)|(\n)|(\r\n)
ident [a-zA-Z_][0-9a-zA-Z_]*
label :[a-zA-Z_][0-9a-zA-Z_]*
oct 0[0-7]+
dec [1-9][0-9]*
hex 0x[0-9A-Fa-f]+
static const char* file;
static int line = 1;
static variant_t* value;
%%
{comment} /* skip comments */
{ws} /* skip white spaces */
{nl} {
line++;
return EOL_TOKEN;
}
, return COMMA_TOKEN;
\[ return LEFT_BRAKET_TOKEN;
\] return RIGHT_BRAKET_TOKEN;
\+ return PLUS_TOKEN;
SET return SET_TOKEN;
ADD return ADD_TOKEN;
SUB return SUB_TOKEN;
MUL return MUL_TOKEN;
DIV return DIV_TOKEN;
MOD return MOD_TOKEN;
SHL return SHL_TOKEN;
SHR return SHR_TOKEN;
AND return AND_TOKEN;
BOR return BOR_TOKEN;
XOR return XOR_TOKEN;
IFE return IFE_TOKEN;
IFN return IFN_TOKEN;
IFG return IFG_TOKEN;
IFB return IFB_TOKEN;
JSR return JSR_TOKEN;
A return A_TOKEN;
B return B_TOKEN;
C return C_TOKEN;
X return X_TOKEN;
Y return Y_TOKEN;
Z return Z_TOKEN;
I return I_TOKEN;
J return J_TOKEN;
POP return POP_TOKEN;
PEEK return PEEK_TOKEN;
PUSH return PUSH_TOKEN;
SP return SP_TOKEN;
PC return PC_TOKEN;
O return O_TOKEN;
0 {
/* Yea we need to handle zero explicitly, since all other codes require values >1 */
value->type = UINT_TYPE;
value->ui = 0;
return INTEGER_TOKEN;
}
{oct} {
/* TODO: Handle overflow of the integer. */
value->type = UINT_TYPE;
value->ui = strtoul(yytext + 1, NULL, 8);
return INTEGER_TOKEN;
}
{dec} {
value->type = UINT_TYPE;
value->ui = strtoul(yytext, NULL, 10);
return INTEGER_TOKEN;
}
{hex} {
value->type = UINT_TYPE;
value->ui = strtoul(yytext + 2, NULL, 16);
return INTEGER_TOKEN;
}
{ident} {
value->type = STRING_TYPE;
value->str = strdup(yytext);
return IDENTIFIER_TOKEN;
}
{label} {
value->type = STRING_TYPE;
value->str = strdup(yytext + 1);
return LABEL_TOKEN;
}
<<EOF>> {
return EOF_TOKEN;
}
. {
fprintf(stderr, "%s:%d: error: Unknown token %s.\n", file, line, yytext);
return ERROR_TOKEN;
}
%%
int yywrap(void)
{
return 1;
}
int start_scan(FILE* fp, const char* f)
{
assert(fp != NULL);
assert(f != NULL);
yyin = fp;
file = f;
return 0;
}
int scan_token(variant_t* v)
{
assert(v != NULL);
value = v;
return yylex();
}
const char* get_scan_file()
{
return file;
}
unsigned int get_scan_line()
{
return line;
}
static const char* token_names[] = {
"ERROR",
",",
"[",
"]",
"+",
"SET",
"ADD",
"SUB",
"MUL",
"DIV",
"MOD",
"SHL",
"SHR",
"AND",
"BOR",
"XOR",
"IFE",
"IFN",
"IFG",
"IFB",
"A",
"B",
"C",
"X",
"Y",
"Z",
"I",
"J",
"identifier",
"label",
"integer",
"end of line",
"end of file"
};
const char* get_token_name(int token)
{
assert(token < sizeof(token_names));
return token_names[token];
}