From 52c33178d382017b090bcc59ea922d5bca498903 Mon Sep 17 00:00:00 2001 From: zongor Date: Sun, 11 May 2025 18:54:31 -0400 Subject: [PATCH] start from scratch with own code --- src/chunk.c | 40 --- src/chunk.h | 57 --- src/common.h | 16 - src/compiler.c | 917 ------------------------------------------------- src/compiler.h | 11 - src/debug.c | 148 -------- src/debug.h | 9 - src/main.c | 77 ----- src/memory.c | 247 ------------- src/memory.h | 28 -- src/object.c | 163 --------- src/object.h | 114 ------ src/scanner.c | 261 -------------- src/scanner.h | 71 ---- src/table.c | 151 -------- src/table.h | 29 -- src/value.c | 51 --- src/value.h | 52 --- src/vm.c | 516 ---------------------------- src/vm.h | 50 --- test/loop.ztl | 12 + 21 files changed, 12 insertions(+), 3008 deletions(-) delete mode 100644 src/chunk.c delete mode 100644 src/chunk.h delete mode 100644 src/common.h delete mode 100644 src/compiler.c delete mode 100644 src/compiler.h delete mode 100644 src/debug.c delete mode 100644 src/debug.h delete mode 100644 src/main.c delete mode 100644 src/memory.c delete mode 100644 src/memory.h delete mode 100644 src/object.c delete mode 100644 src/object.h delete mode 100644 src/scanner.c delete mode 100644 src/scanner.h delete mode 100644 src/table.c delete mode 100644 src/table.h delete mode 100644 src/value.c delete mode 100644 src/value.h delete mode 100644 src/vm.c delete mode 100644 src/vm.h create mode 100644 test/loop.ztl diff --git a/src/chunk.c b/src/chunk.c deleted file mode 100644 index 8eddb39..0000000 --- a/src/chunk.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "chunk.h" -#include "memory.h" -#include "vm.h" - -void initChunk(Chunk* chunk) { - chunk->count = 0; - chunk->capacity = 0; - chunk->lines = NULL; - chunk->code = NULL; - initValueArray(&chunk->constants); -} - -void writeChunk(Chunk* chunk, uint8_t byte, int line) { - if (chunk->capacity < chunk->count + 1) { - int oldCapacity = chunk->capacity; - chunk->capacity = GROW_CAPACITY(oldCapacity); - chunk->code = GROW_ARRAY(uint8_t, chunk->code, - oldCapacity, chunk->capacity); - chunk->lines = GROW_ARRAY(int, chunk->lines, - oldCapacity, chunk->capacity); - } - - chunk->code[chunk->count] = byte; - chunk->lines[chunk->count] = line; - chunk->count++; -} - -void freeChunk(Chunk* chunk) { - FREE_ARRAY(uint8_t, chunk->code, chunk->capacity); - freeValueArray(&chunk->constants); - initChunk(chunk); -} - -int addConstant(Chunk* chunk, Value value) { - push(value); - writeValueArray(&chunk->constants, value); - pop(); - return chunk->constants.count - 1; -} - diff --git a/src/chunk.h b/src/chunk.h deleted file mode 100644 index 122ea0e..0000000 --- a/src/chunk.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef zlc_chunk_h -#define zlc_chunk_h - -#include "common.h" -#include "value.h" - -typedef enum { - OP_CONSTANT, - OP_NIL, - OP_TRUE, - OP_FALSE, - OP_POP, - OP_GET_LOCAL, - OP_GET_GLOBAL, - OP_DEFINE_GLOBAL, - OP_SET_LOCAL, - OP_SET_GLOBAL, - OP_GET_UPVALUE, - OP_SET_UPVALUE, - OP_GET_PROPERTY, - OP_SET_PROPERTY, - OP_EQUAL, - OP_GREATER, - OP_LESS, - OP_ADD, - OP_SUBTRACT, - OP_MULTIPLY, - OP_DIVIDE, - OP_NOT, - OP_NEGATE, - OP_PRINT, - OP_JUMP, - OP_JUMP_IF_FALSE, - OP_LOOP, - OP_CALL, - OP_INVOKE, - OP_CLOSURE, - OP_CLOSE_UPVALUE, - OP_RETURN, - OP_TYPE, - OP_METHOD -} OpCode; - -typedef struct { - int count; - int capacity; - uint8_t *code; - int *lines; - ValueArray constants; -} Chunk; - -void initChunk(Chunk *chunk); -void freeChunk(Chunk *chunk); -void writeChunk(Chunk *chunk, uint8_t byte, int line); -int addConstant(Chunk *chunk, Value value); - -#endif diff --git a/src/common.h b/src/common.h deleted file mode 100644 index 5368a37..0000000 --- a/src/common.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef zlc_common_h -#define zlc_common_h - -#include -#include -#include - -/* #define DEBUG_TRACE_EXECUTION */ -/* #define DEBUG_PRINT_CODE */ - -/* #define DEBUG_STRESS_GC */ -/* #define DEBUG_LOG_GC */ - -#define UINT8_COUNT (UINT8_MAX + 1) - -#endif diff --git a/src/compiler.c b/src/compiler.c deleted file mode 100644 index 780ba66..0000000 --- a/src/compiler.c +++ /dev/null @@ -1,917 +0,0 @@ -#include -#include -#include - -#include "common.h" -#include "compiler.h" -#include "memory.h" -#include "scanner.h" - -#ifdef DEBUG_PRINT_CODE -#include "debug.h" -#endif - -typedef struct { - Token current; - Token previous; - bool hadError; - bool panicMode; -} Parser; - -typedef enum { - PREC_NONE, - PREC_ASSIGNMENT, // = - PREC_OR, // or - PREC_AND, // and - PREC_EQUALITY, // == != - PREC_COMPARISON, // < > <= >= - PREC_TERM, // + - - PREC_FACTOR, // * / - PREC_UNARY, // ! - - PREC_CALL, // . () - PREC_PRIMARY -} Precedence; - -typedef void (*ParseFn)(bool canAssign); - -typedef struct { - ParseFn prefix; - ParseFn infix; - Precedence precedence; -} ParseRule; - -typedef struct { - Token name; - int depth; - bool isCaptured; -} Local; - -typedef struct { - uint8_t index; - bool isLocal; -} Upvalue; - -typedef enum { - TYPE_FUNCTION, - TYPE_INITIALIZER, - TYPE_METHOD, - TYPE_SCRIPT -} FunctionType; - -typedef struct Compiler { - struct Compiler *enclosing; - ObjFunction *function; - FunctionType type; - Local locals[UINT8_COUNT]; - int localCount; - Upvalue upvalues[UINT8_COUNT]; - int scopeDepth; -} Compiler; - -typedef struct TypeCompiler { - struct TypeCompiler *enclosing; -} TypeCompiler; - -Parser parser; -Compiler *current = NULL; -TypeCompiler *currentType = NULL; - -static Chunk *currentChunk() { return ¤t->function->chunk; } - -static void errorAt(Token *token, const char *message) { - if (parser.panicMode) - return; - parser.panicMode = true; - fprintf(stderr, "[line %d] Error", token->line); - - if (token->type == TOKEN_EOF) { - fprintf(stderr, " at end"); - } else if (token->type == TOKEN_ERROR) { - // Nothing. - } else { - fprintf(stderr, " at '%.*s'", token->length, token->start); - } - - fprintf(stderr, ": %s\n", message); - parser.hadError = true; -} - -static void error(const char *message) { errorAt(&parser.previous, message); } - -static void errorAtCurrent(const char *message) { - errorAt(&parser.current, message); -} - -static void advance() { - parser.previous = parser.current; - - for (;;) { - parser.current = scanToken(); - if (parser.current.type != TOKEN_ERROR) - break; - - errorAtCurrent(parser.current.start); - } -} - -static void consume(TokenType type, const char *message) { - if (parser.current.type == type) { - advance(); - return; - } - - errorAtCurrent(message); -} - -static bool check(TokenType type) { return parser.current.type == type; } - -static bool match(TokenType type) { - if (!check(type)) - return false; - advance(); - return true; -} - -static void emitByte(uint8_t byte) { - writeChunk(currentChunk(), byte, parser.previous.line); -} - -static void emitBytes(uint8_t byte1, uint8_t byte2) { - emitByte(byte1); - emitByte(byte2); -} - -static void emitLoop(int loopStart) { - emitByte(OP_LOOP); - - int offset = currentChunk()->count - loopStart + 2; - if (offset > UINT16_MAX) - error("Loop body too large."); - - emitByte((offset >> 8) & 0xff); - emitByte(offset & 0xff); -} - -static int emitJump(uint8_t instruction) { - emitByte(instruction); - emitByte(0xff); - emitByte(0xff); - return currentChunk()->count - 2; -} - -static void emitReturn() { - if (current->type == TYPE_INITIALIZER) { - emitBytes(OP_GET_LOCAL, 0); - } else { - emitByte(OP_NIL); - } - - emitByte(OP_RETURN); -} - -static uint8_t makeConstant(Value value) { - int constant = addConstant(currentChunk(), value); - if (constant > UINT8_MAX) { - error("Too many constants in one chunk."); - return 0; - } - - return (uint8_t)constant; -} - -static void emitConstant(Value value) { - emitBytes(OP_CONSTANT, makeConstant(value)); -} - -static void patchJump(int offset) { - // -2 to adjust for the bytecode for the jump offset itself. - int jump = currentChunk()->count - offset - 2; - - if (jump > UINT16_MAX) { - error("Too much code to jump over."); - } - - currentChunk()->code[offset] = (jump >> 8) & 0xff; - currentChunk()->code[offset + 1] = jump & 0xff; -} - -static void initCompiler(Compiler *compiler, FunctionType type) { - compiler->enclosing = current; - compiler->function = NULL; - compiler->type = type; - compiler->localCount = 0; - compiler->scopeDepth = 0; - compiler->function = newFunction(); - current = compiler; - if (type != TYPE_SCRIPT) { - current->function->name = - copyString(parser.previous.start, parser.previous.length); - } - - Local *local = ¤t->locals[current->localCount++]; - local->depth = 0; - local->isCaptured = false; - if (type != TYPE_FUNCTION) { - local->name.start = "this"; - local->name.length = 4; - } else { - local->name.start = ""; - local->name.length = 0; - } -} - -static ObjFunction *endCompiler() { - emitReturn(); - ObjFunction *function = current->function; - -#ifdef DEBUG_PRINT_CODE - if (!parser.hadError) { - disassembleChunk(currentChunk(), function->name != NULL - ? function->name->chars - : "