aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2026-09-05 11:21:32 +0000
committeronelin <oscar@nelin.dk>2026-09-05 11:21:32 +0000
commitb86db843ec4838f6785bc66a6813f5e324ffb5de (patch)
tree3dd5965944357cc87d3c4d0122a5b5f4667447d4
parentfb6503ce042aaff6d79e172fe895b298b25fb850 (diff)
Add "lines" helper structure
-rw-r--r--main.c77
1 files changed, 46 insertions, 31 deletions
diff --git a/main.c b/main.c
index f07629d..aafad09 100644
--- a/main.c
+++ b/main.c
@@ -54,8 +54,8 @@ typedef struct Token {
static isize f_read(FILE* stream, char** dst);
static i64 f_size(FILE* stream);
-static void l_print_token(Token_t token, char* text);
-static Token_t* l_read(char* text, isize text_length, Token_t* dst);
+static void l_print_token(Token_t token, Location_t** lines, char* text);
+static Location_t** l_read(char* text, isize text_length, Token_t** dst);
int
main(int argc, char* argv[]) {
@@ -64,6 +64,7 @@ main(int argc, char* argv[]) {
char* contents = NULL;
isize content_length = -1;
Token_t* tokens = NULL;
+ Location_t** lines = NULL;
f = fopen(argv[1], "r");
if (f == NULL) {
@@ -79,14 +80,14 @@ main(int argc, char* argv[]) {
}
if (content_length > 0) {
- tokens = l_read(contents, strlen(contents), NULL);
+ lines = l_read(contents, strlen(contents), &tokens);
}
- if (tokens != NULL) {
+ if (lines != NULL) {
i64 num_tokens = tokens[0].location.size;
printf("%ld tokens\n", num_tokens);
for (int t = 0; t < num_tokens; t++) {
- l_print_token(tokens[t], contents);
+ l_print_token(tokens[t], lines, contents);
}
free(contents);
}
@@ -152,7 +153,7 @@ f_size(FILE* stream) {
}
static void
-l_print_token(Token_t token, char* text) {
+l_print_token(Token_t token, Location_t* lines[], char* text) {
fprintf(stdout, "[%d,%d:%d]", token.location.location.line + 1,
token.location.location.column + 1, token.location.location.size);
switch (token.type) {
@@ -176,7 +177,8 @@ l_print_token(Token_t token, char* text) {
break;
case t_number:
fputs("number:", stdout);
- fwrite(&text[token.location.location.line + token.location.location.column],
+ Location_t* line = lines[token.location.location.line];
+ fwrite(&text[line->line + token.location.location.column],
token.location.location.size, sizeof(char), stdout);
break;
case t_newline:
@@ -194,18 +196,22 @@ l_print_token(Token_t token, char* text) {
* @return a pointer to dst
* @error Returns NULL on error.
*/
-static Token_t*
-l_read(char* text, isize text_length, Token_t* dst) {
- if (text_length > 0 && dst == NULL) {
- dst = (Token_t*)malloc(text_length * sizeof(Token_t));
+static Location_t**
+l_read(char* text, isize text_length, Token_t** dst) {
+ Location_t** lines = NULL;
+ if (dst == NULL) {
+ fprintf(stderr, "dst is NULL");
+ }
+ if (text_length > 0 && dst != NULL && *dst == NULL) {
+ *dst = (Token_t*)malloc(text_length * sizeof(Token_t));
}
- if (text_length > 0 && dst == NULL) {
+ if (text_length > 0 && dst != NULL && *dst == NULL) {
fprintf(stderr, "! Failed to allocate %ld bytes\n",
text_length * sizeof(Token_t));
}
- if (text_length > 0 && dst != NULL) {
+ if (text_length > 0 && dst != NULL && *dst != NULL) {
// Offset in text (used for Location.line
isize i = 0;
// Column counter
@@ -218,8 +224,8 @@ l_read(char* text, isize text_length, Token_t* dst) {
enum TokenType token_previous = t_error;
enum TokenType token = t_error;
- dst[1].type = t_newline;
- Location_t* line_current = &(dst[1].location.line);
+ (*dst)[1].type = t_newline;
+ Location_t* line_current = &(*dst)[1].location.line;
line_current->line = 0;
line_current->column = l;
line_current->size = 0;
@@ -280,24 +286,24 @@ l_read(char* text, isize text_length, Token_t* dst) {
if (token == t_newline) {
l++;
- line_current->size = c;
- dst[t].type = token;
- dst[t].location.line.line = i + 1;
- dst[t].location.line.column = l;
- dst[t].location.line.size = 0;
+ line_current->size = c;
+ (*dst)[t].type = token;
+ (*dst)[t].location.line.line = i + 1;
+ (*dst)[t].location.line.column = l;
+ (*dst)[t].location.line.size = 0;
- line_current = &dst[t].location.line;
- c = 0;
+ line_current = &(*dst)[t].location.line;
+ c = 0;
t++;
token_previous = token;
} else if (token == token_previous) {
- dst[t - 1].location.location.size++;
+ (*dst)[t - 1].location.location.size++;
c++;
} else {
- dst[t].type = token;
- dst[t].location.location.line = l;
- dst[t].location.location.column = c;
- dst[t].location.location.size = 1;
+ (*dst)[t].type = token;
+ (*dst)[t].location.location.line = l;
+ (*dst)[t].location.location.column = c;
+ (*dst)[t].location.location.size = 1;
c++;
t++;
token_previous = token;
@@ -306,14 +312,23 @@ l_read(char* text, isize text_length, Token_t* dst) {
i++;
} while (!error && i < text_length);
- dst[0].type = t_size;
- dst[0].location.size = t;
+ (*dst)[0].type = t_size;
+ (*dst)[0].location.size = t;
- if (dst[t - 1].type != t_newline) {
+ if ((*dst)[t - 1].type != t_newline) {
l++;
line_current->size = c;
}
+
+ lines = (Location_t**)malloc(sizeof(Location_t*) * l);
+ i32 j = 0;
+ for (int u = 0; u < t; u++) {
+ if ((*dst)[u].type == t_newline) {
+ lines[j] = &(*dst)[u].location.line;
+ j++;
+ }
+ }
}
- return dst;
+ return lines;
}