From 9ce9a7f510e6ba407c2e14d3eae4d603b38edde7 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 22 Oct 2021 13:58:02 +0200 Subject: Prepare compilation pipeline --- src/bytecode/compiler.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/bytecode/compiler.h (limited to 'src/bytecode/compiler.h') 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 @@ +#ifndef BDL_COMPILER_H +#define BDL_COMPILER_H + +#include "chunk.h" +#include "lexer.h" + +typedef struct Visitor { + Token *tokens; + size_t current; +} Visitor; + +// Mimics the functionality in the Scanner functions, but for entire tokens. +Token next_token(Visitor *visitor); +Token peek_token(const Visitor *visitor); +bool has_next_token(const Visitor *visitor); + +Chunk * compile(Token *tokens); + +Chunk * +compile(Token *tokens) { + Chunk *chunk = NULL; + chunk = chunk_init(); + size_t const_a = add_constant(chunk, 7); + add_code(chunk, OP_CONSTANT, 1, 1); + add_code(chunk, const_a, 1, 1); + size_t const_b = add_constant(chunk, 2); + add_code(chunk, OP_CONSTANT, 1, 2); + add_code(chunk, const_b, 1, 2); + add_code(chunk, OP_MOD, 1, 3); + add_code(chunk, OP_RETURN, 1, 1); + return chunk; +} + +#endif // BDL_COMPILER_H -- cgit v1.2.1