-
Notifications
You must be signed in to change notification settings - Fork 0
/
variant_eq.nim
49 lines (47 loc) · 1.61 KB
/
variant_eq.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
proc ifNeqRetFalse(fld,w,v:NimNode):NimNode =
quote do:
if `w`.`fld` != `v`.`fld`: return false
proc genIfStmts(recList,i,j:NimNode):NimNode =
result = newStmtList()
case recList.kind
of nnkRecList:
for idDef in recList:
expectKind(idDef,nnkIdentDefs)
result.add idDef[0].ifNeqRetFalse(i,j)
of nnkIdentDefs:
result.add recList[0].ifNeqRetFalse(i,j)
else: error "expected RecList or IdentDefs got" & recList.repr
macro equalsImpl[T:object](a,b:T): untyped =
template ifNeqRetFalse(fld:typed):untyped = ifNeqRetFalse(fld,a,b)
template genIfStmts(recList:typed):untyped = genIfStmts(recList,a,b)
let tImpl = a.getTypeImpl
result = newStmtList()
result.add quote do:
result = true
let records = tImpl[2]
records.expectKind(nnkRecList)
for field in records:
case field.kind
of nnkIdentDefs:
result.add field[0].ifNeqRetFalse
of nnkRecCase:
let discrim = field[0][0]
result.add discrim.ifNeqRetFalse
var casestmt = newNimNode(nnkCaseStmt)
casestmt.add newDotExpr(a,discrim)
for ofbranch in field[1..^1]:
case ofbranch.kind
of nnkOfBranch:
let testVal = ofbranch[0]
let reclst = ofbranch[1]
casestmt.add nnkOfBranch.newTree(testVal,reclst.genIfStmts)
of nnkElse:
let reclst = ofbranch[0]
casestmt.add nnkElse.newTree(reclst.genIfStmts)
else: error "Expected OfBranch or Else, got" & ofbranch.repr
result.add casestmt
else:
error "Expected IdentDefs or RecCase, got " & field.repr
#echo result.repr
proc `==`*[T:object](x,y:T):bool =
equalsImpl(x,y)