-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_literal.cpp
110 lines (90 loc) · 2.02 KB
/
node_literal.cpp
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
#include "exception.hpp"
#include "output.hpp"
#include "type.hpp"
#include "node.hpp"
namespace libblock {
template <>
void NodeLiteral<lb_void_t>::renderValue(
std::ostream &os
) const {
os << "(lb_void_t) {}";
}
template <>
void NodeLiteral<lb_bool_t>::renderValue(
std::ostream &os
) const {
os << (value ? "true" : "false");
}
template <>
void NodeLiteral<lb_int_t>::renderValue(
std::ostream &os
) const {
os << value;
}
template <>
void NodeLiteral<lb_real_t>::renderValue(
std::ostream &os
) const {
os << "*(lb_real_t *) (lb_reg_t []) {" << *(lb_reg_t *) &value << "}";
}
template <>
void NodeLiteral<std::string>::renderValue(
std::ostream &os
) const {
os << "\"";
for (char i: value) {
os << "\\x"
<< "0123456789ABCDEF"[i >> 4 & 15]
<< "0123456789ABCDEF"[i & 15];
}
os << "\\x00\"";
}
template <class T>
NodeLiteral<T>::NodeLiteral(T &&_value):
value {std::move(_value)} {}
template <class T>
NodeLiteral<T>::NodeLiteral(const T &_value):
value {_value} {}
template <class T>
void NodeLiteral<T>::buildProc(
Session &,
Instance &
) {
throw ErrorDiscardNotAllowed {};
}
template <class T>
Type &NodeLiteral<T>::buildOut(
Session &,
Instance &instance,
std::gc_function<std::string (Type &)> &&target
) {
// get type
Type &type {
TypeNative<T>::get()
};
// render
instance.content.insert(
[&, target = std::move(target)](OutputContext &oc) {
oc.endl();
oc.os << target(type) << " = ";
renderValue(oc.os);
oc.os << ";";
}
);
// return
return type;
}
template <class T>
void NodeLiteral<T>::buildIn(
Session &,
Instance &, Type &,
std::gc_function<std::string (Type &)> &&
) {
throw ErrorWriteNotAllowed {};
}
template class NodeLiteral<lb_void_t>;
template class NodeLiteral<lb_bool_t>;
template class NodeLiteral<lb_int_t>;
template class NodeLiteral<lb_real_t>;
template class NodeLiteral<std::string>;
}