diff options
| author | 2026-11-08 05:13:29 +0000 | |
|---|---|---|
| committer | 2026-11-08 05:13:29 +0000 | |
| commit | 8bbefe95d25fa2981add4df35bdd9903c91a8e62 (patch) | |
| tree | f45cabee854e1ef7e4b4ce82d4ceced0a47ae2a5 /src | |
| download | lab-01-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic-array.h | 59 | ||||
| -rw-r--r-- | src/export.c | 19 | ||||
| -rw-r--r-- | src/lex.c | 220 | ||||
| -rw-r--r-- | src/lexeme.c | 13 | ||||
| -rw-r--r-- | src/lexeme.h | 74 | ||||
| -rw-r--r-- | src/lexer.c | 172 | ||||
| -rw-r--r-- | src/lexer.h | 26 | ||||
| -rw-r--r-- | src/string-builder.c | 41 | ||||
| -rw-r--r-- | src/string-builder.h | 42 | ||||
| -rw-r--r-- | src/string-view.h | 16 | ||||
| -rw-r--r-- | src/utils.h | 26 |
11 files changed, 708 insertions, 0 deletions
diff --git a/src/dynamic-array.h b/src/dynamic-array.h new file mode 100644 index 0000000..6a02769 --- /dev/null +++ b/src/dynamic-array.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #ifndef INCLUDED_DYNAMIC_ARRAY_H | ||
| 2 | #define INCLUDED_DYNAMIC_ARRAY_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #ifndef DYNAMIC_ARRAY_INIT_CAP | ||
| 7 | #define DYNAMIC_ARRAY_INIT_CAP 256 | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #define DYNAMIC_ARRAY_ENSURE_CAPACITY(DA, COUNT) \ | ||
| 11 | ({ \ | ||
| 12 | if ((DA).count + (COUNT) > (DA).capacity) { \ | ||
| 13 | if ((DA).capacity == 0) \ | ||
| 14 | (DA).capacity = DYNAMIC_ARRAY_INIT_CAP; \ | ||
| 15 | while ((DA).count + (COUNT) > (DA).capacity) \ | ||
| 16 | (DA).capacity *= 2; \ | ||
| 17 | (DA).items = REALLOC((DA).items, \ | ||
| 18 | (DA).capacity * \ | ||
| 19 | sizeof (*(DA).items)); \ | ||
| 20 | ASSERT((DA).items); \ | ||
| 21 | } \ | ||
| 22 | }) | ||
| 23 | |||
| 24 | #define DYNAMIC_ARRAY_SHRINK(DA) \ | ||
| 25 | ({ \ | ||
| 26 | (DA).capacity = (DA).count; \ | ||
| 27 | (DA).items = REALLOC((DA).items, \ | ||
| 28 | (DA).capacity * \ | ||
| 29 | sizeof (*(DA).items)); \ | ||
| 30 | ASSERT((DA).items); \ | ||
| 31 | \ | ||
| 32 | }) | ||
| 33 | |||
| 34 | #define DYNAMIC_ARRAY_APPEND(DA, ITEM) \ | ||
| 35 | ({ \ | ||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), 1); \ | ||
| 37 | (DA).items[(DA).count++] = (ITEM); \ | ||
| 38 | }) | ||
| 39 | |||
| 40 | #define DYNAMIC_ARRAY_APPEND_MANY(DA, NEW_ITEMS, NEW_ITEMS_COUNT) \ | ||
| 41 | ({ \ | ||
| 42 | DYNAMIC_ARRAY_ENSURE_CAPACITY((DA), (NEW_ITEMS_COUNT)); \ | ||
| 43 | memcpy ((DA).items + (DA).count, (NEW_ITEMS), \ | ||
| 44 | (NEW_ITEMS_COUNT)*sizeof (*(DA).items)); \ | ||
| 45 | (DA).count += (NEW_ITEMS_COUNT); \ | ||
| 46 | }) | ||
| 47 | |||
| 48 | #define DYNAMIC_ARRAY_LAST(DA) \ | ||
| 49 | (DA).items[(DA).count - 1] | ||
| 50 | |||
| 51 | #define DYNAMIC_ARRAY_RESET(DA) ({ (DA).count = 0; }) | ||
| 52 | |||
| 53 | #define DYNAMIC_ARRAY_FREE(DA) FREE((DA).items) | ||
| 54 | |||
| 55 | #define DYNAMIC_ARRAY_FOREACH(ITEM, DA) for ( \ | ||
| 56 | typeof ((DA).items) ITEM = (DA).items; \ | ||
| 57 | ITEM < (DA).items + (DA).count; ++ITEM) | ||
| 58 | |||
| 59 | #endif // INCLUDED_DYNAMIC_ARRAY_H | ||
diff --git a/src/export.c b/src/export.c new file mode 100644 index 0000000..3477ef6 --- /dev/null +++ b/src/export.c | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include "utils.h" | ||
| 3 | #include "lexeme.h" | ||
| 4 | #include "lexer.c" | ||
| 5 | |||
| 6 | int | ||
| 7 | main (int argc, char *argv[]) | ||
| 8 | { | ||
| 9 | UNUSED (argc); | ||
| 10 | UNUSED (argv); | ||
| 11 | |||
| 12 | for (enum lexeme_kind kind = 1; kind < LEXEME_KIND_COUNT; ++kind) { | ||
| 13 | printf ("- %s --- =%s=\n", | ||
| 14 | lexeme_kind_names[kind - 1], | ||
| 15 | lexeme_regex_strings[kind - 1]); | ||
| 16 | } | ||
| 17 | |||
| 18 | return EXIT_SUCCESS; | ||
| 19 | } | ||
diff --git a/src/lex.c b/src/lex.c new file mode 100644 index 0000000..3b10266 --- /dev/null +++ b/src/lex.c | |||
| @@ -0,0 +1,220 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <stdarg.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <assert.h> | ||
| 7 | #include <regex.h> | ||
| 8 | #include <errno.h> | ||
| 9 | #include <ctype.h> | ||
| 10 | #include <fcntl.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | #include <sys/stat.h> | ||
| 13 | #include <sys/mman.h> | ||
| 14 | #include "utils.h" | ||
| 15 | #include "string-builder.h" | ||
| 16 | #include "lexer.h" | ||
| 17 | |||
| 18 | const char *file; | ||
| 19 | |||
| 20 | static void | ||
| 21 | print_lexing_error (const struct lexer *lexer, | ||
| 22 | const struct lexeme *lexeme, | ||
| 23 | const char *fmt, ...) | ||
| 24 | { | ||
| 25 | const char *beginning_of_line = lexer->sv.beg; | ||
| 26 | size_t line = 1; | ||
| 27 | |||
| 28 | for (const char *p = lexer->sv.beg; p < lexeme->sv.beg; ++p) { | ||
| 29 | if (*p == '\n') { | ||
| 30 | beginning_of_line = p + 1; | ||
| 31 | line += 1; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | fprintf (stderr, "%s:%d:%d: error: ", file, (int) line, | ||
| 36 | (int) (lexeme->sv.beg - beginning_of_line)); | ||
| 37 | va_list ap; | ||
| 38 | va_start (ap, fmt); | ||
| 39 | vfprintf (stderr, fmt, ap); | ||
| 40 | va_end (ap); | ||
| 41 | putc ('\n', stderr); | ||
| 42 | |||
| 43 | const char *p = beginning_of_line; | ||
| 44 | fprintf (stderr, "%5lu | ", line); | ||
| 45 | while (*p != '\n') | ||
| 46 | putc (*p++, stderr); | ||
| 47 | putc ('\n', stderr); | ||
| 48 | |||
| 49 | p = beginning_of_line; | ||
| 50 | fprintf (stderr, " | "); | ||
| 51 | while (p++ < lexeme->sv.beg) | ||
| 52 | putc (' ', stderr); | ||
| 53 | putc ('^', stderr); | ||
| 54 | while (p++ < lexeme->sv.end) | ||
| 55 | putc ('~', stderr); | ||
| 56 | putc ('\n', stderr); | ||
| 57 | } | ||
| 58 | |||
| 59 | static int | ||
| 60 | display_to_byte_len (const char *str, int len) | ||
| 61 | { | ||
| 62 | while (*str) { | ||
| 63 | if (*str++ & 0xD0) { | ||
| 64 | len += 1; | ||
| 65 | str += 1; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | return len; | ||
| 70 | } | ||
| 71 | |||
| 72 | int | ||
| 73 | main (int argc, char *argv[]) | ||
| 74 | { | ||
| 75 | const char *progname = *argv++; | ||
| 76 | if (argc < 2) DIE ("Usage: %s file", progname); | ||
| 77 | |||
| 78 | file = *argv++; | ||
| 79 | int fd = open (file, O_RDONLY); | ||
| 80 | if (fd == -1) DIE ("error: %s: %s", strerror (errno), file); | ||
| 81 | |||
| 82 | struct stat st; | ||
| 83 | fstat (fd, &st); | ||
| 84 | char *string = mmap (NULL, st.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0); | ||
| 85 | close (fd); | ||
| 86 | |||
| 87 | puts ("Исходный текст:"); | ||
| 88 | fwrite (string, 1, st.st_size, stdout); | ||
| 89 | |||
| 90 | FILE *dump = fopen ("./build/dump.txt", "w"); | ||
| 91 | |||
| 92 | struct { | ||
| 93 | struct lexeme *items; | ||
| 94 | uint32_t count; | ||
| 95 | uint32_t capacity; | ||
| 96 | } invalid_lexemes = {0}; | ||
| 97 | |||
| 98 | struct string_builder sb = {0}; | ||
| 99 | struct lexer lexer = lexer_create (string, st.st_size); | ||
| 100 | putchar ('\n'); | ||
| 101 | printf ("| *Лексема* | *Тип лексемы* | *Значение* |\n"); | ||
| 102 | printf ("|------------------+-----------------------------+------------|\n"); | ||
| 103 | for (struct lexeme lexeme = lexer_next_lexeme (&lexer); | ||
| 104 | lexeme.kind != LEXEME_KIND_END_OF_FILE; | ||
| 105 | lexeme = lexer_next_lexeme (&lexer)) { | ||
| 106 | switch (lexeme.kind) { | ||
| 107 | case LEXEME_KIND_INVALID: | ||
| 108 | case LEXEME_KIND_IDENTIFIER_TOO_LONG: | ||
| 109 | case LEXEME_KIND_CONSTANT_STRING_TOO_LONG: | ||
| 110 | case LEXEME_KIND_CONSTANT_NUMBER_INVALID: | ||
| 111 | case LEXEME_KIND_COMMENT_UNTERMINATED: | ||
| 112 | DYNAMIC_ARRAY_APPEND (invalid_lexemes, lexeme); | ||
| 113 | continue; | ||
| 114 | default: break; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (lexeme.kind == LEXEME_KIND_COMMENT) | ||
| 118 | continue; | ||
| 119 | |||
| 120 | fwrite (lexeme.sv.beg, STRING_VIEW_LEN (lexeme.sv), 1, dump); | ||
| 121 | putc (' ', dump); | ||
| 122 | |||
| 123 | switch (lexeme.kind) { | ||
| 124 | case LEXEME_KIND_KEYWORD: | ||
| 125 | string_builder_printf (&sb, "K[%d]", lexeme.value.table_index); | ||
| 126 | break; | ||
| 127 | case LEXEME_KIND_SYMBOL_COMPARISON: | ||
| 128 | string_builder_printf (&sb, "C[%d]", lexeme.value.table_index); | ||
| 129 | break; | ||
| 130 | case LEXEME_KIND_CONSTANT_INTEGER: | ||
| 131 | string_builder_printf (&sb, "%d", lexeme.value.integer); | ||
| 132 | break; | ||
| 133 | case LEXEME_KIND_CONSTANT_REAL: | ||
| 134 | string_builder_printf (&sb, "%g", lexeme.value.real); | ||
| 135 | break; | ||
| 136 | case LEXEME_KIND_CONSTANT_STRING: | ||
| 137 | string_builder_printf (&sb, "S[%d]", lexeme.value.table_index); | ||
| 138 | break; | ||
| 139 | case LEXEME_KIND_IDENTIFIER: | ||
| 140 | string_builder_printf (&sb, "I[%d]", lexeme.value.table_index); | ||
| 141 | break; | ||
| 142 | default: | ||
| 143 | STRING_BUILDER_APPEND_CSTR (sb, ""); | ||
| 144 | } | ||
| 145 | string_builder_append_ntimes (&sb, ' ', 10 - sb.count); | ||
| 146 | STRING_BUILDER_APPEND_NULL (sb); | ||
| 147 | printf ("| %-16.*s | %-*s | %s |\n", | ||
| 148 | (int) STRING_VIEW_LEN (lexeme.sv), lexeme.sv.beg, | ||
| 149 | display_to_byte_len (lexeme_kind_names[lexeme.kind - 1], 27), | ||
| 150 | lexeme_kind_names[lexeme.kind - 1], | ||
| 151 | sb.items); | ||
| 152 | STRING_BUILDER_RESET (sb); | ||
| 153 | } | ||
| 154 | |||
| 155 | fclose (dump); | ||
| 156 | |||
| 157 | putchar ('\n'); | ||
| 158 | printf ("| *№* | *K* |\n"); | ||
| 159 | printf ("|-----+-----|\n"); | ||
| 160 | for (int32_t i = 0; i < KEYWORD_COUNT; ++i) | ||
| 161 | printf ("| %3d | %-3s |\n", i, keyword_names[i]); | ||
| 162 | |||
| 163 | putchar ('\n'); | ||
| 164 | printf ("| *№* | *C* |\n"); | ||
| 165 | printf ("|-----+-----|\n"); | ||
| 166 | for (int32_t i = 0; i < COMPARISON_COUNT; ++i) | ||
| 167 | printf ("| %3d | %-3s |\n", i, comparison_names[i]); | ||
| 168 | |||
| 169 | putchar ('\n'); | ||
| 170 | printf ("| *№* | *S* |\n"); | ||
| 171 | printf ("|-----+----------------|\n"); | ||
| 172 | DYNAMIC_ARRAY_FOREACH (e, lexer.constant_string_table) { | ||
| 173 | printf ("| %3ld | %-14.*s |\n", e - lexer.constant_string_table.items, | ||
| 174 | (int) STRING_VIEW_LEN (*e), e->beg); | ||
| 175 | } | ||
| 176 | |||
| 177 | putchar ('\n'); | ||
| 178 | printf ("| *№* | *I* |\n"); | ||
| 179 | printf ("|-----+------------------|\n"); | ||
| 180 | DYNAMIC_ARRAY_FOREACH (e, lexer.identifier_table) { | ||
| 181 | printf ("| %3ld | %-16.*s |\n", e - lexer.identifier_table.items, | ||
| 182 | (int) STRING_VIEW_LEN (*e), e->beg); | ||
| 183 | } | ||
| 184 | |||
| 185 | DYNAMIC_ARRAY_FOREACH (invalid_lexeme, invalid_lexemes) { | ||
| 186 | switch (invalid_lexeme->kind) { | ||
| 187 | case LEXEME_KIND_INVALID: | ||
| 188 | print_lexing_error (&lexer, invalid_lexeme, "invalid lexeme `%.*s'", | ||
| 189 | (int) STRING_VIEW_LEN (invalid_lexeme->sv), | ||
| 190 | invalid_lexeme->sv.beg); | ||
| 191 | break; | ||
| 192 | case LEXEME_KIND_CONSTANT_STRING_TOO_LONG: | ||
| 193 | print_lexing_error (&lexer, invalid_lexeme, | ||
| 194 | "string constant longer than 16 symbols (%d)", | ||
| 195 | (int) STRING_VIEW_LEN (invalid_lexeme->sv)); | ||
| 196 | break; | ||
| 197 | case LEXEME_KIND_IDENTIFIER_TOO_LONG: | ||
| 198 | print_lexing_error (&lexer, invalid_lexeme, | ||
| 199 | "identifier longer than 16 symbols (%d)", | ||
| 200 | (int) STRING_VIEW_LEN (invalid_lexeme->sv)); | ||
| 201 | break; | ||
| 202 | case LEXEME_KIND_CONSTANT_NUMBER_INVALID: | ||
| 203 | print_lexing_error (&lexer, invalid_lexeme, "invalid number constant `%.*s'", | ||
| 204 | (int) STRING_VIEW_LEN (invalid_lexeme->sv), | ||
| 205 | invalid_lexeme->sv.beg); | ||
| 206 | break; | ||
| 207 | case LEXEME_KIND_COMMENT_UNTERMINATED: | ||
| 208 | invalid_lexeme->sv.end = invalid_lexeme->sv.beg + 2; | ||
| 209 | print_lexing_error (&lexer, invalid_lexeme, "unterminated comment", | ||
| 210 | (int) STRING_VIEW_LEN (invalid_lexeme->sv), | ||
| 211 | invalid_lexeme->sv.beg); | ||
| 212 | break; | ||
| 213 | default: break; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | munmap (string, st.st_size); | ||
| 218 | |||
| 219 | return EXIT_SUCCESS; | ||
| 220 | } | ||
diff --git a/src/lexeme.c b/src/lexeme.c new file mode 100644 index 0000000..41ae8c8 --- /dev/null +++ b/src/lexeme.c | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | #include "lexeme.h" | ||
| 2 | |||
| 3 | #define X(SYM, STR, ...) [LEXEME_KIND_##SYM - 1] = (STR), | ||
| 4 | const char *lexeme_kind_names[LEXEME_KIND_COUNT - 1] = { LEXEME_KINDS () }; | ||
| 5 | #undef X | ||
| 6 | |||
| 7 | #define X(SYM, STR, ...) [KEYWORD_##SYM] = (STR), | ||
| 8 | const char *keyword_names[KEYWORD_COUNT] = { KEYWORDS () }; | ||
| 9 | #undef X | ||
| 10 | |||
| 11 | #define X(SYM, STR, ...) [COMPARISON_##SYM] = (STR), | ||
| 12 | const char *comparison_names[COMPARISON_COUNT] = { COMPARISONS () }; | ||
| 13 | #undef X | ||
diff --git a/src/lexeme.h b/src/lexeme.h new file mode 100644 index 0000000..ebb8958 --- /dev/null +++ b/src/lexeme.h | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | #ifndef INCLUDED_LEXEME_H | ||
| 2 | #define INCLUDED_LEXEME_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include "utils.h" | ||
| 7 | #include "string-view.h" | ||
| 8 | |||
| 9 | // Входной язык содержит операторы цикла for (...; ...; ...) do, | ||
| 10 | // разделенные символом | (вертикальная полоса). Операторы цикла | ||
| 11 | // содержат идентификаторы, знаки сравнения <, >, =, строковые | ||
| 12 | // константы (последовательность символов в двойных кавычках), знак | ||
| 13 | // присваивания (:=). | ||
| 14 | #define LEXEME_KINDS() \ | ||
| 15 | X (KEYWORD, "Ключевое слово", "for|do" ) \ | ||
| 16 | X (IDENTIFIER_TOO_LONG, "Слишком длинный идентификатор", "[A-Z_a-z][0-9A-Z_a-z]{16,}" ) \ | ||
| 17 | X (IDENTIFIER, "Идентификатор", "[A-Z_a-z][0-9A-Z_a-z]*" ) \ | ||
| 18 | X (SYMBOL_SEPARATOR, "Символ вертикальной полосы", "\\|" ) \ | ||
| 19 | X (SYMBOL_ASSIGNMENT, "Знак присваивания", ":=" ) \ | ||
| 20 | X (SYMBOL_COMPARISON, "Знак операции сравнения", "<|=|>" ) \ | ||
| 21 | X (SYMBOL_PARENTHESIS_LEFT, "Знак левой скобки", "\\(" ) \ | ||
| 22 | X (SYMBOL_PARENTHESIS_RIGHT, "Знак правой скобки", "\\)" ) \ | ||
| 23 | X (SYMBOL_SEMICOLON, "Знак точки с запятой", ";" ) \ | ||
| 24 | X (CONSTANT_NUMBER_INVALID, "Недопустимая числовая константа", "-?[0-9]*\\.?[0-9]+[A-Z_a-z][0-9A-Z_a-z]+") \ | ||
| 25 | X (CONSTANT_INTEGER, "Целочиселная константа", "-?[0-9]+" ) \ | ||
| 26 | X (CONSTANT_REAL, "Вещественная константа", "-?[0-9]*\\.[0-9]+" ) \ | ||
| 27 | X (CONSTANT_STRING_TOO_LONG, "Слишком длинная строковая константа", "\"[^\"]{15,}\"" ) \ | ||
| 28 | X (CONSTANT_STRING, "Строковая константа", "\"[^\"]{0,14}\"" ) \ | ||
| 29 | X (COMMENT, "Комментарий", "//[^\n]*|/\\*+([^*]|\\*[^/])*\\*+/" ) \ | ||
| 30 | X (COMMENT_UNTERMINATED, "Незавершенный Комментарий", "/\\*([^*]|]\\*[^/])*" ) | ||
| 31 | |||
| 32 | #define X(SYM, ...) LEXEME_KIND_##SYM, | ||
| 33 | enum lexeme_kind { | ||
| 34 | LEXEME_KIND_INVALID, | ||
| 35 | LEXEME_KINDS () | ||
| 36 | LEXEME_KIND_COUNT, | ||
| 37 | LEXEME_KIND_END_OF_FILE, | ||
| 38 | }; | ||
| 39 | #undef X | ||
| 40 | |||
| 41 | #define KEYWORDS() \ | ||
| 42 | X (FOR, "for") \ | ||
| 43 | X (DO, "do") | ||
| 44 | |||
| 45 | #define COMPARISONS() \ | ||
| 46 | X (EQUALS, "=") \ | ||
| 47 | X (LESS_THAN, "<") \ | ||
| 48 | X (GREATER_THAN, ">") | ||
| 49 | |||
| 50 | #define X(SYM, ...) KEYWORD_##SYM, | ||
| 51 | enum keyword { KEYWORDS () KEYWORD_COUNT }; | ||
| 52 | #undef X | ||
| 53 | |||
| 54 | #define X(SYM, ...) COMPARISON_##SYM, | ||
| 55 | enum comparison { COMPARISONS () COMPARISON_COUNT }; | ||
| 56 | #undef X | ||
| 57 | |||
| 58 | struct lexeme { | ||
| 59 | struct string_view sv; | ||
| 60 | enum lexeme_kind kind; | ||
| 61 | union { | ||
| 62 | enum keyword keyword; | ||
| 63 | enum comparison comparison; | ||
| 64 | int integer; | ||
| 65 | float real; | ||
| 66 | uint32_t table_index; | ||
| 67 | } value; | ||
| 68 | }; | ||
| 69 | |||
| 70 | extern const char *lexeme_kind_names[LEXEME_KIND_COUNT - 1]; | ||
| 71 | extern const char *keyword_names[KEYWORD_COUNT]; | ||
| 72 | extern const char *comparison_names[COMPARISON_COUNT]; | ||
| 73 | |||
| 74 | #endif | ||
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 ")", | ||
| 10 | static const char *lexeme_regex_strings[LEXEME_KIND_COUNT - 1] = { LEXEME_KINDS () }; | ||
| 11 | #undef X | ||
| 12 | |||
| 13 | static regex_t lexeme_regexes[LEXEME_KIND_COUNT - 1]; | ||
| 14 | |||
| 15 | static void | ||
| 16 | print_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 | |||
| 26 | static void __attribute__((constructor)) | ||
| 27 | compile_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 | |||
| 42 | struct lexer | ||
| 43 | lexer_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 | |||
| 56 | static void | ||
| 57 | lexer_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 | |||
| 68 | static void | ||
| 69 | lexer_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 | |||
| 83 | struct lexeme | ||
| 84 | lexer_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, ®match, 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 | } | ||
diff --git a/src/lexer.h b/src/lexer.h new file mode 100644 index 0000000..563dc60 --- /dev/null +++ b/src/lexer.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #ifndef INCLUDED_LEXER_H | ||
| 2 | #define INCLUDED_LEXER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include "utils.h" | ||
| 6 | #include "lexeme.h" | ||
| 7 | |||
| 8 | struct string_view_table { | ||
| 9 | struct string_view *items; | ||
| 10 | uint32_t count; | ||
| 11 | uint32_t capacity; | ||
| 12 | }; | ||
| 13 | |||
| 14 | struct lexer { | ||
| 15 | struct string_view sv; | ||
| 16 | const char *cursor; | ||
| 17 | const char *beginning_of_line; | ||
| 18 | size_t line; | ||
| 19 | struct string_view_table constant_string_table; | ||
| 20 | struct string_view_table identifier_table; | ||
| 21 | }; | ||
| 22 | |||
| 23 | extern struct lexer lexer_create (const char *string, size_t len); | ||
| 24 | extern struct lexeme lexer_next_lexeme (struct lexer *); | ||
| 25 | |||
| 26 | #endif | ||
diff --git a/src/string-builder.c b/src/string-builder.c new file mode 100644 index 0000000..5880da8 --- /dev/null +++ b/src/string-builder.c | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #include "string-builder.h" | ||
| 2 | #include <stdarg.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | void | ||
| 6 | string_builder_append_ntimes (STRING_BUILDER sb, char c, size_t n) | ||
| 7 | { | ||
| 8 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, n); | ||
| 9 | memset (sb->items + sb->count, c, n); | ||
| 10 | sb->count += n; | ||
| 11 | } | ||
| 12 | |||
| 13 | void | ||
| 14 | string_builder_append_cstrs (STRING_BUILDER sb, ...) | ||
| 15 | { | ||
| 16 | va_list args; | ||
| 17 | |||
| 18 | va_start (args, sb); | ||
| 19 | const char *arg = va_arg (args, const char *); | ||
| 20 | while (arg) { | ||
| 21 | STRING_BUILDER_APPEND_CSTR(*sb, arg); | ||
| 22 | arg = va_arg (args, const char *); | ||
| 23 | } va_end (args); | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | string_builder_printf (STRING_BUILDER sb, const char *format, ...) | ||
| 28 | { | ||
| 29 | va_list args; | ||
| 30 | |||
| 31 | va_start (args, format); | ||
| 32 | int size = vsnprintf (NULL, 0, format, args); | ||
| 33 | va_end (args); | ||
| 34 | ASSERT (size >= 0); | ||
| 35 | |||
| 36 | DYNAMIC_ARRAY_ENSURE_CAPACITY (*sb, size + 1); | ||
| 37 | va_start (args, format); | ||
| 38 | vsnprintf (&sb->items[sb->count], size + 1, format, args); | ||
| 39 | va_end (args); | ||
| 40 | sb->count += size; | ||
| 41 | } | ||
diff --git a/src/string-builder.h b/src/string-builder.h new file mode 100644 index 0000000..6834eed --- /dev/null +++ b/src/string-builder.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #ifndef INCLUDED_STRING_BUILDER_H | ||
| 2 | #define INCLUDED_STRING_BUILDER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include "utils.h" | ||
| 7 | #include "dynamic-array.h" | ||
| 8 | |||
| 9 | struct string_builder { | ||
| 10 | char *items; | ||
| 11 | size_t count; | ||
| 12 | size_t capacity; | ||
| 13 | }; | ||
| 14 | #define STRING_BUILDER struct string_builder * | ||
| 15 | |||
| 16 | #define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \ | ||
| 17 | DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE) | ||
| 18 | |||
| 19 | #define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \ | ||
| 20 | ({ \ | ||
| 21 | const char *s = (CSTR); \ | ||
| 22 | size_t n = strlen (s); \ | ||
| 23 | DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \ | ||
| 24 | }) | ||
| 25 | |||
| 26 | #define STRING_BUILDER_APPEND_CSTRS(SB, ...) \ | ||
| 27 | string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL) | ||
| 28 | |||
| 29 | #define STRING_BUILDER_APPEND_NULL(SB) \ | ||
| 30 | DYNAMIC_ARRAY_APPEND (SB, '\0') | ||
| 31 | |||
| 32 | #define STRING_BUILDER_RESET(SB) \ | ||
| 33 | DYNAMIC_ARRAY_RESET (SB) | ||
| 34 | |||
| 35 | #define STRING_BUILDER_FREE(SB) \ | ||
| 36 | DYNAMIC_ARRAY_FREE(SB) | ||
| 37 | |||
| 38 | extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n); | ||
| 39 | extern void string_builder_append_cstrs (STRING_BUILDER, ...); | ||
| 40 | extern void string_builder_printf (STRING_BUILDER, const char *format, ...); | ||
| 41 | |||
| 42 | #endif | ||
diff --git a/src/string-view.h b/src/string-view.h new file mode 100644 index 0000000..c745d12 --- /dev/null +++ b/src/string-view.h | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | #ifndef INCLUDED_STRING_VIEW_H | ||
| 2 | #define INCLUDED_STRING_VIEW_H | ||
| 3 | |||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | struct string_view { | ||
| 7 | const char *beg; | ||
| 8 | const char *end; | ||
| 9 | }; | ||
| 10 | |||
| 11 | #define STRING_VIEW_LEN(SV) ((SV).end - (SV).beg) | ||
| 12 | #define STRING_VIEW_EQUALS(A, B) \ | ||
| 13 | (STRING_VIEW_LEN (A) == STRING_VIEW_LEN (B) && \ | ||
| 14 | !memcmp ((A).beg, (B).beg, STRING_VIEW_LEN (A))) | ||
| 15 | |||
| 16 | #endif // INCLUDED_STRING_VIEW_H | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..68bb2b5 --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | #ifndef INCLUDED_UTILS_H | ||
| 2 | #define INCLUDED_UTILS_H | ||
| 3 | |||
| 4 | #include <stdlib.h> | ||
| 5 | #include <assert.h> | ||
| 6 | |||
| 7 | #define UNUSED(X) ((void) (X)) | ||
| 8 | #define UNREACHABLE() ASSERT (!"reached the unreachable...") | ||
| 9 | // #define TODO(STR) fprintf (stderr, "%s:%d: warning: TODO "STR"\n", __FILE__, __LINE__) | ||
| 10 | #define TODO(...) | ||
| 11 | |||
| 12 | #define ASSERT assert | ||
| 13 | #define REALLOC realloc | ||
| 14 | #define FREE free | ||
| 15 | |||
| 16 | #define DIE(FMT, ...) \ | ||
| 17 | ({ \ | ||
| 18 | fprintf (stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__); \ | ||
| 19 | exit (EXIT_FAILURE); \ | ||
| 20 | }) | ||
| 21 | |||
| 22 | #define ARRAY_LEN(array) (sizeof (array)/sizeof (*array)) | ||
| 23 | #define ARRAY_GET(array, index) \ | ||
| 24 | (ASSERT(index >= 0), ASSERT(index < ARRAY_LEN(array)), array[index]) | ||
| 25 | |||
| 26 | #endif | ||
