summaryrefslogtreecommitdiff
path: root/vm.h
blob: 44005640d0ad47fa58383ac83948ded574a05fab (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef _vm_h
#define _vm_h

#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct _thread Thread;

#include "val.h"
#include "state.h"

typedef struct {
	struct {
		size_t len;
		size_t cap;
		uint8_t *d;
	} bc;
	struct {
		size_t len;
		size_t cap;
		Val *d;
	} consts;
} Chunk;
Chunk chunk_new(State *S);
size_t chunk_wbc(State *S, Chunk *ch, uint8_t byte);
size_t chunk_wconst(State *S, Chunk *ch, Val v);

#define STACKSIZE 128
typedef struct _thread {
	Chunk *ch;
	size_t ip;
	Val stack[STACKSIZE];
	size_t sp;
} Thread;
Thread thread_new(State *S);

typedef enum {
	OP_RET,
	OP_LOADK,
	OP_PRINT,

	OP_ADD,
	OP_SUB,
	OP_MUL,
	OP_DIV,
} Op;

void runvm(State *S);

#endif