-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.hpp
96 lines (53 loc) · 2.97 KB
/
vm.hpp
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
96
#ifndef MONKEY_PLUSPLUS_VM_HPP
#define MONKEY_PLUSPLUS_VM_HPP
#include <exception>
#include <iostream>
#include "code.hpp"
#include "compiler.hpp"
#include "frame.hpp"
#include "object.hpp"
constexpr int MAXFRAMES = 1024;
constexpr int STACKSIZE = 2048;
constexpr int GLOBALSSIZE = 65536;
struct VM {
VM() = delete;
VM(const VM& other) = delete;
VM(VM&& other) noexcept = delete;
VM& operator=(const VM& other) = delete;
VM& operator=(VM&& other) noexcept = delete;
explicit VM(std::shared_ptr<Bytecode>&& bytecode);
explicit VM(std::shared_ptr<Bytecode>&& bytecode, std::array<std::shared_ptr<Object>, GLOBALSSIZE> s);
std::array<std::shared_ptr<Frame>, MAXFRAMES> frames;
int frames_index;
std::vector<std::shared_ptr<Object>> constants;
std::array<std::shared_ptr<Object>, STACKSIZE> stack;
int sp; // Always points to the next value. Top of stack is stack[sp-1]
std::array<std::shared_ptr<Object>, GLOBALSSIZE> globals;
std::shared_ptr<Frame> current_frame();
void push_frame(std::shared_ptr<Frame> f);
std::shared_ptr<Frame> pop_frame();
std::shared_ptr<Error> push(std::shared_ptr<Object> o);
std::shared_ptr<Object> pop();
std::shared_ptr<Object> last_popped_stack_elem();
std::shared_ptr<Error> execute_call(int num_args);
std::shared_ptr<Error> call_closure(std::shared_ptr<Object> cl, int num_args);
std::shared_ptr<Error> push_closure(int const_index, int num_free);
std::shared_ptr<Error> call_builtin(std::shared_ptr<Object> builtin, int num_args);
std::shared_ptr<Object> build_array(int start_index, int end_index);
std::tuple<std::shared_ptr<Object>, std::shared_ptr<Error>> build_hash(int start_index, int end_index);
std::shared_ptr<Error> execute_binary_operation(OpType op);
std::shared_ptr<Error> execute_comparison(OpType op);
std::shared_ptr<Error> execute_integer_comparison(OpType op, std::shared_ptr<Object> left, std::shared_ptr<Object> right);
std::shared_ptr<Error> execute_binary_integer_operation(OpType op, std::shared_ptr<Object> left, std::shared_ptr<Object> right);
std::shared_ptr<Error> execute_binary_string_operation(OpType op, std::shared_ptr<Object> left, std::shared_ptr<Object> right);
std::shared_ptr<Error> execute_bang_operator();
std::shared_ptr<Error> execute_minus_operator();
std::shared_ptr<Error> execute_index_expression(std::shared_ptr<Object> left, std::shared_ptr<Object> index);
std::shared_ptr<Error> execute_array_index(std::shared_ptr<Object> array, std::shared_ptr<Object> index);
std::shared_ptr<Error> execute_hash_index(std::shared_ptr<Object> hash, std::shared_ptr<Object> index);
std::shared_ptr<Error> run();
};
//std::shared_ptr<VM> new_vm_with_globals_store(std::shared_ptr<Bytecode>&& bytecode, std::vector<std::shared_ptr<Object>> s);
std::shared_ptr<Boolean> native_bool_to_boolean_object(bool input);
bool is_truthy(std::shared_ptr<Object> obj);
#endif //MONKEY_PLUSPLUS_VM_HPP