-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValue.cpp
More file actions
38 lines (36 loc) · 1.07 KB
/
Value.cpp
File metadata and controls
38 lines (36 loc) · 1.07 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
#include "FwdTypes.hpp"
#include "Call.hpp"
#include "Class.hpp"
#include <sstream>
#include <iomanip>
std::string toString(const Value& l) {
switch (static_cast<ValueType>(l.index())) {
case ValueType::Nil:
return "nil";
case ValueType::Boolean:
return get<bool>(l) ? "true" : "false";
case ValueType::Number: {
std::ostringstream oss;
oss << std::setprecision(14) << get<double>(l);
return oss.str();
}
case ValueType::String:
return get<std::string>(l);
case ValueType::Callable:
return get<std::shared_ptr<LoxCallable>>(l)->toString();
case ValueType::Instance:
return get<std::shared_ptr<LoxInstance>>(l)->toString();
}
return "Error: bad ValueType";
}
bool isTruthy(const Value& l) {
if (static_cast<ValueType>(l.index()) == ValueType::Nil) {
return false;
}
else if (static_cast<ValueType>(l.index()) == ValueType::Boolean) {
return get<bool>(l);
}
else {
return true;
}
}