-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.py
178 lines (135 loc) · 4.54 KB
/
Type.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
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
from typing import List
from enum import Enum
# ----------------------------------------------
# -------- Type definitions -----------
# ----------------------------------------------
class Type:
def __init__(self):
super().__init__()
def __str__(self):
return self.type()
def type(self):
return 'Unit'
def unifyBinary(self, ops, other: 'Type') -> ([str], 'Type'):
"""
When performing binary operation either we get list of errors or new type.
"""
if type(self) != type(other):
return ([f"Cannot unify these two types {self} and {other}!"], None)
return self._unifyBinary(ops, other)
def _unifyBinary(self, ops: str, other: 'Type'):
return ([], self)
class PrimitiveType(Type):
"""
Primitive type like 'Int'
"""
def __init__(self, tname: str):
self.tname = tname
def type(self):
return self.tname
def _unifyBinary(self, ops: str, other: 'Primitive'):
t1, t2 = self, other
if ops in binaryOpsTypeTable:
table = binaryOpsTypeTable[ops]
if t1 in table:
table = table[t1]
if t2 in table:
return ([], table[t2])
return ([f'Cannot unify types {t1} and {t2} via {ops} operation!'], None)
class VectorType(Type):
"""
Multidimensional vector like 'Vector<Int>[2, 3, 5]'
Shape contains sizes along each dimensio of our vector.
Note to indicate that we don't know size along particular dimension we put -1 in that row.
For example 'Vector<int>[2, -1, -1]'
"""
def __init__(self, eType: PrimitiveType, size: List[int]):
self.innerType = eType
self.shape = size
def type(self):
return f'Vector<{self.innerType.type()}>{self.shape}'
def dimensions(self):
return len(self.shape)
def dimensionsMatch(self, other: 'VectorType'):
if self.dimensions() != other.dimensions():
return [f'Vector have different number of dimensions: {self.dimensions()} and {other.dimensions()}']
return []
def newShapeMerged(self, other: 'VectorType'):
newShape = []
for s1, s2 in zip(self.shape, other.shape):
if s1 != s2 and (s1 != -1 or s2 != -1):
return [f'Shapes don\'t match: {self.shape} and {other.shape}!'], []
if s1 == -1 or s2 == -1:
newShape.append(-1)
else:
newShape.append(s1)
return [], newShape
def _unifyBinary(self, ops: str, other: 'VectorType'):
errors, newPrimitiveType = self.innerType.unifyBinary(
ops, other.innerType)
dimensionErrors = self.dimensionsMatch(other)
shapeErrors, newShape = self.newShapeMerged(other)
errors += dimensionErrors
errors += shapeErrors
newType = VectorType(newPrimitiveType, newShape) if len(
errors) < 1 else None
return errors, newType
def isNumericType(ttype: Type):
if type(ttype) is PrimitiveType:
return ttype == intType or ttype == floatType
elif type(ttype) is VectorType:
return isNumericType(ttype.innerType)
return False
class AnyType(Type):
def type(self):
return 'Any'
# ----------------------------------------------
# -------- Primitives -----------
# ----------------------------------------------
booleanType = PrimitiveType('Boolean')
intType = PrimitiveType('Int')
stringType = PrimitiveType('String')
floatType = PrimitiveType('Float')
unitType = PrimitiveType('Unit')
anyType = AnyType()
emptyVectorType = VectorType(anyType, [0])
arithmeticTypeTable = {
intType: {
intType: intType,
floatType: floatType
},
floatType: {
intType: floatType,
floatType: floatType
}
}
relationalTypeTable = {
intType: {
intType: booleanType,
floatType: booleanType
},
floatType: {
intType: booleanType,
floatType: booleanType
},
booleanType: {
booleanType: booleanType,
}
}
binaryOpsTypeTable = {
'=': arithmeticTypeTable,
'.+': arithmeticTypeTable,
'.-': arithmeticTypeTable,
'./': arithmeticTypeTable,
'.*': arithmeticTypeTable,
'+': arithmeticTypeTable,
'-': arithmeticTypeTable,
'/': arithmeticTypeTable,
'*': arithmeticTypeTable,
'<': relationalTypeTable,
'<=': relationalTypeTable,
'>': relationalTypeTable,
'>=': relationalTypeTable,
'==': relationalTypeTable,
'!=': relationalTypeTable
}