-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexpression.cpp
More file actions
49 lines (41 loc) · 1.52 KB
/
expression.cpp
File metadata and controls
49 lines (41 loc) · 1.52 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
#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:
return csprintf(buffer, buffer_len,
this->evaluate_string().find('"') != std::string::npos ? "'%s'" : "\"%s\"",
this->evaluate_string().c_str());
case identifier:
return csprintf(buffer, buffer_len, "%s", this->evaluate_identifier().c_str());
default:
throw std::runtime_error("expression has an invalid datatype");
}
}