Skip to content

Commit f9b5a0f

Browse files
committed
adds incExpect and skipParRi proc
1 parent f58bc01 commit f9b5a0f

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ proc nodeKind(n: Cursor): TNodeKind {.inline.} =
1717
assert n.kind == ParLe
1818
pool.tags[n.tagId].parseNodeKind()
1919

20+
proc expect(n: Cursor; k: set[NifKind]) =
21+
if n.kind notin k:
22+
when defined(debug):
23+
writeStackTrace()
24+
quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n
25+
26+
proc expect(n: Cursor; k: NifKind) {.inline.} =
27+
expect n, {k}
28+
29+
proc incExpect(n: var Cursor; k: set[NifKind]) =
30+
inc n
31+
expect n, k
32+
33+
proc incExpect(n: var Cursor; k: NifKind) {.inline.} =
34+
incExpect n, {k}
35+
36+
proc skipParRi(n: var Cursor) =
37+
expect n, {ParRi}
38+
inc n
39+
2040
type
2141
SplittedNifSym = object
2242
name: string
@@ -56,14 +76,12 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode
5676
of nkImportStmt: skModule
5777
of nkEnumTy: skEnumField
5878
else: skConst
59-
inc n
60-
assert n.kind == SymbolDef
79+
incExpect n, SymbolDef
6180
let nifSymId = n.symId
6281
let symdef = pool.syms[nifSymId].splitNifSym
6382
assert symdef.name.len != 0
6483
let ident = c.graph.cache.getIdent(symdef.name)
65-
inc n
66-
assert n.kind == IntLit
84+
incExpect n, IntLit
6785
let itemId = pool.integers[n.intId].int32
6886
inc n
6987
assert n.kind in {Symbol, DotToken}, $n.kind
@@ -95,8 +113,7 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode
95113
let hasSym = c.nifSymToPSym.hasKeyOrPut(nifSymId, psym)
96114
assert not hasSym
97115

98-
assert n.kind == ParRi
99-
inc n
116+
skipParRi n
100117

101118
include nifdecodertypes
102119

@@ -107,7 +124,7 @@ proc fromNifNodeFlags(n: var Cursor): set[TNodeFlag] =
107124
assert n.kind == Ident
108125
result = parseNodeFlags(pool.strings[n.litId])
109126
inc n
110-
expect n, ParRi
127+
skipParRi n
111128

112129
proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
113130
result = newNodeI(kind, unknownLineInfo, 1)
@@ -123,10 +140,8 @@ proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
123140
else:
124141
result[0][0].sym.typ = fromNifType(c, n)
125142
result[0][2] = fromNif(c, n)
126-
assert n.kind == ParRi # nkIdentDefs
127-
inc n
128-
assert n.kind == ParRi
129-
inc n
143+
skipParRi n # nkIdentDefs
144+
skipParRi n
130145

131146
proc fromNifTypeSection(c: var DecodeContext; n: var Cursor): PNode =
132147
result = newNodeI(nkTypeDef, unknownLineInfo, 3)
@@ -153,8 +168,7 @@ proc fromNifTypeSection(c: var DecodeContext; n: var Cursor): PNode =
153168
result[2] = newNode(nkEmpty)
154169
sym.sym.typ = fromNifType(c, n)
155170

156-
assert n.kind == ParRi
157-
inc n
171+
skipParRi n
158172

159173
proc fromNifImport(c: var DecodeContext; n: var Cursor): PNode =
160174
result = newNode(nkImportStmt)
@@ -236,8 +250,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
236250
else:
237251
assert false, "invalid node in suf node " & $n.kind
238252
inc n
239-
assert n.kind == ParRi
240-
inc n
253+
skipParRi n
241254

242255
proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
243256
result = nil

compiler/icnif/nifdecodertypes.nim

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# included from nifdecoder.nim
22

3-
proc expect(n: var Cursor; k: NifKind) =
4-
if n.kind == k:
5-
inc n
6-
else:
7-
when defined(debug):
8-
writeStackTrace()
9-
quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n
10-
113
const SysTypeKinds = {tyBool, tyChar, tyString, tyInt .. tyUInt64}
124

135
proc getSysTypeSym(c: var DecodeContext; typeKind: TTypeKind): PSym =
@@ -31,8 +23,7 @@ proc readTypeKind(n: var Cursor; tag: string): TTypeKind =
3123
if tag.len == 1:
3224
case tag[0]
3325
of 'i':
34-
inc n
35-
assert n.kind == IntLit
26+
incExpect n, IntLit
3627
case pool.integers[n.intId]
3728
of -1: result = tyInt
3829
of 8: result = tyInt8
@@ -42,8 +33,7 @@ proc readTypeKind(n: var Cursor; tag: string): TTypeKind =
4233
else: assert false
4334
inc n
4435
of 'u':
45-
inc n
46-
assert n.kind == IntLit
36+
incExpect n, IntLit
4737
case pool.integers[n.intId]
4838
of -1: result = tyUInt
4939
of 8: result = tyUInt8
@@ -53,17 +43,15 @@ proc readTypeKind(n: var Cursor; tag: string): TTypeKind =
5343
else: assert false
5444
inc n
5545
of 'f':
56-
inc n
57-
assert n.kind == IntLit
46+
incExpect n, IntLit
5847
case pool.integers[n.intId]
5948
of -1: result = tyFloat
6049
of 32: result = tyFloat32
6150
of 64: result = tyFloat64
6251
else: assert false
6352
inc n
6453
of 'c':
65-
inc n
66-
assert n.kind == IntLit
54+
incExpect n, IntLit
6755
case pool.integers[n.intId]
6856
of 8: result = tyChar
6957
else: assert false
@@ -83,7 +71,7 @@ proc fromNifTypeImpl(c: var DecodeContext; n: var Cursor; kind: TTypeKind; res:
8371
var sym = fromNifSymDef(c, n, nkEnumTy)
8472
sym.sym.typ = res
8573
res.n.add sym
86-
expect n, ParRi
74+
inc n
8775
of tyFromExpr:
8876
res.n = fromNif(c, n)
8977
of tyStatic:
@@ -96,7 +84,7 @@ proc fromNifTypeImpl(c: var DecodeContext; n: var Cursor; kind: TTypeKind; res:
9684
else:
9785
while n.kind != ParRi:
9886
res.addAllowNil fromNifType(c, n)
99-
expect n, ParRi
87+
inc n
10088

10189
proc fromNifType(c: var DecodeContext; n: var Cursor): PType =
10290
case n.kind
@@ -128,13 +116,11 @@ proc fromNifType(c: var DecodeContext; n: var Cursor): PType =
128116
#echo "Create non SysTypeKinds type: ", k, ": ", tag
129117
result = newType(k, c.idgen, c.owner)
130118
if n.kind == ParLe and pool.tags[n.tag] == "tf":
119+
incExpect n, Ident
120+
result.flags = parseTypeFlags pool.strings[n.litId]
131121
inc n
132-
if n.kind == Ident:
133-
result.flags = parseTypeFlags pool.strings[n.litId]
134-
inc n
135-
else:
136-
expect n, Ident
137-
expect n, ParRi
122+
skipParRi n
138123
fromNifTypeImpl c, n, k, result
139124
else:
140125
expect n, ParLe
126+
inc n

0 commit comments

Comments
 (0)