-
Notifications
You must be signed in to change notification settings - Fork 3
/
semcheck.nim
1435 lines (1312 loc) · 49.5 KB
/
semcheck.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast, layout, idents, kiwi, tables, razcontext, hashes, strutils
import nimLUA, keywords, sets, interpolator, types, nvg, namedcolors
const
screenWidth* = 800
screenHeight* = 600
namedColorsStart = ord(high(SpecialWords)) + 1
namedColorsEnd = namedColorsStart + NamedColors.len
type
Layout* = ref object of IDobj
root*: View # every layout/scene root
classTbl: Table[Ident, Node] # string to SymbolNode.skClass
aliasTbl: Table[Ident, Node]
animTbl: Table[Ident, Node]
solver: kiwi.Solver # constraint solver
origin: kiwi.Solver
context: RazContext # ref to app global context
lastView: View # last processed parent view/current View
emptyNode: Node
proc hash*(view: View): Hash =
result = hash(cast[int](view))
proc newInternalError(fileName: string, line: int, msg: string): InternalError =
new(result)
result.msg = msg
result.line = line
result.fileName = fileName
# like assert, but better
when defined(release):
template ensure(cond: bool) = discard
else:
template ensure(cond: bool) =
if not cond: raise newInternalError(instantiationInfo().fileName,
instantiationInfo().line, astToStr(cond))
# convert identNode to SpecialWords
proc toKeyWord(n: Node): SpecialWords =
ensure(n.kind == nkIdent)
if n.ident.id > 0 and n.ident.id <= ord(high(SpecialWords)):
result = SpecialWords(n.ident.id)
else:
result = wInvalid
# create view & view node as a child of last parent
proc createView(lay: Layout, n: Node): Node =
var view = lay.lastView.newView(n.ident)
result = newViewSymbol(n, view).newSymbolNode()
lay.lastView = view
view.symNode = result
lay.solver.setBasicConstraint(view)
# one application can have multiple layout a.k.a 'page'
proc newLayout*(id: int, context: RazContext): Layout =
new(result)
result.id = id
result.classTbl = initTable[Ident, Node]()
result.aliasTbl = initTable[Ident, Node]()
result.animTbl = initTable[Ident, Node]()
result.solver = newSolver()
result.origin = result.solver
result.context = context
result.emptyNode = newNode(nkEmpty)
let root = context.getIdent("root")
let n = newIdentNode(root)
result.root = newView(root)
result.root.symNode = newViewSymbol(n, result.root).newSymbolNode()
result.root.node = newTree(nkView, result.root.symNode, result.emptyNode, result.emptyNode)
result.root.curProp.visible = false
result.solver.setBasicConstraint(result.root)
proc getRoot(lay: Layout): View =
result = lay.root
proc getIdent(lay: Layout, s: string): Ident =
result = lay.context.getIdent(s)
proc getAnimation*(lay: Layout, name: string): Animation =
let id = lay.getIdent(name)
let symNode = lay.animTbl.getOrDefault(id)
if symNode != nil: result = symNode.sym.anim
proc internalErrorImpl(lay: Layout, kind: MsgKind,
fileName: string, line: int, args: varargs[string, `$`]) =
# internal error provide debugging information
raise newInternalError(fileName, line, lay.context.msgKindToString(kind, args))
template internalError(lay: Layout, kind: MsgKind, args: varargs[string, `$`]) =
# pointing to Nim source code location
# where the error occured
lay.internalErrorImpl(kind,
instantiationInfo().fileName,
instantiationInfo().line,
args)
proc otherError(lay: Layout, kind: MsgKind, args: varargs[string, `$`]) =
# not internal error and not source error
lay.context.otherError(kind, args)
proc getCurrentLine*(lay: Layout, info: RazLineInfo): string =
# we don't have lexer getCurrentLine anymore
# so we simulate one here
let fileName = lay.context.toFullPath(info)
var f = open(fileName)
if f.isNil(): lay.otherError(errCannotOpenFile, fileName)
var line: string
var n = 1
while f.readLine(line):
if n == info.line:
result = line & "\n"
break
inc n
f.close()
if result.isNil: result = ""
proc sourceError[T: Node or Symbol](lay: Layout, kind: MsgKind, n: T, args: varargs[string, `$`]) =
# report any error during semcheck
var err = new(SourceError)
err.msg = lay.context.msgKindToString(kind, args)
err.line = n.lineInfo.line
err.column = n.lineInfo.col
err.lineContent = lay.getCurrentLine(n.lineInfo)
err.fileIndex = n.lineInfo.fileIndex
raise err
proc sourceWarning[T: Node or Symbol](lay: Layout, kind: MsgKind, n: T, args: varargs[string, `$`]) =
# report any error during semcheck
var err = new(SourceError)
err.msg = lay.context.msgKindToString(kind, args)
err.line = n.lineInfo.line
err.column = n.lineInfo.col
err.lineContent = lay.getCurrentLine(n.lineInfo)
err.fileIndex = n.lineInfo.fileIndex
lay.context.printWarning(err)
proc semViewName(lay: Layout, n: Node, lastIdent: Node): Node =
# check and resolve view name hierarchy
# such as view1.view1child.view1grandson
case n.kind
of nkDotCall:
ensure(n.len == 2)
n[0] = lay.semViewName(n[0], lastIdent)
ensure(n[0].kind == nkSymbol)
lay.lastView = n[0].sym.view
n[1] = lay.semViewName(n[1], lastIdent)
result = n[1]
of nkIdent:
var view = lay.lastView.views.getOrDefault(n.ident)
if view.isNil:
result = lay.createView(n)
else:
let symNode = view.symNode
if lastIdent == n:
let info = symNode.lineInfo
let prev = lay.context.toString(info)
lay.sourceError(errDuplicateView, n, symNode.symString, prev)
result = symNode
else:
internalError(lay, errUnknownNode, n.kind)
proc semViewClass(lay: Layout, n: Node): Node =
result = n
proc semFlexList(lay: Layout, n: Node) =
discard
proc semEventList(lay: Layout, n: Node) =
for ev in n.sons:
ensure(ev.kind == nkEvent)
ensure(ev[0].kind == nkIdent)
let id = toKeyword(ev[0])
if id notin validEvents:
lay.sourceError(errUndefinedEvent, ev[0], ev[0].ident)
proc semPropList(lay: Layout, n: Node) =
discard
proc semViewBody(lay: Layout, n: Node): Node =
ensure(n.kind in {nkStmtList, nkEmpty})
for m in n.sons:
case m.kind
of nkFlexList: lay.semFlexList(m)
of nkEventList: lay.semEventList(m)
of nkPropList: lay.semPropList(m)
of nkEmpty: discard
else:
internalError(lay, errUnknownNode, m.kind)
result = n
proc semView(lay: Layout, n: Node) =
ensure(n.len == 3)
# each time we create new view
# need to reset the lastView
lay.lastView = lay.root
var lastIdent = Node(nil)
if n[0].kind == nkIdent: lastIdent = n[0]
if lastIdent.isNil and n[0].kind == nkDotCall:
let son = n[0].sons[1]
if son.kind == nkIdent: lastIdent = son
n[0] = lay.semViewName(n[0], lastIdent)
n[0].sym.view.node = n
# at this point, the view already created
# and n[0] already replaced with a symbolNode
lay.lastView = n[0].sym.view
n[1] = lay.semViewClass(n[1])
n[2] = lay.semViewBody(n[2])
proc subst(lay: Layout, n: Node, cls: ClassContext): Node =
# substitute node with param symNode
case n.kind
of NodeWithSons - {nkProp}:
for i in 0.. <n.len:
n[i] = lay.subst(n[i], cls)
result = n
of nkProp:
# do not replace lhs
for i in 1.. <n.len:
n[i] = lay.subst(n[i], cls)
result = n
of nkIdent:
let sym = cls.paramTable.getOrDefault(n.ident)
if not sym.isNil:
sym.sym.flags.incl(sfUsed)
return sym
result = n
of nkUint, nkString, nkInt:
result = n
else:
internalError(lay, errUnknownNode, n.kind)
proc substituteParams(lay: Layout, n: Node, cls: ClassContext) =
# iterate over class body and substitue it with param
# if any, then report unused param if any
for i in 0.. <n.len:
n[i] = lay.subst(n[i], cls)
for s in values(cls.paramTable):
if sfUsed notin s.sym.flags:
lay.sourceWarning(warnParamNotUsed, s, s.sym.name)
proc collectParams(lay: Layout, n: Node, cls: ClassContext) =
# build param symbol table and it's default value if any
# check for duplicate param's nama
if n.kind == nkEmpty: return
ensure(n.kind == nkClassParams)
for i in 0.. <n.len:
let m = n.sons[i]
case m.kind
of nkIdent:
let p = cls.paramTable.getOrDefault(m.ident)
if p.isNil:
cls.paramTable[m.ident] = newParamSymbol(m, nil, i).newSymbolNode()
else:
lay.sourceError(errDuplicateParam, m, m.ident)
of nkAsgn:
ensure(m.sons.len == 3)
let paramName = m[1]
let paramValue = m[2]
let p = cls.paramTable.getOrDefault(paramName.ident)
if p.isNil:
cls.paramTable[paramName.ident] = newParamSymbol(paramName, paramValue, i).newSymbolNode()
else:
lay.sourceError(errDuplicateParam, paramName, paramName.ident)
else:
internalError(lay, errUnknownNode, m.kind)
proc semClass(lay: Layout, n: Node) =
# create class and check for duplicate
ensure(n.len == 3)
let className = n[0]
var symNode = lay.classTbl.getOrDefault(className.ident)
if symNode.isNil:
let cls = newClassContext(n)
let sym = newClassSymbol(className, cls)
symNode = newSymbolNode(sym)
lay.classTbl[className.ident] = symNode
n[0] = symNode
else:
let info = symNode.lineInfo
let prev = lay.context.toString(info)
lay.sourceError(errDuplicateClass, className, symNode.symString, prev)
let cls = symNode.sym.class
lay.collectParams(n[1], cls) # n[1] = classParams
if cls.paramTable.len > 0:
lay.substituteParams(n[2], cls) # n[2] = classBody
proc semAliasList(lay: Layout, n: Node) =
for m in n.sons:
ensure(m.kind == nkAlias)
ensure(m.len == 2)
ensure(m[0].kind == nkIdent)
let alias = lay.aliasTbl.getOrDefault(m[0].ident)
if alias.isNil:
let sym = newAliasSymbol(m[0], m[1]).newSymbolNode()
lay.aliasTbl[m[0].ident] = sym
else:
let info = alias.lineInfo
let prev = lay.context.toString(info)
lay.sourceError(errDuplicateAlias, m[0], alias.sym.name, prev)
proc semAnimList(lay: Layout, n: Node) =
discard
proc semStmt(lay: Layout, n: Node) =
case n.kind
of nkView: lay.semView(n)
of nkClass: lay.semClass(n)
of nkAnimList: lay.semAnimList(n)
of nkAliasList: lay.semAliasList(n)
else:
internalError(lay, errUnknownNode, n.kind)
proc semTopLevel*(lay: Layout, n: Node) =
ensure(n.kind == nkStmtList)
for son in n.sons:
lay.semStmt(son)
# entering second pass
proc secViewBody(lay: Layout, n: Node)
proc checkParamCountMatch(lay: Layout, params, classParams: Node) =
# view can have many class and we need to check if the param
# count match
if classParams.kind == nkEmpty:
if params.len > 0:
lay.sourceError(errParamCountNotMatch, params, 0, params.len)
return
ensure(classParams.kind == nkClassParams)
if params.len == classParams.len: return
if params.len > classParams.len:
lay.sourceError(errParamCountNotMatch, params, classParams.len, params.len)
var count = classParams.len
# count params without default value
for i in countdown(classParams.len-1, 0):
if classParams[i].kind != nkAsgn:
count = i + 1
break
# count available and needed param
for i in params.len.. <classParams.len:
if classParams[i].kind != nkAsgn:
# it has no default value
lay.sourceError(errParamCountNotMatch, params, count, params.len)
proc instClass(lay: Layout, n: Node, cls: ClassContext, params: Node): Node =
# replace each param with value supplied from view or
# class's param default value
case n.kind
of NodeWithSons:
for i in 0.. <n.len:
n[i] = lay.instClass(n[i], cls, params)
result = n
of nkSymbol:
if n.sym.pos >= 0 and n.sym.pos < params.len:
result = params[n.sym.pos]
else:
result = n.sym.value
of nkIdent:
let alias = lay.aliasTbl.getOrDefault(n.ident)
if alias.isNil: result = n
else:
alias.sym.flags.incl(sfUsed)
result = alias.sym.alias
of nkUInt, nkString:
result = n
else:
internalError(lay, errUnknownNode, n.kind)
proc instantiateClass(lay: Layout, cls: ClassContext, params: Node) =
# copy only the class body which is essentialy
# has the same structure with view body
# then instantiate it
var n = cls.n[2].copyTree()
for i in 0.. <n.len:
n[i] = lay.instClass(n[i], cls, params)
# don't forget to check instantiated class
lay.secViewbody(n)
proc instantiateArg(lay: Layout, n: Node): Node =
case n.kind
of NodeWithSons:
for i in 0.. <n.len:
n[i] = lay.instantiateArg(n[i])
result = n
of nkIdent:
let alias = lay.aliasTbl.getOrDefault(n.ident)
if alias.isNil: result = n
else:
alias.sym.flags.incl(sfUsed)
result = alias.sym.alias
else: result = n
proc secViewClass(lay: Layout, n: Node) =
# the view have classes
ensure(n.kind in {nkViewClassList, nkEmpty})
for vc in n.sons:
ensure(vc.kind == nkViewClass)
ensure(vc.len == 2)
let name = vc.sons[0]
let params = lay.instantiateArg(vc.sons[1])
ensure(name.kind in {nkIdent, nkSymbol})
let classSymbol = if name.kind == nkIdent:
lay.classTbl.getOrDefault(name.ident)
else: name
if classSymbol.isNil:
lay.sourceError(errClassNotFound, name, name.ident.s)
# mark it as used
classSymbol.sym.flags.incl(sfUsed)
# replace name with symbol
vc.sons[0] = classSymbol
let class = classSymbol.sym.class
let classParams = class.n[1]
lay.checkParamCountMatch(params, classParams)
# replace param(s) with intantiated class
lay.instantiateClass(class, params)
proc selectViewProp(lay: Layout, view: View, id: SpecialWords): Variable =
case id
of wLeft, wX: result = view.current.left
of wRight: result = view.current.right
of wTop, wY: result = view.current.top
of wBottom: result = view.current.bottom
of wWidth: result = view.current.width
of wHeight: result = view.current.height
of wCenterX: result = view.current.centerX
of wCenterY: result = view.current.centerY
else:
internalError(lay, errUnknownProp, id)
proc selectViewRel(lay: Layout, view: View, id: SpecialWords, idx = 1): View =
# get a view related to `this` view
# idx < 0 means the last
case id
of wThis: result = view
of wRoot: result = lay.root
of wParent:
result = view.parent
if idx > 1:
var i = 1
while i < idx:
if result.isNil: break
result = result.parent
inc i
of wChild:
if idx < 0 and view.children.len > 0: return view.children[^1]
if idx >= 0 and idx < view.children.len:
result = view.children[idx]
of wPrev:
if not view.parent.isNil:
if idx < 0 and view.parent.children.len > 0: return view.parent.children[0]
let i = view.idx - idx
if i >= 0 and i < view.parent.children.len:
result = view.parent.children[i]
of wNext:
if not view.parent.isNil:
if idx < 0 and view.parent.children.len > 0: return view.parent.children[^1]
let i = view.idx + idx
if i < view.parent.children.len:
result = view.parent.children[i]
else:
internalError(lay, errUnknownRel, id)
proc computeIdx(lay: Layout, n: Node): int =
# compute integer index used by something like
# prev[idx].left
result = 1
case n.kind
of nkEmpty: result = -1
of nkUInt: result = int(n.uintVal)
of nkFloat:
lay.sourceError(errFloatNotAllowed, n)
of nkString:
lay.sourceError(errFloatNotAllowed, n)
of nkInfix:
let op = toKeyword(n[0])
let lhs = lay.computeIdx(n[1])
let rhs = lay.computeIdx(n[2])
case op
of wPlus: result = lhs + rhs
of wMinus: result = lhs - rhs
of wMul: result = lhs * rhs
of wDiv: result = lhs div rhs
else: internalError(lay, errUnknownBinaryOpr, op)
of nkPrefix:
let op = toKeyword(n[0])
let operand = lay.computeIdx(n[1])
case op
of wMinus: result = -operand
of wPlus: result = abs(operand)
else: lay.sourceError(errIllegalPrefix, n[0], '-', operand)
else: internalError(lay, errUnknownNode, n.kind)
proc findRelation(lay: Layout, n: Node, id: SpecialWords, idx = 1, useBracket = false): View =
# first find from relative relation
if id in flexRel:
result = lay.selectViewRel(lay.lastView, id, idx)
return result
# find among child
result = lay.lastView.views.getOrDefault(n.ident)
if result != nil and useBracket:
if idx < 0 and result.children.len > 0: return result.children[^1]
if idx >= 0 and idx < result.children.len: return result.children[idx]
# find among siblings
if result.isNil:
var view = lay.lastView.parent
while view != nil:
result = view.views.getOrDefault(n.ident)
if result != nil and useBracket:
if idx < 0 and result.children.len > 0: return result.children[^1]
if idx >= 0 and idx < result.children.len: return result.children[idx]
if result != nil: return result
view = view.parent
proc resolveTerm(lay: Layout, n: Node, lastIdent: Ident, choiceMode = false): Node =
# here we try to validate an term
case n.kind
of nkIdent:
let id = toKeyWord(n)
if n.ident == lastIdent:
if id in flexProp:
result = newNodeI(nkFlexVar, n.lineInfo)
result.variable = lay.selectViewProp(lay.lastView, id)
else:
let alias = lay.aliasTbl.getOrDefault(n.ident)
if alias.isNil:
lay.sourceError(errUndefinedVar, n, n.ident)
else:
alias.sym.flags.incl(sfUsed)
result = lay.resolveTerm(alias.sym.alias, lastIdent, choiceMode)
else:
let view = lay.findRelation(n, id)
if view.isNil:
if choiceMode: return lay.emptyNode
else: lay.sourceError(errRelationNotFound, n, n.ident, lay.lastView.name)
view.dependencies.incl(lay.lastView)
lay.lastView.dependencies.incl(view)
result = view.symNode
of nkDotCall:
let tempView = lay.lastView
ensure(n.len == 2)
if n[1].kind == nkIdent and n[1].ident == lastIdent:
if n[0].kind == nkIdent:
let lhs = toKeyWord(n[0])
let rhs = toKeyWord(n[1])
if lhs == wChild and rhs == wCount:
result = newNodeI(nkInt, n[1].lineInfo)
result.intVal = lay.lastView.children.len
return result
n[0] = lay.resolveTerm(n[0], lastIdent, choiceMode)
if choiceMode and n[0].kind == nkEmpty: return n[0]
ensure(n[0].kind == nkSymbol)
lay.lastView = n[0].sym.view
n[1] = lay.resolveTerm(n[1], lastIdent, choiceMode)
if choiceMode and n[1].kind == nkEmpty: return n[1]
lay.lastView = tempView
result = n[1]
of nkBracketExpr:
ensure(n.len == 2)
ensure(n[0].kind in {nkIdent, nkDotCall})
let node = if n[0].kind == nkIdent: n[0] else: n[0][^1]
let id = toKeyWord(node)
if id in flexRel:
let idx = lay.computeIdx(n[1])
let tempView = lay.lastView
if n[0].kind == nkDotCall:
n[0] = lay.resolveTerm(n[0][0], node.ident, choiceMode)
lay.lastView = n[0].sym.view
let view = lay.findRelation(node, id, idx, true)
lay.lastView = tempView
if view.isNil:
if choiceMode: return lay.emptyNode
else:
let node = if n[1].kind == nkEmpty: n else: n[1]
lay.sourceError(errWrongRelationIndex, node, idx)
view.dependencies.incl(lay.lastView)
lay.lastView.dependencies.incl(view)
result = view.symNode
else:
lay.sourceError(errUndefinedRel, n[0], n[0].ident)
of nkString:
lay.sourceError(errStringNotAllowed, n)
of nkSymbol:
result = n
else:
internalError(lay, errUnknownNode, n.kind)
const numberNode = {nkInt, nkUInt, nkFloat}
proc toNumber(n: Node): float64 =
case n.kind
of nkUint: result = float64(n.uintVal)
of nkInt: result = float64(n.intVal)
of nkFloat: result = n.floatVal
else: result = 0.0
proc termOpPlus(lay: Layout, a, b, op: Node): Node =
if a.kind in numberNode and b.kind in numberNode:
result = newNodeI(nkFloat, a.lineInfo)
result.floatVal = a.toNumber() + b.toNumber()
elif a.kind in numberNode and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() + b.variable
elif a.kind in numberNode and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() + b.expression
elif a.kind in numberNode and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() + b.term
elif a.kind == nkFlexVar and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable + b.toNumber()
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable + b.expression
elif a.kind == nkFlexVar and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable + b.term
elif a.kind == nkFlexVar and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable + b.variable
elif a.kind == nkFlexExpr and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression + b.toNumber()
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression + b.variable
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression + b.expression
elif a.kind == nkFlexExpr and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression + b.term
elif a.kind == nkFlexTerm and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term + b.term
elif a.kind == nkFlexTerm and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term + b.expression
elif a.kind == nkFlexTerm and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term + b.variable
elif a.kind == nkFlexTerm and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term + b.toNumber()
else: internalError(lay, errUnknownOperation, a.kind, "'+'", b.kind)
proc termOpMinus(lay: Layout, a, b, op: Node): Node =
if a.kind == nkFlexExpr and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression - b.toNumber()
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression - b.variable
elif a.kind == nkFlexExpr and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression - b.term
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression - b.expression
elif a.kind == nkFlexTerm and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term - b.expression
elif a.kind == nkFlexTerm and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term - b.term
elif a.kind == nkFlexTerm and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term - b.variable
elif a.kind == nkFlexTerm and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.term - b.toNumber()
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable - b.expression
elif a.kind == nkFlexVar and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable - b.term
elif a.kind == nkFlexVar and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable - b.variable
elif a.kind == nkFlexVar and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.variable - b.toNumber()
elif a.kind in numberNode and b.kind in numberNode:
result = newNodeI(nkFloat, a.lineInfo)
result.floatVal = a.toNumber() - b.toNumber()
elif a.kind in numberNode and b.kind == nkFlexVar:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() - b.variable
elif a.kind in numberNode and b.kind == nkFlexTerm:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() - b.term
elif a.kind in numberNode and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() - b.expression
else: internalError(lay, errUnknownOperation, a.kind, "'-'", b.kind)
proc termOpMul(lay: Layout, a, b, op: Node): Node =
if a.kind in numberNode and b.kind in numberNode:
result = newNodeI(nkFloat, a.lineInfo)
result.floatVal = a.toNumber() * b.toNumber()
elif a.kind in numberNode and b.kind == nkFlexVar:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.toNumber() * b.variable
elif a.kind in numberNode and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.toNumber() * b.expression
elif a.kind in numberNode and b.kind == nkFlexTerm:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.toNumber() * b.term
elif a.kind == nkFlexVar and b.kind in numberNode:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.variable * b.toNumber()
elif a.kind == nkFlexExpr and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression * b.toNumber()
elif a.kind == nkFlexTerm and b.kind in numberNode:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.term * b.toNumber()
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression * b.expression
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.expression * b.variable
lay.sourceError(errIllegalOperation, op, a.kind, "'*'", b.kind)
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.variable * b.expression
lay.sourceError(errIllegalOperation, op, a.kind, "'*'", b.kind)
else: internalError(lay, errUnknownOperation, a.kind, "'*'", b.kind)
proc termOpDiv(lay: Layout, a, b, op: Node): Node =
if a.kind == nkFlexVar and b.kind in numberNode:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.variable / b.toNumber()
elif a.kind == nkFlexTerm and b.kind in numberNode:
result = newNodeI(nkFlexTerm, a.lineInfo)
result.term = a.term / b.toNumber()
elif a.kind == nkFlexExpr and b.kind in numberNode:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression / b.toNumber()
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
result = newNodeI(nkFlexExpr, a.lineInfo)
result.expression = a.expression / b.expression
elif a.kind in numberNode and b.kind in numberNode:
result = newNodeI(nkFloat, a.lineInfo)
result.floatVal = a.toNumber() / b.toNumber()
elif a.kind in numberNode and b.kind == nkFlexVar:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.toNumber() / b.variable
lay.sourceError(errIllegalOperation, op, a.kind, "'/'", b.kind)
elif a.kind in numberNode and b.kind == nkFlexExpr:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.toNumber() / b.expression
lay.sourceError(errIllegalOperation, op, a.kind, "'/'", b.kind)
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.expression / b.variable
lay.sourceError(errIllegalOperation, op, a.kind, "'/'", b.kind)
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
#result = newNodeI(nkFlexExpr, a.lineInfo)
#result.expression = a.variable / b.expression
lay.sourceError(errIllegalOperation, op, a.kind, "'/'", b.kind)
else: internalError(lay, errUnknownOperation, a.kind, "'/'", b.kind)
proc binaryTermOp(lay: Layout, a, b, op: Node, id: SpecialWords): Node =
case id
of wPlus: result = lay.termOpPlus(a, b, op)
of wMinus: result = lay.termOpMinus(a, b, op)
of wMul: result = lay.termOpMul(a, b, op)
of wDiv: result = lay.termOpDiv(a, b, op)
else: internalError(lay, errUnknownBinaryOpr, id)
proc termPrefixMinus(lay: Layout, operand, op: Node): Node =
case operand.kind
of numberNode:
result = newNodeI(nkFloat, operand.lineInfo)
result.floatVal = -operand.toNumber()
of nkFlexVar:
result = newNodeI(nkFlexTerm, operand.lineInfo)
result.term = -operand.variable
of nkFlexTerm:
result = newNodeI(nkFlexTerm, operand.lineInfo)
result.term = -operand.term
of nkFlexExpr:
result = newNodeI(nkFlexExpr, operand.lineInfo)
result.expression = -operand.expression
else: internalError(lay, errUnknownPrefix, "'-'", operand.kind)
proc unaryTermOp(lay: Layout, operand, op: Node, id: SpecialWords): Node =
case id
of wMinus: result = lay.termPrefixMinus(operand, op)
else: internalError(lay, errUnknownPrefixOpr, id)
proc secFlexExpr(lay: Layout, n: Node, choiceMode = false): Node =
# here we try to validate an expression
case n.kind
of nkIdent:
let id = toKeyWord(n)
if id in flexProp:
result = newNodeI(nkFlexVar, n.lineInfo)
result.variable = lay.selectViewProp(lay.lastView, id)
else:
let alias = lay.aliasTbl.getOrDefault(n.ident)
if alias.isNil:
lay.sourceError(errUndefinedVar, n, n.ident)
else:
alias.sym.flags.incl(sfUsed)
result = lay.secFlexExpr(alias.sym.alias, choiceMode)
of nkUint:
result = n
of nkDotCall:
ensure(n.len == 2)
ensure(n[1].kind == nkIdent)
result = lay.resolveTerm(n, n[1].ident, choiceMode)
of nkInfix:
ensure(n.len == 3)
let id = toKeyWord(n[0])
if id notin flexBinaryTermOp:
lay.sourceError(errIllegalBinaryOpr, n[0], n[0].ident)
let lhs = lay.secFlexExpr(n[1], choiceMode)
let rhs = lay.secFlexExpr(n[2], choiceMode)
if choiceMode:
if lhs.kind == nkEmpty or lhs.kind == nkEmpty:
return lay.emptyNode
result = lay.binaryTermOp(lhs, rhs, n[0], id)
of nkString:
lay.sourceError(errStringNotAllowed, n)
of nkChoice:
# choose among choices, we pick first valid one
# expr1 | expr2 | expr3
for cc in n.sons:
result = lay.secFlexExpr(cc, true)
if result.kind != nkEmpty: return result
lay.sourceError(errNoValidBranch, n)
of nkPrefix:
ensure(n.len == 2)
let id = toKeyWord(n[0])
if id notin flexUnaryTermOp:
lay.sourceError(errIllegalPrefixOpr, n[0], n[0].ident)
let operand = lay.secFlexExpr(n[1], choiceMode)
if choiceMode and operand.kind == nkEmpty:
return lay.emptyNode
result = lay.unaryTermOp(operand, n[0], id)
of nkFlexVar, nkFlexExpr, nkFlexTerm:
# already processed, just return it
result = n
else:
internalError(lay, errUnknownNode, n.kind)
proc flexOpEQ(lay: Layout, a, b, op: Node) =
if a.kind in numberNode and b.kind == nkFlexVar:
lay.solver.addConstraint(a.toNumber() == b.variable)
elif a.kind in numberNode and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.toNumber() == b.expression)
elif a.kind in numberNode and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.toNumber() == b.term)
elif a.kind == nkFlexVar and b.kind in numberNode:
lay.solver.addConstraint(a.variable == b.toNumber())
elif a.kind == nkFlexVar and b.kind == nkFlexVar:
lay.solver.addConstraint(a.variable == b.variable)
elif a.kind == nkFlexVar and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.variable == b.term)
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.variable == b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.term == b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.term == b.term)
elif a.kind == nkFlexTerm and b.kind == nkFlexVar:
lay.solver.addConstraint(a.term == b.variable)
elif a.kind == nkFlexTerm and b.kind in numberNode:
lay.solver.addConstraint(a.term == b.toNumber())
elif a.kind == nkFlexExpr and b.kind in numberNode:
lay.solver.addConstraint(a.expression == b.toNumber())
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
lay.solver.addConstraint(a.expression == b.variable)
elif a.kind == nkFlexExpr and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.expression == b.term)
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.expression == b.expression)
elif a.kind in numberNode and b.kind in numberNode:
lay.sourceError(errIllegalOperation, op, a.kind, "=", b.kind)
else: internalError(lay, errUnknownOperation, a.kind, '=', b.kind)
proc flexOpLE(lay: Layout, a, b, op: Node) =
if a.kind in numberNode and b.kind == nkFlexVar:
lay.solver.addConstraint(a.toNumber() <= b.variable)
elif a.kind in numberNode and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.toNumber() <= b.expression)
elif a.kind in numberNode and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.toNumber() <= b.term)
elif a.kind == nkFlexVar and b.kind in numberNode:
lay.solver.addConstraint(a.variable <= b.toNumber())
elif a.kind == nkFlexVar and b.kind == nkFlexVar:
lay.solver.addConstraint(a.variable <= b.variable)
elif a.kind == nkFlexVar and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.variable <= b.term)
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.variable <= b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.term <= b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.term <= b.term)
elif a.kind == nkFlexTerm and b.kind == nkFlexVar:
lay.solver.addConstraint(a.term <= b.variable)
elif a.kind == nkFlexTerm and b.kind in numberNode:
lay.solver.addConstraint(a.term <= b.toNumber())
elif a.kind == nkFlexExpr and b.kind in numberNode:
lay.solver.addConstraint(a.expression <= b.toNumber())
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
lay.solver.addConstraint(a.expression <= b.variable)
elif a.kind == nkFlexExpr and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.expression <= b.term)
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.expression <= b.expression)
elif a.kind in numberNode and b.kind in numberNode:
lay.sourceError(errIllegalOperation, op, a.kind, "<=", b.kind)
else: internalError(lay, errUnknownOperation, a.kind, "<=", b.kind)
proc flexOpGE(lay: Layout, a, b, op: Node) =
if a.kind in numberNode and b.kind == nkFlexVar:
lay.solver.addConstraint(a.toNumber() >= b.variable)
elif a.kind in numberNode and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.toNumber() >= b.expression)
elif a.kind in numberNode and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.toNumber() >= b.term)
elif a.kind == nkFlexVar and b.kind in numberNode:
lay.solver.addConstraint(a.variable >= b.toNumber())
elif a.kind == nkFlexVar and b.kind == nkFlexVar:
lay.solver.addConstraint(a.variable >= b.variable)
elif a.kind == nkFlexVar and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.variable >= b.term)
elif a.kind == nkFlexVar and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.variable >= b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.term >= b.expression)
elif a.kind == nkFlexTerm and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.term >= b.term)
elif a.kind == nkFlexTerm and b.kind == nkFlexVar:
lay.solver.addConstraint(a.term >= b.variable)
elif a.kind == nkFlexTerm and b.kind in numberNode:
lay.solver.addConstraint(a.term >= b.toNumber())
elif a.kind == nkFlexExpr and b.kind in numberNode:
lay.solver.addConstraint(a.expression >= b.toNumber())
elif a.kind == nkFlexExpr and b.kind == nkFlexVar:
lay.solver.addConstraint(a.expression >= b.variable)
elif a.kind == nkFlexExpr and b.kind == nkFlexTerm:
lay.solver.addConstraint(a.expression >= b.term)
elif a.kind == nkFlexExpr and b.kind == nkFlexExpr:
lay.solver.addConstraint(a.expression >= b.expression)
elif a.kind in numberNode and b.kind in numberNode:
lay.sourceError(errIllegalOperation, op, a.kind, "<=", b.kind)
else: internalError(lay, errUnknownOperation, a.kind, ">=", b.kind)
proc flexOp(lay: Layout, a, b, op: Node, id: SpecialWords) =
try:
case id
of wEquals: lay.flexOpEQ(a, b, op)
of wGreaterOrEqual: lay.flexOpGE(a, b, op)
of wLessOrEqual: lay.flexOpLE(a, b, op)
else: internalError(lay, errUnknownEqualityOpr, id)
except UnsatisfiableConstraintException:
lay.sourceError(errUnsatisfiableConstraint, op)
except:
raise getCurrentException()
proc secChoiceList(lay: Layout, lhs, rhs, op: Node, opId: SpecialWords) =
if rhs.kind != nkChoiceList or lhs.kind != nkChoiceList:
lay.sourceError(errUnbalancedArm, op)
if rhs.len != lhs.len: lay.sourceError(errUnbalancedArm, op)
for i in 0.. <lhs.len:
lhs[i] = lay.secFlexExpr(lhs[i])
rhs[i] = lay.secFlexExpr(rhs[i])
lay.flexOp(lhs[i], rhs[i], op, opId)
proc secFlexList(lay: Layout, n: Node) =
ensure(n.kind == nkFlexList)
for cc in n.sons:
ensure(cc.kind == nkFlex)
ensure(cc.len >= 3)
for i in countup(0, cc.sons.len-2, 2):
let lhs = cc.sons[i]
let op = cc.sons[i+1]
let rhs = cc.sons[i+2]
let opId = toKeyWord(op)