-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlex.c
More file actions
431 lines (412 loc) · 10.6 KB
/
Copy pathlex.c
File metadata and controls
431 lines (412 loc) · 10.6 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <stdio.h> //for fopen, printf
#include <stdlib.h> //for malloc, exit
#include <memory.h> //for memcp
#include <string.h> //for strcmp
#include <ctype.h> //for isdigit
#include "types.h"
#include "lex.h"
char *source;
int src_size;
char cur_char;
int cur_pos;
int line_number=1;
struct_token token;
void lex_init(char* buf, int buf_size)
{
source = buf;
src_size = buf_size;
cur_char = -1; //set to invalid
cur_pos = -1; //set to invalid
consume_cur_char(); //come to first char in source
}
void consume_cur_char()
{
cur_pos += 1;
if (cur_pos >= src_size)
{
cur_char = '\0';
cur_pos = src_size;
}
else
cur_char = source[cur_pos];
}
char upcoming_char()
{
if (cur_pos + 1 >= src_size)
return '\0';
else
return source[cur_pos+1];
}
void skip_white_space()
{
while( cur_char == ' ' || cur_char == '\t' || cur_char == '\r')
consume_cur_char();
}
void skip_comment()
{
if (cur_char == '#')
while (cur_char != '\n')
consume_cur_char();
}
void init_token_text(int start, int end)
{
int tok_len = end - start + 1;
if(tok_len > MAX_TOK_LEN_USABLE)
{
printf("%d: Identifier too big!\n", line_number);
exit(1);
}
int pos = start;
int i=0;
while(pos <= end && pos < src_size)
{
token.text[i]=source[pos];
i += 1;
pos += 1;
}
token.text[i]='\0';
}
//to decide token kind, pass TBD
void init_token(int start, int end, int kind)
{
init_token_text(start, end);
if(kind != TBD)
{
token.kind = kind;
}
else
{
decide_key_or_id_token();
}
}
int comp_tok_text(char* keyword)
{
if (0==strcmp(token.text, keyword))
return 1;
else
return 0;
}
void decide_key_or_id_token()
{
//keyword
if(comp_tok_text("type"))
token.kind = TYPE;
else if(comp_tok_text("print"))
token.kind = PRINT;
else if(comp_tok_text("input"))
token.kind = INPUT;
else if(comp_tok_text("if"))
token.kind = IF;
else if(comp_tok_text("then"))
token.kind = THEN;
else if(comp_tok_text("else"))
token.kind = ELSE;
else if(comp_tok_text("endif"))
token.kind = ENDIF;
else if(comp_tok_text("while"))
token.kind = WHILE;
else if(comp_tok_text("repeat"))
token.kind = REPEAT;
else if(comp_tok_text("endwhile"))
token.kind = ENDWHILE;
else if(comp_tok_text("int"))
token.kind = INT;
else if(comp_tok_text("char"))
token.kind = CHAR;
else if(comp_tok_text("points"))
token.kind = POINTS;
else if(comp_tok_text("array"))
token.kind = ARRAY;
else if(comp_tok_text("record"))
token.kind = RECORD;
else if(comp_tok_text("endrec"))
token.kind = ENDREC;
else if(comp_tok_text("bind"))
token.kind = BIND;
else if(comp_tok_text("new"))
token.kind = NEW;
else if(comp_tok_text("move"))
token.kind = MOVE;
else if(comp_tok_text("free"))
token.kind = FREE;
else if(comp_tok_text("ref"))
token.kind = REF;
else if(comp_tok_text("func"))
token.kind = FUNC;
else if(comp_tok_text("fdef"))
token.kind = FDEF;
else if(comp_tok_text("return"))
token.kind = RETURN;
else if(comp_tok_text("must"))
token.kind = MUST;
else if(comp_tok_text("mdef"))
token.kind = MDEF;
else if(comp_tok_text("can"))
token.kind = CAN;
else if(comp_tok_text("cdef"))
token.kind = CDEF;
else if(comp_tok_text("poly"))
token.kind = POLY;
else //identifier
token.kind = IDENT;
}
void get_token()
{
skip_white_space();
skip_comment();
if (cur_char == '+')
{
init_token(cur_pos, cur_pos, PLUS);
consume_cur_char(); //+
}
else if (cur_char == '-')
{
init_token(cur_pos, cur_pos, MINUS);
consume_cur_char(); //-
}
else if (cur_char == '*')
{
init_token(cur_pos, cur_pos, ASTERISK);
consume_cur_char(); //*
}
else if (cur_char == '/')
{
init_token(cur_pos, cur_pos, SLASH);
consume_cur_char(); // /
}
else if (cur_char == '=')
{
// Check whether this token is = or ==
if (upcoming_char() == '=')
{
int last_pos = cur_pos;
consume_cur_char(); //first =
init_token(last_pos, cur_pos, EQEQ);
consume_cur_char(); //second =
}
else
{
init_token(cur_pos, cur_pos, EQUAL);
consume_cur_char(); //=
}
}
else if (cur_char == '>')
{
// Check whether this is token is > or >=
if (upcoming_char() == '=')
{
int last_pos = cur_pos;
consume_cur_char(); //>
init_token(last_pos, cur_pos, GTEQ);
consume_cur_char(); //=
}
else
{
init_token(cur_pos, cur_pos, GRET);
consume_cur_char(); //>
}
}
else if (cur_char == '<')
{
// Check whether this is token is < or <=
if (upcoming_char() == '=')
{
int last_pos = cur_pos;
consume_cur_char(); //<
init_token(last_pos, cur_pos, LTEQ);
consume_cur_char(); //=
}
else
{
init_token(cur_pos, cur_pos, LESS);
consume_cur_char(); //<
}
}
else if (cur_char == '!')
{
if (upcoming_char() == '=')
{
int last_pos = cur_pos;
consume_cur_char(); // !
init_token(last_pos, cur_pos, NOTEQ);
consume_cur_char(); //=
}
else
{
printf("%d: Expected !=, got !\n", line_number);
exit(1);
}
}
else if (cur_char == ':')
{
if (upcoming_char() == ':')
{
int last_pos = cur_pos;
consume_cur_char(); //first :
init_token(last_pos, cur_pos, TWOCOLON);
consume_cur_char(); //second :
}
else
{
printf("%d: Expected ::, got :\n", line_number);
exit(1);
}
}
else if (cur_char == '(')
{
init_token(cur_pos, cur_pos, LPAREN);
consume_cur_char(); // (
}
else if (cur_char == ')')
{
init_token(cur_pos, cur_pos, RPAREN);
consume_cur_char(); // )
}
else if (cur_char == '|')
{
init_token(cur_pos, cur_pos, PIPE);
consume_cur_char(); // |
}
else if (cur_char == '&')
{
init_token(cur_pos, cur_pos, AMPERSAND);
consume_cur_char(); // &
}
else if (cur_char == '~')
{
init_token(cur_pos, cur_pos, TILDE);
consume_cur_char(); // ~
}
else if (cur_char == '.')
{
// Check whether this is token is . or ..
if (upcoming_char() == '.')
{
int last_pos = cur_pos;
consume_cur_char(); //first .
init_token(last_pos, cur_pos, DOTDOT);
consume_cur_char(); //second .
}
else
{
init_token(cur_pos, cur_pos, DOT);
consume_cur_char(); //.
}
}
else if (cur_char == '[')
{
init_token(cur_pos, cur_pos, LBRKT);
consume_cur_char(); // [
}
else if (cur_char == ']')
{
init_token(cur_pos, cur_pos, RBRKT);
consume_cur_char(); // ]
}
else if (cur_char == ',')
{
init_token(cur_pos, cur_pos, COMMA);
consume_cur_char(); // ,
}
else if (cur_char == '{')
{
init_token(cur_pos, cur_pos, LBRACE);
consume_cur_char(); // {
}
else if (cur_char == '}')
{
init_token(cur_pos, cur_pos, RBRACE);
consume_cur_char(); // }
}
else if (cur_char == '\"')
{
// Get characters between quotations.
consume_cur_char(); //first "
int start_pos = cur_pos;
while (cur_char != '\"')
{
// Don't allow special characters in the string.
// No carriage returns, newlines,
// tabs, escape characters and percentage.
if (cur_char == '\r' || cur_char == '\n'
|| cur_char == '\t' || cur_char == '\\' || cur_char == '%')
{
printf("%d: Illegal character in string.\n", line_number);
exit(1);
}
else
{
consume_cur_char(); //each char in string
}
}
init_token(start_pos, cur_pos-1, STRLIT);
consume_cur_char(); //second "
}
else if (isdigit(cur_char))
{
// Leading character is a digit,
// so this must be a number.
// Get all consecutive digits.
int start_pos = cur_pos;
while (isdigit(upcoming_char()))
{
consume_cur_char(); //each digit of whole part
//except the last one
}
init_token(start_pos, cur_pos, INTLIT);
consume_cur_char(); //last digit of whole part
}
else if (isalpha(cur_char))
{
// Leading character is a letter,
// so this must be an identifier or a keyword.
// Get all consecutive alpha numeric characters and underscore.
int start_pos = cur_pos;
while (isalnum(upcoming_char()) || upcoming_char()=='_')
{
consume_cur_char(); //each char of identifier/keyword
//except the last one
}
// Check if the token is in the list of keywords.
// token kind will be decided inside this function
init_token(start_pos, cur_pos, TBD);
consume_cur_char(); //last char of identifier/keyword
}
else if (cur_char == '\'')
{
// Get character between single quote.
consume_cur_char(); //first '
if (upcoming_char() == '\'')
{
init_token(cur_pos, cur_pos, CHARLIT);
consume_cur_char(); // character
}
else
{
printf("%d: Expected char literal\n", line_number);
exit(1);
}
consume_cur_char(); //second '
}
else if (cur_char == '\n')
{
// Newline.
init_token(cur_pos, cur_pos, NEWLINE);
consume_cur_char(); // \n
line_number++;
}
else if (cur_char == '\0')
{
// ENDOFFILE.
init_token(cur_pos, cur_pos, ENDOFFILE);
consume_cur_char(); // \0
}
else
{
// Unknown token!
printf("%d: Lexing error: ", line_number);
printf("Unknown token: ");
printf("%c\n", cur_char);
exit(1);
}
}