-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
--experimental:vmopsDanger
; add generic conversion for vmops (#…
…13813) * add --experimental:vmopsDanger; vmops cleanups
- Loading branch information
1 parent
77834f0
commit b272031
Showing
3 changed files
with
66 additions
and
9 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
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,41 @@ | ||
import ast | ||
|
||
template elementType*(T: typedesc): typedesc = | ||
typeof(block: | ||
var a: T | ||
for ai in a: ai) | ||
|
||
proc fromLit*(a: PNode, T: typedesc): auto = | ||
## generic PNode => type | ||
## see also reverse operation `toLit` | ||
when T is set: | ||
result = default(T) | ||
type Ti = elementType(T) | ||
for ai in a: | ||
result.incl Ti(ai.intVal) | ||
else: | ||
static: doAssert false, "not yet supported: " & $T # add as needed | ||
|
||
proc toLit*[T](a: T): PNode = | ||
## generic type => PNode | ||
## see also reverse operation `fromLit` | ||
when T is string: newStrNode(nkStrLit, a) | ||
elif T is Ordinal: newIntNode(nkIntLit, a.ord) | ||
elif T is (proc): newNode(nkNilLit) | ||
elif T is ref: | ||
if a == nil: newNode(nkNilLit) | ||
else: toLit(a[]) | ||
elif T is tuple: | ||
result = newTree(nkTupleConstr) | ||
for ai in fields(a): result.add toLit(ai) | ||
elif T is object: | ||
result = newTree(nkObjConstr) | ||
result.add(newNode(nkEmpty)) | ||
for k, ai in fieldPairs(a): | ||
let reti = newNode(nkExprColonExpr) | ||
reti.add k.toLit | ||
reti.add ai.toLit | ||
result.add reti | ||
else: | ||
static: doAssert false, "not yet supported: " & $T # add as needed | ||
|
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