summaryrefslogtreecommitdiff
path: root/igen/lexer.c
blob: 15226b83b95650aa52a1df9be409cd75ab99f95a (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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lexer.h"
#include "misc.h"
#include "preprocessor.h"

char *skip_seperators(source *src, whitespace *wspace, int preserve_src, int dbg) {
	char *text = NULL;
	char *tmp = src->text;

	src->text = max(src->text, text);

	src->text = skip_whitespace(src, wspace, 1, 1, dbg);
	src->text = skip_comment(src, wspace, dbg);

	text = src->text;
	if (preserve_src) {
		src->text = tmp;
	}
	return text;
}

cond_stmt *lex_cond_stmt(source *src, int dbg) {
	const keyword *cond_keywords[] = {
		{"do", COND_DO, NULL},
		{"for", COND_FOR, NULL},
		{"if", COND_IF, NULL},
		{"while", COND_WHILE, NULL},
	};
	const char *text = src->text;
	char *key = NULL;
	size_t key_len = 0;
	cond_type type = COND_NONE;
	whitespace wsp = {0};

	text = skip_seperators(src, &wsp, 1, dbg);
	key_len = strcspn(text, " \t\v\b");

	key = calloc(key_len+1, sizeof(char));
	memcpy(key, text, key_len);
	type = find_keyword(key, cond_keywords, NULL, NULL, dbg);
	free(key);

	text += key_len+1;

	if (type >= COND_NONE) {
		cond_stmt *cond = calloc(1, sizeof(cond_stmt));
		cond->type = type;

		src->text = text;
		src->cur.x += key_len+1;

		if (type != COND_DO) {
			cond->expr = lex_expr(src, dbg);
		}

		cond->stmt = lex_stmt(src, dbg);

		if (type == COND_DO) {
			text = skip_seperators(src, &wsp, 1, dbg);
			key_len = strcspn(text, " \t\v\b");

			key = calloc(key_len+1, sizeof(char));
			memcpy(key, text, key_len);

			if (!strcmp(key, "while")) {
				text += key_len+1;

				src->text = text;
				src->cur.x += key_len+1;

				cond->expr = lex_expr(src, dbg);

				text = skip_seperators(src, &wsp, 1, dbg);

				if (*text++ != ';') {
					--text;
					throw_error(src, 1, "Missing \';\' after do while statement.");
				} else {
					src->text = text;
					++src->cur.x;
				}
			} else {
				throw_error(src, 1, "Missing \'while\' after do while statement.");
			}
			free(key);
		}
		return s;
	}

}

stmt *lex_comp_stmt(source *src, int dbg) {
	char *text = NULL;
	whitespace wsp = {0};

	text = skip_seperators(src, &wsp, dbg);

	if (*text++ == '{') {
		stmt *s;

		src->text = text;
		++src->cur.x;

		s = lex_stmt(src, dbg);
		s->wsp = wsp;

		text = src->text;

		if (*text++ == '}') {
			++src->cur.x;
		} else {
			--text;
			throw_error(src, 1, "Missing terminating \'}\' in compound statement.");
		}
		src->text = text;
		return s;
	}
	return NULL;
}

stmt *lex_stmt(source *src, int dbg) {
	const char *text = src->text;
	const alt_stmt alts[] = {
		{STMT_FUNC, offsetof(stmt, func), lex_func},
		{STMT_EXPR, offsetof(stmt, expr), lex_exprs},
		{STMT_COND, offsetof(stmt, cond_stmt), lex_cond_stmt},
		{STMT_COMP, offsetof(stmt, down), lex_comp_stmt},
	};

	for (int i = 0; i < NUM_STMTS; ++i) {
		src->text = text;
		void *data = alts[i].lex(src, dbg);
		if (data != NULL) {
			stmt *s = calloc(1, sizeof(stmt));
			void **member = (char **)s+alts[i].offset;
			*member = data;
			return s;
		}
	}

	src->text = text;
	return NULL;
}

void lex_library(source *src, int dbg) {
	src->root = lex_stmt(src, dbg);
	src->last = (src->last != NULL) ? src->last : src->root;
	for (stmt *s = src->root; s != NULL; s = lex_stmt(str, dbg)) {
		src->last->next = s;
		src->last = s;
	}
	return start;
};

int lex(source *src, int dbg) {
	char *text = src->text;

	library(src, dbg);
	src->text = text;

	return (src->root != NULL && src->last != NULL);
}