-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule.hpp
182 lines (166 loc) · 5.14 KB
/
rule.hpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#pragma once
#include "basicType.h"
#include "typeHelper.hpp"
enum ruleOp{EQU,GRAT,SMAL,AND,OR,NOT};
class ruleExp
{
protected:
ruleOp op;
Basic* operand2B=nullptr;
ruleExp* operand2E=nullptr; //可以给两个布尔值比较
int nestingLevel=0;
public:
ruleExp(ruleOp op, Basic* operand) : op(op), operand2B(operand) {}
ruleExp(ruleOp op, ruleExp* operand) : op(op), operand2E(operand), nestingLevel(operand->nestingLevel+1) {}
bool operandIsBasic() { return operand2B!=nullptr; }
void resetOperand(Basic* operand)
{
this->operand2B=operand;
delete this->operand2E;
this->operand2E=nullptr;
}
//一般认为规则构造之后整体结构就不变了,如果把原先的reluExp操作符改成basic相当于舍弃了一些层级,是在改变结构
ruleOp getOp() { return op; }
int getNestingLevel() { return nestingLevel; }
Basic* getOperand2B() { return this->operand2B; }
ruleExp* getOperand2E() { return this->operand2E; }
virtual bool eval(Basic* operand1B)
{
if(op==EQU)
{
if(operandIsBasic()) //basic之间运算
return typeHelper::typehelper->isEqu(operand1B,operand2B);
else
{
if(operand1B->getType()==BOOL)
{
Bool* boolOp1B=((Bool*)(operand1B));
bool result=operand2E->eval(operand1B);
return boolOp1B->val==result;
}
else
throw string("type mismatch");
}
}
else
throw string("error eval case");
}
virtual ~ruleExp()
{
delete operand2B; //持有所有权
delete operand2E;
}
};
class numExp : public ruleExp
{
private:
static bool isNumType(Basic* v)
{
return v->getType()==INT || v->getType()==FLOAT;
}
static float getVal(Basic* v)
{
if(v->getType()==INT)
return ((Int*)(v))->val;
else if(v->getType()==FLOAT)
return ((Float*)(v))->val;
else
throw string("type mismatch");
}
public:
numExp(ruleOp _op, Basic* _operand) : ruleExp(_op,_operand) {}
numExp(ruleOp _op, ruleExp* _operand) : ruleExp(_op,_operand) {}
virtual bool eval(Basic* operand1B)
{
if(op==GRAT || op==SMAL)
{
if(operandIsBasic())
{
if(isNumType(operand2B) && isNumType(operand1B))
{
float val2=getVal(operand2B);
float val1=getVal(operand1B);
if(op==GRAT)
return val1>val2;
else
return val1<val2;
}
else
throw string("type mismatch");
}
//数值比较必须两个都是basic,因为expBase必然是bool
}
throw string("error eval case");
}
};
class logExp : public ruleExp
{
protected:
ruleExp* operand1E=nullptr;
bool isDoubleExp() { return operand2E!=nullptr && operand1E!=nullptr; }
public:
logExp(ruleOp _op, Basic* _operand) : ruleExp(_op,_operand) {}
logExp(ruleOp _op, ruleExp* o1e, ruleExp* o2e) : ruleExp(_op,o2e), operand1E(o1e) {}
virtual bool eval(Basic* operand1B)
{
if(op==NOT)
{
if(operandIsBasic())
{
if(operand2B->getType()==BOOL)
{
return !((Bool*)(operand2B))->val;
}
else
throw string("type mismatch");
}
else
{
bool result=operand2E->eval(operand1B);
return !result;
}
}
else if(op==AND || op==OR)
{
if(operandIsBasic())
{
if(operand2B->getType()==BOOL && operand1B->getType()==BOOL)
{
Bool* boolOp1B=((Bool*)(operand1B));
Bool* boolOp2B=((Bool*)(operand2B));
if(op==AND)
return boolOp1B->val && boolOp2B->val;
else
return boolOp1B->val || boolOp2B->val;
}
else
throw string("type mismatch");
}
else if(isDoubleExp())
{
bool r1=operand1E->eval(operand1B);
bool r2=operand2E->eval(operand1B);
if(op==AND)
return r1 && r2;
else
return r1 || r2;
}
else
{
if(operand1B->getType()==BOOL)
{
Bool* boolOp1B=((Bool*)(operand1B));
bool result=operand2E->eval(operand1B);
if(op==AND)
return boolOp1B->val && result;
else
return boolOp1B->val || result;
}
else
throw string("type mismatch");
}
}
else
throw string("error eval case");
}
};