-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestLanguage.h
146 lines (125 loc) · 4.1 KB
/
TestLanguage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#pragma once
#include "tao/pegtl.hpp"
#include "tao/pegtl/contrib/parse_tree.hpp"
// A simple language for tests which supports expressions like "(a + b) + c"
// and rewrite rules like "$x * ($y * $z) => ($x * $y) * $z" or "$x * 0 => 0"
namespace TestLanguage
{
namespace Ast
{
using namespace tao::pegtl;
using namespace tao::pegtl::parse_tree;
using Symbol = plus<ascii::ranges<'a', 'z', 'A', 'Z', '0', '9'>>;
struct Term : Symbol {};
struct PatternVariable : seq<one<'$'>, Symbol> {};
struct Operation : sor<one<'-'>, one<'+'>, one<'*'>, one<'/'>> {};
struct Arrow : string<'=', '>'> {};
struct Spacing : star<space> {};
struct Expression;
struct BracketExpression : if_must<one<'('>, Spacing, Expression, Spacing, one<')'>> {};
struct Value : sor<PatternVariable, Term, BracketExpression> {};
template <typename S, typename O>
struct LeftAssociative : seq<S, Spacing, star_must<O, Spacing, S, Spacing>> {};
struct Expression : LeftAssociative<Value, Operation> {};
struct RewriteRuleOrExpression : LeftAssociative<Expression, Arrow> {};
struct Grammar : must<Spacing, RewriteRuleOrExpression, eof> {};
template <typename Rule>
struct Selector : selector<Rule,
store_content::on<Term, PatternVariable, Operation>,
fold_one::on<Value, Expression>,
discard_empty::on<Arrow>> {};
struct Node : basic_node<Node> {};
} // namespace Ast
using namespace e;
Pattern makePattern(const Ast::Node &astNode)
{
if (astNode.is_root())
{
assert(!astNode.children.empty());
return makePattern(*astNode.children.front());
}
else if (astNode.is_type<Ast::Expression>())
{
PatternTerm term;
assert(astNode.children.size() == 3);
for (auto &childNode : astNode.children)
{
if (childNode->is_type<Ast::Operation>())
{
term.name = childNode->string();
}
else
{
term.arguments.push_back(makePattern(*childNode));
}
}
return term;
}
else if (astNode.is_type<Ast::Term>())
{
PatternTerm term;
term.name = astNode.string();
return term;
}
else if (astNode.is_type<Ast::PatternVariable>())
{
PatternVariable variable = astNode.string();
return variable;
}
assert(false);
}
RewriteRule makeRewriteRule(const Ast::Node &astNode)
{
assert(astNode.is_root());
assert(astNode.children.size() == 2);
RewriteRule rule;
rule.leftHand = makePattern(*astNode.children.front());
rule.rightHand = makePattern(*astNode.children.back());
return rule;
}
ClassId makeExpression(const Ast::Node &astNode, Graph &eGraph)
{
if (astNode.is_root())
{
assert(!astNode.children.empty());
return makeExpression(*astNode.children.front(), eGraph);
}
else if (astNode.is_type<Ast::Expression>())
{
Symbol name;
Vector<ClassId> childrenIds;
assert(astNode.children.size() == 3); // only binary operations are supported
for (auto &childNode : astNode.children)
{
if (childNode->is_type<Ast::Operation>())
{
name = childNode->string();
}
else
{
childrenIds.push_back(makeExpression(*childNode, eGraph));
}
}
return eGraph.addOperation(name, childrenIds);
}
else if (astNode.is_type<Ast::Term>())
{
return eGraph.addTerm(astNode.string());
}
assert(false);
}
ClassId makeExpression(const std::string &expression, Graph &eGraph)
{
using namespace tao::pegtl;
string_input input(expression, "");
const auto node = parse_tree::parse<Ast::Grammar, Ast::Node, Ast::Selector>(input);
return makeExpression(*node, eGraph);
}
RewriteRule makeRewriteRule(const std::string &expression)
{
using namespace tao::pegtl;
string_input input(expression, "");
const auto node = parse_tree::parse<Ast::Grammar, Ast::Node, Ast::Selector>(input);
return makeRewriteRule(*node);
}
} // namespace TestLanguage