forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.bal
316 lines (294 loc) · 9.33 KB
/
eval.bal
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import wso2/nballerina.comm.err;
import wso2/nballerina.front.syntax as s;
import wso2/nballerina.types as t;
function intArithmeticEval(err:SemanticContext cx, Position pos, s:BinaryArithmeticOp op, int left, int right) returns int|err:Semantic {
int|error result = trappedIntArithmeticEval(op, left, right);
if result is int {
return result;
}
return cx.semanticErr(`evaluation of int constant ${op} expression failed`, pos, cause=result);
}
// JBUG have to separate this function out, because match stmt not recognized as exhaustive
function trappedIntArithmeticEval(s:BinaryArithmeticOp op, int left, int right) returns int|error {
match op {
"+" => {
return trap left + right;
}
"-" => {
return trap left - right;
}
"*" => {
return trap left * right;
}
"/" => {
return trap left / right;
}
"%" => {
return trap left % right;
}
}
panic err:impossible();
}
function intNegateEval(err:SemanticContext cx, Position pos, int value) returns int|err:Semantic {
if value == int:MIN_VALUE {
return cx.semanticErr(`${"-"} applied to minimum integer value`, pos);
}
return -value;
}
function floatArithmeticEval(s:BinaryArithmeticOp op, float left, float right) returns float {
match op {
"+" => {
return left + right;
}
"-" => {
return left - right;
}
"*" => {
return left * right;
}
"/" => {
return left / right;
}
"%" => {
return left % right;
}
}
panic err:impossible();
}
function decimalArithmeticEval(err:SemanticContext cx, Position pos, s:BinaryArithmeticOp op, decimal left, decimal right) returns decimal|err:Semantic {
decimal|error result = trappedDecimalArithmeticEval(op, left, right);
if result is decimal {
return result;
}
return cx.semanticErr(`evaluation of decimal constant ${op} expression failed`, pos, cause=result);
}
function trappedDecimalArithmeticEval(s:BinaryArithmeticOp op, decimal left, decimal right) returns decimal|error {
match op {
"+" => {
return trap left + right;
}
"-" => {
return trap left - right;
}
"*" => {
return trap left * right;
}
"/" => {
return trap left / right;
}
"%" => {
return trap left % right;
}
}
panic err:impossible();
}
function bitwiseEval(s:BinaryBitwiseOp op, int left, int right) returns int {
match op {
"|" => {
return left | right;
}
"^" => {
return left ^ right;
}
"&" => {
return left & right;
}
">>" => {
return left >> right;
}
">>>" => {
return left >>> right;
}
"<<" => {
return left << right;
}
}
panic err:impossible();
}
function logicalEval(s:BinaryLogicalOp op, boolean left, boolean right) returns boolean {
match op {
"||" => {
return left || right;
}
"&&" => {
return left && right;
}
}
panic err:impossible();
}
function relationalEval(err:SemanticContext cx, Position pos, s:BinaryRelationalOp op, t:SingleValue left, t:SingleValue right) returns boolean|err:Semantic {
if left is int && right is int {
return intRelationalEval(op, left, right);
}
else if left is float && right is float {
return floatRelationalEval(op, left, right);
}
else if left is decimal && right is decimal {
return decimalRelationalEval(op, left, right);
}
else if left is string && right is string {
return stringRelationalEval(op, left, right);
}
else if left is boolean && right is boolean {
return booleanRelationalEval(op, left, right);
}
else if left == () || right == () {
// () behaves like NaN, EQ iff `left = right = ()`
return op is "<="|">=" ? left == right : false;
}
return cx.semanticErr(`invalid operand types for ${op}`, pos);
}
function stringRelationalEval(s:BinaryRelationalOp op, string left, string right) returns boolean {
match op {
"<" => {
return left < right;
}
"<=" => {
return left <= right;
}
">" => {
return left > right;
}
">=" => {
return left >= right;
}
}
panic err:impossible();
}
function floatRelationalEval(s:BinaryRelationalOp op, float left, float right) returns boolean {
match op {
"<" => {
return left < right;
}
"<=" => {
return left <= right;
}
">" => {
return left > right;
}
">=" => {
return left >= right;
}
}
panic err:impossible();
}
function decimalRelationalEval(s:BinaryRelationalOp op, decimal left, decimal right) returns boolean {
match op {
"<" => {
return left < right;
}
"<=" => {
return left <= right;
}
">" => {
return left > right;
}
">=" => {
return left >= right;
}
}
panic err:impossible();
}
function intRelationalEval(s:BinaryRelationalOp op, int left, int right) returns boolean {
match op {
"<" => {
return left < right;
}
"<=" => {
return left <= right;
}
">" => {
return left > right;
}
">=" => {
return left >= right;
}
}
panic err:impossible();
}
function booleanRelationalEval(s:BinaryRelationalOp op, boolean left, boolean right) returns boolean {
match op {
"<" => {
return left < right;
}
"<=" => {
return left <= right;
}
">" => {
return left > right;
}
">=" => {
return left >= right;
}
}
panic err:impossible();
}
function convertToIntEval(err:SemanticContext cx, Position pos, float|decimal value) returns int|err:Semantic {
int|error result = trap <int>value;
if result is int {
return result;
}
return cx.semanticErr(`cannot convert ${value} to int`, pos, cause=result);
}
function convertToDecimalEval(err:SemanticContext cx, Position pos, float|int value) returns decimal|err:Semantic {
decimal|error result = trap <decimal>value;
if result is decimal {
return result;
}
return cx.semanticErr(`cannot convert ${value} to decimal`, pos, cause=result);
}
function fpLiteralValue(err:SemanticContext cx, t:SemType? expectedType, string untypedLiteral, string? typeSuffix, Position pos) returns float|decimal|err:Semantic {
if typeSuffix != () {
return typeSuffix is s:FLOAT_TYPE_SUFFIX ? floatFromFpLiteral(cx, untypedLiteral, pos) : check decimalFromFpLiteral(cx, untypedLiteral, pos);
}
else if expectedType == () || t:includesSome(expectedType, t:FLOAT) || !t:includesSome(expectedType, t:DECIMAL) {
return floatFromFpLiteral(cx, untypedLiteral, pos);
}
else {
return check decimalFromFpLiteral(cx, untypedLiteral, pos);
}
}
function intLiteralValue(err:SemanticContext cx, t:SemType? expectedType, s:IntLiteralBase base, string digits, Position pos) returns int|float|decimal|err:Semantic {
if expectedType == () || t:includesSome(expectedType, t:INT) {
return check intFromLiteral(cx, base, digits, pos);
}
else if t:includesSome(expectedType, t:FLOAT) {
return floatFromFpLiteral(cx, digits, pos, base);
}
else if t:includesSome(expectedType, t:DECIMAL) {
return check decimalFromFpLiteral(cx, digits, pos, base);
}
else {
return check intFromLiteral(cx, base, digits, pos);
}
}
// Since the binary floating point literal is parsed correctly,
// it is impossible to return an error.
function floatFromFpLiteral(err:SemanticContext cx, string digits, Position pos, s:IntLiteralBase base = 10) returns float|err:Semantic {
float|error f = base == 10 ? float:fromString(digits) : float:fromHexString("0x" + digits + "p0");
if f is error {
return cx.semanticErr("invalid float literal", cause=f, pos=pos);
}
return f;
}
// Even if the decimal floating point literal is parsed correctly,
// overflows should return an error.
function decimalFromFpLiteral(err:SemanticContext cx, string decimalStr, Position pos, s:IntLiteralBase base = 10) returns decimal|err:Semantic {
if base == 16 {
return cx.semanticErr("decimal values must not use hexadecimal literals", pos=pos);
}
decimal|error d = decimal:fromString(decimalStr);
if d is error {
return cx.semanticErr("invalid decimal floating point number", cause=d, pos=pos);
}
return d;
}
// Even if the integer literal is parsed correctly,
// overflows should return an error.
function intFromLiteral(err:SemanticContext cx, s:IntLiteralBase base, string digits, Position pos) returns int|err:Semantic {
int|error i = s:intFromIntLiteral(base, digits);
if i is error {
return cx.semanticErr("invalid int literal", cause=i, pos=pos);
}
return i;
}