added add_file_infos function

This commit is contained in:
Kyler Olsen 2025-06-19 00:54:48 -06:00
parent e968058249
commit 759db1ea7a
2 changed files with 48 additions and 0 deletions

View File

@ -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

38
src/types.c Normal file
View File

@ -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};
}