From 16dd3d0ec9aa7fe33b6e2a2208bc0018dcc1bbcd Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 27 Aug 2024 20:23:41 +0800 Subject: [PATCH 01/11] fixes #34; Remove number and string literal suffixes --- src/nifler/emitter.nim | 78 +++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/src/nifler/emitter.nim b/src/nifler/emitter.nim index 0b3052d3..b0dcef97 100644 --- a/src/nifler/emitter.nim +++ b/src/nifler/emitter.nim @@ -45,24 +45,6 @@ proc addIdent*(em: var Emitter; s: string) = r.add c emit em, r, r.len -proc addStrLit*(em: var Emitter; s, suffix: string) = - var r = "\"" - var l = em.lineLen + 1 - var lastPart = 1 - var afterNewline = false - for c in s: - if c in ControlChars: - r.escape c - inc l, 3 - inc lastPart, 3 - else: - r.add c - inc l, 1 - inc lastPart, 1 - afterNewline = false - r.add "\"" - em.emit r, lastPart - em.emit suffix, suffix.len type @@ -126,6 +108,45 @@ proc addEmpty*(em: var Emitter; count = 1) = for i in 1..count: em.emit ".", 1 +template addSuffixLit(em: var Emitter, suffix: string, body: typed) = + let suffixLit = '"' & suffix & '"' + var a = prepare(em, "suf") + em.addSep a + body + em.addSep a + em.emit suffixLit, suffixLit.len + em.patch(a) + +template addSuffixLitDispatch(em: var Emitter, suffix: string, body: typed) = + if suffix.len > 0: + addSuffixLit(em, suffix): + body + else: + body + + +proc addStrLit*(em: var Emitter; s: string) = + var r = "\"" + var l = em.lineLen + 1 + var lastPart = 1 + var afterNewline = false + for c in s: + if c in ControlChars: + r.escape c + inc l, 3 + inc lastPart, 3 + else: + r.add c + inc l, 1 + inc lastPart, 1 + afterNewline = false + r.add "\"" + em.emit r, lastPart + +proc addStrLit*(em: var Emitter; s, suffix: string) = + addSuffixLitDispatch(em, suffix): + em.addStrLit s + proc addCharLit*(em: var Emitter; c: char) = em.output.add '\'' if c.needsEscape: @@ -141,27 +162,36 @@ template upateLen(body) = body inc em.lineLen, em.output.len - oldLen -proc addIntLit*(em: var Emitter; i: BiggestInt; suffix = "") = +proc addIntLit*(em: var Emitter; i: BiggestInt) = upateLen: em.output.addInt i - em.output.add suffix + +proc addIntLit*(em: var Emitter; i: BiggestInt; suffix: string) = + addSuffixLitDispatch(em, suffix): + addIntLit(em, i) proc addLine*(em: var Emitter; i: int32) = upateLen: em.output.addInt i -proc addUIntLit*(em: var Emitter; u: BiggestUInt; suffix = "") = +proc addUIntLit*(em: var Emitter; u: BiggestUInt) = upateLen: em.output.add $u - em.output.add suffix -proc addFloatLit*(em: var Emitter; f: BiggestFloat; suffix = "") = +proc addUIntLit*(em: var Emitter; u: BiggestUInt; suffix: string) = + addSuffixLitDispatch(em, suffix): + addUIntLit(em, u) + +proc addFloatLit*(em: var Emitter; f: BiggestFloat) = let myLen = em.output.len upateLen: em.output.addFloat f for i in myLen ..< em.output.len: if em.output[i] == 'e': em.output[i] = 'E' - em.output.add suffix + +proc addFloatLit*(em: var Emitter; f: BiggestFloat; suffix: string) = + addSuffixLitDispatch(em, suffix): + addFloatLit(em, f) when isMainModule: var em = Emitter() From c8fe075aac7b063c6c8e54137ee5840e2eb62028 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:37:04 +0800 Subject: [PATCH 02/11] support nifreader --- src/lib/nifreader.nim | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/lib/nifreader.nim b/src/lib/nifreader.nim index a8aeb95f..20c566d4 100644 --- a/src/lib/nifreader.nim +++ b/src/lib/nifreader.nim @@ -230,13 +230,6 @@ proc handleNumber(r: var Reader; result: var Token) = while p < eof and ^p in Digits: inc p inc result.s.len - if p < eof and ^p in NumberSuffixChars: - result.suffix.p = p - if ^p == 'u': result.tk = UIntLit - elif ^p == 'f': result.tk = FloatLit - while p < eof and ^p in NumberSuffixChars: - inc p - inc result.suffix.len proc handleLineInfo(r: var Reader; result: var Token) = useCpuRegisters: @@ -316,6 +309,27 @@ proc next*(r: var Reader): Token = let repl = r.ksubs.getOrDefault(result.s) if repl[0] != UnknownToken: result.s = repl[1] + elif result.s == "suf": + result = next(r) + skipWhitespace(r) + echo result + assert ^r.p == '"' + inc r.p + result.suffix.p = r.p + if result.tk != StringLit: + if ^r.p == 'u': + result.tk = UIntLit + elif ^r.p == 'f': + result.tk = FloatLit + + while true: + inc r.p + inc result.suffix.len + if r.p == eof or ^r.p == '"': break + inc r.p # skip '"' + skipWhitespace(r) + inc r.p # skip ')' + of ')': result.tk = ParRi result.s.p = r.p @@ -343,13 +357,6 @@ proc next*(r: var Reader): Token = inc r.line inc result.s.len inc p - - if p < eof and ^p in StringSuffixChars: - result.suffix.p = p - while true: - inc p - inc result.suffix.len - if p == eof or ^p notin StringSuffixChars: break of '\'': inc r.p result.s.p = r.p From 61763bfb8b1a05e50df9ee38ab4b600f275b912a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:47:50 +0800 Subject: [PATCH 03/11] supports nifbuilder --- src/lib/nifbuilder.nim | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/lib/nifbuilder.nim b/src/lib/nifbuilder.nim index 2ffc3fe1..fc936d23 100644 --- a/src/lib/nifbuilder.nim +++ b/src/lib/nifbuilder.nim @@ -120,7 +120,7 @@ proc addSymbolDef*(b: var Builder; s: string) = else: b.put c -proc addStrLit*(b: var Builder; s: string; suffix = "") = +proc addStrLit*(b: var Builder; s: string) = addSep b b.put '"' for c in s: @@ -129,7 +129,6 @@ proc addStrLit*(b: var Builder; s: string; suffix = "") = else: b.put c b.put '"' - b.put suffix proc addEmpty*(b: var Builder; count = 1) = addSep b @@ -145,17 +144,15 @@ proc addCharLit*(b: var Builder; c: char) = b.put c b.put '\'' -proc addIntLit*(b: var Builder; i: BiggestInt; suffix = "") = +proc addIntLit*(b: var Builder; i: BiggestInt) = addSep b b.put $i - b.put suffix -proc addUIntLit*(b: var Builder; u: BiggestUInt; suffix = "") = +proc addUIntLit*(b: var Builder; u: BiggestUInt) = addSep b b.put $u - b.put suffix -proc addFloatLit*(b: var Builder; f: BiggestFloat; suffix = "") = +proc addFloatLit*(b: var Builder; f: BiggestFloat) = addSep b let myLen = b.buf.len drainPending b @@ -165,7 +162,6 @@ proc addFloatLit*(b: var Builder; f: BiggestFloat; suffix = "") = if b.mode == UsesFile: b.f.write b.buf b.buf.setLen 0 - b.put suffix proc addLineInfo*(b: var Builder; col, line: int32; file = "") = addSep b @@ -228,6 +224,26 @@ template withTree*(b: var Builder; kind: string; body: untyped) = body endTree b +proc addIntLit*(b: var Builder; i: BiggestInt; suffix: string) = + withTree(b, "suf"): + addIntLit(b, i) + addStrLit(b, suffix) + +proc addUIntLit*(b: var Builder; u: BiggestUInt; suffix: string) = + withTree(b, "suf"): + addUIntLit(b, u) + addStrLit(b, suffix) + +proc addFloatLit*(b: var Builder; f: BiggestFloat; suffix: string) = + withTree(b, "suf"): + addFloatLit(b, f) + addStrLit(b, suffix) + +proc addStrLit*(b: var Builder; s: string; suffix: string) = + withTree(b, "suf"): + addStrLit(b, s) + addStrLit(b, suffix) + proc addHeader*(b: var Builder; vendor = "", dialect = "") = b.put "(.nif24)\n" if vendor.len > 0: @@ -259,6 +275,8 @@ when isMainModule: b.addSymbol "foo.3.mymod" b.addIntLit 3423 b.addFloatLit 50.4 + b.addIntLit 3423, "u32" + b.addFloatLit 50.4, "f64" if b.attachedToFile: b.close From 5b7ba47ec280672647d3d6478790fddab269c615 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:49:42 +0800 Subject: [PATCH 04/11] remove echo --- src/lib/nifreader.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/nifreader.nim b/src/lib/nifreader.nim index 20c566d4..395a95da 100644 --- a/src/lib/nifreader.nim +++ b/src/lib/nifreader.nim @@ -312,7 +312,6 @@ proc next*(r: var Reader): Token = elif result.s == "suf": result = next(r) skipWhitespace(r) - echo result assert ^r.p == '"' inc r.p result.suffix.p = r.p From 79934662b58daf6045d4b9bb501dda1529c8de14 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:12:27 +0800 Subject: [PATCH 05/11] handle comments --- src/lib/nifreader.nim | 19 ------------------- src/nifler/emitter.nim | 8 ++++---- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/lib/nifreader.nim b/src/lib/nifreader.nim index 395a95da..0393f6ea 100644 --- a/src/lib/nifreader.nim +++ b/src/lib/nifreader.nim @@ -309,25 +309,6 @@ proc next*(r: var Reader): Token = let repl = r.ksubs.getOrDefault(result.s) if repl[0] != UnknownToken: result.s = repl[1] - elif result.s == "suf": - result = next(r) - skipWhitespace(r) - assert ^r.p == '"' - inc r.p - result.suffix.p = r.p - if result.tk != StringLit: - if ^r.p == 'u': - result.tk = UIntLit - elif ^r.p == 'f': - result.tk = FloatLit - - while true: - inc r.p - inc result.suffix.len - if r.p == eof or ^r.p == '"': break - inc r.p # skip '"' - skipWhitespace(r) - inc r.p # skip ')' of ')': result.tk = ParRi diff --git a/src/nifler/emitter.nim b/src/nifler/emitter.nim index b0dcef97..149b3c64 100644 --- a/src/nifler/emitter.nim +++ b/src/nifler/emitter.nim @@ -125,7 +125,7 @@ template addSuffixLitDispatch(em: var Emitter, suffix: string, body: typed) = body -proc addStrLit*(em: var Emitter; s: string) = +proc addStrLitImpl(em: var Emitter; s: string) = var r = "\"" var l = em.lineLen + 1 var lastPart = 1 @@ -145,7 +145,7 @@ proc addStrLit*(em: var Emitter; s: string) = proc addStrLit*(em: var Emitter; s, suffix: string) = addSuffixLitDispatch(em, suffix): - em.addStrLit s + em.addStrLitImpl s proc addCharLit*(em: var Emitter; c: char) = em.output.add '\'' @@ -197,9 +197,9 @@ when isMainModule: var em = Emitter() var a = prepare(em, "proc") em.addSep a - em.addStrLit "#(escaped?)\n" + em.addStrLit "#(escaped?)\n", "" em.addSep a - em.addStrLit "more here" + em.addStrLit "more here", "" em.patch(a) echo em.output From 4238461d814c48ae938ce50cbfa15029cb1fb037 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:51:46 +0800 Subject: [PATCH 06/11] document NumberWithSuffix --- doc/nif-spec.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/doc/nif-spec.md b/doc/nif-spec.md index fdeff296..1225b852 100644 --- a/doc/nif-spec.md +++ b/doc/nif-spec.md @@ -194,13 +194,15 @@ Grammar: ``` Digit ::= [0-9] -NumberSuffix ::= [a-z]+ [0-9a-z]* # suffixes can only contain lowercase letters +NumberSuffix ::= '"' [a-z]+ [0-9a-z]* '"' # suffixes can only contain lowercase letters FloatingPointPart ::= ('.' Digit+ ('E' '-'? Digit+)? ) | 'E' '-'? Digit+ -Number ::= '-'? Digit+ FloatingPointPart? NumberSuffix? +BasicNumber ::= '-'? Digit+ FloatingPointPart? +NumberWithSuffix = '(' 'suf' BasicNumber NumberSuffix ')' +Number ::= BasicNumber | NumberWithSuffix ``` Numbers must start with a digit (or a minus) and only their decimal notation is supported. Numbers can have -a suffix that has to start with a lowercase letter. For example Nim's `0xff'i32` would become `256i32x`. +a suffix that has to start with a lowercase letter. For example Nim's `0xff'i32` would become `(suf 0xff "i32")`. (The `x` encodes the fact that the number was originally written in hex.) From 6f3fd93a85dceaacf7f14f6990ea53c6042aba12 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 21:56:59 +0800 Subject: [PATCH 07/11] fixes documentation --- doc/nif-spec.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/doc/nif-spec.md b/doc/nif-spec.md index 1225b852..3f6ebe0e 100644 --- a/doc/nif-spec.md +++ b/doc/nif-spec.md @@ -194,7 +194,7 @@ Grammar: ``` Digit ::= [0-9] -NumberSuffix ::= '"' [a-z]+ [0-9a-z]* '"' # suffixes can only contain lowercase letters +NumberSuffix ::= '"' u [0-9a-z]* '"' # suffixes can only contain lowercase letters FloatingPointPart ::= ('.' Digit+ ('E' '-'? Digit+)? ) | 'E' '-'? Digit+ BasicNumber ::= '-'? Digit+ FloatingPointPart? NumberWithSuffix = '(' 'suf' BasicNumber NumberSuffix ')' @@ -202,7 +202,7 @@ Number ::= BasicNumber | NumberWithSuffix ``` Numbers must start with a digit (or a minus) and only their decimal notation is supported. Numbers can have -a suffix that has to start with a lowercase letter. For example Nim's `0xff'i32` would become `(suf 0xff "i32")`. +a suffix that has to start with the letter `u`. For example Nim's `0xff'u32` would become `(suf 0xff "u32")`. (The `x` encodes the fact that the number was originally written in hex.) @@ -224,9 +224,11 @@ Char literals are enclosed in single quotes. The only supported escape sequence Grammar: ``` -StringSuffix ::= Identifier +StringSuffix ::= '"' Identifier "'" EscapedData ::= (VisibleChar | Escape | Whitespace)* -StringLiteral ::= '"' EscapedData '"' StringSuffix? +BasicStringLiteral ::= '"' EscapedData '"' +StringLiteralWithSuffix ::= '(' 'suf' BasicStringLiteral StringSuffix ')' +StringLiteral ::= BasicStringLiteral | StringLiteralWithSuffix ``` String literals are enclosed in double quotes. The only supported escape sequence is `\xx`. @@ -247,7 +249,7 @@ original format of the string. For example, Nim supports "raw string literals" a string literals". These could be modelled as `R` and `T` suffixes: ```nif - "This was a triple quoted Nim string"T + (suf "This was a triple quoted Nim string", "T") ``` From 10bd07192693fa3d540b9f26de3a4c6c2e3feb47 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:04:16 +0800 Subject: [PATCH 08/11] IntLit, FloatLit don't need suffix --- src/lib/nifbuilder.nim | 12 ------------ src/nifler/bridge.nim | 14 +++++++------- src/nifler/emitter.nim | 8 -------- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/lib/nifbuilder.nim b/src/lib/nifbuilder.nim index fc936d23..23c27590 100644 --- a/src/lib/nifbuilder.nim +++ b/src/lib/nifbuilder.nim @@ -224,21 +224,11 @@ template withTree*(b: var Builder; kind: string; body: untyped) = body endTree b -proc addIntLit*(b: var Builder; i: BiggestInt; suffix: string) = - withTree(b, "suf"): - addIntLit(b, i) - addStrLit(b, suffix) - proc addUIntLit*(b: var Builder; u: BiggestUInt; suffix: string) = withTree(b, "suf"): addUIntLit(b, u) addStrLit(b, suffix) -proc addFloatLit*(b: var Builder; f: BiggestFloat; suffix: string) = - withTree(b, "suf"): - addFloatLit(b, f) - addStrLit(b, suffix) - proc addStrLit*(b: var Builder; s: string; suffix: string) = withTree(b, "suf"): addStrLit(b, s) @@ -275,8 +265,6 @@ when isMainModule: b.addSymbol "foo.3.mymod" b.addIntLit 3423 b.addFloatLit 50.4 - b.addIntLit 3423, "u32" - b.addFloatLit 50.4, "f64" if b.attachedToFile: b.close diff --git a/src/nifler/bridge.nim b/src/nifler/bridge.nim index 59aee980..df166d99 100644 --- a/src/nifler/bridge.nim +++ b/src/nifler/bridge.nim @@ -170,16 +170,16 @@ proc toNif*(n, parent: PNode; em: var Emitter; c: var TranslationContext) = em.addIntLit n.intVal of nkInt8Lit: relLineInfo(n, parent, em, c, true) - em.addIntLit n.intVal, "i8" + em.addIntLit n.intVal of nkInt16Lit: relLineInfo(n, parent, em, c, true) - em.addIntLit n.intVal, "i16" + em.addIntLit n.intVal of nkInt32Lit: relLineInfo(n, parent, em, c, true) - em.addIntLit n.intVal, "i32" + em.addIntLit n.intVal of nkInt64Lit: relLineInfo(n, parent, em, c, true) - em.addIntLit n.intVal, "i64" + em.addIntLit n.intVal of nkUIntLit: relLineInfo(n, parent, em, c, true) em.addUIntLit cast[BiggestUInt](n.intVal), "u" @@ -200,13 +200,13 @@ proc toNif*(n, parent: PNode; em: var Emitter; c: var TranslationContext) = em.addFloatLit n.floatVal of nkFloat32Lit: relLineInfo(n, parent, em, c, true) - em.addFloatLit n.floatVal, "f32" + em.addFloatLit n.floatVal of nkFloat64Lit: relLineInfo(n, parent, em, c, true) - em.addFloatLit n.floatVal, "f64" + em.addFloatLit n.floatVal of nkFloat128Lit: relLineInfo(n, parent, em, c, true) - em.addFloatLit n.floatVal, "f128" + em.addFloatLit n.floatVal of nkIdent: relLineInfo(n, parent, em, c, true) em.addIdent n.ident.s diff --git a/src/nifler/emitter.nim b/src/nifler/emitter.nim index 149b3c64..d3b24495 100644 --- a/src/nifler/emitter.nim +++ b/src/nifler/emitter.nim @@ -166,10 +166,6 @@ proc addIntLit*(em: var Emitter; i: BiggestInt) = upateLen: em.output.addInt i -proc addIntLit*(em: var Emitter; i: BiggestInt; suffix: string) = - addSuffixLitDispatch(em, suffix): - addIntLit(em, i) - proc addLine*(em: var Emitter; i: int32) = upateLen: em.output.addInt i @@ -189,10 +185,6 @@ proc addFloatLit*(em: var Emitter; f: BiggestFloat) = for i in myLen ..< em.output.len: if em.output[i] == 'e': em.output[i] = 'E' -proc addFloatLit*(em: var Emitter; f: BiggestFloat; suffix: string) = - addSuffixLitDispatch(em, suffix): - addFloatLit(em, f) - when isMainModule: var em = Emitter() var a = prepare(em, "proc") From e7da10785892061397bb0233f3f00ea87abe2b4e Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:15:23 +0800 Subject: [PATCH 09/11] fixes gear2 bridge --- src/gear2/bridge.nim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gear2/bridge.nim b/src/gear2/bridge.nim index 5a6c5c83..987315e3 100644 --- a/src/gear2/bridge.nim +++ b/src/gear2/bridge.nim @@ -137,16 +137,16 @@ proc toNif*(n, parent: PNode; c: var WContext) = c.b.addIntLit n.intVal of nkInt8Lit: relLineInfo(n, parent, c, true) - c.b.addIntLit n.intVal, "i8" + c.b.addIntLit n.intVal of nkInt16Lit: relLineInfo(n, parent, c, true) - c.b.addIntLit n.intVal, "i16" + c.b.addIntLit n.intVal of nkInt32Lit: relLineInfo(n, parent, c, true) - c.b.addIntLit n.intVal, "i32" + c.b.addIntLit n.intVal of nkInt64Lit: relLineInfo(n, parent, c, true) - c.b.addIntLit n.intVal, "i64" + c.b.addIntLit n.intVal of nkUIntLit: relLineInfo(n, parent, c, true) c.b.addUIntLit cast[BiggestUInt](n.intVal), "u" @@ -167,13 +167,13 @@ proc toNif*(n, parent: PNode; c: var WContext) = c.b.addFloatLit n.floatVal of nkFloat32Lit: relLineInfo(n, parent, c, true) - c.b.addFloatLit n.floatVal, "f32" + c.b.addFloatLit n.floatVal of nkFloat64Lit: relLineInfo(n, parent, c, true) - c.b.addFloatLit n.floatVal, "f64" + c.b.addFloatLit n.floatVal of nkFloat128Lit: relLineInfo(n, parent, c, true) - c.b.addFloatLit n.floatVal, "f128" + c.b.addFloatLit n.floatVal of nkIdent: relLineInfo(n, parent, c, true) c.b.addIdent n.ident.s From eabcad60b2e00fc5bd514991a0c0dc2ec590c03a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:57:08 +0800 Subject: [PATCH 10/11] supports nifc --- src/nifc/nifc_model.nim | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/nifc/nifc_model.nim b/src/nifc/nifc_model.nim index b9114bef..d70894dd 100644 --- a/src/nifc/nifc_model.nim +++ b/src/nifc/nifc_model.nim @@ -6,7 +6,7 @@ ## Parse NIF into a packed tree representation. -import std / [hashes, tables] +import std / [hashes, tables, strutils] import "../lib" / [bitabs, lineinfos, stringviews, packedtrees, nifreader, keymatcher, nifbuilder] @@ -112,6 +112,7 @@ type ImpC = "imp" NodeclC = "nodecl" InclC = "incl" + SufC = "suf" const CallingConventions* = {CdeclC..MemberC} @@ -163,12 +164,23 @@ proc parse*(r: var Reader; dest: var PackedTree[NifcKind]; m: var Module; parent result = false of ParLe: let kind = whichNifcKeyword(t.s, Err) - var d = if kind == TypeC: addr(m.types) else: addr(dest) - copyInto(d[], kind, currentInfo): - while true: - let progress = parse(r, d[], m, currentInfo) - if not progress: break - + if kind == SufC: + let t1 = next(r) + let t2 = next(r) + # currentInfo? + case t1.tk + of StringLit: + dest.addAtom StrLit, m.lits.strings.getOrIncl(decodeStr t1), currentInfo + else: + assert t2.decodeStr.startsWith("u") + dest.addAtom UIntLit, m.lits.strings.getOrIncl(decodeStr t1), currentInfo + discard next(r) + else: + var d = if kind == TypeC: addr(m.types) else: addr(dest) + copyInto(d[], kind, currentInfo): + while true: + let progress = parse(r, d[], m, currentInfo) + if not progress: break of UnknownToken: copyInto dest, Err, currentInfo: dest.addAtom StrLit, m.lits.strings.getOrIncl(decodeStr t), currentInfo From f0c4d731bfdace442149987fb5c5eea52bbe8d78 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 29 Aug 2024 19:23:21 +0800 Subject: [PATCH 11/11] supports SufC --- src/nifc/codegen.nim | 2 +- src/nifc/genexprs.nim | 8 ++++++++ src/nifc/nifc_model.nim | 22 +++++----------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index 5333a07c..7fb8e9cc 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -9,7 +9,7 @@ # We produce C code as a list of tokens. -import std / [assertions, syncio, tables, sets, intsets, formatfloat] +import std / [assertions, syncio, tables, sets, intsets, formatfloat, strutils] from std / strutils import parseBiggestInt, parseBiggestUInt, parseInt from std / os import changeFileExt diff --git a/src/nifc/genexprs.nim b/src/nifc/genexprs.nim index 8c2b53a6..40a0cc54 100644 --- a/src/nifc/genexprs.nim +++ b/src/nifc/genexprs.nim @@ -185,5 +185,13 @@ proc genx(c: var GeneratedCode; t: Tree; n: NodePos) = of LtC: cmpop " < " of CastC: typedUnOp "" of ConvC: typedUnOp "" + of SufC: + let (value, suffix) = sons2(t, n) + case t[value].kind + of StrLit: + c.add makeCString(c.m.lits.strings[t[value].litId]) + else: + assert c.m.lits.strings[t[suffix].litId].startsWith('u') + genUIntLit c, t[value].litId else: genLvalue c, t, n diff --git a/src/nifc/nifc_model.nim b/src/nifc/nifc_model.nim index d70894dd..02dc258d 100644 --- a/src/nifc/nifc_model.nim +++ b/src/nifc/nifc_model.nim @@ -164,23 +164,11 @@ proc parse*(r: var Reader; dest: var PackedTree[NifcKind]; m: var Module; parent result = false of ParLe: let kind = whichNifcKeyword(t.s, Err) - if kind == SufC: - let t1 = next(r) - let t2 = next(r) - # currentInfo? - case t1.tk - of StringLit: - dest.addAtom StrLit, m.lits.strings.getOrIncl(decodeStr t1), currentInfo - else: - assert t2.decodeStr.startsWith("u") - dest.addAtom UIntLit, m.lits.strings.getOrIncl(decodeStr t1), currentInfo - discard next(r) - else: - var d = if kind == TypeC: addr(m.types) else: addr(dest) - copyInto(d[], kind, currentInfo): - while true: - let progress = parse(r, d[], m, currentInfo) - if not progress: break + var d = if kind == TypeC: addr(m.types) else: addr(dest) + copyInto(d[], kind, currentInfo): + while true: + let progress = parse(r, d[], m, currentInfo) + if not progress: break of UnknownToken: copyInto dest, Err, currentInfo: dest.addAtom StrLit, m.lits.strings.getOrIncl(decodeStr t), currentInfo