aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/compiler.h
blob: 283d7a6b77e5a44aa33344be2b32277d97bfa00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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