ref: split reader into separate files

This commit is contained in:
samsonium
2026-10-28 23:12:46 +03:00
parent 7c04d8b7ec
commit bbdaa4a9b9
8 changed files with 375 additions and 327 deletions
+62
View File
@@ -0,0 +1,62 @@
#include <compiler/fzl_reader.h>
#include <compiler/fzl_token.h>
#include <compiler/internals/reader.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Try parse string literal
fzl_token_t *rd_string_literal(fzl_reader_t *reader)
{
size_t lpos = reader->position;
size_t size = 0;
if (reader->source[lpos] != '\'' && reader->source[lpos] != '"')
return NULL;
char open_symbol = reader->source[lpos];
char close_symbol = '\0';
do
{
lpos++;
size++;
if (reader->source[lpos] == open_symbol)
close_symbol = open_symbol;
} while (reader->source[lpos] != open_symbol && lpos < reader->size);
size++;
fzl_file_loc l;
bool ok = fzl_linked_getpos(reader->linked, reader->position, &l);
if (!ok)
{
snprintf(last_err, sizeof(last_err), "failed to locate line:col");
return NULL;
}
if (close_symbol != open_symbol)
{
snprintf(last_err, sizeof(last_err), "'%s': unterminated string at %ld:%ld",
l.filename, l.line, l.col);
return NULL;
}
if (size == 2)
{
fzl_token_t *tok =
fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, "", 0, l.filename);
move(reader, 2);
return tok;
}
char *value = malloc(size - 2);
memcpy(value, reader->source + reader->position + 1, size - 2);
fzl_token_t *tok = fzl_mktoken(FZL_TOKTYPE_STR_LITERAL, l.line, l.col, value,
size - 2, l.filename);
free(value);
move(reader, size);
return tok;
}