-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathListStuff.zu
473 lines (454 loc) · 17.3 KB
/
ListStuff.zu
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#
# The Zimbu compiler written in Zimbu
#
# Stuff for List that is not target language specific.
#
# Copyright 2009 Bram Moolenaar All Rights Reserved.
# Licensed under the Apache License, Version 2.0. See the LICENSE file or
# obtain a copy at: http://www.apache.org/licenses/LICENSE-2.0
#
IMPORT.PROTO "zui.proto"
IMPORT "ContainerStuff.zu"
IMPORT "ContainerType.zu"
IMPORT "Declaration.zu"
IMPORT "Generate.zu"
IMPORT "MethodType.zu"
IMPORT "MethodRefType.zu"
IMPORT "SContext.zu"
IMPORT "Type.zu"
IMPORT "WriteCommon.zu"
IMPORT "ZuiExpressionExt.zu"
IMPORT "ZuiMethodCallExt.zu"
IMPORT "lib/ILoader.zu"
MODULE ListStuff @items=public # TODO: restrict visibility
FUNC generateMethodCall(ContainerType type,
Zui.MethodCall call,
SContext ctx,
Type destType
) Type
Type retType
Zui.Expression name = call.getName()
CHECK.true(name.getType() == Zui.ExprType.eMEMBER)
string methodName = name.getRight().getId().getName()
Zui.Expression objExpr = name.getLeft()
VAR objExprExt = ZuiExpressionExt.get(objExpr)
list<Zui.Expression> argList = call.getArgumentList()
VAR callExt = ZuiMethodCallExt.get(call)
IF methodName == "ToString"
IF Generate.checkArgCount(call, 0, 1, ctx) == FAIL
callExt.undefined = 3
ELSEIF type.itemType == NIL
callExt.undefined = 2
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
IF argList.Size() == 1
Generate.genExpr(argList[0], ctx, Type.aString)
callExt.undefined += ZuiExpressionExt.get(argList[0]).undefined
}
# argument for item type
WriteCommon.oneToStringArgument(type.itemType, call, TRUE, ctx)
}
retType = Type.aString
ELSEIF methodName == "Type"
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSEIF type.itemType != NIL
type.typeUsed(ctx)
}
retType = Type.aType
ELSEIF methodName == "get"
# list.get(idx) is the same as list[idx].
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
Zui.Expression expr = NEW()
expr.setLeft(objExpr)
expr.setRight(argList[0])
retType = ctx.gen.subscript(expr, ctx, NIL)
}
ELSEIF methodName == "add" || methodName == "insert"
# For C:
# numberList.add(item) -> ZLa(numberList, -1, item, NULL)
# numberList.add(item, i) -> ZLa(numberList, i, item, NULL)
# pointerList.add(item) -> ZLa(pointerList, -1, 0, item)
# pointerList.add(item, i) -> ZLa(pointerList, i, 0, item)
# numberList.insert(item) -> ZListInsert(numberList, 0, item, NULL)
# numberList.insert(item, i) -> ZListInsert(numberList, i, item, NULL)
# pointerList.insert(item) -> ZListInsert(pointerList, 0, 0, item)
# pointerList.insert(item, i) -> ZListInsert(pointerList, i, 0, item)
#
# For JS there is no difference between number and pointer:
# list.add(item) -> ZLa(list, -1, item)
# list.add(item, i) -> ZLa(list, i, item)
# list.insert(item) -> ZListInsert(list, 0, item)
# list.insert(item, i) -> ZListInsert(list, i, item)
IF Generate.checkArgCount(call, 1, 2, ctx) == FAIL
callExt.undefined = 7
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
# generate the index argument
IF call.sizeArgument() > 1
Zui.Expression arg1 = call.getArgument(1)
Generate.genExpr(arg1, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg1).undefined
}
# generate the pointer or integer value
IF type.itemType != NIL
Zui.Expression arg0 = call.getArgument(0)
Generate.genExpr(arg0, ctx, type.itemType)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
# TODO: when this is an i_object type, do we need to check it's
# the same as requested?
ELSE
callExt.undefined += 5
}
}
retType = newList(type)
ELSEIF methodName == "extend" || methodName == "concat"
# List.extend(list)
# List.concat(list)
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.generateVarname(objExpr, ctx, type)
# type of both lists should be equal
Zui.Expression arg0 = call.getArgument(0)
Generate.genExpr(arg0, ctx, type)
callExt.undefined = objExprExt.undefined
+ ZuiExpressionExt.get(arg0).undefined
}
retType = newList(type)
ELSEIF methodName == "clear" && call.sizeArgument() == 0
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
retType = newList(type)
}
ELSEIF methodName == "find" || methodName == "has"
# TODO: mylist.find(expr, startIdx)
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
# mylist.find(expr)
# mylist.has(expr)
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
Zui.Expression arg = call.getArgument(0)
Generate.genExpr(arg, ctx, type.itemType)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
retType = methodName == "has" ? Type.aBool : Type.anInt
}
ELSEIF methodName == "remove"
|| (methodName == "clear" && call.sizeArgument() == 1)
IF Generate.checkArgCount(call, 0, 2, ctx) == FAIL
callExt.undefined = 3
ELSE
bool removeOne
bool hasArg
IF !call.hasArgument()
# mylist.remove()
removeOne = TRUE
ELSEIF call.sizeArgument() == 1
# mylist.remove(index)
removeOne = TRUE
hasArg = TRUE
}
IF removeOne
# mylist.remove() or mylist.remove(index) or mylist.clear(index)
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
IF hasArg
Zui.Expression arg0 = call.getArgument(0)
Generate.genExpr(arg0, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
}
IF methodName == "clear"
retType = type
ELSEIF type.itemType != NIL
retType = type.itemType
}
ELSE
# mylist.remove(start, end)
Generate.generateVarname(objExpr, ctx, type)
Zui.Expression arg0 = call.getArgument(0)
Generate.genExpr(arg0, ctx, Type.anInt)
Zui.Expression arg1 = call.getArgument(1)
Generate.genExpr(arg1, ctx, Type.anInt)
retType = newList(type)
callExt.undefined = objExprExt.undefined
+ ZuiExpressionExt.get(arg0).undefined
+ ZuiExpressionExt.get(arg1).undefined
}
}
ELSEIF methodName == "slice"
IF Generate.checkArgCount(call, 1, 2, ctx) == FAIL
callExt.undefined = 3
ELSE
# mylist.slice(start, end)
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
Zui.Expression arg0 = call.getArgument(0)
IF call.sizeArgument() > 1
Generate.genExpr(arg0, ctx, Type.anInt)
Zui.Expression arg1 = call.getArgument(1)
Generate.genExpr(arg1, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
+ ZuiExpressionExt.get(arg1).undefined
ELSE
# slice(start) == slice(start, -1)
Generate.genExpr(arg0, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
}
retType = newList(type)
}
ELSEIF methodName == "sort"
bool supportedType
IF type.itemType != NIL
SWITCH type.itemType.ttype
CASE Type.Enum.string
CASE Type.Enum.int
CASE Type.Enum.int8
CASE Type.Enum.int16
CASE Type.Enum.int32
CASE Type.Enum.nat
CASE Type.Enum.byte
CASE Type.Enum.nat16
CASE Type.Enum.nat32
CASE Type.Enum.float
CASE Type.Enum.float32
CASE Type.Enum.float80
CASE Type.Enum.float128
supportedType = TRUE
CASE Type.Enum.object
CASE Type.Enum.iobject
Declaration sortFunc = Generate.findCompare(type.itemType, ctx)
IF sortFunc != NIL
supportedType = TRUE
ctx.addUsedItem(sortFunc)
IF type.itemType.ttype == Type.Enum.iobject
&& type.itemType.compareFunc != NIL
# sortFunc is a wrapper around compareFunc. Need to mark all
# implementations of it as used.
int udef
Declaration.C arg = NEW("arg")
arg.type = type.itemType
ctx.gen.usingIobjectMethod(type.itemType.compareFunc,
[arg], type.itemType, &udef,
"Compare", name.getPos(),
sortFunc.pName, ctx)
callExt.undefined += udef
}
}
}
}
IF Generate.checkArgCount(call, 0, 1, ctx) == FAIL
callExt.undefined = 3
ELSEIF !supportedType
callExt.undefined = 2
ELSE
# mylist.sort()
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
IF call.hasArgument()
# sort(ascending)
Zui.Expression arg0 = call.getArgument(0)
Generate.genExpr(arg0, ctx, Type.aBool)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
}
retType = newList(type)
}
ELSEIF methodName == "join"
IF Generate.checkArgCount(call, 0, 1, ctx) == FAIL
callExt.undefined = 3
ELSEIF type.itemType == NIL
callExt.undefined = 2
ELSE
# mylist.join()
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
IF call.hasArgument()
# join(separator)
Zui.Expression arg0 = call.getArgument(0)
Generate.genExprDoConv(arg0, ctx, Type.aString)
callExt.undefined += ZuiExpressionExt.get(arg0).undefined
}
WriteCommon.oneToStringArgument(type.itemType, call, FALSE, ctx)
retType = Type.aString
}
ELSEIF methodName == "map" || methodName == "keyMap"
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
Zui.Expression arg = argList[0]
Generate.genFuncArg(arg, methodName == "map" ? NIL : Type.anInt,
type.itemType, ctx)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
retType = newList(type)
}
ELSEIF methodName == "copy"
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
retType = newList(type)
}
ELSEIF methodName == "size" || methodName == "Size"
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.generateVarname(objExpr, ctx, type)
callExt.undefined = objExprExt.undefined
}
retType = Type.anInt
ELSE
callExt.undefined = 5
}
IF retType == NIL
RETURN Type.anUnknown
}
RETURN retType
}
# List functions common to all languages. These are defined in
# lib/LISTModule.zu
FUNC commonMethodCall(ContainerType type,
Zui.MethodCall call,
SContext ctx,
Type destType,
Type &retTypeRef
) status
Type retType
Zui.Expression name = call.getName()
CHECK.true(name.getType() == Zui.ExprType.eMEMBER)
string methodName = name.getRight().getId().getName()
Zui.Expression objExpr = name.getLeft()
list<Zui.Expression> argList = call.getArgumentList()
VAR callExt = ZuiMethodCallExt.get(call)
IF methodName == "Iterator"
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Declaration iteratorDecl = ILoader.getIteratorDecl()
IF iteratorDecl != NIL
retType = Generate.interfaceTemplate(iteratorDecl, type.itemType, ctx)
IF retType != NIL
retType = retType.getValueType(ctx)
}
}
Generate.genLibCall(call, "MLISTModule", "FIterator",
objExpr, type, [], retType, ctx)
}
ELSEIF methodName == "KeyIterator"
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Declaration iteratorDecl = ILoader.getKeyIteratorDecl()
IF iteratorDecl != NIL
retType = Generate.interfaceTemplate(iteratorDecl, type.itemType, ctx)
IF retType != NIL
retType = retType.getValueType(ctx)
}
}
Generate.genLibCall(call, "MLISTModule", "FKeyIterator",
objExpr, type, [], retType, ctx)
}
ELSEIF methodName == "clearItem"
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
list<Type> argTypes = [type.itemType]
retType = type
Generate.genLibCall(call, "MLISTModule", "FclearItem",
objExpr, type, argTypes, retType, ctx)
}
ELSEIF methodName == "removeItem"
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
list<Type> argTypes = [type.itemType]
retType = type.itemType
Generate.genLibCall(call, "MLISTModule", "FremoveItem",
objExpr, type, argTypes, retType, ctx)
}
ELSEIF methodName == "set"
# list.set(idx, val)
IF Generate.checkArgCount(call, 2, 2, ctx) == FAIL
callExt.undefined = 5
ELSE
list<Type> argTypes = [Type.anInt, type.itemType]
retType = type
Generate.genLibCall(call, "MLISTModule", "Fset",
objExpr, type, argTypes, retType, ctx)
}
ELSEIF methodName == "reduce"
MethodType m = NEW(Type.Enum.func, "lambda")
m.returnType = type.itemType
m.arguments.add(NEW("a", type.itemType))
m.arguments.add(NEW("b", type.itemType))
Type ft = MethodRefType.NEW(Type.Enum.funcRef, m, "lambda")
retType = type.itemType
IF Generate.checkArgCount(call, 1, 2, ctx) == FAIL
callExt.undefined = 3
ELSEIF call.sizeArgument() == 1
list<Type> argTypes = [ft]
Generate.genLibCall(call, "MLISTModule", "Freduce",
objExpr, type, argTypes, retType, ctx)
ELSE
list<Type> argTypes = [type.itemType, ft]
Generate.genLibCall(call, "MLISTModule", "Freduce",
objExpr, type, argTypes, retType, ctx)
}
ELSEIF methodName == "reduceTo"
MethodType m = NEW(Type.Enum.func, "lambda")
retType = destType
m.returnType = destType
m.arguments.add(NEW("a", destType))
m.arguments.add(NEW("b", type.itemType))
Type ft = MethodRefType.NEW(Type.Enum.funcRef, m, "lambda")
IF Generate.checkArgCount(call, 2, 2, ctx) == FAIL
callExt.undefined = 3
ELSE
list<Type> argTypes = [destType, ft]
Generate.genLibCall(call, "MLISTModule", "FreduceTo",
objExpr, type, argTypes, retType, ctx)
}
ELSEIF methodName == "filter"
retType = type
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
MethodType m = NEW(Type.Enum.func, "lambda")
m.returnType = Type.aBool
m.arguments.add(NEW("a", type.itemType))
Type ft = MethodRefType.NEW(Type.Enum.funcRef, m, "lambda")
Generate.genLibCall(call, "MLISTModule", "Ffilter",
objExpr, type, [ft], retType, ctx)
}
ELSEIF methodName == "mapTo" || methodName == "keyMapTo"
retType = ContainerStuff.generateMapCall(
type, call, methodName, ctx, destType, "MLISTModule")
ELSEIF methodName == "forEach" || methodName == "forKeyEach"
retType = ContainerStuff.generateForEachCall(
type, call, methodName, ctx, destType, "MLISTModule")
ELSE
RETURN FAIL
}
IF retType == NIL
retType = Type.anUnknown
}
retTypeRef = retType
RETURN OK
}
# Return a new list Symbol with item type set from |type|.
FUNC newList(ContainerType type) Type
ContainerType ret = NEW(Type.Enum.list, "list")
ret.itemType = type.itemType
RETURN ret
}
}