aboutsummaryrefslogtreecommitdiffstats
path: root/src/bytecode/chunk.h
blob: 6157057655804838aef720dbc5df3ac6c460162a (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
35
#ifndef BDL_CHUNK_H
#define BDL_CHUNK_H

#include "objects.h"
#include "darray.h"

typedef struct LineInfo {
    size_t line;
    size_t col;
} LineInfo;

typedef struct Chunk {
    u8 *code;
    Object *constants;
    LineInfo *lines;
} Chunk;

void add_code(Chunk chunk, u8 byte, size_t line, size_t col);
size_t add_constant(Chunk chunk, Object obj);

void
add_code(Chunk chunk, u8 byte, size_t line, size_t col) {
    array_push(chunk.code, byte);
    LineInfo info = (LineInfo){line, col};
    array_push(chunk.lines, info);
}

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