-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.h
75 lines (60 loc) · 2.31 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
//
// Created by paul on 27/05/2021.
//
#ifndef LOXCOMPILER_VM_H
#define LOXCOMPILER_VM_H
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PROJECT INCLUDES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "chunk.h"
#include "value.h"
#include "table.h"
#include "object.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC CONSTANTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define FRAMES_MAX 64
#define STACK_MAX (FRAMES_MAX * UINT8_COUNT)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct CallFrame {
ObjClosure* closure;
uint8_t* ip;
Value* slots;
} CallFrame;
typedef struct VM {
CallFrame frames[FRAMES_MAX];
int frame_count;
Value stack[STACK_MAX];
Value* stack_top;
Table globals;
Table strings;
ObjString* init_string;
ObjUpvalue* open_upvalues;
size_t bytes_allocated;
size_t next_gc;
Obj* objects;
int gray_count;
int gray_capacity;
Obj** gray_stack;
} VM;
typedef enum InterpretResult {
INTERPRET_OK,
INTERPRET_COMPILE_ERROR,
INTERPRET_RUNTIME_ERROR
} InterpretResult;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern VM vm;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTION DECLARATIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void define_native(const char* name, NativeFn function);
void free_vm();
void init_vm();
InterpretResult interpret(const char* source);
Value pop();
void push(Value value);
#endif //LOXCOMPILER_VM_H