Fixed sample.

This commit is contained in:
yhirose 2015-02-15 22:21:18 -05:00
parent f7eca8eac5
commit f0351a9909
2 changed files with 7 additions and 9 deletions

View File

@ -53,9 +53,11 @@ int main(void) {
[](const vector<any>& v) { return v[0]; } // 2nd choice [](const vector<any>& v) { return v[0]; } // 2nd choice
}; };
/* This action is not necessary.
parser["Primary"] = [](const vector<any>& v) { parser["Primary"] = [](const vector<any>& v) {
return v.size() == 1 ? v[0] : v[1]; return v[0];
}; };
*/
parser["Number"] = [](const char* s, size_t l) { parser["Number"] = [](const char* s, size_t l) {
return stoi(string(s, l), nullptr, 10); return stoi(string(s, l), nullptr, 10);
@ -63,9 +65,9 @@ int main(void) {
// (4) Parse // (4) Parse
int val; int val;
parser.parse("1+2*3", val); parser.parse("(1+2)*3", val);
assert(val == 7); assert(val == 9);
} }
``` ```

View File

@ -164,18 +164,14 @@ TEST_CASE("Simple calculator test", "[general]")
return v.size() == 1 ? v[0].get<int>() : v[0].get<int>() * v[1].get<int>(); return v.size() == 1 ? v[0].get<int>() : v[0].get<int>() * v[1].get<int>();
}; };
parser["Primary"] = [](const vector<any>& v) {
return v.size() == 1 ? v[0] : v[1];
};
parser["Number"] = [](const char* s, size_t l) { parser["Number"] = [](const char* s, size_t l) {
return atoi(s); return atoi(s);
}; };
int val; int val;
parser.parse("1+2*3", val); parser.parse("(1+2)*3", val);
REQUIRE(val == 7); REQUIRE(val == 9);
} }
TEST_CASE("Calculator test", "[general]") TEST_CASE("Calculator test", "[general]")