/
RelationParser.py
117 lines (105 loc) · 3.62 KB
/
RelationParser.py
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
from VarDecl import VarDecl
from Constraint import Constraint
def lookForVarDeclWithName(ent, filtered):
if type(ent)==VarDecl:
return ent
else:
for i in filtered:
if type(i)==VarDecl and i.name==ent[0]:
return i
def findNextVarDecl(filtered):
for ent in filtered:
if type(ent) is VarDecl:
return ent
def findPrevVarDecl(filtered):
filtered = reversed(filtered)
for ent in filtered:
if type(ent) is VarDecl:
return ent
def find2PrevVarDecl(filtered):
l = []
for ent in filtered:
if len(l)==2:
return l
if type(ent) is int:
continue
elif type(ent) is VarDecl or ent[1] == "declaredBefore":
l.append(lookForVarDeclWithName(ent, filtered))
return l
def findNext2VarDecl(filtered):
l = []
for ent in filtered:
if len(l)==2:
return l
if type(ent) is int:
continue
elif type(ent) is VarDecl or ent[1] == "declaredBefore":
l.append(lookForVarDeclWithName(ent, filtered))
return l
def BinRelFunc(filtered, func_ent):
binRelFunc = {"Injection", "Surjection", "Bijection"}
# function contraint eg. Injection(f)
fconstraint = Constraint()
# Bin rel constraint eg. From(f, A, B)
tconstraint = Constraint()
for i, ent in enumerate(filtered):
if type(ent) is VarDecl or type(ent) is int:
continue
if ent[0] in binRelFunc:
fconstraint.func = ent[0]
fconstraint.args.append(func_ent)
elif ent[1] == "From":
fromSet = findNextVarDecl(filtered[i:])
tconstraint.func = "From"
tconstraint.args.append(func_ent)
tconstraint.args.append(fromSet)
elif ent[1] == "To":
to = findNextVarDecl(filtered[i:])
tconstraint.args.append(to)
return [fconstraint, tconstraint]
def BinRelVec(filtered, func_ent):
binRelVec = {"In", "Orthogonal"}
operations = {"+", "=", "-"}
constraints = []
for i, ent in enumerate(filtered):
if type(ent) is VarDecl or type(ent) is int:
continue
if ent[1] in binRelVec:
c = Constraint()
c.func = ent[1]
vec, space = find2PrevVarDecl(filtered[:i])
c.args.append(vec)
c.args.append(space)
constraints.append(c)
if ent[0] in binRelVec:
c = Constraint()
c.func = ent[0]
vec1, vec2 = find2PrevVarDecl(filtered[:i])
c.args.append(vec1)
c.args.append(vec2)
constraints.append(c)
if ent[0] in operations:
if ent[0]=="=":
#exp = s1 + s2
exp = findPrevVarDecl(filtered[:i])
s1, s2 = findNext2VarDecl(filtered[i+1:])
c = Constraint()
c.func = "Sum"
c.args = [exp, s1, s2]
constraints.append(c)
return constraints
# def BinRelSet(func, filtered, setVarDecl):
# Constraints = namedtuple('Constraints', ['name', 'args'])
# fconstraint = Constraints(func, [funcVarDecl])
# tconstraint = Constraints("From", [])
# tconstraint.args.append(funcVarDecl)
# for i, ent in enumerate(filtered):
# if not type(ent) is tuple:
# continue
# if ent[1] == "From":
# fromSet = findNextVarDecl(filtered[i:])
# tconstraint.args.append(fromSet)
# elif ent[i] == "To":
# to = findNextVarDecl(filtered[i:])
# tconstraint.args.append(to)
# return [fconstraint, tconstraint]