From e554bd22b4d095a0e0f1d0895a1296b6966c9399 Mon Sep 17 00:00:00 2001 From: Saem Ghani Date: Sun, 9 Jan 2022 02:33:39 -0800 Subject: [PATCH] still broken, but fast! --- compiler/ast/ast.nim | 21 +++++++++------------ compiler/ast/ast_types.nim | 37 ++++++++++--------------------------- compiler/sem/sem.nim | 1 - 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/compiler/ast/ast.nim b/compiler/ast/ast.nim index 200580cc09a..c47e2ed62c7 100644 --- a/compiler/ast/ast.nim +++ b/compiler/ast/ast.nim @@ -343,12 +343,7 @@ proc add*(father, son: Indexable) {.inline.} = template `[]`*(n: Indexable, i: int): Indexable = n.sons[i] template `[]=`*(n: Indexable, i: int; x: Indexable) = - when n is PNode: - var tmp = state.astData[n.id] - tmp[i] = x - state.astData[n.id] = tmp - else: - n.sons[i] = x + n.sons[i] = x template `[]`*(n: Indexable, i: BackwardsIndex): Indexable = n[n.len - i.int] template `[]=`*(n: Indexable, i: BackwardsIndex; x: Indexable) = n[n.len - i.int] = x @@ -411,7 +406,9 @@ template newNodeImpl(kind: TNodeKind, info2: TLineInfo) = of ExtraDataIdentifier: state.nodeIdt.add nil state.nodeList[nodeIdx].extra = ExtraDataId state.nodeIdt.len - of ExtraDataNone: + of ExtraDataAst, ExtraDataNone: + state.astData.add @[] + state.nodeList[nodeIdx].extra = ExtraDataId state.astData.len discard result = PNode(id: nodeId) @@ -839,8 +836,6 @@ proc applyToNode*(src, dest: PNode) = # assert not dest.isNil # assert not src.isNil # assert dest.id != src.id, "applying to self, id: " & $src.id - if state.astData.hasKey(src.id): - state.astData[dest.id] = state.astData[src.id] state.nodeList[dest.idx] = state.nodeList[src.idx] state.nodeFlag[dest.idx] = state.nodeFlag[src.idx] state.nodeInf[dest.idx] = state.nodeInf[src.idx] @@ -859,8 +854,8 @@ proc applyToNode*(src, dest: PNode) = dest.sym = src.sym of ExtraDataIdentifier: dest.ident = src.ident - of ExtraDataNone: - discard + of ExtraDataAst, ExtraDataNone: + dest.sons = src.sons proc copyNode*(src: PNode): PNode = # does not copy its sons! @@ -932,7 +927,9 @@ template transitionNodeKindCommon(k, old: TNodeKind) = for clear in clears.items: case clear - of nodeClearAst: discard # state.astData.del(n.id) + of nodeClearAst: + if oldExtraDataKind == ExtraDataAst: + state.astData[n.extraIdx].setLen(0) of nodeClearFlg: state.nodeFlag[n.idx] = {} of nodeClearInf: state.nodeInf[n.idx] = unknownLineInfo of nodeClearTyp: state.nodeTyp.del(n.id) diff --git a/compiler/ast/ast_types.nim b/compiler/ast/ast_types.nim index f770606d948..fbceee64bb5 100644 --- a/compiler/ast/ast_types.nim +++ b/compiler/ast/ast_types.nim @@ -839,29 +839,13 @@ type extra*: ExtraDataId ## id into extra data about this node, depends on ## `kind`, for lookup of literal, sym, ident, etc - AstTree* = OrderedTable[NodeId, TNodeSeq] - ## store the tree structure for nodes, this is effectively `sons` - - # ExtraData = object - # case kind: ExtraDataKind - # of IntLiteralKind: - # intVal: BiggestInt - # of FloatLiteralKind: - # floatVal: BiggestFloat - # of StringLiteralKind: - # strVal: string - # of SymbolDataKind: - # sym: PSym - # of IdentifierDataKind: - # ident: PIdent - State* = ref object nodeList*: NodeList ## each node, NodeId is their sequence index nodeFlag*: seq[TNodeFlags] ## flags for each node, rarely accessed bloat nodeInf*: seq[TLineInfo] ## info on a per node basis - astData*: AstTree ## actual tree structure for the various AST + # astData*: AstTree ## actual tree structure for the various AST # sparse data, not all nodes have these nodeTyp*: OrderedTable[NodeId, PType] ## types @@ -873,20 +857,16 @@ type nodeInt*: seq[BiggestInt] ## int literals nodeFlt*: seq[BiggestFloat] ## float literals nodeStr*: seq[string] ## string literals + astData*: seq[TNodeSeq] ## the sons for ast nodes - # nodeSym*: OrderedTable[NodeId, PSym] ## symbols - # nodeIdt*: OrderedTable[NodeId, PIdent] ## identifiers - # nodeInt*: OrderedTable[NodeId, BiggestInt] ## int literals - # nodeFlt*: OrderedTable[NodeId, BiggestFloat] ## float literals - # nodeStr*: OrderedTable[NodeId, string] ## string literals - ExtraDataKind* {.pure.} = enum ExtraDataNone, ExtraDataInt, ExtraDataFloat, ExtraDataString, ExtraDataSymbol, - ExtraDataIdentifier + ExtraDataIdentifier, + ExtraDataAst # IDs # xxx: disabled distincts until the basics work @@ -1197,7 +1177,7 @@ func extraDataKind*(k: TNodeKind): ExtraDataKind {.inline.} = of nkStrLit..nkTripleStrLit: ExtraDataString of nkSym: ExtraDataSymbol of nkIdent: ExtraDataIdentifier - else: ExtraDataNone + else: ExtraDataAst const # unknownLineInfoId = InfoId 0 @@ -1331,11 +1311,14 @@ proc sons*(n: PNode): var TNodeSeq {.inline.} = # assert n.kind notin haveNoSons, "not a parent, id: " & $n.id {.cast(noSideEffect).}: # assert n.id.int <= state.nodeList.len, "invalid node id: " & $n.id.int - result = state.astData.mgetOrPut(n.id, @[]) + if n.extraId == nilExtraDataId: + state.astData.add @[] + state.nodeList[n.idx].extra = ExtraDataId state.astData.len + result = state.astData[n.extraId.idx] proc `sons=`*(n: PNode, sons: TNodeSeq) = # assert n.kind notin haveNoSons, "not a parent, id: " & $n.id # assert n.id.int <= state.nodeList.len, "invalid node id: " & $n.id.int - state.astData[n.id] = sons + state.astData[n.extraIdx] = sons proc reportId*(n: PNode): var ReportId {.inline.} = state.nodeRpt[n.id] diff --git a/compiler/sem/sem.nim b/compiler/sem/sem.nim index 9b6221852ab..62d7c064822 100644 --- a/compiler/sem/sem.nim +++ b/compiler/sem/sem.nim @@ -776,7 +776,6 @@ proc myProcess(context: PPassContext, n: PNode): PNode {.nosinks.} = ## from a module as it's parsed and uses the context to accumulate data. var c = PContext(context) # no need for an expensive 'try' if we stop after the first error anyway: - echo "processing: ", c.config.`$`(n.info) if c.config.errorMax <= 1: result = semStmtAndGenerateGenerics(c, n) else: