buffalo
Loading...
Searching...
No Matches
buffalo.test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include <buffalo/buffalo.h>
3#include <cmath>
4
5/*
6 * Grammar Definition
7 */
9
10/*
11 * Terminals
12 */
13bf::DefineTerminal<G, R"(\d+(\.\d+)?)", double> NUMBER([](auto const &tok) {
14 return std::stod(std::string(tok.raw));
15});
16
18
23
24bf::DefineTerminal<G, R"(\‍()"> PAR_OPEN;
26
27/*
28 * Non-Terminals
29 */
32 | (PAR_OPEN + expression + PAR_CLOSE)<=>[](auto &$) { $ = $[1]; }
33 | (expression + OP_EXP + expression)<=>[](auto &$) { $ = std::pow($[0], $[2]); }
34 | (expression + OP_MUL + expression)<=>[](auto &$) { $ = $[0] * $[2]; }
35 | (expression + OP_DIV + expression)<=>[](auto &$) { $ = $[0] / $[2]; }
36 | (expression + OP_ADD + expression)<=>[](auto &$) { $ = $[0] + $[2]; }
37 | (expression + OP_SUB + expression)<=>[](auto &$) { $ = $[0] - $[2]; }
38 ;
39
42 ;
43
44TEST(Parser, Construction)
45{
47 ASSERT_TRUE(parser.has_value());
48}
49
50TEST(Parser, Evaluation)
51{
52 auto parser = *bf::SLRParser<G>::Build(statement);
53
54 auto res = parser.Parse("3 * 3 + 4^2 - (9 / 3)");
55 ASSERT_TRUE(res.has_value());
56
57 ASSERT_EQ(res->GetValue(), 22.0);
58}
bf::GrammarDefinition< double > G
bf::DefineTerminal< G, R"(\*)"> OP_MUL(bf::Left)
bf::DefineNonTerminal< G, "expression"> expression
bf::DefineTerminal< G, R"(\‍()"> PAR_OPEN;bf::DefineTerminal< G, R"(\‍))"> PAR_CLOSE
bf::DefineTerminal< G, R"(\+)"> OP_ADD(bf::Left)
bf::DefineTerminal< G, R"(\^)"> OP_EXP(bf::Right)
bf::DefineTerminal< G, R"(\d+(\.\d+)?)", double > NUMBER([](auto const &tok) { return std::stod(std::string(tok.raw));})
bf::DefineTerminal< G, R"(\/)"> OP_DIV(bf::Left)
bf::DefineNonTerminal< G, "statement"> statement
bf::DefineTerminal< G, R"(\-)"> OP_SUB(bf::Left)
TEST(Parser, Construction)
static std::expected< SLRParser, Error > Build(NonTerminal< G > &start)
Definition buffalo.h:1355
@ Left
Definition buffalo.h:398
@ Right
Definition buffalo.h:399
ProductionRule< G > PR
Definition buffalo.h:680