Wrote value, chunk, memory sections, tested value

chunk and memory remain untested for now
This commit is contained in:
2024-08-13 23:42:14 +10:00
parent 190294e5d9
commit e6ad46f1ff
11 changed files with 204 additions and 10 deletions

24
source/toy_chunk.c Normal file
View File

@@ -0,0 +1,24 @@
#include "toy_chunk.h"
#include "toy_memory.h"
void Toy_initChunk(Toy_Chunk* chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->code = NULL;
}
void Toy_pushChunk(Toy_Chunk* chunk, uint8_t byte) {
if (chunk->count +1 > chunk->capacity) {
int oldCapacity = chunk->capacity;
chunk->capacity = TOY_GROW_CAPACITY(oldCapacity);
chunk->code = TOY_GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
}
chunk->code[chunk->count++] = byte;
}
void Toy_freeChunk(Toy_Chunk* chunk) {
TOY_FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
Toy_initChunk(chunk);
}

13
source/toy_chunk.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include "toy_common.h"
typedef struct Toy_Chunk {
int count;
int capacity;
uint8_t* code;
} Toy_Chunk;
TOY_API void Toy_initChunk(Toy_Chunk* chunk);
TOY_API void Toy_pushChunk(Toy_Chunk* chunk, uint8_t byte);
TOY_API void Toy_freeChunk(Toy_Chunk* chunk);

View File

@@ -317,13 +317,13 @@ static void trim(char** s, int* l) { //util
void Toy_private_printToken(Toy_Token* token) {
//print errors
if (token->type == TOY_TOKEN_ERROR) {
printf(TOY_CC_ERROR "Error\t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
printf(TOY_CC_ERROR "ERROR: \t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
return;
}
//read pass token, even though it isn't generated
if (token->type == TOY_TOKEN_PASS) {
printf(TOY_CC_NOTICE "Error\t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
printf(TOY_CC_NOTICE "PASS: \t%d\t%.*s\n" TOY_CC_RESET, token->line, token->length, token->lexeme);
return;
}

22
source/toy_memory.c Normal file
View File

@@ -0,0 +1,22 @@
#include "toy_memory.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <stdlib.h>
void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(pointer);
return NULL;
}
void* result = realloc(pointer, newSize);
if (result == NULL) {
fprintf(stderr, TOY_CC_ERROR "[internal] ERROR: Memory allocation error (requested %d, replacing %d)\n" TOY_CC_RESET, (int)newSize, (int)oldSize);
exit(1);
}
return result;
}

14
source/toy_memory.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include "toy_common.h"
#define TOY_GROW_CAPACITY(capacity) \
((capacity) < 8 ? 8 : (capacity) * 2)
#define TOY_GROW_ARRAY(type, pointer, oldSize, newSize) \
(type*)Toy_reallocate(pointer, sizeof(type)*oldSize, sizeof(type)*newSize)
#define TOY_FREE_ARRAY(type, pointer, oldSize) \
(type*)Toy_reallocate(pointer, sizeof(type)*oldSize, 0)
TOY_API void* Toy_reallocate(void* pointer, size_t oldSize, size_t newSize);

5
source/toy_opcodes.h Normal file
View File

@@ -0,0 +1,5 @@
#pragma once
typedef enum Toy_OpcodeType {
TOY_OPCODE_RETURN,
} Toy_OpcodeType;

21
source/toy_value.c Normal file
View File

@@ -0,0 +1,21 @@
#include "toy_value.h"
#include "toy_console_colors.h"
#include <stdio.h>
bool Toy_private_isTruthy(Toy_Value value) {
//null is an error
if (TOY_VALUE_IS_NULL(value)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: 'null' is neither true nor false\n" TOY_CC_RESET);
return false;
}
//only 'false' is falsy
if (TOY_VALUE_IS_BOOLEAN(value)) {
return TOY_VALUE_AS_BOOLEAN(value);
}
//anything else is truthy
return true;
}

55
source/toy_value.h Normal file
View File

@@ -0,0 +1,55 @@
#pragma once
#include "toy_common.h"
typedef enum Toy_ValueType {
TOY_VALUE_NULL,
TOY_VALUE_BOOLEAN,
TOY_VALUE_INTEGER,
TOY_VALUE_FLOAT,
TOY_VALUE_STRING,
TOY_VALUE_ARRAY,
TOY_VALUE_DICTIONARY,
TOY_VALUE_FUNCTION,
TOY_VALUE_OPAQUE,
} Toy_ValueType;
typedef struct Toy_Value {
union {
bool boolean; //1
int integer; //4
float number; //4
//TODO: strings
//TODO: arrays
//TODO: dictonaries
//TODO: functions
//TODO: opaque
} as; //4
Toy_ValueType type; //4 bytes
} Toy_Value;
#define TOY_VALUE_IS_NULL(value) ((value).type == TOY_VALUE_NULL)
#define TOY_VALUE_IS_BOOLEAN(value) ((value).type == TOY_VALUE_BOOLEAN)
#define TOY_VALUE_IS_INTEGER(value) ((value).type == TOY_VALUE_INTEGER)
#define TOY_VALUE_IS_FLOAT(value) ((value).type == TOY_VALUE_FLOAT)
#define TOY_VALUE_IS_STRING(value) ((value).type == TOY_VALUE_STRING)
#define TOY_VALUE_IS_ARRAY(value) ((value).type == TOY_VALUE_ARRAY)
#define TOY_VALUE_IS_DICTIONARY(value) ((value).type == TOY_VALUE_DICTIONARY)
#define TOY_VALUE_IS_FUNCTION(value) ((value).type == TOY_VALUE_FUNCTION)
#define TOY_VALUE_IS_OPAQUE(value) ((value).type == TOY_VALUE_OPAQUE)
#define TOY_VALUE_AS_BOOLEAN(value) ((value).as.boolean)
#define TOY_VALUE_AS_INTEGER(value) ((value).as.integer)
#define TOY_VALUE_AS_FLOAT(value) ((value).as.number)
//TODO: more
#define TOY_VALUE_TO_NULL() ((Toy_Value){{ .integer = 0 }, TOY_VALUE_NULL})
#define TOY_VALUE_TO_BOOLEAN(value) ((Toy_Value){{ .boolean = value }, TOY_VALUE_BOOLEAN})
#define TOY_VALUE_TO_INTEGER(value) ((Toy_Value){{ .integer = value }, TOY_VALUE_INTEGER})
#define TOY_VALUE_TO_FLOAT(value) ((Toy_Value){{ .number = value }, TOY_VALUE_FLOAT})
//TODO: more
#define TOY_VALUE_IS_TRUTHY(value) Toy_private_isTruthy(value)
TOY_API bool Toy_private_isTruthy(Toy_Value value);