Initial commit, basic lexer structure

This commit is contained in:
2025-03-30 17:45:51 +02:00
commit df948b18c6
13 changed files with 794 additions and 0 deletions

42
src/error.c Normal file

@ -0,0 +1,42 @@
#include "error.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
error_t *const err_errorf_alloc = &(error_t){
.message = "Allocation failed during formatting of another error"};
error_t *const err_errorf_length = &(error_t){
.message =
"Formatting of another error failed to determine the error length"};
error_t *errorf(const char *fmt, ...) {
error_t *err = calloc(1, sizeof(error_t));
if (err == nullptr)
return err_errorf_alloc;
va_list args;
va_list args_count;
va_start(args, fmt);
va_copy(args_count, args);
int size = vsnprintf(nullptr, 0, fmt, args_count) + 1;
va_end(args_count);
if (size <= 0) {
free(err);
va_end(args);
return err_errorf_length;
}
err->message = malloc(size);
if (err->message == nullptr) {
free(err);
va_end(args);
return err_errorf_alloc;
}
vsnprintf(err->message, size, fmt, args);
va_end(args);
err->is_heap_allocated = true;
return err;
}