-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename, reorganize, new modules, slight cleanup
- Loading branch information
Showing
16 changed files
with
422 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
# unions | ||
# variantsugar | ||
|
||
Object variants utilities. Due for a cleanup and rename. See tests for current modules | ||
Utility macros mostly for object variants | ||
|
||
```nim | ||
import variantsugar | ||
type Value {.sum, equals.} = object | ||
case kind: _ | ||
of None: discard | ||
of Integer, Boolean: | ||
_: int | ||
of Unsigned: | ||
_: uint | ||
of Float: | ||
_: float | ||
# == for case objects generated by equals | ||
assert Value(kind: Integer, integer: 1) == Integer(1) | ||
assert Value(kind: Boolean, integer: 1) == Boolean(1) | ||
assert Value(kind: Unsigned, unsigned: 1) == Unsigned(1) | ||
assert Value(kind: Float, float: 1) == Float(1) | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import variantsugar/[conditional, sum, unionfield, equals] | ||
export conditional, sum, unionfield, equals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## generates == for objects including case objects | ||
|
||
runnableExamples: | ||
type Foo {.equals.} = object | ||
name: string | ||
case kind: bool | ||
of false: | ||
a: int | ||
of true: | ||
b: float | ||
|
||
assert Foo(name: "abc", kind: false, a: 1) == Foo(name: "abc", kind: false, a: 1) | ||
assert Foo(name: "abc", kind: false, a: 1) != Foo(name: "abc", kind: true, b: 1) | ||
assert Foo(name: "abc", kind: false, a: 1) != Foo(name: "def", kind: false, a: 1) | ||
|
||
import macros, private/utils | ||
|
||
proc patchTypeSection(typeSec: NimNode, poststmts: var seq[NimNode]) = | ||
for td in typeSec: | ||
var objectNode = td[^1] | ||
while objectNode.kind in {nnkRefTy, nnkPtrTy}: | ||
objectNode = objectNode[0] | ||
if objectNode.kind == nnkObjectTy: | ||
let doExport = td[0].isNodeExported | ||
let typeName = td[0].realBasename | ||
# == | ||
proc generateEquals(sl: NimNode, field: NimNode) = | ||
case field.kind | ||
of nnkIdentDefs: | ||
for f in field[0 .. ^3]: | ||
sl.add quote do: | ||
if a.`f` != b.`f`: | ||
return false | ||
of nnkRecCase: | ||
let kf = field[0][0] | ||
sl.add quote do: | ||
if a.`kf` != b.`kf`: | ||
return false | ||
let cs = newTree(nnkCaseStmt, newDotExpr(ident"a", kf)) | ||
for b in field[1 .. ^1]: | ||
let branch = copy b | ||
branch[^1] = newStmtList() | ||
for r in b[^1]: | ||
generateEquals(branch[^1], r) | ||
cs.add(branch) | ||
sl.add(cs) | ||
of nnkRecWhen: | ||
let ws = newTree(nnkWhenStmt) | ||
for b in field: | ||
let branch = copy b | ||
branch[^1] = newStmtList() | ||
for r in b[^1]: | ||
generateEquals(branch[^1], r) | ||
ws.add(branch) | ||
sl.add(ws) | ||
else: discard | ||
let equalsBody = newStmtList() | ||
for r in objectNode[^1]: | ||
generateEquals(equalsBody, r) | ||
equalsBody.add(newTree(nnkReturnStmt, ident"true")) | ||
poststmts.add(newProc( | ||
name = ident"==".exportIf(doExport), | ||
params = [ident"bool", newTree(nnkIdentDefs, ident"a", ident"b", ident(typeName), newEmptyNode())], | ||
body = equalsBody, | ||
pragmas = newTree(nnkPragma, ident"used") | ||
)) | ||
|
||
macro equals*(body) = | ||
result = applyTypeMacro(body, patchTypeSection) |
Oops, something went wrong.