aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 10:34:25 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 10:34:25 +0200
commit33372512fc32c26913c8385637d20f6d98c8376c (patch)
tree9aa30e3e376d4769e858c37c912866dfdb4b4a62 /src/bytecode
parenteeff5e273f22aa28e81ab080e9ffdce85ac394b8 (diff)
downloadbdl-33372512fc32c26913c8385637d20f6d98c8376c.tar.gz
bdl-33372512fc32c26913c8385637d20f6d98c8376c.zip
Add constants operation
Diffstat (limited to 'src/bytecode')
-rw-r--r--src/bytecode/chunk.h22
-rw-r--r--src/bytecode/debug.h21
-rw-r--r--src/bytecode/main.c24
-rw-r--r--src/bytecode/objects.h27
-rw-r--r--src/bytecode/ops.h3
5 files changed, 83 insertions, 14 deletions
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 @@
1#ifndef BDL_CHUNK_H
2#define BDL_CHUNK_H
3
4#include "objects.h"
5#include "darray.h"
6
7typedef struct Chunk {
8 u8 *code;
9 Object *constants;
10} Chunk;
11
12
13size_t add_constant(Chunk chunk, Object obj);
14
15size_t
16add_constant(Chunk chunk, Object obj) {
17 size_t pos = array_size(chunk.constants);
18 array_push(chunk.constants, obj);
19 return pos;
20}
21
22#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 @@
1#ifndef BDL_DEBUG_H 1#ifndef BDL_DEBUG_H
2#define BDL_DEBUG_H 2#define BDL_DEBUG_H
3 3
4void disassemble_chunk(u8 *chunk, const char *name); 4#include "chunk.h"
5size_t disassemble_instruction(u8 *chunk, size_t offset); 5
6void disassemble_chunk(Chunk chunk, const char *name);
7size_t disassemble_instruction(Chunk chunk, size_t offset);
6 8
7void 9void
8disassemble_chunk(u8 *chunk, const char *name) { 10disassemble_chunk(Chunk chunk, const char *name) {
9 printf("== %s ==\n", name); 11 printf("== %s ==\n", name);
10 size_t offset = 0; 12 size_t offset = 0;
11 while (offset < array_size(chunk)) { 13 while (offset < array_size(chunk.code)) {
12 offset = disassemble_instruction(chunk, offset); 14 offset = disassemble_instruction(chunk, offset);
13 } 15 }
14} 16}
15 17
16size_t 18size_t
17disassemble_instruction(u8 *chunk, size_t offset) { 19disassemble_instruction(Chunk chunk, size_t offset) {
18 printf("%04ld ", offset); 20 printf("%04ld ", offset);
19 u8 instruction = chunk[offset]; 21 u8 instruction = chunk.code[offset];
20 switch (instruction) { 22 switch (instruction) {
21 case OP_RETURN: { 23 case OP_RETURN: {
22 printf("OP_RETURN\n"); 24 printf("OP_RETURN\n");
23 return offset + 1; 25 return offset + 1;
24 } break; 26 } break;
27 case OP_CONSTANT: {
28 u8 constant = chunk.code[offset + 1];
29 printf("%-16s %4d (", "OP_CONSTANT", constant);
30 display(chunk.constants[constant]);
31 printf(")\n");
32 return offset + 2;
33 } break;
25 default: { 34 default: {
26 printf("Unknown OP: %d\n", instruction); 35 printf("Unknown OP: %d\n", instruction);
27 return offset + 1; 36 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 @@
5#include <string.h> 5#include <string.h>
6 6
7#include "types.h" 7#include "types.h"
8#include "chunk.h"
8#include "darray.h" 9#include "darray.h"
9#include "ops.h" 10#include "ops.h"
10#include "debug.h" 11#include "debug.h"
@@ -27,15 +28,24 @@ process_source(const StringView *source) {
27 } 28 }
28 29
29 // Test chunks and debugging utilities. 30 // Test chunks and debugging utilities.
30 u8 *chunk = NULL; 31 // Initialize chunk.
31 array_init(chunk, 0); 32 Chunk chunk = {0};
32 array_push(chunk, OP_RETURN); 33 array_init(chunk.code, 0);
33 array_push(chunk, OP_RETURN); 34 array_init(chunk.constants, 0);
34 array_push(chunk, OP_RETURN); 35
35 array_push(chunk, OP_RETURN); 36 // Push some test instructions.
37 size_t const_idx = add_constant(chunk, 7);
38 array_push(chunk.code, OP_CONSTANT);
39 array_push(chunk.code, const_idx);
40 array_push(chunk.code, OP_RETURN);
41
42 // Disassemble the chunk.
36 disassemble_chunk(chunk, "test chunk"); 43 disassemble_chunk(chunk, "test chunk");
37 44
38 array_free(chunk); 45 // Free chunk.
46 array_free(chunk.code);
47 array_free(chunk.constants);
48
39 array_free(tokens); 49 array_free(tokens);
40} 50}
41 51
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 @@
1#ifndef BDL_OBJECTS_H
2#define BDL_OBJECTS_H
3
4typedef enum ObjectType {
5 OBJ_TYPE_FIXNUM,
6 OBJ_TYPE_BOOL,
7 OBJ_TYPE_NIL,
8 OBJ_TYPE_SYMBOL,
9 OBJ_TYPE_STRING,
10 OBJ_TYPE_PAIR,
11 OBJ_TYPE_PROCEDURE,
12 OBJ_TYPE_LAMBDA,
13 OBJ_TYPE_ERR,
14} ObjectType;
15
16struct Environment;
17
18typedef s64 Object;
19
20void display(Object obj);
21
22void
23display(Object obj) {
24 printf("%ld", obj);
25}
26
27#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 @@
2#define BDL_OPS_H 2#define BDL_OPS_H
3 3
4typedef enum Ops { 4typedef enum Ops {
5 OP_RETURN = 1, 5 OP_CONSTANT,
6 OP_RETURN,
6} Ops; 7} Ops;
7 8
8#endif // BDL_OPS_H 9#endif // BDL_OPS_H