-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.hpp
103 lines (76 loc) · 2.44 KB
/
code.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
97
98
99
100
101
102
103
#ifndef MONKEY_PLUSPLUS_CODE_HPP
#define MONKEY_PLUSPLUS_CODE_HPP
#include <cstdint>
#include <exception>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <string>
#include <vector>
typedef std::vector<uint8_t> Instructions;
std::ostream& operator<<(std::ostream& out, const Instructions& ins);
typedef uint8_t Opcode;
enum class OpType : Opcode {
OpConstant,
OpAdd,
OpSub,
OpMul,
OpDiv,
OpPop,
OpTrue,
OpFalse,
OpEqual,
OpNotEqual,
OpGreaterThan,
OpMinus,
OpBang,
OpJumpNotTruthy,
OpJump,
OpNull,
OpSetGlobal,
OpGetGlobal,
OpArray,
OpHash,
OpIndex,
OpCall,
OpReturnValue,
OpReturn,
OpSetLocal,
OpGetLocal,
OpGetBuiltin,
OpClosure,
OpGetFree,
OpCurrentClosure
};
struct Definition {
Definition() = delete;
Definition(std::string name);
Definition(const Definition& other) = default;
Definition(Definition&& other) noexcept = default;
Definition& operator=(const Definition& other) = default;
Definition& operator=(Definition&& other) noexcept = default;
std::string name;
std::vector<int> operand_widths;
};
extern std::map<OpType, std::shared_ptr<Definition>> definitions;
std::shared_ptr<Definition> new_definition(std::string name);
std::shared_ptr<Definition> new_definition(std::string name, int operand_width);
std::shared_ptr<Definition> new_definition(std::string name, int first_operand_width, int second_operand_width);
std::tuple<std::shared_ptr<Definition>, bool> lookup(const OpType& op);
// Convert an OpType to the underlying Opcode type
// Reproduced from: https://stackoverflow.com/questions/11421432/how-can-i-output-the-value-of-an-enum-class-in-c11
template<typename Enumeration>
constexpr std::enable_if_t<std::is_enum<Enumeration>::value,
std::underlying_type_t<Enumeration>> as_opcode(const Enumeration value) {
return static_cast<std::underlying_type_t<Enumeration>>(value);
}
std::string fmt_instruction(std::shared_ptr<Definition> def, std::vector<int> operands);
Instructions make(OpType op);
Instructions make(OpType op, int operand);
Instructions make(OpType op, int first_operand, int second_operand);
int read_uint_8(Instructions ins, int offset);
int read_uint_16(Instructions ins, int offset);
std::tuple<std::vector<int>, int> read_operands(std::shared_ptr<Definition> def, Instructions ins);
#endif //MONKEY_PLUSPLUS_CODE_HPP