summaryrefslogtreecommitdiff
path: root/masm/lex.c
blob: b835a7f96c3c7a2309c16759a9cd4597182202e9 (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
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
432
433
434
435
436
437
438
439
440
441
442
#include "lex.h"

#include <mlimits.h>
#include <merror.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>

static struct {
	int x;
	int y;
} pos;

/* get next char in lexer */
static int lex_next(struct lexer *lexer)
{
	if (lexer->peek != EOF) {
		int c = lexer->peek;
		lexer->peek = EOF;
		return c;
	}

	int c = getc(lexer->file);
	if (c == '\n') {
		lexer->x = 1;
		lexer->y++;
	} else {
		lexer->x++;
	}
	return c;
}

/* peek next char in lexer */
static int lex_peek(struct lexer *lexer)
{
	if (lexer->peek == EOF)
		lexer->peek = lex_next(lexer);
	return lexer->peek;
}

/* skip all characters until EOF or newline */
static void skip_comment(struct lexer *lexer)
{
	int c;
	while (1) {
		c = lex_next(lexer);
		if (c == EOF || c == '\n')
			break;
	}
}

/* lexes a string until closing quote
 * returns error if string is too long or hit newline */
static int lex_string(struct lexer *lexer, struct string *string)
{
	char c;
	string_init(string);

	while (1) {
		c = lex_next(lexer);

		// stop on ending quote
		if (c == '"')
			break;

		// strings cannot span multiple lines
		if (c == '\n') {
			ERROR_POS(pos, "reached newline before end of string");
			string_free(string);
			return M_ERROR;
		}

		// match escape character
		if (c == '\\') {
			switch (lex_peek(lexer)) {
			case 'n':
				c = '\n';
				lex_next(lexer);
				break;
			case 't':
				c = '\t';
				lex_next(lexer);
				break;
			case '\\':
				c = '\\';
				lex_next(lexer);
				break;
			case '"':
				c = '"';
				lex_next(lexer);
				break;
			}
		}

		// push char into string
		if (string_push(string, c)) {
			string_free(string);
			return M_ERROR;
		}
	}

	// null terminate string
	if (string_push(string, '\0')) {
		free(string->str);
		return M_ERROR;
	}

	return M_SUCCESS;
}

/* lexes text until whitespace
 * returns error on zero length or too long */
static int lex_ident(struct lexer *lexer, struct string *string,
		     char prefix)
{
	char c;
	string_init(string);

	if (prefix != '\0' && string_push(string, prefix)) {
		string_free(string);
		return M_ERROR;
	}

	while (1) {
		c = lex_peek(lexer);
		if (!(
			(c >= 'a' && c <= 'z') ||
			(c >= 'A' && c <= 'Z') ||
			(c >= '0' && c <= '9') ||
			(c == '_')
		)) {
			break;
		}

		// pop char out of lexer
		lex_next(lexer);

		// push char into string
		if (string_push(string, c)) {
			free(string->str);
			return M_ERROR;
		}
	}

	// empty idents are not allowed
	if (string->len < 1) {
		string_free(string);
		ERROR("empty ident tokens are not allowed");
		return M_ERROR;
	}

	// null terminate string
	if (string_push(string, '\0')) {
		string_free(string);
		return M_ERROR;
	}

	return M_SUCCESS;
}


/* lexes a integer number in base 2,8,10, or 16,
 * uses base 10 by default but chan be changed by 0b, 0o, and 0x */
static int lex_number(struct lexer *lexer, int64_t *n)
{
	int64_t number = 0;
	int base = 10;
	int neg = 0;

	// check if negative
	if (lex_peek(lexer) == '-') {
		lex_next(lexer);
		neg = 1;
	}


	// skip all leading zeros, they dont do anything.
	// this also allows us to directly check for 0b, 0o, and 0x
	// right away!
	while (1) {
		if (lex_peek(lexer) == '0')
			lex_next(lexer);
		else
			break;
	}

	// match change of base
	switch (lex_peek(lexer)) {
	case 'b':
		base = 2;
		lex_next(lexer);
		break;
	case 'o':
		base = 8;
		lex_next(lexer);
		break;
	case 'x':
		base = 16;
		lex_next(lexer);
		break;
	}

	while (1) {
		char c = lex_peek(lexer);
		int n = 0;
		if (c >= '0' && c <= '9') {
			n = c - '0';
		} else if (c >= 'a' && c <= 'z') { // match A-Z so we can
			n = c - 'a' + 10;          // catch the errors
		} else if (c >= 'A' && c <= 'Z') { // here instead of later
			n = c - 'A' + 10;
		} else {
			break; // no longer a number
		}
		// if number provided is bigger than my base,
		// error !
		if (n >= base) {
			ERROR_POS(pos, "character '%c' is bigger than number base"
				"'%d'", c, base);
			return M_ERROR;
		}
		lex_next(lexer);
		number *= base;
		number += n;
	}

	if (neg)
		number = -number;

	*n = number;
	return M_SUCCESS;
}

/* lex the next token on the file */
int lexer_next(struct lexer *lexer, struct token *token)
{
again: // use label to avoid whitespace recursion
	token->x = lexer->x;
	token->y = lexer->y;
	token->off = ftell(lexer->file);
	pos.x = lexer->x;
	pos.y = lexer->y;
	token->type = TOK_EOF;

	int c = lex_peek(lexer);
	int res = M_SUCCESS;

	switch (c) {

	case EOF:

	// return a EOF token
	case '\0':
		token->type = TOK_EOF;
		break;

	// skip the comment
	// .. and return a NL token
	case ';':
	case '#':
		skip_comment(lexer);
		token->type = TOK_NL;
		break;

	// skip the whitespace and
	// try to parse the next character
	case ' ':
	case '\t':
		// skip white space
		lex_next(lexer);
		goto again;

	// return a NL token
	case '\n':
		lex_next(lexer);
		token->type = TOK_NL;
		break;

	// return a comma token
	case ',':
		lex_next(lexer);
		token->type = TOK_COMMA;
		break;

	// return a equal token
	case '=':
		lex_next(lexer);
		token->type = TOK_EQUAL;
		break;

	// return a left paren token
	case '(':
		lex_next(lexer);
		token->type = TOK_LPAREN;
		break;

	// return a right paren token
	case ')':
		token->type = TOK_RPAREN;
		lex_next(lexer);
		break;

	// return a register token
	case '$':
		token->type = TOK_REG;
		lex_next(lexer);
		res = lex_ident(lexer, &token->string, '\0');
		break;

	// return a directive token
	case '.':
		token->type = TOK_DIRECTIVE;
		lex_next(lexer);
		res = lex_ident(lexer, &token->string, '.');
		break;

	// return a string token
	case '"':
		token->type = TOK_STRING;
		lex_next(lexer);
		res = lex_string(lexer, &token->string);
		break;

	// return a number token
	case '-':
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
		token->type = TOK_NUMBER;
		res = lex_number(lexer, &token->number);
		break;

	// return a ident or label token depending
	// if it ends with a colon
	default:
		token->type = TOK_IDENT;
		res = lex_ident(lexer, &token->string, '\0');
		if (lex_peek(lexer) == ':') {
			lex_next(lexer);
			token->type = TOK_LABEL;
		}
		break;
	}

	return res;
}

int lexer_init(const char *path, struct lexer *lexer)
{
	/// defaults
	lexer->file = NULL;
	lexer->peek = EOF;
	lexer->x = 1;
	lexer->y = 1;

	/// load file
	lexer->file = fopen(path, "r");
	if (lexer->file == NULL) {
		PERROR("cannot read");
		return M_ERROR;
	}

	return M_SUCCESS;
}

void lexer_free(struct lexer *lexer)
{
	if (lexer->file)
		fclose(lexer->file);
}

char *token_str(enum token_type type)
{
	switch (type) {
	case TOK_IDENT:
		return "ident";
	case TOK_REG:
		return "register";
	case TOK_LABEL:
		return "label";
	case TOK_STRING:
		return "string";
	case TOK_COMMA:
		return "comma";
	case TOK_EQUAL:
		return "equal";
	case TOK_LPAREN:
		return "left parentheses";
	case TOK_RPAREN:
		return "right parentheses";
	case TOK_NUMBER:
		return "number";
	case TOK_EOF:
		return "end of file";
	case TOK_NL:
		return "new line";
	case TOK_DIRECTIVE:
		return "directive";
	}
	return "unknown";
}

/* save the current state from the lexer */
void lexer_save(struct lexer *lexer, struct lexer_state *state)
{
	state->x = lexer->x;
	state->y = lexer->y;
	state->peek = lexer->peek;
	state->offset = ftell(lexer->file);
}

/* load a different state into a lexer */
void lexer_load(struct lexer *lexer, const struct lexer_state *state)
{
	lexer->x = state->x;
	lexer->y = state->y;
	lexer->peek = state->peek;
	fseek(lexer->file, state->offset, SEEK_SET);
}

void token_free(struct token *token)
{
	switch (token->type) {
	case TOK_REG:
	case TOK_IDENT:
	case TOK_LABEL:
	case TOK_STRING:
	case TOK_DIRECTIVE:
		if (token->string.str)
			free(token->string.str);
		break;
	default:
	}
}