-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.h
95 lines (70 loc) · 2.04 KB
/
vm.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
仮想マシン
*/
#ifndef CLOX_VM_H
#define CLOX_VM_H
#include "object.h"
#include "chunk.h"
#include "table.h"
#include "value.h"
#define FRAMES_MAX 64
#define STACK_MAX (FRAMES_MAX * UINT8_COUNT)
/// @brief 関数のローカル変数
typedef struct {
ObjClosure* closure;
uint8_t* ip;
/// @brief VMのスタックでこの関数が利用できるスロット
Value* slots;
} CallFrame;
/// @brief 仮想マシン
typedef struct {
/// @brief コールフレーム
CallFrame frames[FRAMES_MAX];
/// @brief framesの長さ
int frame_count;
/// @brief スタック
Value stack[STACK_MAX];
/// @brief スタックの一番上
Value* stack_top;
/// @brief グローバルのハッシュ表
Table globals;
/// @brief インターン化された文字列の集合
Table strings;
ObjString* init_string;
/// @brief オープンな上位値の配列
ObjUpvalue* open_upvalues;
/// @brief 確保されたメモリの大きさ
size_t bytes_allocated;
/// @brief 次のガベージコレクションを実行する,bytes_allocatedの閾値
size_t next_gc;
/// @brief GC用の連結リスト
Obj* objects;
/// @brief グレイのオブジェクトの数
int gray_count;
/// @brief gray_stackの容量
int gray_capacity;
/// @brief グレイのオブジェクトを記録するスタック
Obj** gray_stack;
} Vm;
/// @brief 実行結果
typedef enum {
INTERPRET_OK,
INTERPRET_COMPILE_ERROR,
INTERPRET_RUNTIME_ERROR,
} InterpretResult;
extern Vm vm;
/// @brief 仮想マシンを初期化する
void init_vm();
/// @brief 仮想マシンを解放する
void free_vm();
/// @brief 命令の配列を実行する
/// @param chunk 命令の配列
/// @return 結果
InterpretResult interpret(const char* source);
/// @brief スタックにValueをプッシュする
/// @param value プッシュするValue
void push(Value value);
/// @brief スタックからValueをポップする
/// @return ポップした値
Value pop();
#endif