-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.hpp
94 lines (56 loc) · 2.08 KB
/
compiler.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
#ifndef MONKEY_PLUSPLUS_COMPILER_HPP
#define MONKEY_PLUSPLUS_COMPILER_HPP
#include "ast.hpp"
#include "builtins.hpp"
#include "code.hpp"
#include "object.hpp"
#include "symbol_table.hpp"
struct Bytecode {
Bytecode() = default;
Bytecode(const Bytecode& other) = default;
Bytecode(Bytecode&& other) noexcept = default;
Bytecode& operator=(const Bytecode& other) = default;
Bytecode& operator=(Bytecode&& other) noexcept = default;
Instructions instructions;
std::vector<std::shared_ptr<Object>> constants;
};
struct EmittedInstruction {
OpType opcode;
int position;
};
struct CompilationScope {
Instructions instructions;
EmittedInstruction last_instruction;
EmittedInstruction previous_instruction;
};
struct Compiler {
Compiler() = default;
Compiler(const Compiler& other) = default;
Compiler(Compiler&& other) noexcept = default;
Compiler& operator=(const Compiler& other) = default;
Compiler& operator=(Compiler&& other) noexcept = default;
std::vector<std::shared_ptr<Object>> constants;
std::shared_ptr<SymbolTable> symbol_table;
std::vector<CompilationScope> scopes;
int scope_index;
std::shared_ptr<Error> compile(std::shared_ptr<Node> node);
int add_constant(std::shared_ptr<Object> obj);
int emit(OpType op);
int emit(OpType op, int operand);
int emit(OpType op, int first_operand, int second_operand);
int add_instruction(Instructions ins);
void set_last_instruction(OpType op, int pos);
bool last_instruction_is(OpType op) const;
void remove_last_pop();
void replace_last_pop_with_return();
void replace_instruction(int pos, Instructions new_instruction);
void change_operand(int op_pos, int operand);
void enter_scope();
Instructions leave_scope();
void load_symbol(Symbol s);
std::shared_ptr<Bytecode> bytecode();
};
std::shared_ptr<Compiler> new_compiler();
std::shared_ptr<Compiler> new_compiler_with_state(
std::shared_ptr<SymbolTable> s, std::vector<std::shared_ptr<Object>> constants);
#endif //MONKEY_PLUSPLUS_COMPILER_HPP