1- import std / [assertions, tables]
2- import " ../../dist/nimony/src/lib" / [bitabs, nifreader, nifstreams, nifcursors]
3- import " .." / [ast, astalgo, idents, lineinfos, options, modulegraphs, pathutils]
1+ import std / [assertions, tables, parseutils ]
2+ import " ../../dist/nimony/src/lib" / [bitabs, nifreader, nifstreams, nifcursors, symparser ]
3+ import " .." / [ast, astalgo, idents, lineinfos, options, modulegraphs, msgs, pathutils]
44import enum2nif
55
66type
77 DecodeContext = object
88 graph: ModuleGraph
9+ nifSymToPSym: Table [string , PSym ] # foo.1.modsuffix -> PSym
910
1011proc nodeKind (n: Cursor ): TNodeKind {.inline .} =
1112 assert n.kind == ParLe
@@ -21,36 +22,87 @@ proc getSysType(typeKind: TTypeKind): PType =
2122 else :
2223 result = PType (itemId: ItemId (module: 0 , item: typeKind.int32 ), kind: typeKind)
2324
25+ type
26+ SplittedNifSym = object
27+ name: string
28+ id: int
29+ module: string
30+
31+ proc splitNifSym (s: string ): SplittedNifSym =
32+ result = SplittedNifSym ()
33+ var i = s.len - 2
34+ var mp = - 1
35+ while i > 0 :
36+ if s[i] == '.' :
37+ if s[i+ 1 ] in {'0' .. '9' }:
38+ var id = 0
39+ discard parseutils.parseInt (s, id, i + 1 )
40+ return SplittedNifSym (
41+ name: s.substr (0 , i - 1 ),
42+ id: id,
43+ module: if mp < 0 : " " else : s.substr (mp, s.high))
44+ else :
45+ mp = i + 1
46+ dec i
47+
2448proc fromNif (c: var DecodeContext ; n: var Cursor ): PNode
2549
26- proc fromNifLocal (c: var DecodeContext ; n: var Cursor ; kind: TNodeKind ): PNode =
27- result = newNodeI (kind, unknownLineInfo, 1 )
28- inc n
29- assert n.nodeKind == nkIdentDefs
30- result [0 ] = newNodeI (nkIdentDefs, unknownLineInfo, 3 )
31- inc n
50+ proc fromNifSymDef (c: var DecodeContext ; n: var Cursor ; kind: TNodeKind ): PNode =
3251 assert n.nodeKind == nkSym
3352 let symKind = case kind:
3453 of nkVarSection: skVar
3554 of nkLetSection: skLet
55+ of nkImportStmt: skModule
3656 else : skConst
3757 inc n
38- assert n.kind == Ident
39- let ident = c.graph.cache.getIdent (pool.strings[n.litId])
58+ assert n.kind == SymbolDef
59+ let nifSym = pool.syms[n.symId]
60+ let symdef = nifSym.splitNifSym
61+ assert symdef.name.len != 0
62+ let ident = c.graph.cache.getIdent (symdef.name)
63+ inc n
64+ assert n.kind in {Ident , DotToken }, $ n.kind
65+ let flags = if n.kind == Ident : pool.strings[n.litId].parseSymFlags else : {}
4066 inc n
41- assert n.kind == IntLit
42- let id = pool.integers[n.intId]
43- result [0 ][0 ] = PSym (itemId: ItemId (module: 0 , item: id.int32 ), kind: symKind, name: ident).newSymNode ()
67+ var position = 0
68+ if symKind == skModule:
69+ assert n.kind == StringLit
70+ let path = pool.strings[n.litId].AbsoluteFile
71+ position = fileInfoIdx (c.graph.config, path).int
72+ else :
73+ assert n.kind == IntLit
74+ position = pool.integers[n.intId]
4475 inc n
76+
77+ let psym = PSym (itemId: ItemId (module: 0 , item: symdef.id.int32 ), kind: symKind, name: ident, flags: flags, position: position)
78+ result = newSymNode (psym)
79+ let hasSym = c.nifSymToPSym.hasKeyOrPut (nifSym, psym)
80+ assert not hasSym
81+
4582 assert n.kind == ParRi
4683 inc n
84+
85+ proc fromNifLocal (c: var DecodeContext ; n: var Cursor ; kind: TNodeKind ): PNode =
86+ result = newNodeI (kind, unknownLineInfo, 1 )
87+ inc n
88+ assert n.nodeKind == nkIdentDefs
89+ result [0 ] = newNodeI (nkIdentDefs, unknownLineInfo, 3 )
90+ inc n
91+ result [0 ][0 ] = fromNifSymDef (c, n, kind)
4792 result [0 ][1 ] = fromNif (c, n)
4893 result [0 ][2 ] = fromNif (c, n)
49- assert n.kind == ParRi
94+ assert n.kind == ParRi # nkIdentDefs
5095 inc n
5196 assert n.kind == ParRi
5297 inc n
5398
99+ proc fromNifImport (c: var DecodeContext ; n: var Cursor ): PNode =
100+ result = newNode (nkImportStmt)
101+ inc n
102+ while n.kind != ParRi :
103+ result .add fromNifSymDef (c, n, nkImportStmt)
104+ inc n
105+
54106proc fromNif (c: var DecodeContext ; n: var Cursor ): PNode =
55107 result = nil
56108 case n.kind:
@@ -60,6 +112,10 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
60112 of IntLit :
61113 result = newIntTypeNode (pool.integers[n.intId], getSysType (tyInt))
62114 inc n
115+ of Symbol :
116+ let sym = c.nifSymToPSym[pool.syms[n.symId]]
117+ result = newSymNode (sym)
118+ inc n
63119 of ParLe :
64120 let kind = n.nodeKind
65121 case kind:
@@ -71,6 +127,8 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
71127 inc n
72128 of nkVarSection, nkLetSection:
73129 result = fromNifLocal (c, n, kind)
130+ of nkImportStmt:
131+ result = fromNifImport (c, n)
74132 else :
75133 assert false , " Not yet implemented " & $ kind
76134 else :
0 commit comments