commit 0cc603389b08252b018c330f35c9e16075ff7e17
parent b21f1ed60ef477c87581b0c38a3a0551109f2059
Author: citbl <citbl@citbl.org>
Date: Mon, 11 May 2026 21:36:25 +1000
super minimal str impl
Diffstat:
4 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/src/common.h b/src/common.h
@@ -5,6 +5,8 @@
#include <stdio.h>
#include <stdlib.h>
+#include "str.h"
+
typedef enum Token_Type {
NOTYETSET = 9,
IDENT,
@@ -64,7 +66,7 @@ typedef struct String {
typedef struct Token {
Token_Type type;
- String lexeme;
+ Str lexeme;
const char* path;
const char* filename;
size_t line;
diff --git a/src/lexer.c b/src/lexer.c
@@ -1,8 +1,10 @@
-#include "lexer.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
+#include "lexer.h"
+#include "str.h"
+
static void add_token(Lexer* lex, Token t)
{
if (lex->len >= lex->cap) {
@@ -39,16 +41,8 @@ void print_tokens(Lexer* lex)
static void add_to_string(Token* tok, char c)
{
- String* str = &tok->lexeme;
-
- if (str->len >= str->cap) {
- str->cap = str->cap == 0 ? 256 : str->cap * 2;
- str->value = realloc(str->value, str->cap * sizeof(char));
- check(str->value == NULL, "could not allocate memory for string in lexer\n");
- }
-
- str->value[str->len++] = c;
- str->value[str->len] = '\0';
+ Str* str = &tok->lexeme;
+ str_append(str, c);
}
static char peek(Lexer* lex)
@@ -88,20 +82,13 @@ static void run_until_char(Lexer* lex, char c)
static void lex_number(Lexer* lex, Token* tok)
{
char c;
- String* str = &tok->lexeme;
+ Str* str = &tok->lexeme;
tok->type = LIT_INT;
while (1) {
advance(lex);
c = peek(lex);
if (c == '.') tok->type = LIT_DECIMAL;
-
- if (str->len >= str->cap) {
- str->cap = str->cap == 0 ? 256 : str->cap * 2;
- str->value = realloc(str->value, str->cap * sizeof(char));
- check(str->value == NULL, "could not allocate memory for string in lexer\n");
- }
-
- str->value[str->len++] = c;
+ str_append(str, c);
if (c != '.' && !isdigit(c)) break;
}
str->value[str->len] = '\0';
diff --git a/src/str.c b/src/str.c
@@ -0,0 +1,14 @@
+#include <stdlib.h>
+#include "str.h"
+#include "common.h"
+
+void str_append(Str* str, const char c)
+{
+ if (str->len >= str->cap) {
+ str->cap = str->cap == 0 ? 32 : str->cap * 2;
+ str->value = realloc(str->value, str->cap * sizeof(char));
+ check(str->value == NULL, "str_append: could not alloc string\n");
+ }
+
+ str->value[str->len++] = c;
+}
diff --git a/src/str.h b/src/str.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include <stddef.h>
+
+typedef struct Str {
+ char* value;
+ size_t len;
+ size_t cap;
+} Str;
+
+void str_append(Str* str, const char c);