Improved SemanticValue interface.

This commit is contained in:
yhirose 2015-03-03 21:51:28 -05:00
parent 1fc0a6819e
commit fd90882894
4 changed files with 17 additions and 7 deletions

View File

@ -33,10 +33,10 @@ int main(int argc, const char** argv)
const char* s = argv[1];
auto reduce = [](const SemanticValues& sv) -> long {
auto result = sv[0].val.get<long>();
auto result = sv[0].get<long>();
for (auto i = 1u; i < sv.size(); i += 2) {
auto num = sv[i + 1].val.get<long>();
auto ope = sv[i].val.get<char>();
auto num = sv[i + 1].get<long>();
auto ope = sv[i].get<char>();
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;

View File

@ -32,10 +32,10 @@ int main(int argc, const char** argv)
const char* s = argv[1];
auto reduce = [](const SemanticValues& sv) -> long {
auto result = sv[0].val.get<long>();
auto result = sv[0].get<long>();
for (auto i = 1u; i < sv.size(); i += 2) {
auto num = sv[i + 1].val.get<long>();
auto ope = sv[i].val.get<char>();
auto num = sv[i + 1].get<long>();
auto ope = sv[i].get<char>();
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;

View File

@ -51,7 +51,7 @@ struct ast_ope : public ast_node
return SemanticValues::reduce(
sv.begin() + 1,
sv.end(),
sv[0].val.get<shared_ptr<ast_node>>(),
sv[0].get<shared_ptr<ast_node>>(),
[](shared_ptr<ast_node> r, SemanticValues::const_iterator i) {
auto ope = (i++)->val.get<char>();
auto nd = (i++)->val.get<shared_ptr<ast_node>>();

View File

@ -158,6 +158,16 @@ struct SemanticValue {
SemanticValue(const any& _val, const char* _name, const char* _s, size_t _l)
: val(_val), name(_name), s(_s), l(_l) {}
template <typename T>
T& get() {
return val.get<T>();
}
template <typename T>
const T& get() const {
return val.get<T>();
}
any val;
const char* name;
const char* s;