-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.h
70 lines (62 loc) · 1.59 KB
/
chunk.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
This module will define our code representation.
We will use "chunk" to refer to sequences of bytecode.
*/
#ifndef clox_chunk_h
#define clox_chunk_h
#include "common.h"
#include "value.h"
/*
each instruction has a one-byte operation code
(universally shortened to opcode)
*/
typedef enum {
OP_CONSTANT,
OP_NIL,
OP_TRUE,
OP_FALSE,
OP_POP,
OP_GET_LOCAL,
OP_SET_LOCAL,
OP_GET_GLOBAL,
OP_DEFINE_GLOBAL,
OP_SET_GLOBAL,
OP_GET_UPVALUE,
OP_SET_UPVALUE,
OP_EQUAL,
OP_GREATER,
OP_LESS,
OP_ADD,
OP_SUBTRACT,
OP_MULTIPLY,
OP_DIVIDE,
OP_INT_DIVIDE, /* integer division */
OP_MODULUS, /* modulus operator */
OP_NOT, /* logical not (!true == false) */
OP_NEGATE, /* Unary negation (a = 12 | -a == -12) */
OP_PRINT,
OP_JUMP, /* Unconditional jump */
OP_JUMP_IF_FALSE,
OP_LOOP,
OP_CALL, /* For function calls */
OP_CLOSURE,
OP_CLOSE_UPVALUE,
OP_RETURN, /* return instruction*/
} OpCode;
/*
Bytecode is a series of instructions. Eventually,
we’ll store some other data along with the instruction
*/
typedef struct {
int count;
int capacity;
uint8_t* code;
int* lines; /* This array will keep track of line information */
ValueArray constants;
} Chunk;
void initChunk(Chunk* chunk);
void freeChunk(Chunk* chunk);
void writeChunk(Chunk* chunk, uint8_t byte, int line);
/* This is a convinence method to add a new constant to the chunk */
int addConstant(Chunk* chunk, Value value);
#endif