Skip to content

Commit

Permalink
still broken, but fast!
Browse files Browse the repository at this point in the history
  • Loading branch information
saem committed Jan 23, 2022
1 parent 2d87d3a commit e554bd2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
21 changes: 9 additions & 12 deletions compiler/ast/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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!
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 10 additions & 27 deletions compiler/ast/ast_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion compiler/sem/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e554bd2

Please sign in to comment.