MCPcopy Create free account
hub / github.com/BowenFu/matchit.cpp / eval

Function eval

sample/Evaluating-Expression-Trees.cpp:47–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45const auto asMulDs = asDsVia<Mul>(&Mul::lhs, &Mul::rhs);
46
47int eval(const Expr &ex)
48{
49 Id<int> i;
50 Id<Expr> e, l, r;
51 return match(ex)(
52 // clang-format off
53 // FIXME: Expr{5} won't match the following line.
54 pattern | as<int>(i) = expr(i),
55 pattern | asNegDs(some(e)) = [&]{ return -eval(*e); },
56 pattern | asAddDs(some(l), some(r)) = [&]{ return eval(*l) + eval(*r); },
57 // Optimize multiplication by 0.
58 pattern | asMulDs(some(as<int>(0)), _) = expr(0),
59 pattern | asMulDs(_, some(as<int>(0))) = expr(0),
60 pattern | asMulDs(some(l), some(r)) = [&]{ return eval(*l) * eval(*r); },
61 pattern | _ = expr(-1)
62 // clang-format on
63 );
64}
65
66int32_t main()
67{

Callers 1

mainFunction · 0.70

Calls 2

matchFunction · 0.50
exprFunction · 0.50

Tested by

no test coverage detected