summaryrefslogtreecommitdiff
path: root/tokenizer.c
blob: 5fafd3b870ba62232ca9593494231c19c0cb0f09 (plain)
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
typedef enum {
			  TOKEN_KW,
			  TOKEN_IDENT,
			  TOKEN_NUM_CONST,
			  TOKEN_CHAR_CONST,
			  TOKEN_EOF
			  /* TODO: char constnats, str constants */
} TokenKind;

typedef enum {
			  KW_SEMICOLON,
			  KW_EQEQ,
			  KW_LT,
			  KW_LE,
			  KW_EQ,
			  KW_COUNT
} Keyword;

/* OPTIM: Use a trie or just a function if this gets too long */
static const char *keywords[KW_COUNT] =
	{";", "==", "<", "<=", "="}; 

#define TOKENIZER_USE_LLONG 1

typedef unsigned long long IntConst;

typedef long double RealConst; /* OPTIM: Maybe only use double */

typedef enum {
			  NUM_CONST_INT,
			  NUM_CONST_REAL
} NumConstKind;

typedef struct {
	NumConstKind kind;
	union {
		IntConst intval;
		RealConst realval;
	};
} NumConst;

/* NOTE: LineNo is typedef'd in util/err.c */
typedef struct {
	TokenKind kind;
	LineNo line;
    LineNo col;
	union {
		Keyword kw;
		Identifier ident;
		NumConst num;
		char chr;
	};
} Token;

typedef struct {
	Token *tokens;
	size_t ntokens;
	size_t cap;	/* used internally */
	Token *token; /* token currently being processed */
} Tokenizer;

static void token_fprint(FILE *out, Token *t) {
	fprintf(out, "l%luc%lu-", (unsigned long)t->line, (unsigned long)t->col);
	switch (t->kind) {
	case TOKEN_KW:
		fprintf(out, "keyword: %s", keywords[t->kw]);
		break;
	case TOKEN_IDENT:
		fprintf(out, "identifier: %ld:", t->ident->id);
		ident_fprint(out, t->ident);
		break;
	case TOKEN_NUM_CONST:
		fprintf(out, "number: ");
		switch (t->num.kind) {
		case NUM_CONST_INT:
			fprintf(out, "%llu", t->num.intval);
			break;
		case NUM_CONST_REAL:
			fprintf(out, "%g", (double)t->num.realval);
			break;
		}
		break;
	case TOKEN_CHAR_CONST:
		fprintf(out, "char: '%c' (%d)", t->chr, t->chr);
		break;
	case TOKEN_EOF:
		fprintf(out, "eof");
		break;
	}
}

static void tokenizer_add(Tokenizer *t, Token *token, LineNo line, LineNo col) {
	if (t->ntokens >= t->cap) {
		t->cap *= 2;
		t->tokens = err_realloc(t->tokens, t->cap * sizeof(*t->tokens));
	}
	token->line = line;
	token->col = col;
	t->tokens[t->ntokens++] = *token;
}

