-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcode.h
97 lines (85 loc) · 2.17 KB
/
pcode.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
96
97
//
// Created by 23207 on 2020/11/5.
//
/*
* add #
* sub #
* mult #
* div #
* lod name #
* allocate name size #
* initialize_a name array #
* li constant #
* const type name #
* var type name size #
* eq
* print_s string #
* print_v #
* scanf name #
* set_function_flag name #
* assign name
*/
#ifndef UNTITLED_PCODE_H
#define UNTITLED_PCODE_H
#include "string"
#include "list"
using namespace std;
class Pcode {
public:
Pcode(string type, string op1, string op2, string op3, vector<int> arr) {
this->type = type;
this->op1 = op1;
this->op2 = op2;
this->op3 = op3;
this->arr = arr;
}
string type;
string op1;
string op2;
string op3;
vector<int> arr;
};
list<Pcode> pcodes;
list<Pcode> buffer;
static bool buffer_on = false;
void cache_emit() {
buffer_on = true;
}
list<Pcode> get_cache() {
return buffer;
}
void close_cache() {
buffer_on = false;
}
void clear_cache() {
buffer = list<Pcode>();
buffer_on = false;
}
void emit(string type, string op1="", string op2="", string op3="") {
if (!buffer_on) pcodes.emplace_back(type, op1, op2, op3, vector<int>());
else {
pcodes.emplace_back(type, op1, op2, op3, vector<int>());
buffer.emplace_back(type, op1, op2, op3, vector<int>());
}
Pcode pc = pcodes.back();
cout << pc.type << " " << pc.op1 << " " << pc.op2 << " " << pc.op3 << endl;
}
void emit(string type, vector<int> arr, string name="") {
if (!buffer_on) pcodes.emplace_back(type, name, "", "", arr);
else {
pcodes.emplace_back(type, name, "", "", arr);
buffer.emplace_back(type, name, "", "", arr);
}
Pcode pc = pcodes.back();
cout << pc.type << " " << pc.op1 << " " << pc.op2 << " " << pc.op3 << endl;
}
void emit(string type, vector<int> arr, string op1, string op2, string op3) {
if (!buffer_on) pcodes.emplace_back(type, op1, op2, op3, arr);
else {
pcodes.emplace_back(type, op1, op2, op3, arr);
buffer.emplace_back(type, op1, op2, op3, arr);
}
Pcode pc = pcodes.back();
cout << pc.type << " " << pc.op1 << " " << pc.op2 << " " << pc.op3 << endl;
}
#endif //UNTITLED_PCODE_H