cpp-peglib/example/calc5.cc

69 lines
1.6 KiB
C++
Raw Normal View History

2020-02-07 16:55:21 +00:00
//
// calc5.cc
//
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
// MIT License
//
#include <cstdlib>
2020-10-02 01:26:04 +00:00
#include <iostream>
#include <peglib.h>
2020-02-07 16:55:21 +00:00
using namespace peg;
2020-10-02 01:26:04 +00:00
int main(int argc, const char **argv) {
if (argc < 2 || std::string("--help") == argv[1]) {
std::cout << "usage: calc5 [formula]" << std::endl;
return 1;
}
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
std::function<long(const Ast &)> eval = [&](const Ast &ast) {
if (ast.name == "NUMBER") {
return ast.token_to_number<long>();
} else {
const auto &nodes = ast.nodes;
auto result = eval(*nodes[0]);
if (nodes.size() > 1) {
auto ope = nodes[1]->token[0];
auto num = eval(*nodes[2]);
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
2020-02-07 16:55:21 +00:00
}
2020-10-02 01:26:04 +00:00
}
return result;
}
};
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
parser parser(R"(
2020-02-07 16:55:21 +00:00
EXPRESSION <- ATOM (OPERATOR ATOM)* {
precedence
L - +
L / *
}
ATOM <- NUMBER / '(' EXPRESSION ')'
OPERATOR <- < [-+/*] >
NUMBER <- < '-'? [0-9]+ >
%whitespace <- [ \t\r\n]*
)");
2020-10-02 01:26:04 +00:00
parser.enable_ast();
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
auto expr = argv[1];
std::shared_ptr<Ast> ast;
if (parser.parse(expr, ast)) {
ast = AstOptimizer(true).optimize(ast);
std::cout << ast_to_s(ast);
std::cout << expr << " = " << eval(*ast) << std::endl;
return 0;
}
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
std::cout << "syntax error..." << std::endl;
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
return -1;
2020-02-07 16:55:21 +00:00
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix