|
| 1 | +import glaze, ./[schema, data], std/[macros, macrocache], cosm/caseutils |
| 2 | + |
| 3 | +type TypeSectionGen = enum Statement, TypeSection, TypeDef |
| 4 | + |
| 5 | +proc wrap(gen: TypeSectionGen, typeSection: NimNode, remaining: seq[NimNode]): NimNode = |
| 6 | + case gen |
| 7 | + of Statement: |
| 8 | + result = newStmtList() |
| 9 | + if not typeSection.isNil: result.add typeSection |
| 10 | + result.add remaining |
| 11 | + of TypeSection: |
| 12 | + result = if typeSection.isNil: newNimNode(nnkTypeSection) else: typeSection |
| 13 | + var val = newNimNode(nnkStmtListType, typeSection) |
| 14 | + val.add remaining |
| 15 | + val.add bindSym"void" |
| 16 | + result.add newTree(nnkTypeDef, genSym(nskType, "_"), newEmptyNode(), val) |
| 17 | + of TypeDef: |
| 18 | + if not typeSection.isNil and typeSection.len == 1: |
| 19 | + result = typeSection[0] |
| 20 | + else: |
| 21 | + var val = newNimNode(nnkStmtListType) |
| 22 | + if not typeSection.isNil: val.add typeSection |
| 23 | + val.add remaining |
| 24 | + val.add bindSym"void" |
| 25 | + result = newTree(nnkTypeDef, genSym(nskType, "_"), newEmptyNode(), val) |
| 26 | + |
| 27 | +type |
| 28 | + FabricKind* = enum |
| 29 | + Warm, Cold |
| 30 | + FabricOptions* = object |
| 31 | + requiresFreeze*: bool |
| 32 | + id*: string |
| 33 | + |
| 34 | +proc processFabricType(node: NimNode): KetenAtomType = |
| 35 | + if node.kind in nnkCallKinds + {nnkBracket} and node.len == 2: |
| 36 | + if node[0].eqIdent"typedesc" or node[0].eqIdent"type": |
| 37 | + return KetenAtomType(kind: TypeAtom, constraint: RawNimNode node[1]) |
| 38 | + elif node[0].eqIdent"static": |
| 39 | + return KetenAtomType(kind: StaticAtom, constraint: RawNimNode node[1]) |
| 40 | + elif node.kind in {nnkIdent, nnkAccQuoted, nnkSym, nnkOpenSymChoice, nnkClosedSymChoice}: |
| 41 | + if node.eqIdent"typedesc" or node.eqIdent"type": |
| 42 | + return KetenAtomType(kind: TypeAtom, constraint: RawNimNode nil) |
| 43 | + elif node.eqIdent"static": |
| 44 | + return KetenAtomType(kind: StaticAtom, constraint: RawNimNode nil) |
| 45 | + result = KetenAtomType(kind: ExprAtom, constraint: RawNimNode node) |
| 46 | + |
| 47 | +proc iterFabricFields(fields: var seq[KetenField], node: NimNode) = |
| 48 | + case node.kind |
| 49 | + of nnkRecList, nnkTupleTy: |
| 50 | + for n in node: |
| 51 | + iterFabricFields(fields, n) |
| 52 | + of nnkRecWhen: |
| 53 | + error("when not supported in fabric", node) |
| 54 | + of nnkRecCase: |
| 55 | + error("case not allowed in fabric", node) |
| 56 | + of nnkIdentDefs: |
| 57 | + let typPos = node.len - 2 |
| 58 | + let typ = processFabricType(node[typPos]) |
| 59 | + for i in 0 ..< typPos: |
| 60 | + var nameNode = node[i] |
| 61 | + if nameNode.kind == nnkPragmaExpr: nameNode = nameNode[0] |
| 62 | + if nameNode.kind == nnkPostfix: nameNode = nameNode[1] |
| 63 | + let name = $nameNode |
| 64 | + let col = fields.len |
| 65 | + fields.add KetenField(column: col, name: name, type: typ, default: RawNimNode(copy node[typPos + 1])) |
| 66 | + of nnkDiscardStmt, nnkNilLit, nnkEmpty: discard |
| 67 | + else: |
| 68 | + error("invalid type ast for fabric: " & $node.kind, node) |
| 69 | + |
| 70 | +proc buildFabric(options: FabricOptions, body: NimNode): tuple[typeSection: NimNode, remaining: seq[NimNode]] = |
| 71 | + if body.kind in {nnkStmtList, nnkTypeSection}: |
| 72 | + for b in body: |
| 73 | + let (types, remaining) = buildFabric(options, b) |
| 74 | + if result.typeSection.isNil: |
| 75 | + result.typeSection = types |
| 76 | + elif result.remaining.len == 0: |
| 77 | + for td in types: result.typeSection.add td |
| 78 | + else: |
| 79 | + result.remaining.add types |
| 80 | + result.remaining.add remaining |
| 81 | + return result |
| 82 | + expectKind body, nnkTypeDef |
| 83 | + var nameNode = body[0] |
| 84 | + if nameNode.kind == nnkPragmaExpr: nameNode = nameNode[0] |
| 85 | + let isPostfix = nameNode.kind == nnkPostfix |
| 86 | + if isPostfix: nameNode = nameNode[1] |
| 87 | + let name = $nameNode |
| 88 | + var schema = KetenSchema(readRequiresFreeze: options.requiresFreeze) |
| 89 | + if body[2].kind != nnkObjectTy: |
| 90 | + error "expected object type for fabric", body[2] |
| 91 | + iterFabricFields(schema.fields, body[2][^1]) |
| 92 | + var rawId = options.id |
| 93 | + if rawId.len == 0: |
| 94 | + rawId = $schemas.len & "-" & toKebabCase(name) |
| 95 | + let id = SchemaId(rawId) |
| 96 | + addSchema id, schema |
| 97 | + var newType = newNimNode(nnkTypeDef, body) |
| 98 | + newType.add body[0] |
| 99 | + newType.add body[1] |
| 100 | + newType.add newTree(nnkObjectTy, newEmptyNode(), newEmptyNode(), newEmptyNode()) |
| 101 | + result.typeSection = newTree(nnkTypeSection, newType) |
| 102 | + result.remaining.add newProc( |
| 103 | + procType = nnkTemplateDef, |
| 104 | + name = |
| 105 | + if isPostfix: newTree(nnkPostfix, ident"*", ident"getFabricSchemaId") |
| 106 | + else: ident"getFabricSchemaId", |
| 107 | + params = [bindSym"SchemaId", |
| 108 | + newTree(nnkIdentDefs, ident"T", newTree(nnkBracketExpr, ident"typedesc", ident name), newEmptyNode())], |
| 109 | + body = newLit id |
| 110 | + ) |
| 111 | + var stitchName = ident"stitch" |
| 112 | + if isPostfix: stitchName = newTree(nnkPostfix, ident"*", stitchName) |
| 113 | + var stitchParams = @[newEmptyNode()] |
| 114 | + stitchParams.add newTree(nnkIdentDefs, ident"T", newTree(nnkBracketExpr, ident"typedesc", ident name), newEmptyNode()) |
| 115 | + result.remaining.add defineRowAdd(id, stitchName, stitchParams) |
| 116 | + |
| 117 | +proc fabricImpl(options: FabricOptions, body: NimNode): NimNode = |
| 118 | + let gen = |
| 119 | + case body.kind |
| 120 | + of nnkTypeDef: |
| 121 | + when (NimMajor, NimMinor) >= (2, 0): TypeSection |
| 122 | + else: TypeDef |
| 123 | + of nnkTypeSection: TypeSection |
| 124 | + else: Statement |
| 125 | + let (typeSection, remaining) = buildFabric(options, body) |
| 126 | + result = wrap(gen, typeSection, remaining) |
| 127 | + |
| 128 | +macro fabric*(option: static tuple[], body: untyped): untyped = |
| 129 | + fabricImpl(FabricOptions(), body) |
| 130 | + |
| 131 | +macro fabric*(option: static FabricKind, body: untyped): untyped = |
| 132 | + fabricImpl(FabricOptions(requiresFreeze: option == Cold), body) |
| 133 | + |
| 134 | +macro fabric*(option: static string, body: untyped): untyped = |
| 135 | + fabricImpl(FabricOptions(id: option), body) |
| 136 | + |
| 137 | +macro fabric*(option: static FabricOptions, body: untyped): untyped = |
| 138 | + fabricImpl(option, body) |
| 139 | + |
| 140 | +macro dispatchImpl(id: static SchemaId, column: untyped, value: typed, toApply: untyped, elses: varargs[untyped]): untyped = |
| 141 | + let schema = getSchema(id) |
| 142 | + let realColumn = |
| 143 | + if column.kind in {nnkIntLit..nnkUint64Lit}: column.intVal.int |
| 144 | + else: getColumn(schema, $column) |
| 145 | + var callPattern = toApply |
| 146 | + result = newStmtList() |
| 147 | + if toApply.kind in {nnkLambda, nnkDo}: |
| 148 | + let (kind, symkind, kindname) = |
| 149 | + if toApply.kind == nnkLambda: (nnkProcDef, nskProc, "Proc") |
| 150 | + else: (nnkTemplateDef, nskTemplate, "Templ") |
| 151 | + var routine = newNimNode(kind, toApply) |
| 152 | + for child in toApply: routine.add child |
| 153 | + let routineName = genSym(symkind, "dispatch" & kindname) |
| 154 | + routine[0] = routineName |
| 155 | + callPattern = routineName |
| 156 | + result.add routine |
| 157 | + var realElse: NimNode = nil |
| 158 | + if elses.len > 0: |
| 159 | + if elses.len != 1: error "only 1 else supported", elses |
| 160 | + if elses[0].kind != nnkElse: error "expected else", elses[0] |
| 161 | + realElse = elses[0][0] |
| 162 | + result.add dispatch(id, realColumn, value, callPattern, realElse) |
| 163 | + |
| 164 | +macro dispatch*(T: typedesc, column: untyped, value: typed, toApply: untyped, elses: varargs[untyped]): untyped = |
| 165 | + result = newCall(bindSym"dispatchImpl", newCall(ident"getFabricSchemaId", T), column, value, toApply) |
| 166 | + for e in elses: result.add e |
0 commit comments