-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.cpp
86 lines (80 loc) · 1.82 KB
/
context.cpp
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
#include "context.h"
#include <sstream>
using namespace std;
namespace YGO
{
Condition* Utils::parseSingle(Context& context, const t_string& cond)
{
Condition* res = nullptr;
if ((res = context.getCondition(cond)) != nullptr)
{
return res;
}
if (cond.size() > 1 && cond[0] == '!')
{
auto child = parseSingle(context, cond.substr(1));
return new NotCondition(child);
}
if (cond.size() > 2 && cond[1] == ':')
{
switch (cond[0])
{
case 'a':
return new CardAttributeCondition(cond.substr(2));
case 'A':
return new CardAttributeWildcardCondition(cond.substr(2));
}
}
panic("Cannot parse condition [2]: " + cond);
}
Condition* Utils::parseSingle(const t_string& cond)
{
Condition* res = nullptr;
if (cond.size() > 1 && cond[0] == '!')
{
auto child = parseSingle(cond.substr(1));
return new NotCondition(child);
}
if (cond.size() > 2 && cond[1] == ':')
{
switch (cond[0])
{
case 'a':
return new CardAttributeCondition(cond.substr(2));
case 'A':
return new CardAttributeWildcardCondition(cond.substr(2));
}
}
else {
return new CardNameCondition(cond);
}
panic("Cannot parse condition [1]: " + cond);
}
std::vector<t_string> Utils::strSplit(const t_string& s)
{
std::istringstream iss(s);
return vector<t_string>(istream_iterator<t_string>(iss), istream_iterator<t_string>());
}
Condition* Utils::parse(Context& context, const t_string& cond)
{
vector<t_string> conds = strSplit(cond);
if (conds.size() == 0)
{
panic("condition cannot be empty");
}
auto andCond = new AndCondition();
for (auto& c : conds)
{
andCond->addChild(parseSingle(context, c));
}
return andCond;
}
Condition* Context::getCondition(const t_string& key)
{
if (m_context.count(key))
{
return m_context[key];
}
return nullptr;
}
}