From a03360bfda9be655fed698d7ea85bd0380624f42 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Sat, 29 Jul 2023 04:24:45 +0200 Subject: [PATCH] add some runtime --- src/lexer.c | 37 ++++++++------ src/lexer.h | 23 ++++----- src/parser.c | 22 ++++----- src/parser.h | 4 +- src/runtime.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++- src/runtime.h | 50 ++++++++++++++++--- 6 files changed, 220 insertions(+), 50 deletions(-) diff --git a/src/lexer.c b/src/lexer.c index 22e5765..4e7a2f8 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -30,6 +30,25 @@ void string_destroy(String* string) const size_t string_starting_alloc_size = 8; +void string_from_cstr(String* string, const char* value) +{ + string_construct(string); + string_append_cstr(string, value); +} + +void string_from_slice(String* string, StringSlice slice) +{ + string_construct(string); + for (size_t i = 0; i < slice.length; ++i) { + string_append_char(string, slice.data[i]); + } +} + +void string_append(String* string, const String* value) +{ + string_append_cstr(string, value->data); +} + void string_append_char(String* string, char value) { if (string->length + 1 >= string->capacity) { @@ -53,20 +72,6 @@ void string_append_char(String* string, char value) string->data[string->length] = '\0'; } -void string_from_cstr(String* string, const char* value) -{ - string_construct(string); - string_append_cstr(string, value); -} - -void string_from_slice(String* string, StringSlice slice) -{ - string_construct(string); - for (size_t i = 0; i < slice.length; ++i) { - string_append_char(string, slice.data[i]); - } -} - void string_append_cstr(String* string, const char* value) { size_t value_length = strlen(value); @@ -89,7 +94,7 @@ void string_append_cstr(String* string, const char* value) string->data[string->length] = '\0'; } -void string_append_formatted(String* string, const char* format, ...) +void string_append_format(String* string, const char* format, ...) { va_list varargs; va_start(varargs, format); @@ -245,7 +250,7 @@ const char* token_type_value(TokenType type) { \ String error_message; \ string_construct(&error_message); \ - string_append_formatted(&error_message, __VA_ARGS__); \ + string_append_format(&error_message, __VA_ARGS__); \ Error error; \ error_construct(&error, (POS), error_message); \ error_collector_add((LEXER)->errors, error); \ diff --git a/src/lexer.h b/src/lexer.h index 50ec38c..911851f 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -45,26 +45,27 @@ void string_construct(String* string); void string_destroy(String* string); void string_from_cstr(String* string, const char* value); void string_from_slice(String* string, StringSlice slice); +void string_append(String* string, const String* value); void string_append_char(String* string, char value); void string_append_cstr(String* string, const char* value); -void string_append_formatted(String* string, const char* format, ...); +void string_append_format(String* string, const char* format, ...); bool string_equal(const String* string, const char* value); -typedef struct CharReader { - char (*next)(struct CharReader* reader); - StringSlice (*value)( - const struct CharReader* reader, size_t index, size_t length - ); -} CharReader; +typedef struct CharReader CharReader; +struct CharReader { + char (*next)(CharReader* reader); + StringSlice (*value)(const CharReader* reader, size_t index, size_t length); +}; -typedef struct FileCharReader { - char (*next)(struct FileCharReader* reader); +typedef struct FileCharReader FileCharReader; +struct FileCharReader { + char (*next)(FileCharReader* reader); StringSlice (*value)( - const struct FileCharReader* reader, size_t index, size_t length + const FileCharReader* reader, size_t index, size_t length ); FILE* file; String buffer; -} FileCharReader; +}; void file_char_reader_construct(FileCharReader* reader, FILE* file); void file_char_reader_destroy(FileCharReader* reader); diff --git a/src/parser.c b/src/parser.c index c86e2f6..050a0f9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -53,7 +53,7 @@ Expr error_expr_construct(Pos pos) Expr id_expr_construct(Pos pos, String value) { - return (Expr) { ExprTypeId, pos, .id_value = value }; + return (Expr) { ExprTypeId, pos, .id = value }; } Expr int_expr_construct(Pos pos, int64_t value) @@ -68,7 +68,7 @@ Expr char_expr_construct(Pos pos, char value) Expr string_expr_construct(Pos pos, String value) { - return (Expr) { ExprTypeString, pos, .string_value = value }; + return (Expr) { ExprTypeString, pos, .string = value }; } Expr list_expr_construct(Pos pos, ExprVec exprs) @@ -87,13 +87,13 @@ void expr_destroy(Expr* expr) case ExprTypeError: break; case ExprTypeId: - string_destroy(&expr->id_value); + string_destroy(&expr->id); break; case ExprTypeInt: case ExprTypeChar: break; case ExprTypeString: - string_destroy(&expr->string_value); + string_destroy(&expr->string); break; case ExprTypeList: expr_vec_destroy(&expr->list); @@ -108,21 +108,19 @@ void expr_stringify(const Expr* expr, String* acc) { switch (expr->type) { case ExprTypeError: - string_append_formatted(acc, "Error"); + string_append_format(acc, "Error"); break; case ExprTypeId: - string_append_formatted(acc, "Id(%s)", expr->id_value.data); + string_append_format(acc, "Id(%s)", expr->id.data); break; case ExprTypeInt: - string_append_formatted(acc, "Int(\'%ld\')", expr->int_value); + string_append_format(acc, "Int(\'%ld\')", expr->int_value); break; case ExprTypeChar: - string_append_formatted(acc, "Char(\'%c\')", expr->char_value); + string_append_format(acc, "Char(\'%c\')", expr->char_value); break; case ExprTypeString: - string_append_formatted( - acc, "String(\"%s\")", expr->string_value.data - ); + string_append_format(acc, "String(\"%s\")", expr->string.data); break; case ExprTypeList: string_append_cstr(acc, "["); @@ -153,7 +151,7 @@ void expr_stringify(const Expr* expr, String* acc) { \ String error_message; \ string_construct(&error_message); \ - string_append_formatted(&error_message, __VA_ARGS__); \ + string_append_format(&error_message, __VA_ARGS__); \ Error error; \ error_construct(&error, (POS), error_message); \ error_collector_add((PARSER)->errors, error); \ diff --git a/src/parser.h b/src/parser.h index 9d2a49f..e409835 100644 --- a/src/parser.h +++ b/src/parser.h @@ -34,10 +34,10 @@ struct Expr { ExprType type; Pos pos; union { - String id_value; + String id; int64_t int_value; char char_value; - String string_value; + String string; ExprVec list; ExprVec s; }; diff --git a/src/runtime.c b/src/runtime.c index 19f28ac..ef3f730 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -1,5 +1,135 @@ #include "runtime.h" +#include "lexer.h" +#include -void value_vec_construct(ValueVec* vec, Alloc* alloc) { } +void value_vec_construct(ValueVec* vec, Allocator* allocator) +{ + const size_t starting_capacity = 8; + *vec = (ValueVec) { + .allocator = allocator, + .data = allocator->alloc(allocator, starting_capacity * sizeof(Value)), + .length = 0, + .capacity = starting_capacity, + }; +} -void value_vec_destroy(ValueVec* vec) { } +void value_vec_destroy(ValueVec* vec) +{ + for (size_t i = 0; i < vec->length; ++i) { + value_destroy(&vec->data[i]); + } + free(vec->data); +} + +Value* value_vec_at(const ValueVec* vec, int64_t index) +{ + if (index < 0) { + return value_vec_at(vec, -index); + } + if (index >= vec->length) { + return NULL; + } + return &vec->data[index]; +} + +void value_vec_reserve(ValueVec* vec, size_t min_capacity) +{ + if (vec->capacity < min_capacity + 1) { + while (vec->capacity < min_capacity + 1) { + vec->capacity *= 2; + } + vec->data = vec->allocator->realloc( + vec->allocator, vec->data, vec->capacity * sizeof(Value) + ); + } +} + +void value_vec_push(ValueVec* vec, Value value) +{ + value_vec_reserve(vec, vec->length); + vec->data[vec->length] = value; + vec->length += 1; +} + +Value value_vec_pop(ValueVec* vec) +{ + ASSERT(vec->length > 0); + vec->length -= 1; + return vec->data[vec->length]; +} + +ValueVec value_vec_clone(ValueVec* vec) +{ + ValueVec new_vec; + value_vec_construct(&new_vec, vec->allocator); + if (vec->length == 0) { + return new_vec; + } + value_vec_reserve(&new_vec, vec->length - 1); + new_vec.length = vec->length; + memcpy(new_vec.data, vec->data, new_vec.length * sizeof(Value)); + return new_vec; +} + +ValueVec value_vec_tail(ValueVec* vec) +{ + ValueVec new_vec; + value_vec_construct(&new_vec, vec->allocator); + if (vec->length <= 1) { + return new_vec; + } + value_vec_reserve(&new_vec, vec->length - 1); + new_vec.length = vec->length - 1; + memcpy(new_vec.data, &vec->data[1], new_vec.length * sizeof(Value)); + return new_vec; +} + +void value_vec_stringify(const ValueVec* vec, String* acc) +{ + string_append_cstr(acc, "["); + if (vec->length > 0) { + value_stringify(&vec->data[0], acc); + for (size_t i = 1; i < vec->length; ++i) { + string_append_cstr(acc, ", "); + value_stringify(&vec->data[i], acc); + } + } + string_append_cstr(acc, "]"); +} + +void value_destroy(Value* value) +{ + switch (value->type) { + case ValueTypeSymbol: + string_destroy(&value->symbol); + break; + case ValueTypeInt: + case ValueTypeChar: + case ValueTypeBool: + break; + case ValueTypeList: + value_vec_destroy(&value->list); + break; + } +} + +void value_stringify(const Value* value, String* acc) +{ + switch (value->type) { + case ValueTypeSymbol: + string_append(acc, &value->symbol); + break; + case ValueTypeInt: + string_append_format(acc, "%ld", value->int_value); + break; + case ValueTypeChar: + string_append_format(acc, "%c", value->char_value); + break; + case ValueTypeBool: + string_append_cstr(acc, value->bool_value ? "true" : "false"); + break; + case ValueTypeList: + value_vec_stringify(&value->list, acc); + break; + } +} diff --git a/src/runtime.h b/src/runtime.h index b25b585..93fcda9 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -5,9 +5,11 @@ #include #include -typedef struct Alloc { - void* (*alloc)(struct Alloc* alloc, size_t size); -} Alloc; +typedef struct Allocator Allocator; +struct Allocator { + void* (*alloc)(Allocator* allocator, size_t size); + void* (*realloc)(Allocator* allocator, void* data, size_t size); +}; typedef enum { ValueTypeSymbol, @@ -20,29 +22,63 @@ typedef enum { typedef struct Value Value; typedef struct { - Alloc* alloc; + Allocator* allocator; Value* data; size_t length; size_t capacity; } ValueVec; -void value_vec_construct(ValueVec* vec, Alloc* alloc); +void value_vec_construct(ValueVec* vec, Allocator* allocator); void value_vec_destroy(ValueVec* vec); +Value* value_vec_at(const ValueVec* vec, int64_t index); +void value_vec_reserve(ValueVec* vec, size_t min_capacity); +void value_vec_push(ValueVec* vec, Value value); +Value value_vec_pop(ValueVec* vec); +ValueVec value_vec_clone(ValueVec* vec); +ValueVec value_vec_tail(ValueVec* vec); +void value_vec_stringify(const ValueVec* vec, String* acc); struct Value { ValueType type; union { + String symbol; int64_t int_value; char char_value; bool bool_value; - String string_value; + ValueVec list; }; }; +void value_destroy(Value* value); +void value_stringify(const Value* value, String* acc); + +typedef enum { + GCBufferA, + GCBufferB, +} GCBuffer; + +typedef struct GC GC; +struct GC { + void* (*alloc)(GC* allocator, size_t size); + void* (*realloc)(GC* allocator, void* data, size_t size); + void* a_buffer; + size_t a_capacity; + void* b_buffer; + size_t b_capacity; + size_t alloc_ptr; + GCBuffer selected_buffer; +}; + +void gc_construct(GC* allocator, size_t start_capacity); +void gc_destroy(GC* allocator); +void* gc_alloc(GC* allocator, size_t size); +void* gc_realloc(GC* allocator, void* data, size_t size); + typedef struct { + Allocator* allocator; } Runtime; -void runtime_construct(Runtime* runtime); +void runtime_construct(Runtime* runtime, Allocator* allocator); void runtime_destroy(Runtime* runtime); #endif