aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2026-09-05 21:11:31 +0000
committeronelin <oscar@nelin.dk>2026-09-05 21:23:41 +0000
commitb5dbbb6f4253c3d4f598b4e1016af2bf450a47b6 (patch)
tree9fd41f7de4bb1e0b3dc476a19a6d9e7693fc608d
parent58f43f79f9b9c6b943e87775d1a004ad353f26fd (diff)
chore(style): Rename variables for readability
-rw-r--r--main.c150
1 files changed, 85 insertions, 65 deletions
diff --git a/main.c b/main.c
index 92603f2..0723531 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,6 @@
#include "types.h"
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -52,31 +53,34 @@ typedef struct Token {
// Should also add helper structure with pointers to each line
-static isize f_read(FILE* stream, char** dst);
-static i64 f_size(FILE* stream);
+static usize f_read(FILE* stream, char** dst);
+static usize f_size(FILE* stream);
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);
+static Location_t** l_read(char const* text, usize text_length, Token_t** dst);
int
main(int argc, char* argv[]) {
if (argc == 2) {
- FILE* f = NULL;
+ FILE* srcfile = NULL;
char* contents = NULL;
- isize content_length = -1;
+ usize content_length = 0;
Token_t* tokens = NULL;
Location_t** lines = NULL;
- f = fopen(argv[1], "re");
- if (f == NULL) {
+ srcfile = fopen(argv[1], "re");
+ if (srcfile == NULL) {
fprintf(stdout, "! Failed to open file: %s\n", argv[1]);
}
- if (f != NULL) {
- content_length = f_read(f, &contents);
+ if (srcfile != NULL) {
+ content_length = f_read(srcfile, &contents);
if (content_length > 0) {
printf("%s:%ld\n%s\n", argv[1], content_length, contents);
}
- fclose(f);
+ if (fclose(srcfile)) {
+ fprintf(stdout, "! Failed to close file %s: %s\n", argv[1],
+ strerror(errno));
+ }
}
if (content_length > 0) {
@@ -84,11 +88,20 @@ main(int argc, char* argv[]) {
}
if (lines != NULL) {
- i64 num_tokens = tokens[0].location.size;
+ const 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], lines, contents);
+ for (int tokenidx = 0; tokenidx < num_tokens; tokenidx++) {
+ l_print_token(tokens[tokenidx], lines, contents);
}
+ }
+
+ if (tokens != NULL) {
+ free(tokens);
+ }
+ if (lines != NULL) {
+ free((void*)lines);
+ }
+ if (contents != NULL) {
free(contents);
}
}
@@ -102,9 +115,9 @@ main(int argc, char* argv[]) {
* @return The number of bytes copied into dst.
* @error returns -1 on error.
*/
-static isize
+static usize
f_read(FILE* stream, char** dst) {
- i64 file_sz = f_size(stream);
+ usize file_sz = f_size(stream);
if (dst == NULL) {
fprintf(stderr, "! Got NULL ptr\n");
@@ -115,10 +128,11 @@ f_read(FILE* stream, char** dst) {
}
if (dst != NULL && *dst == NULL && file_sz > 0) {
- fprintf(stderr, "! Failed to allocate %ld bytes\n", file_sz * sizeof(char));
+ fprintf(stderr, "! Failed to allocate %ld bytes: %s\n",
+ file_sz * sizeof(char), strerror(errno));
}
- isize bytes_read = -1;
+ usize bytes_read = 0;
if (dst != NULL && *dst != NULL && file_sz > 0) {
bytes_read = fread(*dst, sizeof(char), file_sz, stream);
}
@@ -136,7 +150,7 @@ f_read(FILE* stream, char** dst) {
* @return the size of the file in bytes.
* @error Returns -1 on error.
*/
-static i64
+static usize
f_size(FILE* stream) {
// Could ftell and rewind to previous position, but scratch that
i64 size = -1;
@@ -146,16 +160,26 @@ f_size(FILE* stream) {
// TODO: handle pipes (check errno == ESPIPE)
fprintf(stderr, "! Failed to seek file: %s\n", strerror(errno));
} else {
- size = ftell(stream);
- rewind(stream);
+ size = ftell(stream);
+ error = fseek(stream, 0, SEEK_SET);
+
+ if (error) {
+ fprintf(stderr, "! Failed to rewind file: %s\n", strerror(errno));
+ }
+ }
+
+ if (size == -1) {
+ fprintf(stderr, "! Failed to ftell file: %s\n", strerror(errno));
+ size = 0;
}
- return size;
+ return (usize)size;
}
static void
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) {
case t_error:
fputs(" error ", stdout);
@@ -188,6 +212,7 @@ l_print_token(Token_t token, Location_t* lines[], char* text) {
default:
break;
}
+
puts("");
}
@@ -197,7 +222,7 @@ l_print_token(Token_t token, Location_t* lines[], char* text) {
* @error Returns NULL on error.
*/
static Location_t**
-l_read(char* text, isize text_length, Token_t** dst) {
+l_read(char const* text, usize text_length, Token_t** dst) {
Location_t** lines = NULL;
if (dst == NULL) {
fprintf(stderr, "dst is NULL");
@@ -212,14 +237,15 @@ l_read(char* text, isize text_length, Token_t** dst) {
}
if (text_length > 0 && dst != NULL && *dst != NULL) {
- // Offset in text (used for Location.line
- isize i = 0;
+ // Offset in text (used for location.line and line.column -- the lineno if
+ // token==newline)
+ usize offset = 0;
// Column counter
- u16 c = 0;
- // Line offset
- i32 l = 0;
+ u16 column = 0;
+ // Line number
+ i32 lineno = 0;
// Token counter
- isize t = 2;
+ isize tokenno = 2;
i32 error = 0;
enum TokenType token_previous = t_error;
enum TokenType token = t_error;
@@ -227,17 +253,11 @@ l_read(char* text, isize text_length, Token_t** dst) {
(*dst)[1].type = t_newline;
Location_t* line_current = &(*dst)[1].location.line;
line_current->line = 0;
- line_current->column = l;
+ line_current->column = lineno;
line_current->size = 0;
- // TODO: rewrite to
- // if token != last-token
- // dst = token,line,col
- // last-token = token
- // t++;
- // while (last-token != error)
do {
- switch (text[i]) {
+ switch (text[offset]) {
// case t_size:
// break;
case '*':
@@ -285,47 +305,47 @@ 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;
+ lineno++;
+ line_current->size = column;
+ (*dst)[tokenno].type = token;
+ (*dst)[tokenno].location.line.line = (i32)(offset + 1);
+ (*dst)[tokenno].location.line.column = lineno;
+ (*dst)[tokenno].location.line.size = 0;
- line_current = &(*dst)[t].location.line;
- c = 0;
- t++;
+ line_current = &(*dst)[tokenno].location.line;
+ column = 0;
+ tokenno++;
token_previous = token;
} else if (token == token_previous) {
- (*dst)[t - 1].location.location.size++;
- c++;
+ (*dst)[tokenno - 1].location.location.size++;
+ column++;
} else {
- (*dst)[t].type = token;
- (*dst)[t].location.location.line = l;
- (*dst)[t].location.location.column = c;
- (*dst)[t].location.location.size = 1;
- c++;
- t++;
+ (*dst)[tokenno].type = token;
+ (*dst)[tokenno].location.location.line = lineno;
+ (*dst)[tokenno].location.location.column = column;
+ (*dst)[tokenno].location.location.size = 1;
+ column++;
+ tokenno++;
token_previous = token;
}
- i++;
- } while (!error && i < text_length);
+ offset++;
+ } while (!error && offset < text_length && offset < INT_MAX);
(*dst)[0].type = t_size;
- (*dst)[0].location.size = t;
+ (*dst)[0].location.size = tokenno;
- if ((*dst)[t - 1].type != t_newline) {
- l++;
- line_current->size = c;
+ if ((*dst)[tokenno - 1].type != t_newline) {
+ lineno++;
+ line_current->size = column;
}
- 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++;
+ lines = (Location_t**)malloc(sizeof(Location_t*) * (lineno + 1));
+ i32 line_idx = 0;
+ for (int tokenidx = 0; tokenidx < tokenno; tokenidx++) {
+ if ((*dst)[tokenidx].type == t_newline) {
+ lines[line_idx] = &(*dst)[tokenidx].location.line;
+ line_idx++;
}
}
}