-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexpression.cpp
More file actions
57 lines (49 loc) · 1.66 KB
/
expression.cpp
File metadata and controls
57 lines (49 loc) · 1.66 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
#include "expression.h"
#include "../utils/string_utils.h"
#include <stdexcept>
Expression::Expression(const Type type) : type(type) {
}
bool Expression::evaluate_boolean() const {
throw std::runtime_error("not implemented");
}
int64_t Expression::evaluate_integer() const {
return evaluate_boolean() ? 1 : 0;
}
double Expression::evaluate_number() const {
return evaluate_integer();
}
std::string Expression::evaluate_identifier() const {
throw std::runtime_error("not implemented");
}
std::string Expression::evaluate_string() const {
throw std::runtime_error("not implemented");
}
bool Expression::is_numbery() const {
return this->type == number || this->type == integer || this->type == boolean;
}
int Expression::print_to_buffer(char *buffer, size_t buffer_len) const {
switch (this->type) {
case boolean:
return csprintf(buffer, buffer_len, "%s", this->evaluate_boolean() ? "true" : "false");
case integer:
return csprintf(buffer, buffer_len, "%lld", this->evaluate_integer());
case number:
return csprintf(buffer, buffer_len, "%f", this->evaluate_number());
case string: {
char escaped[buffer_len];
int pos = 0;
for (char c : this->evaluate_string()) {
if (c == '"' || c == '\\') {
escaped[pos++] = '\\';
}
escaped[pos++] = c;
}
escaped[pos] = '\0';
return csprintf(buffer, buffer_len, "\"%s\"", escaped);
}
case identifier:
return csprintf(buffer, buffer_len, "%s", this->evaluate_identifier().c_str());
default:
throw std::runtime_error("expression has an invalid datatype");
}
}