2015-02-08 01:52:26 +00:00
|
|
|
//
|
|
|
|
// calc2.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
|
|
|
|
2015-08-10 20:37:56 +00:00
|
|
|
using namespace peg;
|
2015-02-08 01:52:26 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// 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
|
|
|
{
|
2019-11-21 16:52:31 +00:00
|
|
|
if (argc < 2 || std::string("--help") == argv[1]) {
|
|
|
|
std::cout << "usage: calc [formula]" << std::endl;
|
2015-02-09 22:12:59 +00:00
|
|
|
return 1;
|
2015-02-08 01:52:26 +00:00
|
|
|
}
|
|
|
|
|
2015-02-22 00:38:30 +00:00
|
|
|
auto reduce = [](const SemanticValues& sv) -> long {
|
2019-11-21 16:52:31 +00:00
|
|
|
auto result = any_cast<long>(sv[0]);
|
2015-02-22 00:38:30 +00:00
|
|
|
for (auto i = 1u; i < sv.size(); i += 2) {
|
2019-11-21 16:52:31 +00:00
|
|
|
auto num = any_cast<long>(sv[i + 1]);
|
|
|
|
auto ope = any_cast<char>(sv[i]);
|
2015-02-08 01:52:26 +00:00
|
|
|
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-06-05 12:54:02 +00:00
|
|
|
Definition EXPRESSION, TERM, FACTOR, TERM_OPERATOR, FACTOR_OPERATOR, NUMBER;
|
2015-02-08 01:52:26 +00:00
|
|
|
|
2015-02-09 22:12:59 +00:00
|
|
|
EXPRESSION <= seq(TERM, zom(seq(TERM_OPERATOR, TERM))), reduce;
|
|
|
|
TERM <= seq(FACTOR, zom(seq(FACTOR_OPERATOR, FACTOR))), reduce;
|
|
|
|
FACTOR <= cho(NUMBER, seq(chr('('), EXPRESSION, chr(')')));
|
2016-05-25 07:16:01 +00:00
|
|
|
TERM_OPERATOR <= cls("+-"), [](const SemanticValues& sv) { return static_cast<char>(*sv.c_str()); };
|
|
|
|
FACTOR_OPERATOR <= cls("*/"), [](const SemanticValues& sv) { return static_cast<char>(*sv.c_str()); };
|
2016-01-24 01:26:54 +00:00
|
|
|
NUMBER <= oom(cls("0-9")), [](const SemanticValues& sv) { return atol(sv.c_str()); };
|
2015-02-08 01:52:26 +00:00
|
|
|
|
2015-03-11 17:53:24 +00:00
|
|
|
auto expr = argv[1];
|
2015-02-08 01:52:26 +00:00
|
|
|
long val = 0;
|
2015-03-11 17:53:24 +00:00
|
|
|
if (EXPRESSION.parse_and_get_value(expr, val).ret) {
|
2019-11-21 16:52:31 +00:00
|
|
|
std::cout << expr << " = " << val << std::endl;
|
2015-02-08 01:52:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|