forked from Araq/wxnim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genui.nim
261 lines (234 loc) · 7.97 KB
/
genui.nim
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
import macros
{.experimental.}
proc `[]`(s: NimNode, x: Slice[int]): seq[NimNode] =
## slice operation for NimNodes.
var a = x.a
var L = x.b - a + 1
newSeq(result, L)
for i in 0.. <L: result[i] = s[i + a]
proc high(s: NimNode):int =
s.len-1
type WidgetArguments = ref object
identifier:NimNode
name: string
addArguments: seq[NimNode]
arguments: seq[NimNode]
children: seq[WidgetArguments]
parent: WidgetArguments
sizer: WidgetArguments
event: tuple[evname: NimNode, evcallback: NimNode]
isStr: bool
isIdentified: bool
isSizer: bool
proc parseNode(node:NimNode):WidgetArguments
proc parseBracketExpr(bracketExpr:NimNode):WidgetArguments
proc parseChildren(p: WidgetArguments, stmtList:NimNode): seq[WidgetArguments] =
result = @[]
for child in stmtList:
var node = parseNode(child)
node.parent = p
result.add node
proc parseCall(call:NimNode):WidgetArguments =
let hasAddArguments = call[0].kind == nnkBracketExpr
let hasChildren = call[call.high].kind == nnkStmtList
if hasAddArguments:
result = parseBracketExpr(call[0])
else:
new result
result.name = $call[0].ident
if result.arguments == nil:
result.arguments = if hasChildren: call[1..<call.high] else: call[1..call.high]
#else:
# for arg in if hasChildren: call[1..<call.high] else: call[1..call.high]:
# result.arguments.add arg
result.children = if hasChildren: parseChildren(result, call[call.high]) else: nil
proc parseBracketExpr(bracketExpr:NimNode):WidgetArguments =
let hasArguments = bracketExpr[0].kind == nnkCall
let hasChildren = bracketExpr[bracketExpr.high].kind == nnkStmtList
if hasArguments:
result = parseCall(bracketExpr[0])
else:
new result
result.name = $bracketExpr[0].ident
result.addArguments = if hasChildren: bracketExpr[1..<bracketExpr.high] else: bracketExpr[1..bracketExpr.high]
result.children = if hasChildren: result.parseChildren(bracketExpr[bracketExpr.high]) else: nil
proc parseInfix(infix:NimNode):WidgetArguments=
if infix[0].ident == !"%":
result = parseNode(infix[2])
result.identifier = infix[1]
elif infix[0].ident == !"->":
result = parseNode(infix[1])
result.event = (evname: infix[2][0], evcallback: infix[2][1])
elif infix[0].ident == !"|":
result = parseNode(infix[1])
result.sizer = parseNode(infix[2])
result.sizer.isSizer = true
if infix[infix.high].kind == nnkStmtList:
result.children = result.parseChildren(infix[infix.high])
proc parseIdent(i:NimNode):WidgetArguments=
result = WidgetArguments(
identifier: nil,
name: $i.ident,
addArguments: nil,
arguments: nil,
children: nil,
)
proc parsePrefix(prefix:NimNode):WidgetArguments=
result = parseNode(prefix[1])
result.isIdentified = true
if prefix[prefix.high].kind == nnkStmtList:
result.children = result.parseChildren(prefix[prefix.high])
proc parseString(str:NimNode):WidgetArguments=
result = WidgetArguments(
name: str.strVal,
isStr: true
)
proc parseNode(node:NimNode):WidgetArguments =
case node.kind:
of nnkCall:
result = parseCall(node)
of nnkBracketExpr:
result = parseBracketExpr(node)
of nnkInfix:
result = parseInfix(node)
of nnkPrefix:
result = parsePrefix(node)
of nnkIdent:
result = parseIdent(node)
of nnkStrLit:
result = parseString(node)
else:
discard
template updateOrCreate(ident: untyped, value: untyped)=
when not declared(ident):
var ident = value
else:
ident = value
proc createAddCall(sizerIdent: NimNode, child: WidgetArguments): NimNode =
var addCall =
if child.sizer != nil and (ident(child.sizer.name) == ident"staticboxsizer" or ident(child.sizer.name) == ident"Staticboxsizer"):
newCall("add",sizerIdent,child.sizer.identifier)
else:
newCall("add",sizerIdent,child.identifier)
var overridesDefaults: tuple[border, proportion, flag: bool]
for addArg in child.addArguments:
addCall.add addArg
if addArg.kind == nnkExprEqExpr and addArg[0].kind == nnkIdent:
if addArg[0].ident == !"border":
overridesDefaults.border = true
if addArg[0].ident == !"proportion":
overridesDefaults.proportion = true
if addArg[0].ident == !"flag":
overridesDefaults.flag = true
# Add in defaults since the wxWidgets defaults are pretty bad
if not overridesDefaults.border:
addCall.add nnkExprEqExpr.newTree(
newIdentNode("border"),
newIntLitNode(5)
)
if not overridesDefaults.flag:
addCall.add nnkExprEqExpr.newTree(
newIdentNode("flag"),
nnkInfix.newTree(
newIdentNode("or"),
newIdentNode("wxExpand"),
newIdentNode("wxAll")
)
)
return addCall
proc createWidget(widget: WidgetArguments):NimNode =
result = newStmtList()
var call:NimNode
if widget.isIdentified:
call = newIdentNode(widget.name)
else:
call = newCall("constructWx" & widget.name)
var hasID = false
if widget.arguments!=nil:
for arg in widget.arguments:
call.add arg
if arg.kind == nnkExprEqExpr and $arg[0] == "id":
hasID = true
if not widget.isSizer:
call.add nnkExprEqExpr.newTree(
newIdentNode("parent"),
if widget.parent == nil: newNilLit() else: widget.parent.identifier
)
if not hasID:
call.add nnkExprEqExpr.newTree(
newIdentNode("id"),
newIdentNode("wxID_ANY")
)
if widget.children.len == 1 and widget.children[0].isStr:
call.add nnkExprEqExpr.newTree(
newIdentNode("label"),
newStrLitNode(widget.children[0].name)
)
let command =
nnkCommand.newTree(
newIdentNode("cnew"),
call
)
if widget.identifier==nil:
widget.identifier = genSym(nskVar)
result.add nnkVarSection.newTree(
nnkIdentDefs.newTree(
widget.identifier,
newEmptyNode(),
command
)
)
else:
result.add getAst(updateOrCreate(widget.identifier, command))
if widget.sizer != nil:
if ident(widget.sizer.name) == ident"staticboxsizer" or ident(widget.sizer.name) == ident"Staticboxsizer" :
widget.sizer.arguments.add nnkExprEqExpr.newTree(
newIdentNode("box"),
widget.identifier
)
let sizerCode = createWidget(widget.sizer)
for node in sizerCode:
result.add node
if ident(widget.sizer.name) != ident"staticboxsizer" and ident(widget.sizer.name) != ident"Staticboxsizer":
result.add newCall("setSizer",widget.identifier, widget.sizer.identifier)
if widget.event.evname != nil:
result.add newCall("bind", widget.identifier, widget.event.evname, widget.event.evcallback)
for child in widget.children:
if not child.isStr:
let childCode = createWidget(child)
for node in childCode:
result.add node
#if child.addArguments != nil and child.addArguments.len != 0:
if widget.sizer != nil:
result.add widget.sizer.identifier.createAddCall(child)
macro genui*(args: varargs[untyped]): untyped =
#echo treeRepr args[0]
let parsed = nil.parseChildren(args[0])
result = newStmtList()
for widget in parsed:
var w = widget
let widgetCode = createWidget(w)
for node in widgetCode:
result.add(node)
#echo result.toStrLit
macro addElements*(parentWindow:untyped, args: varargs[untyped]):untyped=
template addToSizer(parentWidget:NimNode, sizerIdent:NimNode, addBody:NimNode):untyped=
let sizerIdent = parentWidget.getSizer()
if sizerIdent!=nil:
addBody
sizerIdent.layout()
#echo treeRepr args[0]
let parsed = nil.parseChildren(args[0])
result = newStmtList()
var parentWidgetArguments = WidgetArguments(identifier: parentWindow)
let sizerSym = genSym(nskLet)
var addCalls = newStmtList()
for widget in parsed:
var w = widget
w.parent = parentWidgetArguments
let widgetCode = createWidget(w)
for node in widgetCode:
result.add(node)
addCalls.add sizerSym.createAddCall(widget)
result.add getAst(addToSizer(parentWindow, sizerSym, addCalls))
#echo result.toStrLit