-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinctnnks.nim
95 lines (89 loc) · 3.81 KB
/
distinctnnks.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
import macros,std/genasts
import fusion/astdsl #tiny bit prettier
macro distinctNimNode(names:static varargs[NimNodeKind]) =
result = nnkStmtList.newNimNode
for name in names:
let tmp = genast(name = ident(($name)[3..^1])):
type name = distinct NimNode
#converter `to name`(x:NimNode):name =
# x.expectKind(`nnk name`)
# x.name
#converter toNimNode(x:name):NimNode = x.NimNode
discard
for x in tmp:
result.add x
#echo result.treeRepr
#[type
Infix = distinct NimNode
converter toInfix(x:NimNode):Infix =
x.expectKind(nnkInfix)
x.Infix
converter toNimNode(x:Infix):NimNode = x.NimNode
]#
#expandMacros:
#[
nnkNone, nnkEmpty, nnkIdent, nnkSym, nnkType, nnkCharLit, nnkIntLit,
nnkInt8Lit, nnkInt16Lit, nnkInt32Lit, nnkInt64Lit, nnkUIntLit, nnkUInt8Lit,
nnkUInt16Lit, nnkUInt32Lit, nnkUInt64Lit, nnkFloatLit, nnkFloat32Lit,
nnkFloat64Lit, nnkFloat128Lit, nnkStrLit, nnkRStrLit, nnkTripleStrLit,
nnkNilLit, nnkComesFrom, nnkDotCall, nnkCommand, nnkCall, nnkCallStrLit,
nnkInfix, nnkPrefix, nnkPostfix, nnkHiddenCallConv, nnkExprEqExpr,
nnkExprColonExpr, nnkIdentDefs, nnkVarTuple, nnkPar, nnkObjConstr, nnkCurly,
nnkCurlyExpr, nnkBracket, nnkBracketExpr, nnkPragmaExpr, nnkRange, nnkDotExpr,
nnkCheckedFieldExpr, nnkDerefExpr, nnkIfExpr, nnkElifExpr, nnkElseExpr,
nnkLambda, nnkDo, nnkAccQuoted, nnkTableConstr, nnkBind, nnkClosedSymChoice,
nnkOpenSymChoice, nnkHiddenStdConv, nnkHiddenSubConv, nnkConv, nnkCast,
nnkStaticExpr, nnkAddr, nnkHiddenAddr, nnkHiddenDeref, nnkObjDownConv,
nnkObjUpConv, nnkChckRangeF, nnkChckRange64, nnkChckRange, nnkStringToCString,
nnkCStringToString, nnkAsgn, nnkFastAsgn, nnkGenericParams, nnkFormalParams,
nnkOfInherit, nnkImportAs, nnkProcDef, nnkMethodDef, nnkConverterDef,
nnkMacroDef, nnkTemplateDef, nnkIteratorDef, nnkOfBranch, nnkElifBranch,
nnkExceptBranch, nnkElse, nnkAsmStmt, nnkPragma, nnkPragmaBlock, nnkIfStmt,
nnkWhenStmt, nnkForStmt, nnkParForStmt, nnkWhileStmt, nnkCaseStmt,
nnkTypeSection, nnkVarSection, nnkLetSection, nnkConstSection, nnkConstDef,
nnkTypeDef, nnkYieldStmt, nnkDefer, nnkTryStmt, nnkFinally, nnkRaiseStmt,
nnkReturnStmt, nnkBreakStmt, nnkContinueStmt, nnkBlockStmt, nnkStaticStmt,
nnkDiscardStmt, nnkStmtList, nnkImportStmt, nnkImportExceptStmt,
nnkExportStmt, nnkExportExceptStmt, nnkFromStmt, nnkIncludeStmt, nnkBindStmt,
nnkMixinStmt, nnkUsingStmt, nnkCommentStmt, nnkStmtListExpr, nnkBlockExpr,
nnkStmtListType, nnkBlockType, nnkWith, nnkWithout, nnkTypeOfExpr,
nnkObjectTy, nnkTupleTy, nnkTupleClassTy, nnkTypeClassTy, nnkStaticTy,
nnkRecList, nnkRecCase, nnkRecWhen, nnkRefTy, nnkPtrTy, nnkVarTy, nnkConstTy,
nnkMutableTy, nnkDistinctTy, nnkProcTy, nnkIteratorTy, nnkSharedTy, nnkEnumTy,
nnkEnumFieldDef, nnkArgList, nnkPattern, nnkHiddenTryStmt, nnkClosure,
nnkGotoState, nnkState, nnkBreakState, nnkFuncDef, nnkTupleConstr, nnkError
]#
import std/enumutils,sequtils
distinctNimNode(toSeq(NimNodeKind))
macro dispatchImpl(n:NimNode,b:untyped):untyped =
buildAst(caseStmt):
genAst(n):
n.kind
for k in NimNodeKind:
ofBranch ident($k),
genast(b, n,it = ident(n.strval), name = ident ($k)[3..^1]) do:
var it{.inject.} = name(n)
b
template dispatch(n:NimNode,body:untyped):untyped =
dispatchImpl(n,body)
when isMainModule:
proc test[T](x:T) = echo "unimplemented ", T
proc test(x:Call)
proc test(x:StmtList) =
echo "test StmtList"
for j in x.NimNode:
dispatch(j):
test(j)
proc test(x:Call) =
echo "test Call"
for i in x.NimNode:
i.dispatch:
test(i)
macro foo(x:untyped) =
#echo x.kind
x.dispatch: #TODO: dispatch(x,y...)
test(x)
#echo fooImpl(x).treeRepr
proc bar(y:int):int = y + 1
foo:
bar(7)