From 33372512fc32c26913c8385637d20f6d98c8376c Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 10:34:25 +0200 Subject: Add constants operation --- src/bytecode/chunk.h | 22 ++++++++++++++++++++++ src/bytecode/debug.h | 21 +++++++++++++++------ src/bytecode/main.c | 24 +++++++++++++++++------- src/bytecode/objects.h | 27 +++++++++++++++++++++++++++ src/bytecode/ops.h | 3 ++- 5 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 src/bytecode/chunk.h create mode 100644 src/bytecode/objects.h (limited to 'src/bytecode') diff --git a/src/bytecode/chunk.h b/src/bytecode/chunk.h new file mode 100644 index 0000000..29dd99d --- /dev/null +++ b/src/bytecode/chunk.h @@ -0,0 +1,22 @@ +#ifndef BDL_CHUNK_H +#define BDL_CHUNK_H + +#include "objects.h" +#include "darray.h" + +typedef struct Chunk { + u8 *code; + Object *constants; +} Chunk; + + +size_t add_constant(Chunk chunk, Object obj); + +size_t +add_constant(Chunk chunk, Object obj) { + size_t pos = array_size(chunk.constants); + array_push(chunk.constants, obj); + return pos; +} + +#endif // BDL_CHUNK_H diff --git a/src/bytecode/debug.h b/src/bytecode/debug.h index 3d08d8f..e07b9a2 100644 --- a/src/bytecode/debug.h +++ b/src/bytecode/debug.h @@ -1,27 +1,36 @@ #ifndef BDL_DEBUG_H #define BDL_DEBUG_H -void disassemble_chunk(u8 *chunk, const char *name); -size_t disassemble_instruction(u8 *chunk, size_t offset); +#include "chunk.h" + +void disassemble_chunk(Chunk chunk, const char *name); +size_t disassemble_instruction(Chunk chunk, size_t offset); void -disassemble_chunk(u8 *chunk, const char *name) { +disassemble_chunk(Chunk chunk, const char *name) { printf("== %s ==\n", name); size_t offset = 0; - while (offset < array_size(chunk)) { + while (offset < array_size(chunk.code)) { offset = disassemble_instruction(chunk, offset); } } size_t -disassemble_instruction(u8 *chunk, size_t offset) { +disassemble_instruction(Chunk chunk, size_t offset) { printf("%04ld ", offset); - u8 instruction = chunk[offset]; + u8 instruction = chunk.code[offset]; switch (instruction) { case OP_RETURN: { printf("OP_RETURN\n"); return offset + 1; } break; + case OP_CONSTANT: { + u8 constant = chunk.code[offset + 1]; + printf("%-16s %4d (", "OP_CONSTANT", constant); + display(chunk.constants[constant]); + printf(")\n"); + return offset + 2; + } break; default: { printf("Unknown OP: %d\n", instruction); return offset + 1; diff --git a/src/bytecode/main.c b/src/bytecode/main.c index 78fdfd3..ce196c8 100644 --- a/src/bytecode/main.c +++ b/src/bytecode/main.c @@ -5,6 +5,7 @@ #include #include "types.h" +#include "chunk.h" #include "darray.h" #include "ops.h" #include "debug.h" @@ -27,15 +28,24 @@ process_source(const StringView *source) { } // Test chunks and debugging utilities. - u8 *chunk = NULL; - array_init(chunk, 0); - array_push(chunk, OP_RETURN); - array_push(chunk, OP_RETURN); - array_push(chunk, OP_RETURN); - array_push(chunk, OP_RETURN); + // Initialize chunk. + Chunk chunk = {0}; + array_init(chunk.code, 0); + array_init(chunk.constants, 0); + + // Push some test instructions. + size_t const_idx = add_constant(chunk, 7); + array_push(chunk.code, OP_CONSTANT); + array_push(chunk.code, const_idx); + array_push(chunk.code, OP_RETURN); + + // Disassemble the chunk. disassemble_chunk(chunk, "test chunk"); - array_free(chunk); + // Free chunk. + array_free(chunk.code); + array_free(chunk.constants); + array_free(tokens); } diff --git a/src/bytecode/objects.h b/src/bytecode/objects.h new file mode 100644 index 0000000..9bfa2cf --- /dev/null +++ b/src/bytecode/objects.h @@ -0,0 +1,27 @@ +#ifndef BDL_OBJECTS_H +#define BDL_OBJECTS_H + +typedef enum ObjectType { + OBJ_TYPE_FIXNUM, + OBJ_TYPE_BOOL, + OBJ_TYPE_NIL, + OBJ_TYPE_SYMBOL, + OBJ_TYPE_STRING, + OBJ_TYPE_PAIR, + OBJ_TYPE_PROCEDURE, + OBJ_TYPE_LAMBDA, + OBJ_TYPE_ERR, +} ObjectType; + +struct Environment; + +typedef s64 Object; + +void display(Object obj); + +void +display(Object obj) { + printf("%ld", obj); +} + +#endif // BDL_OBJECTS_H diff --git a/src/bytecode/ops.h b/src/bytecode/ops.h index f7001ad..79092ab 100644 --- a/src/bytecode/ops.h +++ b/src/bytecode/ops.h @@ -2,7 +2,8 @@ #define BDL_OPS_H typedef enum Ops { - OP_RETURN = 1, + OP_CONSTANT, + OP_RETURN, } Ops; #endif // BDL_OPS_H -- cgit v1.2.1