From fd908828948ceb80dc394fe63270dfd7ba5061d4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 3 Mar 2015 21:51:28 -0500 Subject: [PATCH] Improved SemanticValue interface. --- example/calc.cc | 6 +++--- example/calc2.cc | 6 +++--- example/calc3.cc | 2 +- peglib.h | 10 ++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/example/calc.cc b/example/calc.cc index 8c1ee4b..825e846 100644 --- a/example/calc.cc +++ b/example/calc.cc @@ -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(); + auto result = sv[0].get(); for (auto i = 1u; i < sv.size(); i += 2) { - auto num = sv[i + 1].val.get(); - auto ope = sv[i].val.get(); + auto num = sv[i + 1].get(); + auto ope = sv[i].get(); switch (ope) { case '+': result += num; break; case '-': result -= num; break; diff --git a/example/calc2.cc b/example/calc2.cc index d532bb8..04b0b45 100644 --- a/example/calc2.cc +++ b/example/calc2.cc @@ -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(); + auto result = sv[0].get(); for (auto i = 1u; i < sv.size(); i += 2) { - auto num = sv[i + 1].val.get(); - auto ope = sv[i].val.get(); + auto num = sv[i + 1].get(); + auto ope = sv[i].get(); switch (ope) { case '+': result += num; break; case '-': result -= num; break; diff --git a/example/calc3.cc b/example/calc3.cc index 6c69e2b..140c920 100644 --- a/example/calc3.cc +++ b/example/calc3.cc @@ -51,7 +51,7 @@ struct ast_ope : public ast_node return SemanticValues::reduce( sv.begin() + 1, sv.end(), - sv[0].val.get>(), + sv[0].get>(), [](shared_ptr r, SemanticValues::const_iterator i) { auto ope = (i++)->val.get(); auto nd = (i++)->val.get>(); diff --git a/peglib.h b/peglib.h index 70618b8..121b785 100644 --- a/peglib.h +++ b/peglib.h @@ -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 + T& get() { + return val.get(); + } + + template + const T& get() const { + return val.get(); + } + any val; const char* name; const char* s;