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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* Lexer for GML */
#include "config.h"
#include <ctype.h>
#include "gmllexer.h"
#include "gml.h"
struct lexeme current_lexeme;
struct bucket {
int kind;
struct bucket * next;
int op;
char string[1];
};
#define HASHTABLE_SIZE 256
static struct bucket * hashtable[HASHTABLE_SIZE] = { NULL, /*all nulls*/};
#define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE];
#define STORE_BUFFER(pos,c) if (pos < sizeof(buffer) - 1) buffer[pos++] = c
static inline unsigned int hash_ident(char * s)
{
unsigned int h;
for (h = 0; *s != 0; s++) h = (h << 4) + h + *s;
return h % HASHTABLE_SIZE;
}
static void get_ident(int firstchar)
{
int c, pos;
unsigned int h;
struct bucket * b;
/* Read characters of the ident */
buffer[0] = firstchar;
pos = 1;
while (1) {
c = getchar();
if (! (isalnum(c) || c == '-' || c == '_')) break;
STORE_BUFFER(pos, c);
}
if (c != EOF) ungetc(c, stdin);
buffer[pos] = 0;
/* Hash the ident */
h = hash_ident(buffer);
/* Look it up in the hash table */
for (b = hashtable[h]; b != NULL; b = b->next) {
if (strcmp(b->string, buffer) == 0) {
/* Found: return hash table entry */
current_lexeme.kind = b->kind;
if (b->kind == IDENTIFIER)
current_lexeme.u.s = b->string;
else
current_lexeme.u.op = b->op;
return;
}
}
/* Not found: enter it */
b = malloc(sizeof(struct bucket) + pos);
b->kind = IDENTIFIER;
strcpy(b->string, buffer);
b->next = hashtable[h];
hashtable[h] = b;
current_lexeme.kind = IDENTIFIER;
current_lexeme.u.s = b->string;
}
static void get_binder(void)
{
int c = getchar();
if (! isalpha(c)) {
fprintf(stderr, "Bad binder /%c...\n", c);
exit(2);
}
get_ident(c);
if (current_lexeme.kind != IDENTIFIER) {
fprintf(stderr, "Binder /%s rebinds reserved identifier\n",
current_lexeme.u.s);
exit(2);
}
current_lexeme.kind = BINDER;
}
static void get_number(int firstchar)
{
int c, pos, is_real;
pos = 0;
is_real = 0;
c = firstchar;
/* Initial sign */
if (c == '-') {
STORE_BUFFER(pos, c);
c = getchar();
if (! isdigit(c)) goto bad_number;
}
/* Decimal number */
do {
STORE_BUFFER(pos, c);
c = getchar();
} while (isdigit(c));
/* Period and fractional part */
if (c == '.') {
is_real = 1;
STORE_BUFFER(pos, c);
c = getchar();
if (! isdigit(c)) goto bad_number;
do {
STORE_BUFFER(pos, c);
c = getchar();
} while (isdigit(c));
}
/* Exponent */
if (c == 'e' || c == 'E') {
is_real = 1;
STORE_BUFFER(pos, c);
c = getchar();
if (c == '-') {
STORE_BUFFER(pos, c);
c = getchar();
}
if (! isdigit(c)) goto bad_number;
do {
STORE_BUFFER(pos, c);
c = getchar();
} while (isdigit(c));
}
if (c != EOF) ungetc(c, stdin);
/* Convert to token */
buffer[pos] = 0;
if (is_real) {
current_lexeme.kind = REAL;
current_lexeme.u.d = strtod(buffer, NULL);
} else {
current_lexeme.kind = INTEGER;
current_lexeme.u.i = atoi(buffer);
}
return;
bad_number:
fprintf(stderr, "Illegal number\n");
exit(2);
}
static void get_string()
{
int c, pos;
pos = 0;
while (1) {
c = getchar();
if (c == '"') break;
if (! isprint(c)) goto bad_string;
STORE_BUFFER(pos, c);
}
buffer[pos] = 0;
current_lexeme.kind = STRING;
current_lexeme.u.s = strdup(buffer);
return;
bad_string:
fprintf(stderr, "Illegal string literal\n");
exit(2);
}
void get_lexeme(void)
{
int c;
if (current_lexeme.kind != NONE) return;
again:
c = getchar();
switch (c) {
case EOF:
current_lexeme.kind = END_OF_FILE; break;
case ' ': case '\n': case '\t': case '\r': case 11:
goto again;
case '%':
do { c = getchar(); } while (c != '\n' && c != EOF);
goto again;
case '/':
get_binder(); break;
case '-': case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
get_number(c); break;
case '"':
get_string(); break;
case '{':
current_lexeme.kind = LBRACE; break;
case '}':
current_lexeme.kind = RBRACE; break;
case '[':
current_lexeme.kind = LBRACKET; break;
case ']':
current_lexeme.kind = RBRACKET; break;
default:
if (isalpha(c)) {
get_ident(c);
} else {
fprintf(stderr, "Illegal character `%c'\n", c);
exit(2);
}
break;
}
}
void discard_lexeme(void)
{
current_lexeme.kind = NONE;
}
/* Enter the operators in the hashtable */
struct op_init { char * name; int kind; int op; };
static struct op_init operators[] =
{ { "true", BOOLEAN, 1 },
{ "false", BOOLEAN, 0 },
{ "acos", OPERATOR, Op_acos },
{ "addi", OPERATOR, Op_addi },
{ "addf", OPERATOR, Op_addf },
{ "apply", OPERATOR, Op_apply },
{ "asin", OPERATOR, Op_asin },
{ "clampf", OPERATOR, Op_clampf },
{ "cone", OPERATOR, Op_cone },
{ "cos", OPERATOR, Op_cos },
{ "cube", OPERATOR, Op_cube },
{ "cylinder", OPERATOR, Op_cylinder },
{ "difference", OPERATOR, Op_difference },
{ "divi", OPERATOR, Op_divi },
{ "divf", OPERATOR, Op_divf },
{ "eqi", OPERATOR, Op_eqi },
{ "eqf", OPERATOR, Op_eqf },
{ "floor", OPERATOR, Op_floor },
{ "frac", OPERATOR, Op_frac },
{ "get", OPERATOR, Op_get },
{ "getx", OPERATOR, Op_getx },
{ "gety", OPERATOR, Op_gety },
{ "getz", OPERATOR, Op_getz },
{ "if", OPERATOR, Op_if },
{ "intersect", OPERATOR, Op_intersect },
{ "length", OPERATOR, Op_length },
{ "lessi", OPERATOR, Op_lessi },
{ "lessf", OPERATOR, Op_lessf },
{ "light", OPERATOR, Op_light },
{ "modi", OPERATOR, Op_modi },
{ "muli", OPERATOR, Op_muli },
{ "mulf", OPERATOR, Op_mulf },
{ "negi", OPERATOR, Op_negi },
{ "negf", OPERATOR, Op_negf },
{ "plane", OPERATOR, Op_plane },
{ "point", OPERATOR, Op_point },
{ "pointlight", OPERATOR, Op_pointlight },
{ "real", OPERATOR, Op_real },
{ "render", OPERATOR, Op_render },
{ "rotatex", OPERATOR, Op_rotatex },
{ "rotatey", OPERATOR, Op_rotatey },
{ "rotatez", OPERATOR, Op_rotatez },
{ "scale", OPERATOR, Op_scale },
{ "sin", OPERATOR, Op_sin },
{ "sphere", OPERATOR, Op_sphere },
{ "spotlight", OPERATOR, Op_spotlight },
{ "sqrt", OPERATOR, Op_sqrt },
{ "subi", OPERATOR, Op_subi },
{ "subf", OPERATOR, Op_subf },
{ "translate", OPERATOR, Op_translate },
{ "union", OPERATOR, Op_union },
{ "uscale", OPERATOR, Op_uscale },
{ "print", OPERATOR, Op_print },
};
void init_lexer(void)
{
int i;
for (i = 0; i < sizeof(operators) / sizeof(struct op_init); i++) {
struct op_init * opi = &(operators[i]);
struct bucket * b = malloc(sizeof(struct bucket) + strlen(opi->name));
unsigned int h = hash_ident(opi->name);
b->kind = opi->kind;
strcpy(b->string, opi->name);
b->op = opi->op;
b->next = hashtable[h];
hashtable[h] = b;
}
}
|