Shorten pragmas

This commit is contained in:
Ganesh Viswanathan 2019-05-07 16:52:13 -05:00
commit 2679833d22
3 changed files with 18 additions and 10 deletions

View file

@ -154,6 +154,7 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) =
nimState.identifiers = newTable[string, string]() nimState.identifiers = newTable[string, string]()
nimState.currentHeader = getCurrentHeader(fullpath) nimState.currentHeader = getCurrentHeader(fullpath)
nimState.impHeader = nimState.currentHeader.replace("header", "imp")
nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\"" nimState.constStr &= &"\n {nimState.currentHeader} {{.used.}} = \"{fp}\""
nimState.debug = gStateRT.debug nimState.debug = gStateRT.debug
@ -166,6 +167,11 @@ proc printNim*(fullpath: string, root: TSNode, astTable: AstTable) =
if nimState.constStr.nBl: if nimState.constStr.nBl:
echo &"const {nimState.constStr}\n" echo &"const {nimState.constStr}\n"
echo &"""
{{.pragma: {nimState.impHeader}, importc, header: {nimState.currentHeader}.}}
{{.pragma: {nimState.impHeader}C, {nimState.impHeader}, cdecl.}}
"""
if nimState.typeStr.nBl: if nimState.typeStr.nBl:
echo &"type {nimState.typeStr}\n" echo &"type {nimState.typeStr}\n"

View file

@ -65,7 +65,7 @@ type
debug*: bool debug*: bool
currentHeader*: string currentHeader*, impHeader*: string
data*: seq[tuple[name, val: string]] data*: seq[tuple[name, val: string]]

View file

@ -8,12 +8,12 @@ type
Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, nimState: NimState) {.nimcall.}]] Grammar = seq[tuple[grammar: string, call: proc(ast: ref Ast, node: TSNode, nimState: NimState) {.nimcall.}]]
proc genImportC(nimState: NimState, origName, nimName: string): string = proc genImportC(nimState: NimState, origName, nimName: string): string =
result = "importc"
if nimName != origName: if nimName != origName:
result.add &": \"{origName}\"" # used as {.importc: "foo".} result = &"importc: \"{origName}\", header: {nimState.currentHeader}"
result.add &", header: {nimState.currentHeader}" else:
result = nimState.impHeader
proc genPragma(pragmas: varargs[string]): string = proc genPragma(nimState: NimState, pragmas: varargs[string]): string =
result = "" result = ""
for pragma in pragmas.items(): for pragma in pragmas.items():
if pragma.len != 0: if pragma.len != 0:
@ -21,6 +21,8 @@ proc genPragma(pragmas: varargs[string]): string =
if result.len != 0: if result.len != 0:
result = " {." & result[0 .. ^2] & ".}" result = " {." & result[0 .. ^2] & ".}"
result = result.replace(nimState.impHeader & ", cdecl", nimState.impHeader & "C")
proc initGrammar(): Grammar = proc initGrammar(): Grammar =
# #define X Y # #define X Y
result.add((""" result.add(("""
@ -170,7 +172,7 @@ proc initGrammar(): Grammar =
i += 1 i += 1
let 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 typ.nBl and nname.nBl and nimState.identifiers.addNewIdentifer(nname):
if i < nimState.data.len and nimState.data[^1].name == "function_declarator": if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
@ -227,7 +229,7 @@ proc initGrammar(): Grammar =
else: else:
if nimState.identifiers.addNewIdentifer(ndname): if nimState.identifiers.addNewIdentifer(ndname):
let let
pragma = genPragma(nimState.genImportc(dname, ndname), "bycopy") pragma = nimState.genPragma(nimState.genImportc(dname, ndname), "bycopy")
nimState.typeStr &= nimState.typeStr &=
&"\n {ndname}*{pragma} = {dptr}{nname}" &"\n {ndname}*{pragma} = {dptr}{nname}"
@ -267,7 +269,7 @@ proc initGrammar(): Grammar =
nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}" nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}"
else: else:
let 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}" nimState.typeStr &= &"\n {nname}*{pragma} = object{union}"
var var
@ -579,7 +581,7 @@ proc initGrammar(): Grammar =
if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname): if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname):
let let
ftyp = nimState.data[0].val.getIdentifier(nskType, fnname) 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": if fptr.len != 0 or ftyp != "object":
nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}" nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"