-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
111 lines (108 loc) · 3.49 KB
/
utils.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
# Cubo aritmético de operaciones
from collections import namedtuple
INT = "Int"
ANY = "Any"
ARR_ANY = "[Any]"
ARR_DELIMITERS = "[]"
STRING = "String"
FLOAT = "Float"
BOOL = "Bool"
NON = "non"
TYPE_ERROR = "Operator {} not defined for types {} and {}"
def check_operation_type(op, t1, t2):
t1 = t1.strip(ARR_DELIMITERS)
t2 = t2.strip(ARR_DELIMITERS)
if op == "+":
if t1 == STRING and t2 == STRING:
return STRING
if t1 == FLOAT:
if t2 in (INT, FLOAT):
return FLOAT
if t1 == INT:
if t2 == INT:
return INT
if t2 == FLOAT:
return FLOAT
if t1 == BOOL and t2 == BOOL:
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "-":
if t1 == FLOAT:
if t2 in (INT, FLOAT):
return FLOAT
if t1 == INT:
if t2 == INT:
return INT
if t2 == FLOAT:
return FLOAT
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "*":
if t1 == INT and t2 == INT:
return INT
if t1 == FLOAT:
if t2 in (INT, FLOAT):
return FLOAT
if t1 == INT:
if t2 in (INT, FLOAT):
return FLOAT
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "/":
if t1 == FLOAT:
if t2 in (INT, FLOAT):
return FLOAT
if t1 == INT:
if t2 == INT:
return INT
if t2 == FLOAT:
return FLOAT
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "%":
if t1 == INT and t2 == INT:
return INT
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == ">":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == STRING and t2 == STRING:
return BOOL
if t1 in (INT, FLOAT) and t2 in (INT, FLOAT):
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == ">=":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == STRING and t2 == STRING:
return BOOL
if t1 in (INT, FLOAT) and t2 in (INT, FLOAT):
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "=":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == t2:
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "<":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == STRING and t2 == STRING:
return BOOL
if t1 in (INT, FLOAT) and t2 in (INT, FLOAT):
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "<=":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == STRING and t2 == STRING:
return BOOL
if t1 in (INT, FLOAT) and t2 in (INT, FLOAT):
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
elif op == "!=":
if t1 == ANY or t2 == ANY or t1 == ARR_ANY or t2 == ARR_ANY:
return BOOL
if t1 == t2:
return BOOL
raise TypeError(TYPE_ERROR.format(op, t1, t2))
else:
raise ValueError("Operator {} does not exist".format(op))