-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.h
More file actions
82 lines (66 loc) · 2.62 KB
/
chunk.h
File metadata and controls
82 lines (66 loc) · 2.62 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef LISA_CHUNK_H
#define LISA_CHUNK_H
#include "value.h"
#include <stdint.h>
typedef enum {
OP_CONSTANT, /* [idx] push constants[idx] */
OP_NIL, /* push nil */
OP_TRUE, /* push true */
OP_FALSE, /* push false */
OP_POP, /* pop top */
OP_GET_LOCAL, /* [slot] push stack[base+slot] */
OP_SET_LOCAL, /* [slot] stack[base+slot] = peek */
OP_GET_UPVALUE, /* [idx] push *upvalues[idx]->location */
OP_SET_UPVALUE, /* [idx] *upvalues[idx]->location = peek */
OP_GET_GLOBAL, /* [idx] push globals[constants[idx]] */
OP_DEF_GLOBAL, /* [idx] globals[constants[idx]] = pop */
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
OP_MOD,
OP_NEGATE,
OP_EQUAL,
OP_NOT_EQUAL,
OP_LESS,
OP_LESS_EQUAL,
OP_GREATER,
OP_GREATER_EQUAL,
OP_NOT,
OP_JUMP, /* [lo][hi] ip += offset */
OP_JUMP_IF_FALSE, /* [lo][hi] if falsey(pop) ip += offset */
OP_LOOP, /* [lo][hi] ip -= offset */
OP_CLOSURE, /* [idx] then pairs of [is_local, index] */
OP_CALL, /* [argc] call top function with argc args */
OP_TAIL_CALL, /* [argc] tail call: reuse current frame */
OP_RETURN, /* return top of stack */
OP_CLOSE_UPVALUE, /* close upvalue at stack top, pop */
OP_CLOSE_UPVALUES_AT,/* [slot] close upvalues at slot and above (no pop) */
OP_CONS, /* push cons(pop2, pop1) */
OP_CAR, /* push car(pop) */
OP_CDR, /* push cdr(pop) */
OP_LIST, /* [n] pop n items, build list */
OP_PRINTLN, /* [argc] print argc values with spaces, newline */
} lisa_op;
/* Dynamic array of constants */
typedef struct {
int count;
int capacity;
lisa_value *values;
} lisa_value_array;
void lisa_value_array_init(lisa_value_array *arr);
void lisa_value_array_write(lisa_value_array *arr, lisa_value value);
void lisa_value_array_free(lisa_value_array *arr);
/* Bytecode chunk */
typedef struct {
int count;
int capacity;
uint8_t *code;
int *lines; /* source line per bytecode byte */
lisa_value_array constants;
} lisa_chunk;
void lisa_chunk_init(lisa_chunk *chunk);
void lisa_chunk_write(lisa_chunk *chunk, uint8_t byte, int line);
void lisa_chunk_free(lisa_chunk *chunk);
int lisa_chunk_add_constant(lisa_chunk *chunk, lisa_value value);
#endif