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
|
module static-semantics/actions/binops
imports
static-semantics/types/built-ins
static-semantics/webdsl-types
static-semantics/webdsl
rules // binary operators
// (e1 + e2)
typeOfExp(s, exp@Add(e1, e2)) = :-
typeOfAdd(s, e1, e2) == T,
@exp.type := T.
typeOfPlaceholderExp(s, exp@PHAdd(e1, e2)) = :-
typeOfAdd(s, e1, e2) == T,
@exp.type := T.
: scope * Exp * Exp -> TYPE
typeOfAdd(, , ) = :- { }
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAdd(T1, T2) == T,
inequalType(T, UNTYPED()) | error $[Wrong operand types for operator Add: [e1] has type [T1], [e2] has type [T2]]. // correct error message for tests
// (e1 - e2)
typeOfExp(, exp@Sub(, )) = :- { }
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) == T,
inequalType(T, UNTYPED()) | error $[Wrong operand types for operator Sub: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := T.
// (e1 * e2)
typeOfExp(, exp@Mul(, )) = :- { }
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) == T,
inequalType(T, UNTYPED()) | error $[Wrong operand types for operator Mul: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := T.
// (e1 / e2)
typeOfExp(, exp@Div(, )) = :- { }
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) == T,
inequalType(T, UNTYPED()) | error $[Wrong operand types for operator Div: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := T.
// (e1 % e2)
typeOfExp(, exp@Mod(, )) = :- { }
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) == T,
inequalType(T, UNTYPED()) | error $[Wrong operand types for operator Mod: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := T.
// (e1 > e2)
typeOfExp(, exp@LargerThan(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) != UNTYPED() | error $[Wrong operand types for operator LargerThan: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := t.
// (e1 >= e2)
typeOfExp(, exp@LargerThanOrEqual(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) != UNTYPED() | error $[Wrong operand types for operator LargerThanOrEqual: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := t.
// (e1 < e2)
typeOfExp(, exp@SmallerThan(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) != UNTYPED() | error $[Wrong operand types for operator SmallerThan: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := t.
// (e1 <= e2)
typeOfExp(, exp@SmallerThanOrEqual(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
lubForAddNumeric(T1, T2) != UNTYPED() | error $[Wrong operand types for operator SmallerThanOrEqual: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := t.
// (e1 && e2)
typeOfExp(, exp@And(, )) = :- { }
b == bool(s),
typeOfExp(s, e1) == T1, typeCompatible(T1, b) | error $[Wrong operand types for operator And: [e1] has type [T1], expected Bool], // correct error message for tests
typeOfExp(s, e2) == T2, typeCompatible(T2, b) | error $[Wrong operand types for operator And: [e2] has type [T2], expected Bool], // correct error message for tests
@exp.type := b.
// (e1 || e2)
typeOfExp(, exp@Or(, )) = :- { }
b == bool(s),
typeOfExp(s, e1) == T1, typeCompatible(T1, b) | error $[Wrong operand types for operator Or: [e1] has type [T1], expected Bool], // correct error message for tests
typeOfExp(s, e2) == T2, typeCompatible(T2, b) | error $[Wrong operand types for operator Or: [e2] has type [T2], expected Bool], // correct error message for tests
@exp.type := b.
// (e1 == e2)
typeOfExp(, exp@Eq(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
or(
typeCompatibleB(T1, T2),
typeCompatibleB(T2, T1)
) | error $[Wrong operand types for operator Eq: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type := t.
// (e1 != e2)
typeOfExp(, exp@NotEq(, )) = :- { }
t == bool(s),
typeOfExp(s, e1) == T1,
typeOfExp(s, e2) == T2,
or(
typeCompatibleB(T1, T2),
typeCompatibleB(T2, T1)
) | error $[Wrong operand types for operator !=: [e1] has type [T1], [e2] has type [T2]], // correct error message for tests
@exp.type :=t.
// e1 in e2
typeOfExp(, exp@InColl(, )) = bool(s) :- { }
typeOfExp(s, e1) == T1,
stripGenericType(typeOfExp(s, e2)) == T2,
typeCompatible(T1, T2) | error $[Wrong operand types for operator in: [e1] has type [T1], [e2] has type [T2]]. // correct error message for tests
rules // unary operators
// (!e)
typeOfExp(, exp@Not()) = :- { }
t == bool(s),
typeOfExp(s, e) == T,
T == t | error $[Wrong operand types for operator !: [e] has type [T], expected Bool], // correct error message for tests
@exp.type := t.
rules // utils
: TYPE * TYPE -> TYPE
lubForAdd(T1, T2) = lubForAddNumeric(T1, T2).
lubForAdd(t@BUILTINTYPE("String", _), _) = t.
lubForAdd(_, t@BUILTINTYPE("String", _)) = t.
: TYPE * TYPE -> TYPE
lubForAddNumeric(_, _) = UNTYPED().
lubForAddNumeric(t@BUILTINTYPE("Int", _) , t) = t.
lubForAddNumeric(t@BUILTINTYPE("Long", _) , t) = t.
lubForAddNumeric(t@BUILTINTYPE("Float", _) , t) = t.
lubForAddNumeric(t@NATIVECLASS("Double", _) , t) = t.
// implicit widening from int to long
lubForAddNumeric(BUILTINTYPE("Int", _) , t@BUILTINTYPE("Long", _)) = t.
lubForAddNumeric(t@BUILTINTYPE("Long", _) , BUILTINTYPE("Int", _)) = t.
// implicit widening from float to double
lubForAddNumeric(t@NATIVECLASS("Double", _) , BUILTINTYPE("Float", _)) = t.
lubForAddNumeric(BUILTINTYPE("Float", _) , t@NATIVECLASS("Double", _)) = t.
|