Skip to content

Commit d55634f

Browse files
committed
supports string/char/int/uint/float literals
1 parent 1c88bca commit d55634f

File tree

4 files changed

+177
-2
lines changed

4 files changed

+177
-2
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ proc getSysType(typeKind: TTypeKind): PType =
2222
result = sysTypes[typeKind]
2323
else:
2424
result = PType(itemId: ItemId(module: 0, item: typeKind.int32), kind: typeKind)
25+
sysTypes[typeKind] = result
2526

2627
type
2728
SplittedNifSym = object
@@ -122,17 +123,106 @@ proc fromNifImport(c: var DecodeContext; n: var Cursor): PNode =
122123
result.add fromNifSymDef(c, n, nkImportStmt)
123124
inc n
124125

126+
proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
127+
inc n
128+
case n.kind:
129+
of StringLit:
130+
let v = pool.strings[n.litId]
131+
inc n
132+
assert n.kind == StringLit
133+
let suffix = pool.strings[n.litId]
134+
let kind = case suffix
135+
of "R":
136+
nkRStrLit
137+
of "T":
138+
nkTripleStrLit
139+
else:
140+
assert false, "Unknown string literal suffix " & suffix
141+
nkNone
142+
result = newStrNode(kind, v)
143+
of IntLit:
144+
let v = pool.integers[n.intId]
145+
inc n
146+
assert n.kind == StringLit
147+
let suffix = pool.strings[n.litId]
148+
let kind = case suffix
149+
of "i8":
150+
tyInt8
151+
of "i16":
152+
tyInt16
153+
of "i32":
154+
tyInt32
155+
of "i64":
156+
tyInt64
157+
else:
158+
assert false, "Unknown int literal suffix " & suffix
159+
tyNone
160+
result = newIntTypeNode(v, getSysType(kind))
161+
of UIntLit:
162+
let v = pool.uintegers[n.uintId]
163+
inc n
164+
assert n.kind == StringLit
165+
let suffix = pool.strings[n.litId]
166+
let kind = case suffix
167+
of "u8":
168+
tyUInt8
169+
of "u16":
170+
tyUInt16
171+
of "u32":
172+
tyUInt32
173+
of "u64":
174+
tyUInt64
175+
else:
176+
assert false, "Unknown uint literal suffix " & suffix
177+
tyNone
178+
result = newIntTypeNode(cast[BiggestInt](v), getSysType(kind))
179+
of FloatLit:
180+
let v = pool.floats[n.floatId]
181+
inc n
182+
assert n.kind == StringLit
183+
let suffix = pool.strings[n.litId]
184+
let kind = case suffix
185+
of "f32":
186+
(nkFloat32Lit, tyFloat32)
187+
of "f64":
188+
(nkFloat64Lit, tyFloat64)
189+
of "f128":
190+
(nkFloat128Lit, tyFloat128)
191+
else:
192+
assert false, "Unknown uint literal suffix " & suffix
193+
(nkNone, tyNone)
194+
result = newFloatNode(kind[0], v)
195+
result.typ() = getSysType(kind[1])
196+
else:
197+
assert false, "invalid node in suf node " & $n.kind
198+
inc n
199+
assert n.kind == ParRi
200+
inc n
201+
125202
proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
126203
result = nil
127204
case n.kind:
128205
of DotToken:
129206
result = newNode(nkEmpty)
130207
inc n
208+
of Symbol:
209+
result = newSymNode(fromNifSymbol(c, n))
210+
of StringLit:
211+
result = newStrNode(nkStrLit, pool.strings[n.litId])
212+
inc n
213+
of CharLit:
214+
result = newIntNode(nkCharLit, n.charLit.int)
215+
inc n
131216
of IntLit:
132217
result = newIntTypeNode(pool.integers[n.intId], getSysType(tyInt))
133218
inc n
134-
of Symbol:
135-
result = newSymNode(fromNifSymbol(c, n))
219+
of UIntLit:
220+
result = newIntTypeNode(cast[BiggestInt](pool.uintegers[n.uintId]), getSysType(tyUInt))
221+
inc n
222+
of FloatLit:
223+
result = newFloatNode(nkFloatLit, pool.floats[n.floatId])
224+
result.typ() = getSysType(tyFloat)
225+
inc n
136226
of ParLe:
137227
let kind = n.nodeKind
138228
case kind:
@@ -146,6 +236,12 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
146236
result = fromNifLocal(c, n, kind)
147237
of nkImportStmt:
148238
result = fromNifImport(c, n)
239+
of nkNone:
240+
case pool.tags[n.tagId]:
241+
of "suf":
242+
result = fromNifSuf(c, n)
243+
else:
244+
assert false, "Unknown tag " & pool.tags[n.tagId]
149245
else:
150246
assert false, "Not yet implemented " & $kind
151247
else:

compiler/icnif/nifencoder.nim

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ proc modname(c: var EncodeContext; idx: FileIndex): string =
1919
result = moduleSuffix(fp, cast[seq[string]](c.conf.searchPaths))
2020
c.toSuffix[idx] = result
2121