static Tokenizer tokenize_string(char *s) {	/* NOTE: May modify string. Don't even try to pass it a string literal.*/
	int has_err = 0;
	Tokenizer t;
	t.cap = 256;
	t.ntokens = 0;
	t.tokens = err_malloc(t.cap * sizeof(*t.tokens));

	LineNo line = 1;
	LineNo col = 1;
	
	while (1) {
	    if (*s == 0) break;
		if (isspace(*s)) {
			if (*s == '\n') {
				line++;
				col = 0;
			}
			s++; col++;
	    	continue;
		}

		if (*s == '/') {
			/* maybe it's a comment */
			int is_comment = 1;
			s++; col++;
			switch (*s) {
			case '/': /* single line comment */
				for (s++; *s != '\n' && *s; s++);
				line++;
				col = 1;
				break;
			case '*': { /* multi line comment */
				int comment_level = 1; /* allow nested multi-line comments */
			    while (*s) {
					if (*s == '\n') {
						line++;
						col = 1;
						s++;
						continue;
					}
					if (s[0] == '*' && s[1] == '/') {
						s += 2; col += 2;
						comment_level--;
						if (comment_level == 0) {
							break;
						}
					} else if (s[0] == '/' && s[1] == '*') {
						s += 2; col += 2;
						comment_level++;
					} else {
						s++; col++;
					}
				}
				if (*s == 0) {
					err_print(line, col, "End of file reached inside multi-line comment.");
					abort(); /* there won't be any further errors, of course */
				}
			} break;
			default:
				is_comment = 0;
				s--; /* go back */
				break;
			}
			if (is_comment) continue;
		}
		Keyword kw;
		for (kw = 0; kw < KW_COUNT; kw++) {
			if (strncmp(s, keywords[kw], strlen(keywords[kw])) == 0) {
				break;
			}
		}
		if (kw != KW_COUNT) {
			/* it's a keyword */
			Token token;
			token.kind = TOKEN_KW;
			token.kw = kw;
			tokenizer_add(&t, &token, line, col);
			col += (LineNo)strlen(keywords[kw]);
			s += (LineNo)strlen(keywords[kw]);
			continue;
		}
		
		/* check if it's a number */

		if (isdigit(*s)) {
			/* it's a numeric constant */
			int base = 10;
			RealConst decimal_pow10;
			NumConst n;
			n.kind = NUM_CONST_INT;
			n.intval = 0;
			LineNo line_start = line, col_start = col;
			if (*s == '0') {
				s++; col++;
				/* octal/hexadecimal/binary (or zero) */
				char format = *s;
				if (isdigit(format)) /* octal */
					base = 8;
				else {
					switch (format) {
					case 'b':
						base = 2;
						s++; col++;
						break;
					case 'x':
						base = 16;
						s++; col++;
						break;
					default:
						/* it's 0/0.something etc.  */
						break;
					}
				}
			}

			while (1) {
				if (*s == '.') {
					if (n.kind == NUM_CONST_REAL) {
						err_print(line, col, "Double . in number.");
						goto err;
					}
					if (base != 10) {
						err_print(line, col, "Decimal point in non base 10 number.");
						goto err;
					}
				    n.kind = NUM_CONST_REAL;
					decimal_pow10 = 0.1;
					n.realval = (RealConst)n.intval;
					s++, col++;
					continue;
				} else if (*s == 'e') {
					s++; col++;
					if (n.kind == NUM_CONST_INT) {
						n.kind = NUM_CONST_REAL;
						n.realval = (RealConst)n.intval;
					}
					/* TODO: check if exceeding maximum exponent */
					int exponent = 0;
					if (*s == '+') {
						s++; col++;
					}
					
					int negative_exponent = 0;
					if (*s == '-') {
						s++; col++;
						negative_exponent = 1;
					}
					for (; isdigit(*s); s++, col++) {
						exponent *= 10;
						exponent += *s - '0';
					}
					/* OPTIM: Slow for very large exponents (unlikely to happen) */
					for (int i = 0; i < exponent; i++) {
						if (negative_exponent)
							n.realval /= 10;
						else
							n.realval *= 10;
					}
						
					break;
				}
				int digit = -1;
				if (base == 16) {
					if (*s >= 'a' && *s <= 'f')
						digit = 10 + *s - 'a';
					else if (*s >= 'A' && *s <= 'F')
						digit = *s - 'A';
				}
				if (digit == -1) {
					if (*s >= '0' && *s <= '9')
						digit = *s - '0';
				}
				if (digit < 0 || digit >= base) {
					if (isdigit(*s)) {
						/* something like 0b011012 */
						err_print(line, col, "Digit %d cannot appear in a base %d number.", digit, base);
						goto err;
					}
					/* end of numeric constant */
					break;
				}
				switch (n.kind) {
				case NUM_CONST_INT:
					if (n.intval > ULLONG_MAX / (IntConst)base) {
						/* too big! */
						err_print(line, col, "Number too big to fit in a numeric constant.");
						goto err;
					}
					n.intval *= (IntConst)base;
					n.intval += (IntConst)digit;
					break;
				case NUM_CONST_REAL:
					n.realval += decimal_pow10 * (RealConst)digit;
					decimal_pow10 /= 10;
					break;
				}
				s++; col++;
			}
			Token token;
			token.kind = TOKEN_NUM_CONST;
			token.num = n;
			tokenizer_add(&t, &token, line_start, col_start);
			continue;
		}

		if (*s == '\'') {
			/* it's a character constant! */
			s++; col++;
			char c;
			if (*s == '\\') {
				/* escape sequence */
				s++; col++;
				/* TODO: Separate into function when string literals are added; add more of these */
				switch (*s) {
				case '\'':
					c = '\'';
					break;
				case '\\':
					c = '\\';
					break;
				case 'n':
					c = '\n';
					break;
				default:
					err_print(line, col, "Unrecognized escape character: '%c'.", *s);
					goto err;
				}
			} else {
				c = *s;
			}
			s++; col++;
			if (*s != '\'') {
				err_print(line, col, "End of character constant expected.");
				goto err;
			}
			s++; col++;
			Token token;
			token.kind = TOKEN_CHAR_CONST;
			token.chr = c;
			tokenizer_add(&t, &token, line, col);
			continue;
		}
		
		if (isidentstart(*s)) {
			/* it's an identifier */
			Identifier ident = ident_insert(&s);
			Token token;
			token.kind = TOKEN_IDENT;
			token.ident = ident;
			tokenizer_add(&t, &token, line, col);			
			continue;
		}
		int has_newline;
		char *end_of_line = strchr(s, '\n');
		has_newline = end_of_line != NULL;
		if (has_newline)
			*end_of_line = 0;
		
		err_print(line, col, TEXT_IMPORTANT("Unrecognized token:") "\n\there --> %s\n", s);
		if (has_newline)
			*end_of_line = '\n';
	err:
		has_err = 1;
		s = strchr(s, '\n');
		if (s == NULL) break;
		s++; /* move past newline */
		col = 1;
		line++;
				
	}
	if (has_err) {
		fprintf(stderr, TEXT_IMPORTANT("Errors occured while preprocessing.\n"));
		abort();
	}
	t.token = t.tokens;
	return t;
}

static void tokenizer_free(Tokenizer *t) {
	free(t->tokens);
}