-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArrayStuff.zu
172 lines (158 loc) · 5.66 KB
/
ArrayStuff.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
#
# The Zimbu compiler written in Zimbu
#
# Stuff for Array 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"
MODULE ArrayStuff @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 object = name.getLeft()
VAR objectExt = ZuiExpressionExt.get(object)
list<Zui.Expression> argList = call.getArgumentList()
VAR callExt = ZuiMethodCallExt.get(call)
IF methodName == "map" || methodName == "keyMap"
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
# ELSEIF type.arraySize.Size() == 0
# callExt.undefined = 2
ELSE
Generate.genExpr(object, ctx, type)
callExt.undefined = objectExt.undefined
Zui.Expression arg = argList[0]
Generate.genFuncArg(arg, methodName == "map" ? NIL : Type.anInt,
type.itemType, ctx)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
}
retType = type
ELSEIF methodName == "set"
# set(offset, items)
# TODO: multi-dimensional arrays.
IF Generate.checkArgCount(call, 2, 2, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.genExpr(object, ctx, type)
callExt.undefined = objectExt.undefined
Zui.Expression arg = argList[0]
Generate.genExpr(arg, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
arg = argList[1]
Generate.genExpr(arg, ctx)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
}
retType = type
ELSEIF methodName == "resize"
# TODO: multi-dimensional arrays.
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.genExpr(object, ctx, type)
callExt.undefined = objectExt.undefined
Zui.Expression arg = argList[0]
Generate.genExpr(arg, ctx, Type.anInt)
callExt.undefined += ZuiExpressionExt.get(arg).undefined
}
retType = type
ELSEIF methodName == "size" || methodName == "Size"
# TODO: multi-dimensional arrays.
IF Generate.checkArgCount(call, 0, 0, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.genExpr(object, ctx, type)
callExt.undefined = objectExt.undefined
}
retType = Type.anInt
ELSEIF methodName == "ToString"
IF Generate.checkArgCount(call, 0, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
Generate.genExpr(object, ctx, type)
callExt.undefined = objectExt.undefined
IF argList.Size() == 1
Generate.genExpr(argList[0], ctx, Type.aString)
callExt.undefined += ZuiExpressionExt.get(argList[0]).undefined
}
# Mark ToString method as used.
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 == "find" || methodName == "has"
# TODO: myarray.find(expr, startIdx)
IF Generate.checkArgCount(call, 1, 1, ctx) == FAIL
callExt.undefined = 3
ELSE
# myarray.find(expr)
# myarray.has(expr)
Generate.generateVarname(object, ctx, type)
callExt.undefined = objectExt.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
}
ELSE
callExt.undefined = 5
}
IF retType == NIL
RETURN Type.anUnknown
}
RETURN retType
}
# Array functions common to all languages. These are defined in
# lib/ARRAYModule.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()
IF methodName == "mapTo" || methodName == "keyMapTo"
retType = ContainerStuff.generateMapCall(
type, call, methodName, ctx, destType, "MARRAYModule")
ELSEIF methodName == "forEach" || methodName == "forKeyEach"
retType = ContainerStuff.generateForEachCall(
type, call, methodName, ctx, destType, "MARRAYModule")
ELSE
RETURN FAIL
}
IF destType != NIL && retType == NIL
retType = Type.anUnknown
}
retTypeRef = retType
RETURN OK
}
}