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 9, 2022
1 parent d7f4f52 commit 9f7c768
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
58 changes: 19 additions & 39 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -785,28 +785,13 @@ type
kind: TNodeKind ## presently the same as the nim node
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 @@ -817,20 +802,16 @@ type
nodeInt: seq[BiggestInt] ## int literals
nodeFlt: seq[BiggestFloat] ## float literals
nodeStr: seq[string] ## string literals

# 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
astData: seq[TNodeSeq] ## the sons for ast nodes

ExtraDataKind {.pure.} = enum
ExtraDataNone,
ExtraDataInt,
ExtraDataFloat,
ExtraDataString,
ExtraDataSymbol,
ExtraDataIdentifier
ExtraDataIdentifier,
ExtraDataAst

# IDs
# xxx: disabled distincts until the basics work
Expand Down Expand Up @@ -1108,7 +1089,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 @@ -1246,11 +1227,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 idToNode*(id: NodeId): PNode {.inline.} =
# assert id.int <= state.nodeList.len, "invalid node id: " & $id.int
Expand Down Expand Up @@ -1559,13 +1543,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
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 @@ -1628,7 +1606,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 @@ -2049,8 +2029,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 @@ -2067,8 +2045,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 @@ -2137,7 +2115,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
1 change: 0 additions & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,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 9f7c768

Please sign in to comment.