diff options
| -rw-r--r-- | nimterop/ast.nim | 6 | ||||
| -rw-r--r-- | nimterop/globals.nim | 4 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 18 |
3 files changed, 18 insertions, 10 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index c40e8c8..dce61f9 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -154,6 +154,7 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = nimState.identifiers = newTable[string, string]() nimState.currentHeader = getCurrentHeader(fullpath) + nimState.impHeader = nimState.currentHeader.replace("header", "imp") nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\"" nimState.debug = gStateRT.debug @@ -166,6 +167,11 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) = if nimState.constStr.nBl: echo &"const {nimState.constStr}\n" + echo &""" +{{.pragma: {nimState.impHeader}, importc, header: {nimState.currentHeader}.}} +{{.pragma: {nimState.impHeader}C, {nimState.impHeader}, cdecl.}} +""" + if nimState.typeStr.nBl: echo &"type {nimState.typeStr}\n" diff --git a/nimterop/globals.nim b/nimterop/globals.nim index f866e71..85aa06e 100644 --- a/nimterop/globals.nim +++ b/nimterop/globals.nim @@ -65,7 +65,7 @@ type debug*: bool - currentHeader*: string + currentHeader*, impHeader*: string data*: seq[tuple[name, val: string]] @@ -84,4 +84,4 @@ type CompileMode = enum const modeDefault {.used.} = $cpp # TODO: USE this everywhere relevant when not declared(CIMPORT): - export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, gStateRT, nBl, CompileMode, modeDefault
\ No newline at end of file + export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, gStateRT, nBl, CompileMode, modeDefault diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 5ae8741..11c5f55 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -8,12 +8,12 @@ type Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, nimState: NimState) {.nimcall.}]] proc genImportC(nimState: NimState, origName, nimName: string): string = - result = "importc" if nimName != origName: - result.add &": \"{origName}\"" # used as {.importc: "foo".} - result.add &", header: {nimState.currentHeader}" + result = &"importc: \"{origName}\", header: {nimState.currentHeader}" + else: + result = nimState.impHeader -proc genPragma(pragmas: varargs[string]): string = +proc genPragma(nimState: NimState, pragmas: varargs[string]): string = result = "" for pragma in pragmas.items(): if pragma.len != 0: @@ -21,6 +21,8 @@ proc genPragma(pragmas: varargs[string]): string = if result.len != 0: result = " {." & result[0 .. ^2] & ".}" + result = result.replace(nimState.impHeader & ", cdecl", nimState.impHeader & "C") + proc initGrammar(): Grammar = # #define X Y result.add((""" @@ -170,7 +172,7 @@ proc initGrammar(): Grammar = i += 1 let - pragma = genPragma(nimState.genImportC(name, nname)) + pragma = nimState.genPragma(nimState.genImportC(name, nname)) if typ.nBl and nname.nBl and nimState.identifiers.addNewIdentifer(nname): if i < nimState.data.len and nimState.data[^1].name == "function_declarator": @@ -227,7 +229,7 @@ proc initGrammar(): Grammar = else: if nimState.identifiers.addNewIdentifer(ndname): let - pragma = genPragma(nimState.genImportc(dname, ndname), "bycopy") + pragma = nimState.genPragma(nimState.genImportc(dname, ndname), "bycopy") nimState.typeStr &= &"\n {ndname}*{pragma} = {dptr}{nname}" @@ -267,7 +269,7 @@ proc initGrammar(): Grammar = nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}" else: let - pragma = genPragma(nimState.genImportC(prefix & name, nname), "bycopy") + pragma = nimState.genPragma(nimState.genImportC(prefix & name, nname), "bycopy") nimState.typeStr &= &"\n {nname}*{pragma} = object{union}" var @@ -579,7 +581,7 @@ proc initGrammar(): Grammar = if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname): let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname) - pragma = genPragma(nimState.genImportC(fname, fnname), "cdecl") + pragma = nimState.genPragma(nimState.genImportC(fname, fnname), "cdecl") if fptr.len != 0 or ftyp != "object": nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}" |
