-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.cpp
More file actions
217 lines (201 loc) · 7.5 KB
/
expr.cpp
File metadata and controls
217 lines (201 loc) · 7.5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cstdio>
#include <cstring>
#include <optional>
#include <string>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/regex.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "expr",
.version = CFBOX_VERSION_STRING,
.one_line = "evaluate expressions",
.usage = "expr EXPRESSION...",
.options = "",
.extra = "Supports: + - * / % < <= = != >= > : length substr index",
};
} // namespace
struct Value {
enum Type { Int, Str } type;
long ival = 0;
std::string sval;
static auto integer(long v) -> Value { return {Int, v, {}}; }
static auto str(const std::string& s) -> Value {
char* end = nullptr;
long v = std::strtol(s.c_str(), &end, 10);
if (*end == '\0' && !s.empty()) return {Int, v, {}};
return {Str, 0, s};
}
auto to_int() const -> long {
if (type == Int) return ival;
char* end = nullptr;
return std::strtol(sval.c_str(), &end, 10);
}
auto to_str() const -> std::string {
if (type == Int) return std::to_string(ival);
return sval;
}
auto is_zero_or_null() const -> bool {
if (type == Int) return ival == 0;
return sval.empty();
}
};
static auto eval(std::vector<std::string>::iterator& it,
std::vector<std::string>::iterator end) -> Value;
static auto eval_primary(std::vector<std::string>::iterator& it,
std::vector<std::string>::iterator end) -> Value {
if (it == end) return Value::str("");
if (*it == "length" && std::next(it) != end) {
++it;
auto v = eval_primary(it, end);
return Value::integer(static_cast<long>(v.to_str().size()));
}
if (*it == "substr" && std::distance(it, end) >= 4) {
++it;
auto s = eval_primary(it, end).to_str();
auto pos = eval_primary(it, end).to_int();
auto len = eval_primary(it, end).to_int();
if (pos >= 1 && static_cast<std::size_t>(pos - 1) <= s.size()) {
return Value::str(s.substr(static_cast<std::size_t>(pos - 1),
static_cast<std::size_t>(len)));
}
return Value::str("");
}
if (*it == "index" && std::distance(it, end) >= 3) {
++it;
auto s = eval_primary(it, end).to_str();
auto chars = eval_primary(it, end).to_str();
for (std::size_t i = 0; i < s.size(); ++i) {
if (chars.find(s[i]) != std::string::npos) {
return Value::integer(static_cast<long>(i + 1));
}
}
return Value::integer(0);
}
auto token = *it++;
if (token == "(") {
auto v = eval(it, end);
if (it != end && *it == ")") ++it;
return v;
}
return Value::str(token);
}
static auto eval_compare(std::vector<std::string>::iterator& it,
std::vector<std::string>::iterator end) -> Value {
auto left = eval_primary(it, end);
if (it != end) {
auto op = *it;
if (op == ":" && std::next(it) != end) {
++it;
auto pattern = eval_primary(it, end).to_str();
auto str = left.to_str();
cfbox::util::scoped_regex regex;
if (regex.compile(pattern.c_str(), REG_EXTENDED) != 0) {
return Value::integer(0);
}
regmatch_t match;
if (regex.exec(str.c_str(), 1, &match, 0) == 0) {
if (match.rm_so >= 0 && match.rm_eo > match.rm_so) {
return Value::str(str.substr(
static_cast<std::size_t>(match.rm_so),
static_cast<std::size_t>(match.rm_eo - match.rm_so)));
}
return Value::integer(static_cast<long>(match.rm_eo - match.rm_so));
}
return Value::integer(0);
}
if ((op == "<" || op == "<=" || op == "=" || op == "==" ||
op == "!=" || op == ">=" || op == ">") && std::next(it) != end) {
++it;
auto right = eval_primary(it, end);
bool result = false;
if (left.type == Value::Int && right.type == Value::Int) {
if (op == "<") result = left.ival < right.ival;
if (op == "<=") result = left.ival <= right.ival;
if (op == "=" || op == "==") result = left.ival == right.ival;
if (op == "!=") result = left.ival != right.ival;
if (op == ">=") result = left.ival >= right.ival;
if (op == ">") result = left.ival > right.ival;
} else {
auto ls = left.to_str(), rs = right.to_str();
if (op == "<") result = ls < rs;
if (op == "<=") result = ls <= rs;
if (op == "=" || op == "==") result = ls == rs;
if (op == "!=") result = ls != rs;
if (op == ">=") result = ls >= rs;
if (op == ">") result = ls > rs;
}
return Value::integer(result ? 1 : 0);
}
}
return left;
}
static auto eval_add(std::vector<std::string>::iterator& it,
std::vector<std::string>::iterator end) -> Value {
auto left = eval_compare(it, end);
while (it != end) {
auto op = *it;
if (op != "+" && op != "-") break;
++it;
auto right = eval_compare(it, end);
if (op == "+") left = Value::integer(left.to_int() + right.to_int());
else left = Value::integer(left.to_int() - right.to_int());
}
return left;
}
static auto eval(std::vector<std::string>::iterator& it,
std::vector<std::string>::iterator end) -> Value {
auto left = eval_add(it, end);
while (it != end) {
auto op = *it;
if (op == "*") {
++it; auto right = eval_add(it, end);
left = Value::integer(left.to_int() * right.to_int());
} else if (op == "/") {
++it; auto right = eval_add(it, end);
if (right.to_int() == 0) {
CFBOX_ERR("expr", "division by zero");
return Value::integer(0);
}
left = Value::integer(left.to_int() / right.to_int());
} else if (op == "%") {
++it; auto right = eval_add(it, end);
if (right.to_int() == 0) {
CFBOX_ERR("expr", "division by zero");
return Value::integer(0);
}
left = Value::integer(left.to_int() % right.to_int());
} else if (op == "|") {
++it;
auto right = eval_add(it, end);
if (!left.is_zero_or_null()) return left;
left = right;
} else if (op == "&") {
++it;
auto right = eval_add(it, end);
if (left.is_zero_or_null()) return left;
left = right;
} else {
break;
}
}
return left;
}
auto expr_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("expr", "missing operand");
return 2;
}
std::vector<std::string> args;
for (auto p : pos) args.emplace_back(p);
auto it = args.begin();
auto result = eval(it, args.end());
std::puts(result.to_str().c_str());
return result.is_zero_or_null() ? 1 : 0;
}