diff options
| author | genotrance <dev@genotrance.com> | 2019-01-30 22:52:49 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-30 22:52:49 -0600 |
| commit | 731398c98860ca0cdf671a38539e48283be04d4d (patch) | |
| tree | 993e00640e4f63ae2fec10bbabc7d00de50e95e9 | |
| parent | 3709628b6627cdf5cfb2931b1b8e88a86c9b5dec (diff) | |
| download | nimterop-731398c98860ca0cdf671a38539e48283be04d4d.tar.gz nimterop-731398c98860ca0cdf671a38539e48283be04d4d.zip | |
Fix #72 (#97)
* Remove enum converter - #72
* Workaround gc crash on Windows
* Suppress hint, import types in toast
| -rw-r--r-- | config.nims | 7 | ||||
| -rw-r--r-- | nimterop/ast.nim | 13 | ||||
| -rw-r--r-- | nimterop/cimport.nim | 2 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 41 | ||||
| -rw-r--r-- | nimterop/setup.nim | 4 | ||||
| -rw-r--r-- | nimterop/treesitter/c.nim | 2 | ||||
| -rw-r--r-- | nimterop/treesitter/cpp.nim | 2 | ||||
| -rw-r--r-- | nimterop/treesitter/runtime.nim | 4 | ||||
| -rw-r--r-- | nimterop/types.nim | 28 |
9 files changed, 69 insertions, 34 deletions
diff --git a/config.nims b/config.nims index 5aac628..d43b04f 100644 --- a/config.nims +++ b/config.nims @@ -1,5 +1,6 @@ #[ -see D20190127T231316 workaround for fact that toast needs to build scanner.cc, which would otherwise result in link erros such as: +see D20190127T231316 workaround for fact that toast needs to build +scanner.cc, which would otherwise result in link errors such as: "std::terminate()", referenced from: ___clang_call_terminate in scanner.cc.o ]# @@ -7,3 +8,7 @@ when defined(MacOSX): switch("clang.linkerexe", "g++") else: switch("gcc.linkerexe", "g++") + +# Workaround for NilAccessError crash on Windows #98 +when defined(Windows): + switch("gc", "markAndSweep")
\ No newline at end of file diff --git a/nimterop/ast.nim b/nimterop/ast.nim index 0c5c4bc..1cc2c6c 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -99,7 +99,7 @@ proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) = if searchAstForNode(ast, node, nimState): ast.tonim(ast, node, nimState) if gStateRT.debug: - nimState.debugStr &= "\n\n# " & nimState.data.join("\n# ") + nimState.debugStr &= "\n# " & nimState.data.join("\n# ") break nimState.data = @[] else: @@ -134,6 +134,9 @@ proc printNimHeader*() = # $2 $3 {.experimental: "codeReordering".} +{.hint[ConvFromXtoItselfNotNeeded]: off.} + +import nimterop/types """ % [$now(), getAppFilename(), commandLineParams().join(" ")] proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = @@ -143,7 +146,7 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = nimState.identifiers = newTable[string, string]() nimState.currentHeader = getCurrentHeader(fullpath) - nimState.constStr &= &" {nimState.currentHeader} = \"{fp}\"\n" + nimState.constStr &= &"\n {nimState.currentHeader} = \"{fp}\"" root.searchAst(astTable, nimState) @@ -151,13 +154,13 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = echo nimState.enumStr if nimState.constStr.nBl: - echo "const\n" & nimState.constStr + echo &"const {nimState.constStr}\n" if nimState.typeStr.nBl: - echo "type\n" & nimState.typeStr + echo &"type {nimState.typeStr}\n" if nimState.procStr.nBl: - echo nimState.procStr + echo &"{nimState.procStr}\n" if gStateRT.debug and nimState.debugStr.nBl: echo nimState.debugStr
\ No newline at end of file diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim index 7fcd14d..298c8a2 100644 --- a/nimterop/cimport.nim +++ b/nimterop/cimport.nim @@ -2,7 +2,7 @@ import hashes, macros, os, strformat, strutils const CIMPORT {.used.} = 1 -import "." / [globals,types,paths] +import "." / [globals, types, paths] export types proc interpPath(dir: string): string= diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index ad7ad9e..6b1e051 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -24,7 +24,7 @@ proc initGrammar(): Grammar = name = nimState.data[0].val.getIdentifier(nskConst) if name.nBl and nimState.identifiers.addNewIdentifer(name): - nimState.constStr &= &" {name}* = {val}\n" + nimState.constStr &= &"\n {name}* = {val}" )) let @@ -148,9 +148,9 @@ proc initGrammar(): Grammar = pout = pout[0 .. ^2] if tptr == "ptr " or typ != "object": - nimState.typeStr &= &" {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}\n" + nimState.typeStr &= &"\n {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}" else: - nimState.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n" + nimState.typeStr &= &"\n {name}* = proc({pout}) {{.nimcall.}}" else: if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]: var @@ -158,12 +158,12 @@ proc initGrammar(): Grammar = if nimState.data[i].name == "identifier": flen = flen.getIdentifier(nskConst, name) - nimState.typeStr &= &" {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]\n" + nimState.typeStr &= &"\n {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]" else: if name == typ: - nimState.typeStr &= &" {name}* = object\n" + nimState.typeStr &= &"\n {name}* = object" else: - nimState.typeStr &= &" {name}* = {getPtrType(tptr&typ)}\n" + nimState.typeStr &= &"\n {name}* = {getPtrType(tptr&typ)}" )) proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) = @@ -179,11 +179,11 @@ proc initGrammar(): Grammar = if ndname.nBl and ndname != nname: if isEnum: if nimState.identifiers.addNewIdentifer(ndname): - nimState.enumStr &= &"type {ndname}* = {dptr}{nname}\n" + nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}" else: if nimState.identifiers.addNewIdentifer(ndname): nimState.typeStr &= - &" {ndname}* {{.importc: \"{dname}\", header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}\n" + &"\n {ndname}* {{.importc: \"{dname}\", header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}" proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) = var @@ -215,9 +215,9 @@ proc initGrammar(): Grammar = if nname.nBl and nimState.identifiers.addNewIdentifer(nname): if nimState.data.len == 1: - nimState.typeStr &= &" {nname}* {{.bycopy.}} = object{union}\n" + nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}" else: - nimState.typeStr &= &" {nname}* {{.importc: \"{prefix}{name}\", header: {nimState.currentHeader}, bycopy.}} = object{union}\n" + nimState.typeStr &= &"\n {nname}* {{.importc: \"{prefix}{name}\", header: {nimState.currentHeader}, bycopy.}} = object{union}" var i = fstart @@ -248,7 +248,7 @@ proc initGrammar(): Grammar = if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals: let flen = nimState.data[i+1].val.getNimExpression() - nimState.typeStr &= &" {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]\n" + nimState.typeStr &= &"\n {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]" i += 2 elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "function_declarator": var @@ -269,15 +269,15 @@ proc initGrammar(): Grammar = if pout.len != 0 and pout[^1] == ',': pout = pout[0 .. ^2] if fptr == "ptr " or ftyp != "object": - nimState.typeStr &= &" {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.nimcall.}}\n" + nimState.typeStr &= &"\n {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.nimcall.}}" else: - nimState.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n" + nimState.typeStr &= &"\n {fname}*: proc({pout}) {{.nimcall.}}" i += 1 else: if ftyp == "object": - nimState.typeStr &= &" {fname}*: pointer\n" + nimState.typeStr &= &"\n {fname}*: pointer" else: - nimState.typeStr &= &" {fname}*: {getPtrType(fptr&ftyp)}\n" + nimState.typeStr &= &"\n {fname}*: {getPtrType(fptr&ftyp)}" i += 1 if node.tsNodeType() == "type_definition" and @@ -364,8 +364,7 @@ proc initGrammar(): Grammar = name.getIdentifier(nskType) if nname.nBl and nimState.identifiers.addNewIdentifer(nname): - nimState.enumStr &= &"\ntype {nname}* = distinct int" - nimState.enumStr &= &"\nconverter enumToInt(en: {nname}): int {{.used.}} = en.int\n" + nimState.enumStr &= &"\ndefineEnum({nname})" var i = fstart @@ -381,7 +380,7 @@ proc initGrammar(): Grammar = if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals: if fname.nBl and nimState.identifiers.addNewIdentifer(fname): - nimState.constStr &= &" {fname}* = ({nimState.data[i+1].val.getNimExpression()}).{nname}\n" + nimState.constStr &= &"\n {fname}* = ({nimState.data[i+1].val.getNimExpression()}).{nname}" try: count = nimState.data[i+1].val.parseInt() + 1 except: @@ -389,7 +388,7 @@ proc initGrammar(): Grammar = i += 2 else: if fname.nBl and nimState.identifiers.addNewIdentifer(fname): - nimState.constStr &= &" {fname}* = {count}.{nname}\n" + nimState.constStr &= &"\n {fname}* = {count}.{nname}" i += 1 count += 1 @@ -494,9 +493,9 @@ proc initGrammar(): Grammar = let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname) if fptr == "ptr " or ftyp != "object": - nimState.procStr &= &"proc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}\n" + nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}" else: - nimState.procStr &= &"proc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}\n" + nimState.procStr &= &"\nproc {fnname}*({pout}) {{.importc: \"{fname}\", header: {nimState.currentHeader}.}}" )) diff --git a/nimterop/setup.nim b/nimterop/setup.nim index 2c1c7c0..3c7afac 100644 --- a/nimterop/setup.nim +++ b/nimterop/setup.nim @@ -1,6 +1,6 @@ import os, strutils -import "."/[git,paths] +import "."/[git, paths] proc treesitterSetup*() = gitPull("https://github.com/tree-sitter/tree-sitter/", incDir() / "treesitter", """ @@ -12,7 +12,7 @@ src/runtime/* *.c *.h """) - + # TODO: does this work on windows? if not use `os.unixToNativePath` let stack = incDir() / "treesitter/src/runtime/stack.c" diff --git a/nimterop/treesitter/c.nim b/nimterop/treesitter/c.nim index b4a8228..f34eaf9 100644 --- a/nimterop/treesitter/c.nim +++ b/nimterop/treesitter/c.nim @@ -1,6 +1,6 @@ import strutils, os -import ".."/[setup,paths] +import ".."/[setup, paths] static: treesitterCSetup() diff --git a/nimterop/treesitter/cpp.nim b/nimterop/treesitter/cpp.nim index c0eca0c..7fc0b13 100644 --- a/nimterop/treesitter/cpp.nim +++ b/nimterop/treesitter/cpp.nim @@ -1,6 +1,6 @@ import strutils, os -import ".."/[setup,paths] +import ".."/[setup, paths] static: treesitterCppSetup() diff --git a/nimterop/treesitter/runtime.nim b/nimterop/treesitter/runtime.nim index acb2289..8885400 100644 --- a/nimterop/treesitter/runtime.nim +++ b/nimterop/treesitter/runtime.nim @@ -2,7 +2,7 @@ import strutils, os -import ".."/[setup,paths] +import ".."/[setup, paths] static: treesitterSetup() @@ -16,7 +16,7 @@ when defined(Linux): {.passC: "-I$1/src" % sourcePath.} {.passC: "-I$1/../utf8proc" % sourcePath.} # pending https://github.com/nim-lang/Nim/issues/10299 we need to rename the -# object files (via compile:(foo,bar)) to avoid name collisions, here +# object files (via compile:(foo,bar)) to avoid name collisions, here # and everywhere `compile` is used {.compile: sourcePath / "src/runtime/runtime.c".} diff --git a/nimterop/types.nim b/nimterop/types.nim index c90f2e4..ecc5345 100644 --- a/nimterop/types.nim +++ b/nimterop/types.nim @@ -31,3 +31,31 @@ when defined(c): elif defined(cpp): type wchar_t* {.importc.} = object + +template enumOp*(op, typ, typout) = + proc op*(x: typ, y: int): typout {.borrow.} + proc op*(x: int, y: typ): typout {.borrow.} + proc op*(x, y: typ): typout {.borrow.} + +template defineEnum*(typ) = + type + typ* = distinct int + + enumOp(`+`, typ, typ) + enumOp(`-`, typ, typ) + enumOp(`*`, typ, typ) + enumOp(`<`, typ, bool) + enumOp(`<=`, typ, bool) + enumOp(`==`, typ, bool) + enumOp(`div`, typ, typ) + enumOp(`mod`, typ, typ) + + proc `shl`*(x: typ, y: int): typ {.borrow.} + proc `shl`*(x: int, y: typ): typ {.borrow.} + proc `shl`*(x, y: typ): typ {.borrow.} + + proc `shr`*(x: typ, y: int): typ {.borrow.} + proc `shr`*(x: int, y: typ): typ {.borrow.} + proc `shr`*(x, y: typ): typ {.borrow.} + + proc `$` *(x: typ): string {.borrow.}
\ No newline at end of file |
