add some runtime

This commit is contained in:
SimonFJ20 2023-07-29 04:24:45 +02:00
parent 333f846768
commit a03360bfda
6 changed files with 220 additions and 50 deletions

View File

@ -30,6 +30,25 @@ void string_destroy(String* string)
const size_t string_starting_alloc_size = 8; 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) void string_append_char(String* string, char value)
{ {
if (string->length + 1 >= string->capacity) { if (string->length + 1 >= string->capacity) {
@ -53,20 +72,6 @@ void string_append_char(String* string, char value)
string->data[string->length] = '\0'; 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) void string_append_cstr(String* string, const char* value)
{ {
size_t value_length = strlen(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'; 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_list varargs;
va_start(varargs, format); va_start(varargs, format);
@ -245,7 +250,7 @@ const char* token_type_value(TokenType type)
{ \ { \
String error_message; \ String error_message; \
string_construct(&error_message); \ string_construct(&error_message); \
string_append_formatted(&error_message, __VA_ARGS__); \ string_append_format(&error_message, __VA_ARGS__); \
Error error; \ Error error; \
error_construct(&error, (POS), error_message); \ error_construct(&error, (POS), error_message); \
error_collector_add((LEXER)->errors, error); \ error_collector_add((LEXER)->errors, error); \

View File

@ -45,26 +45,27 @@ void string_construct(String* string);
void string_destroy(String* string); void string_destroy(String* string);
void string_from_cstr(String* string, const char* value); void string_from_cstr(String* string, const char* value);
void string_from_slice(String* string, StringSlice slice); 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_char(String* string, char value);
void string_append_cstr(String* string, const 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); bool string_equal(const String* string, const char* value);
typedef struct CharReader { typedef struct CharReader CharReader;
char (*next)(struct CharReader* reader); struct CharReader {
StringSlice (*value)( char (*next)(CharReader* reader);
const struct CharReader* reader, size_t index, size_t length StringSlice (*value)(const CharReader* reader, size_t index, size_t length);
); };
} CharReader;
typedef struct FileCharReader { typedef struct FileCharReader FileCharReader;
char (*next)(struct FileCharReader* reader); struct FileCharReader {
char (*next)(FileCharReader* reader);
StringSlice (*value)( StringSlice (*value)(
const struct FileCharReader* reader, size_t index, size_t length const FileCharReader* reader, size_t index, size_t length
); );
FILE* file; FILE* file;
String buffer; String buffer;
} FileCharReader; };
void file_char_reader_construct(FileCharReader* reader, FILE* file); void file_char_reader_construct(FileCharReader* reader, FILE* file);
void file_char_reader_destroy(FileCharReader* reader); void file_char_reader_destroy(FileCharReader* reader);

View File

@ -53,7 +53,7 @@ Expr error_expr_construct(Pos pos)
Expr id_expr_construct(Pos pos, String value) 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) 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) 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) Expr list_expr_construct(Pos pos, ExprVec exprs)
@ -87,13 +87,13 @@ void expr_destroy(Expr* expr)
case ExprTypeError: case ExprTypeError:
break; break;
case ExprTypeId: case ExprTypeId:
string_destroy(&expr->id_value); string_destroy(&expr->id);
break; break;
case ExprTypeInt: case ExprTypeInt:
case ExprTypeChar: case ExprTypeChar:
break; break;
case ExprTypeString: case ExprTypeString:
string_destroy(&expr->string_value); string_destroy(&expr->string);
break; break;
case ExprTypeList: case ExprTypeList:
expr_vec_destroy(&expr->list); expr_vec_destroy(&expr->list);
@ -108,21 +108,19 @@ void expr_stringify(const Expr* expr, String* acc)
{ {
switch (expr->type) { switch (expr->type) {
case ExprTypeError: case ExprTypeError:
string_append_formatted(acc, "Error"); string_append_format(acc, "Error");
break; break;
case ExprTypeId: case ExprTypeId:
string_append_formatted(acc, "Id(%s)", expr->id_value.data); string_append_format(acc, "Id(%s)", expr->id.data);
break; break;
case ExprTypeInt: case ExprTypeInt:
string_append_formatted(acc, "Int(\'%ld\')", expr->int_value); string_append_format(acc, "Int(\'%ld\')", expr->int_value);
break; break;
case ExprTypeChar: case ExprTypeChar:
string_append_formatted(acc, "Char(\'%c\')", expr->char_value); string_append_format(acc, "Char(\'%c\')", expr->char_value);
break; break;
case ExprTypeString: case ExprTypeString:
string_append_formatted( string_append_format(acc, "String(\"%s\")", expr->string.data);
acc, "String(\"%s\")", expr->string_value.data
);
break; break;
case ExprTypeList: case ExprTypeList:
string_append_cstr(acc, "["); string_append_cstr(acc, "[");
@ -153,7 +151,7 @@ void expr_stringify(const Expr* expr, String* acc)
{ \ { \
String error_message; \ String error_message; \
string_construct(&error_message); \ string_construct(&error_message); \
string_append_formatted(&error_message, __VA_ARGS__); \ string_append_format(&error_message, __VA_ARGS__); \
Error error; \ Error error; \
error_construct(&error, (POS), error_message); \ error_construct(&error, (POS), error_message); \
error_collector_add((PARSER)->errors, error); \ error_collector_add((PARSER)->errors, error); \

View File

@ -34,10 +34,10 @@ struct Expr {
ExprType type; ExprType type;
Pos pos; Pos pos;
union { union {
String id_value; String id;
int64_t int_value; int64_t int_value;
char char_value; char char_value;
String string_value; String string;
ExprVec list; ExprVec list;
ExprVec s; ExprVec s;
}; };

View File

@ -1,5 +1,135 @@
#include "runtime.h" #include "runtime.h"
#include "lexer.h"
#include <string.h>
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;
}
}

View File

@ -5,9 +5,11 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
typedef struct Alloc { typedef struct Allocator Allocator;
void* (*alloc)(struct Alloc* alloc, size_t size); struct Allocator {
} Alloc; void* (*alloc)(Allocator* allocator, size_t size);
void* (*realloc)(Allocator* allocator, void* data, size_t size);
};
typedef enum { typedef enum {
ValueTypeSymbol, ValueTypeSymbol,
@ -20,29 +22,63 @@ typedef enum {
typedef struct Value Value; typedef struct Value Value;
typedef struct { typedef struct {
Alloc* alloc; Allocator* allocator;
Value* data; Value* data;
size_t length; size_t length;
size_t capacity; size_t capacity;
} ValueVec; } ValueVec;
void value_vec_construct(ValueVec* vec, Alloc* alloc); void value_vec_construct(ValueVec* vec, Allocator* allocator);
void value_vec_destroy(ValueVec* vec); 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 { struct Value {
ValueType type; ValueType type;
union { union {
String symbol;
int64_t int_value; int64_t int_value;
char char_value; char char_value;
bool bool_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 { typedef struct {
Allocator* allocator;
} Runtime; } Runtime;
void runtime_construct(Runtime* runtime); void runtime_construct(Runtime* runtime, Allocator* allocator);
void runtime_destroy(Runtime* runtime); void runtime_destroy(Runtime* runtime);
#endif #endif