From 759db1ea7ad4f02be9316483c35bf0603c4bca4b Mon Sep 17 00:00:00 2001 From: Kyler Date: Thu, 19 Jun 2025 00:54:48 -0600 Subject: [PATCH] added add_file_infos function --- include/sync/types.h | 10 ++++++++++ src/types.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/types.c diff --git a/include/sync/types.h b/include/sync/types.h index 7e7bbff..805cab7 100644 --- a/include/sync/types.h +++ b/include/sync/types.h @@ -38,4 +38,14 @@ typedef enum { SYNC_RESULT, } SyncResultType; +typedef struct { + SyncResultType type; + union { + FileInfo result; + GeneralError error; + }; +} FileInfoResult; + +FileInfoResult add_file_infos(FileInfo a, FileInfo b, const char* start); + #endif // SYNC_TYPES_H diff --git a/src/types.c b/src/types.c new file mode 100644 index 0000000..9f50f7a --- /dev/null +++ b/src/types.c @@ -0,0 +1,38 @@ +// Kyler Olsen +// ZINC Bootstrap compiler +// Type helper functions +// June 2025 + +#include "sync/types.h" + +FileInfoResult add_file_infos(FileInfo a, FileInfo b, const char* start) { + if (a.filename != b.filename) + return (FileInfoResult){SYNC_ERROR, .error = (GeneralError){"Cannot add two FileInfo structs from different files.", 1}}; + size_t line = a.line; + size_t column = a.column; + size_t length = a.length; + size_t lines = a.lines; + for (int i = a.length; ; i++) { + if (start[i] == '\0') + return (FileInfoResult){SYNC_ERROR, .error = (GeneralError){"Encountered end of string.", 1}}; + if (start[i] == '\n') { + column = 1; + line++; + lines++; + } else column++; + length++; + if (line == b.line && column == b.column) { + length += b.length; + lines += b.lines; + break; + } + } + FileInfo file_info = { + .filename = a.filename, + .line = a.line, + .column = a.column, + .length = length, + .lines = lines + }; + return (FileInfoResult){SYNC_RESULT, .result = file_info}; +}