22+
proc addIntLit(b: var Builder; i: int64; suffix: string) =
23+
withTree(b, "suf"):
24+
addIntLit(b, i)
25+
addStrLit(b, suffix)
26+
27+
proc addFloatLit(b: var Builder; f: float; suffix: string) =
28+
withTree(b, "suf"):
29+
addFloatLit(b, f)
30+
addStrLit(b, suffix)
31+
2232
proc toNifSym(c: var EncodeContext; sym: PSym): string =
2333
result = sym.name.s & '.' & $sym.disamb
2434
let owner = sym.skipGenericOwner()
@@ -74,8 +84,42 @@ proc toNif(c: var EncodeContext; n: PNode) =
7484
case n.kind:
7585
of nkEmpty:
7686
c.b.addEmpty()
87+
of nkCharLit:
88+
c.b.addCharLit n.intVal.char
7789
of nkIntLit:
7890
c.b.addIntLit n.intVal
91+
of nkInt8Lit:
92+
c.b.addIntLit n.intVal, "i8"
93+
of nkInt16Lit:
94+
c.b.addIntLit n.intVal, "i16"
95+
of nkInt32Lit:
96+
c.b.addIntLit n.intVal, "i32"
97+
of nkInt64Lit:
98+
c.b.addIntLit n.intVal, "i64"
99+
of nkUIntLit:
100+
c.b.addUIntLit cast[BiggestUInt](n.intVal)
101+
of nkUInt8Lit:
102+
c.b.addUIntLit cast[BiggestUInt](n.intVal), "u8"
103+
of nkUInt16Lit:
104+
c.b.addUIntLit cast[BiggestUInt](n.intVal), "u16"
105+
of nkUInt32Lit:
106+
c.b.addUIntLit cast[BiggestUInt](n.intVal), "u32"
107+
of nkUInt64Lit:
108+
c.b.addUIntLit cast[BiggestUInt](n.intVal), "u64"
109+
of nkFloatLit:
110+
c.b.addFloatLit n.floatVal
111+
of nkFloat32Lit:
112+
c.b.addFloatLit n.floatVal, "f32"
113+
of nkFloat64Lit:
114+
c.b.addFloatLit n.floatVal, "f64"
115+
of nkFloat128Lit:
116+
c.b.addFloatLit n.floatVal, "f128"
117+
of nkStrLit:
118+
c.b.addStrLit n.strVal
119+
of nkRStrLit:
120+
c.b.addStrLit n.strVal, "R"
121+
of nkTripleStrLit:
122+
c.b.addStrLit n.strVal, "T"
79123
of nkIdentDefs:
80124
c.b.addTree toNifTag(n.kind)
81125
assert n.len == 3

tests/icnif/tencode_node2node.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import std/assertions
22
import "../../compiler/icnif" / [nifencoder, nifdecoder]
33
import "../../compiler" / [idents, ast, astalgo, options, pathutils, modulegraphs, modules, msgs, pipelines, syntaxes, sem, llstream, lineinfos]
44

5+
# This test generates PNode by semchecks test code.
6+
# Then it is used to test icnif/nifencoder and nifdecoder.
7+
58
const TestCodeDir = currentSourcePath().AbsoluteFile.splitFile().dir / RelativeDir"testcode"
69

710
proc newConfigRefForTest(): ConfigRef =
811
var conf = newConfigRef()
912
conf.setDefaultLibpath()
1013
conf.searchPaths.add(conf.libpath)
14+
excl(conf.notes, hintProcessing)
15+
excl(conf.mainPackageNotes, hintProcessing)
1116
result = conf
1217

1318
proc newModuleGraphForSem(cache: IdentCache; conf: ConfigRef): ModuleGraph =
@@ -105,9 +110,12 @@ proc testNifEncDec(graph: ModuleGraph; src: string) =
105110
let n2 = loadNifFromBuffer(nif, fullPath, graphForLoad)
106111
#debug(n)
107112
#debug(n2)
113+
#if src == "modtestliterals.nim":
114+
# echo nif
108115
assert eql(n, n2)
109116

110117
var conf = newConfigRefForTest()
111118
var cache = newIdentCache()
112119
var graph = newModuleGraphForSem(cache, conf)
113120
testNifEncDec(graph, "modtest1.nim")
121+
testNifEncDec(graph, "modtestliterals.nim")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var strlit = "test string"
2+
var rstrlit = r"test\t raw"
3+
var triplestrlit = """Triple
4+
string
5+
literal
6+
"""
7+
var charlit = 'a'
8+
var intlit1 = 123456
9+
var intlit2 = -123456
10+
var int8litH = 127'i8
11+
var int8litL = -128'i8
12+
var int16litH = 32767'i16
13+
var int16litL = -32768'i16
14+
var int32litH = 2147483647'i32
15+
var int32litL = -2147483648'i32
16+
var int64litH = 9223372036854775807'i64
17+
var int64litL = -9223372036854775808'i64
18+
19+
var uintlitH = 18446744073709551615'u
20+
var uint8litH = 255'u8
21+
var uint16litH = 65535'u16
22+
var uint32litH = 4294967295'u32
23+
var uint64litH = 18446744073709551615'u64
24+
25+
var floatlit = 1.25
26+
var float32lit = 1.25'f32
27+
var float64lit = 1.25'f64

0 commit comments

Comments
 (0)