Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Jul 18, 2024
1 parent dab0b8d commit adc23b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions doc/nif-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ code describes. The values are vendor specific. For Nim the possible values are:
| --------------- | --------------------------------------------------------------------------- |
| `"nim-parsed"` | Parsed Nim code without semantic checking. |
| `"nim-sem"` | Parsed Nim code after semantic checking. |
| `"nim-inlined"` | Nim code after inlining. |
| `"nim-arc"` | Nim code after destructor injections (final step before code generation). |
| `"nim-gear3"` | Nim code after inlining. |
| `"nim-gear4"` | Nim code after destructor injections (final step before code generation). |
| `"nif-c"` | NIF code that is very close to C code. |


Expand Down
48 changes: 48 additions & 0 deletions src/nim_sem/bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,61 @@ proc toNifDecl(n, parent: PNode; c: var TranslationContext) =
else:
toNif n, parent, c

proc magicToKind(m: TMagic): string =
case m
of mNone: "<cannot happen>"
of mArray: "array"
of mOpenArray: "oarray"
of mRange: "range"
of mSet: "set"
of mSeq: "seq"
of mVarargs: "varargs"
of mRef: "ref"
of mPtr: "ptr"
of mVar: "mut"
of mVoid, mVoidType: "void"
of mIterableType: "iterable"
of mType: "typeof"
of mUncheckedArray: "uarray"
of mAppendStrCh: "addc"
of mAppendStrStr: "adds"
of mAppendSeqElem: "adde"
of mInSet: "contains"
of mSetLengthStr: "ssetlen"
of mSetLengthSeq: "qsetlen"
of mLengthOpenArray: "olen"
of mLengthStr: "slen"
of mLengthArray: "alen"
of mLengthSeq: "qlen"
else:
let s = $m
if s.endsWith"F64":
s.substr(1, s.len-4).toLowerAscii
else:
s.substr(1, s.len-1).toLowerAscii

proc magicCall(m: TMagic; n: PNode; c: var TranslationContext) =
c.b.addTree(magicToKind(m))
for i in 1..<n.len:
toNif(n[i], n, c)
c.b.endTree

proc toNif*(n, parent: PNode; c: var TranslationContext) =
case n.kind
of nkNone, nkEmpty:
c.b.addEmpty 1
of nkSym:
relLineInfo(n, parent, c)
symToNif(n, parent, c)
of nkCommand, nkCall, nkCallStrLit, nkInfix, nkPrefix, nkPostfix, nkHiddenCallConv:
relLineInfo(n, parent, c)
if n.len > 0 and n[0].kind == nkSym and n[0].sym.magic != mNone:
magicCall n[0].sym.magic, n, c
else:
c.b.addTree(nodeKindTranslation(n.kind))
for i in 0..<n.len:
toNif(n[i], n, c)
c.b.endTree
of nkNilLit:
relLineInfo(n, parent, c)
c.b.addKeyw "nil"
Expand Down

0 comments on commit adc23b3

Please sign in to comment.