aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:13:29 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:13:29 +0000
commit8bbefe95d25fa2981add4df35bdd9903c91a8e62 (patch)
treef45cabee854e1ef7e4b4ce82d4ceced0a47ae2a5 /src/lexer.c
downloadlab-01-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/lexer.c b/src/lexer.c
new file mode 100644
index 0000000..a219c91
--- /dev/null
+++ b/src/lexer.c
@@ -0,0 +1,172 @@
1#include "lexer.h"
2#include <regex.h>
3#include <assert.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <ctype.h>
7#include "dynamic-array.h"
8
9#define X(SYM, STR, REG) [LEXEME_KIND_##SYM - 1] = "^(" REG ")",
10static const char *lexeme_regex_strings[LEXEME_KIND_COUNT - 1] = { LEXEME_KINDS () };
11#undef X
12
13static regex_t lexeme_regexes[LEXEME_KIND_COUNT - 1];
14
15static void
16print_regex_error (const regex_t *comp, const char *regex, int errcode)
17{
18 size_t length = regerror (errcode, comp, NULL, 0);
19 char *buffer = malloc (length);
20 regerror (errcode, comp, buffer, length);
21 fprintf (stderr, "error: regcomp (\"%s\"): %s\n",
22 regex, buffer);
23 free (buffer);
24}
25
26static void __attribute__((constructor))
27compile_regexes (void)
28{
29 for (enum lexeme_kind kind = 1; kind < LEXEME_KIND_COUNT; ++kind) {
30 int errcode = regcomp (&lexeme_regexes[kind - 1],
31 lexeme_regex_strings[kind - 1],
32 REG_EXTENDED);
33 if (errcode) {
34 print_regex_error (&lexeme_regexes[kind - 1],
35 lexeme_regex_strings[kind - 1],
36 errcode);
37 exit (1);
38 }
39 }
40}
41
42struct lexer
43lexer_create (const char *string, size_t len)
44{
45 return (struct lexer) {
46 .sv = {
47 .beg = string,
48 .end = string + len,
49 },
50 .cursor = string,
51 .beginning_of_line = string,
52 .line = 0,
53 };
54}
55
56static void
57lexer_skip_whitespace (struct lexer *lexer)
58{
59 while (lexer->cursor < lexer->sv.end && isspace (*lexer->cursor)) {
60 if (*lexer->cursor == '\n') {
61 lexer->line += 1;
62 lexer->beginning_of_line = lexer->cursor + 1;
63 }
64 lexer->cursor += 1;
65 }
66}
67
68static void
69lexer_progress_cursor_by (struct lexer *lexer, int count)
70{
71 const char *end = lexer->cursor + count;
72 ASSERT (end <= lexer->sv.end);
73
74 while (lexer->cursor < end) {
75 if (*lexer->cursor == '\n') {
76 lexer->line += 1;
77 lexer->beginning_of_line = lexer->cursor + 1;
78 }
79 lexer->cursor += 1;
80 }
81}
82
83struct lexeme
84lexer_next_lexeme (struct lexer *lexer)
85{
86 lexer_skip_whitespace (lexer);
87 struct lexeme lexeme = { .sv.beg = lexer->cursor };
88
89 if (lexer->cursor >= lexer->sv.end) {
90 lexeme.kind = LEXEME_KIND_END_OF_FILE;
91 lexeme.sv.end = lexer->cursor;
92 return lexeme;
93 }
94
95 regmatch_t regmatch;
96 enum lexeme_kind longest_matching_lexeme_kind = LEXEME_KIND_INVALID;
97 int longest_matching_lexeme_kind_length = 0;
98 for (enum lexeme_kind lexeme_kind = 1; lexeme_kind < LEXEME_KIND_COUNT; ++lexeme_kind) {
99 if (regexec (&lexeme_regexes[lexeme_kind - 1], lexer->cursor, 1, &regmatch, 0)) continue;
100 if (regmatch.rm_eo <= longest_matching_lexeme_kind_length) continue;
101 longest_matching_lexeme_kind_length = regmatch.rm_eo;
102 longest_matching_lexeme_kind = lexeme_kind;
103 }
104
105 if (longest_matching_lexeme_kind) {
106 lexeme.kind = longest_matching_lexeme_kind;
107 lexer_progress_cursor_by (lexer, longest_matching_lexeme_kind_length);
108 lexeme.sv.end = lexer->cursor;
109
110 switch (lexeme.kind) {
111 case LEXEME_KIND_KEYWORD: {
112 for (enum keyword keyword = 0; keyword < KEYWORD_COUNT; ++keyword) {
113 struct string_view keyword_sv = {
114 .beg = keyword_names[keyword],
115 .end = keyword_names[keyword] + strlen (keyword_names[keyword]),
116 }; if (!STRING_VIEW_EQUALS (lexeme.sv, keyword_sv)) continue;
117
118 lexeme.value.table_index = keyword;
119 }
120 } break;
121 case LEXEME_KIND_SYMBOL_COMPARISON: {
122 for (enum comparison comparison = 0; comparison < COMPARISON_COUNT; ++comparison) {
123 struct string_view comparison_sv = {
124 .beg = comparison_names[comparison],
125 .end = comparison_names[comparison] + strlen (comparison_names[comparison]),
126 }; if (!STRING_VIEW_EQUALS (lexeme.sv, comparison_sv)) continue;
127
128 lexeme.value.table_index = comparison;
129 }
130 } break;
131 case LEXEME_KIND_CONSTANT_INTEGER: {
132 TODO ("fix overflow");
133 lexeme.value.integer = strtol (lexeme.sv.beg, NULL, 10);
134 } break;
135 case LEXEME_KIND_CONSTANT_REAL:
136 TODO ("fix overflow");
137 lexeme.value.real = strtof (lexeme.sv.beg, NULL);
138 break;
139 case LEXEME_KIND_CONSTANT_STRING: {
140 struct string_view sv = {
141 .beg = lexeme.sv.beg + 1,
142 .end = lexeme.sv.end - 1,
143 };
144 DYNAMIC_ARRAY_FOREACH (e, lexer->constant_string_table) {
145 if (STRING_VIEW_EQUALS (sv, *e)) {
146 lexeme.value.table_index = e - lexer->constant_string_table.items;
147 return lexeme;
148 }
149 }
150 lexeme.value.table_index = lexer->constant_string_table.count;
151 DYNAMIC_ARRAY_APPEND (lexer->constant_string_table, sv);
152 } break;
153 case LEXEME_KIND_IDENTIFIER: {
154 DYNAMIC_ARRAY_FOREACH (e, lexer->identifier_table) {
155 if (STRING_VIEW_EQUALS (lexeme.sv, *e)) {
156 lexeme.value.table_index = e - lexer->identifier_table.items;
157 return lexeme;
158 }
159 }
160 lexeme.value.table_index = lexer->identifier_table.count;
161 DYNAMIC_ARRAY_APPEND (lexer->identifier_table, lexeme.sv);
162 } break;
163 default:
164 break;
165 }
166
167 return lexeme;
168 }
169
170 lexeme.sv.end = ++lexer->cursor;
171 return lexeme;
172}