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.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"

View file

@ -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
export gAtoms, gExpressions, gEnumVals, Kind, Ast, AstTable, State, NimState, gStateRT, nBl, CompileMode, modeDefault

View file

@ -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}"