cpp-peglib/example/calc.cc

78 lines
2.2 KiB
C++
Raw Normal View History

2015-02-08 01:52:26 +00:00
//
// calc.cc
//
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
// MIT License
//
#include <peglib.h>
#include <iostream>
2015-02-09 22:12:59 +00:00
#include <cstdlib>
2015-02-08 01:52:26 +00:00
using namespace peglib;
using namespace std;
//
// PEG syntax:
//
// EXPRESSION <- TERM (TERM_OPERATOR TERM)*
// TERM <- FACTOR (FACTOR_OPERATOR FACTOR)*
// FACTOR <- NUMBER / '(' EXPRESSION ')'
// TERM_OPERATOR <- [-+]
// FACTOR_OPERATOR <- [/*]
// NUMBER <- [0-9]+
//
2015-02-09 22:12:59 +00:00
int main(int argc, const char** argv)
2015-02-08 01:52:26 +00:00
{
2015-02-09 22:12:59 +00:00
if (argc < 2 || string("--help") == argv[1]) {
cout << "usage: calc [formula]" << endl;
return 1;
2015-02-08 01:52:26 +00:00
}
2015-02-09 22:12:59 +00:00
const char* s = argv[1];
2015-02-08 01:52:26 +00:00
2015-02-14 15:13:10 +00:00
auto reduce = [](const vector<any>& v) -> long {
2015-02-08 01:52:26 +00:00
auto result = v[0].get<long>();
for (auto i = 1u; i < v.size(); i += 2) {
auto num = v[i + 1].get<long>();
auto ope = v[i].get<char>();
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
}
}
return result;
2015-02-09 22:12:59 +00:00
};
2015-02-08 01:52:26 +00:00
2015-02-09 22:12:59 +00:00
const char* syntax =
" EXPRESSION <- TERM (TERM_OPERATOR TERM)* "
" TERM <- FACTOR (FACTOR_OPERATOR FACTOR)* "
" FACTOR <- NUMBER / '(' EXPRESSION ')' "
" TERM_OPERATOR <- [-+] "
" FACTOR_OPERATOR <- [/*] "
" NUMBER <- [0-9]+ "
;
2015-02-08 01:52:26 +00:00
2015-02-14 15:13:10 +00:00
peg parser(syntax);
2015-02-08 01:52:26 +00:00
2015-02-09 22:12:59 +00:00
parser["EXPRESSION"] = reduce;
parser["TERM"] = reduce;
parser["TERM_OPERATOR"] = [](const char* s, size_t l) { return (char)*s; };
parser["FACTOR_OPERATOR"] = [](const char* s, size_t l) { return (char)*s; };
parser["NUMBER"] = [](const char* s, size_t l) { return atol(s); };
2015-02-08 01:52:26 +00:00
long val = 0;
2015-02-14 17:58:17 +00:00
if (parser.parse(s, val)) {
2015-02-08 01:52:26 +00:00
cout << s << " = " << val << endl;
return 0;
}
cout << "syntax error..." << endl;
return -1;
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix