-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprogram.hpp
More file actions
145 lines (115 loc) · 3.62 KB
/
program.hpp
File metadata and controls
145 lines (115 loc) · 3.62 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include <array>
#include <map>
#include <string>
#include <vector>
#include "math/number.hpp"
class Operand {
public:
enum class Type { CONSTANT, DIRECT, INDIRECT };
Operand() : Operand(Type::CONSTANT, 0) {}
Operand(Type t, const Number &v) : type(t), value(v) {}
inline bool operator==(const Operand &o) const {
return (type == o.type) && (value == o.value);
}
inline bool operator!=(const Operand &o) const { return !((*this) == o); }
inline bool operator<(const Operand &o) const {
if (type != o.type) return type < o.type;
if (value != o.value) return value < o.value;
return false; // equal
}
Type type;
Number value;
};
class Operation {
public:
enum class Type {
NOP, // no operation
MOV, // assignment
ADD, // addition
SUB, // subtraction
TRN, // truncated subtraction
MUL, // multiplication
DIV, // division
DIF, // conditional division
DIR, // repeated division
MOD, // modulo
POW, // power
GCD, // greatest common divisor
LEX, // largest exponent
BIN, // binomial coefficient
FAC, // falling and rising factorial
LOG, // logarithm
NRT, // n-th root
DGS, // digit sum
DGR, // digital root
EQU, // equality
NEQ, // inequality
LEQ, // less or equal
GEQ, // greater or equal
MIN, // minimum
MAX, // maximum
BAN, // bitwise and
BOR, // bitwise or
BXO, // bitwise xor
LPB, // loop begin
LPE, // loop end
CLR, // clear
FIL, // fill
ROL, // rotate left
ROR, // rotate right
SEQ, // sequence
PRG, // program
DBG, // debug
__COUNT // number of operation types
};
static const std::array<Type, 37> Types;
class Metadata {
public:
static const Metadata &get(Type t);
static const Metadata &get(const std::string &name);
Type type;
std::string name;
int64_t ref_id;
size_t num_operands;
bool is_public;
bool is_reading_target;
bool is_writing_target;
};
Operation() : Operation(Type::NOP) {}
explicit Operation(Type y)
: Operation(y, {Operand::Type::DIRECT, 0}, {Operand::Type::CONSTANT, 0}) {
}
Operation(Type y, Operand t, Operand s, const std::string &c = "")
: type(y), target(t), source(s), comment(c) {}
inline bool operator==(const Operation &op) const {
return (type == op.type) && (source == op.source) && (target == op.target);
}
inline bool operator!=(const Operation &op) const { return !((*this) == op); }
inline bool operator<(const Operation &op) const {
if (type != op.type) return type < op.type;
if (target != op.target) return target < op.target;
if (source != op.source) return source < op.source;
return false; // equal
}
Type type;
Operand target;
Operand source;
std::string comment;
};
class Program {
public:
static constexpr int64_t INPUT_CELL = 0;
static constexpr int64_t OUTPUT_CELL = 0;
void push_front(Operation::Type t, Operand::Type tt, const Number &tv,
Operand::Type st, const Number &sv);
void push_back(Operation::Type t, Operand::Type tt, const Number &tv,
Operand::Type st, const Number &sv);
int64_t getDirective(const std::string &name) const;
int64_t getDirective(const std::string &name, int64_t defaultValue) const;
bool operator==(const Program &p) const;
bool operator!=(const Program &p) const;
bool operator<(const Program &p) const;
std::vector<Operation> ops;
std::map<std::string, int64_t> directives;
};