aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-22 13:58:02 +0200
committerBad Diode <bd@badd10de.dev>2021-10-22 13:58:02 +0200
commit9ce9a7f510e6ba407c2e14d3eae4d603b38edde7 (patch)
treeaaca7e34058761ae4d4f58e7567f126c5dc9a3a5 /src/bytecode/compiler.h
parent6fd244cb04cf1972e9d7dcc5635bf5cfe8194402 (diff)
downloadbdl-9ce9a7f510e6ba407c2e14d3eae4d603b38edde7.tar.gz
bdl-9ce9a7f510e6ba407c2e14d3eae4d603b38edde7.zip
Prepare compilation pipeline
Diffstat (limited to 'src/bytecode/compiler.h')
-rw-r--r--src/bytecode/compiler.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h
new file mode 100644
index 0000000..283d7a6
--- /dev/null
+++ b/src/bytecode/compiler.h
@@ -0,0 +1,34 @@
1#ifndef BDL_COMPILER_H
2#define BDL_COMPILER_H
3
4#include "chunk.h"
5#include "lexer.h"
6
7typedef struct Visitor {
8 Token *tokens;
9 size_t current;
10} Visitor;
11
12// Mimics the functionality in the Scanner functions, but for entire tokens.
13Token next_token(Visitor *visitor);
14Token peek_token(const Visitor *visitor);
15bool has_next_token(const Visitor *visitor);
16
17Chunk * compile(Token *tokens);
18
19Chunk *
20compile(Token *tokens) {
21 Chunk *chunk = NULL;
22 chunk = chunk_init();
23 size_t const_a = add_constant(chunk, 7);
24 add_code(chunk, OP_CONSTANT, 1, 1);
25 add_code(chunk, const_a, 1, 1);
26 size_t const_b = add_constant(chunk, 2);
27 add_code(chunk, OP_CONSTANT, 1, 2);
28 add_code(chunk, const_b, 1, 2);
29 add_code(chunk, OP_MOD, 1, 3);
30 add_code(chunk, OP_RETURN, 1, 1);
31 return chunk;
32}
33
34#endif // BDL_COMPILER_H