mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Improved SemanticValue interface.
This commit is contained in:
parent
1fc0a6819e
commit
fd90882894
@ -33,10 +33,10 @@ int main(int argc, const char** argv)
|
|||||||
const char* s = argv[1];
|
const char* s = argv[1];
|
||||||
|
|
||||||
auto reduce = [](const SemanticValues& sv) -> long {
|
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) {
|
for (auto i = 1u; i < sv.size(); i += 2) {
|
||||||
auto num = sv[i + 1].val.get<long>();
|
auto num = sv[i + 1].get<long>();
|
||||||
auto ope = sv[i].val.get<char>();
|
auto ope = sv[i].get<char>();
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case '+': result += num; break;
|
case '+': result += num; break;
|
||||||
case '-': result -= num; break;
|
case '-': result -= num; break;
|
||||||
|
@ -32,10 +32,10 @@ int main(int argc, const char** argv)
|
|||||||
const char* s = argv[1];
|
const char* s = argv[1];
|
||||||
|
|
||||||
auto reduce = [](const SemanticValues& sv) -> long {
|
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) {
|
for (auto i = 1u; i < sv.size(); i += 2) {
|
||||||
auto num = sv[i + 1].val.get<long>();
|
auto num = sv[i + 1].get<long>();
|
||||||
auto ope = sv[i].val.get<char>();
|
auto ope = sv[i].get<char>();
|
||||||
switch (ope) {
|
switch (ope) {
|
||||||
case '+': result += num; break;
|
case '+': result += num; break;
|
||||||
case '-': result -= num; break;
|
case '-': result -= num; break;
|
||||||
|
@ -51,7 +51,7 @@ struct ast_ope : public ast_node
|
|||||||
return SemanticValues::reduce(
|
return SemanticValues::reduce(
|
||||||
sv.begin() + 1,
|
sv.begin() + 1,
|
||||||
sv.end(),
|
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) {
|
[](shared_ptr<ast_node> r, SemanticValues::const_iterator i) {
|
||||||
auto ope = (i++)->val.get<char>();
|
auto ope = (i++)->val.get<char>();
|
||||||
auto nd = (i++)->val.get<shared_ptr<ast_node>>();
|
auto nd = (i++)->val.get<shared_ptr<ast_node>>();
|
||||||
|
10
peglib.h
10
peglib.h
@ -158,6 +158,16 @@ struct SemanticValue {
|
|||||||
SemanticValue(const any& _val, const char* _name, const char* _s, size_t _l)
|
SemanticValue(const any& _val, const char* _name, const char* _s, size_t _l)
|
||||||
: val(_val), name(_name), s(_s), l(_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;
|
any val;
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* s;
|
const char* s;
|
||||||
|
Loading…
Reference in New Issue
Block a user