Pragma cleanup and fixes
This commit is contained in:
parent
69cfce561a
commit
2457671f9e
4 changed files with 38 additions and 16 deletions
|
|
@ -142,7 +142,6 @@ proc printNimHeader*() =
|
||||||
# Command line:
|
# Command line:
|
||||||
# $2 $3
|
# $2 $3
|
||||||
|
|
||||||
{.experimental: "codeReordering".}
|
|
||||||
{.hint[ConvFromXtoItselfNotNeeded]: off.}
|
{.hint[ConvFromXtoItselfNotNeeded]: off.}
|
||||||
|
|
||||||
import nimterop/types
|
import nimterop/types
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,19 @@ import "."/[getters, globals, lisp, treesitter/api]
|
||||||
type
|
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(origName, nimName: string): string =
|
proc genImportC(nimState: NimState, origName, nimName: string): string =
|
||||||
result = "importc"
|
result = "importc"
|
||||||
if nimName != origName:
|
if nimName != origName:
|
||||||
result.add &": \"{origName}\"" # used as {.importc: "foo".}
|
result.add &": \"{origName}\"" # used as {.importc: "foo".}
|
||||||
|
result.add &", header: {nimState.currentHeader}"
|
||||||
|
|
||||||
|
proc genPragma(pragmas: varargs[string]): string =
|
||||||
|
result = ""
|
||||||
|
for pragma in pragmas.items():
|
||||||
|
if pragma.len != 0:
|
||||||
|
result &= pragma & ", "
|
||||||
|
if result.len != 0:
|
||||||
|
result = " {." & result[0 .. ^2] & ".}"
|
||||||
|
|
||||||
proc initGrammar(): Grammar =
|
proc initGrammar(): Grammar =
|
||||||
# #define X Y
|
# #define X Y
|
||||||
|
|
@ -141,6 +150,7 @@ proc initGrammar(): Grammar =
|
||||||
i = 0
|
i = 0
|
||||||
typ = nimState.data[i].val.getIdentifier(nskType)
|
typ = nimState.data[i].val.getIdentifier(nskType)
|
||||||
name = ""
|
name = ""
|
||||||
|
nname = ""
|
||||||
tptr = ""
|
tptr = ""
|
||||||
aptr = ""
|
aptr = ""
|
||||||
|
|
||||||
|
|
@ -155,13 +165,17 @@ proc initGrammar(): Grammar =
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if i < nimState.data.len:
|
if i < nimState.data.len:
|
||||||
name = nimState.data[i].val.getIdentifier(nskType)
|
name = nimState.data[i].val
|
||||||
|
nname = nimState.data[i].val.getIdentifier(nskType)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if typ.nBl and name.nBl and nimState.identifiers.addNewIdentifer(name):
|
let
|
||||||
|
pragma = 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":
|
if i < nimState.data.len and nimState.data[^1].name == "function_declarator":
|
||||||
var
|
var
|
||||||
fname = name
|
fname = nname
|
||||||
pout, pname, ptyp, pptr = ""
|
pout, pname, ptyp, pptr = ""
|
||||||
count = 1
|
count = 1
|
||||||
|
|
||||||
|
|
@ -175,9 +189,9 @@ proc initGrammar(): Grammar =
|
||||||
pout = pout[0 .. ^3]
|
pout = pout[0 .. ^3]
|
||||||
|
|
||||||
if tptr.len != 0 or typ != "object":
|
if tptr.len != 0 or typ != "object":
|
||||||
nimState.typeStr &= &"\n {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}"
|
nimState.typeStr &= &"\n {nname}*{pragma} = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}"
|
||||||
else:
|
else:
|
||||||
nimState.typeStr &= &"\n {name}* = proc({pout}) {{.nimcall.}}"
|
nimState.typeStr &= &"\n {nname}*{pragma} = proc({pout}) {{.nimcall.}}"
|
||||||
else:
|
else:
|
||||||
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
|
if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
|
||||||
var
|
var
|
||||||
|
|
@ -185,12 +199,12 @@ proc initGrammar(): Grammar =
|
||||||
if nimState.data[i].name == "identifier":
|
if nimState.data[i].name == "identifier":
|
||||||
flen = flen.getIdentifier(nskConst, name)
|
flen = flen.getIdentifier(nskConst, name)
|
||||||
|
|
||||||
nimState.typeStr &= &"\n {name}* = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
|
nimState.typeStr &= &"\n {nname}*{pragma} = {aptr}array[{flen}, {getPtrType(tptr&typ)}]"
|
||||||
else:
|
else:
|
||||||
if name == typ:
|
if name == typ:
|
||||||
nimState.typeStr &= &"\n {name}* = object"
|
nimState.typeStr &= &"\n {nname}*{pragma} = object"
|
||||||
else:
|
else:
|
||||||
nimState.typeStr &= &"\n {name}* = {getPtrType(tptr&typ)}"
|
nimState.typeStr &= &"\n {nname}*{pragma} = {getPtrType(tptr&typ)}"
|
||||||
))
|
))
|
||||||
|
|
||||||
proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) =
|
proc pDupTypeCommon(nname: string, fend: int, nimState: NimState, isEnum=false) =
|
||||||
|
|
@ -212,8 +226,10 @@ proc initGrammar(): Grammar =
|
||||||
nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}"
|
nimState.enumStr &= &"\ntype {ndname}* = {dptr}{nname}"
|
||||||
else:
|
else:
|
||||||
if nimState.identifiers.addNewIdentifer(ndname):
|
if nimState.identifiers.addNewIdentifer(ndname):
|
||||||
|
let
|
||||||
|
pragma = genPragma(nimState.genImportc(dname, ndname), "bycopy")
|
||||||
nimState.typeStr &=
|
nimState.typeStr &=
|
||||||
&"\n {ndname}* {{.{genImportC(dname, ndname)}, header: {nimState.currentHeader}, bycopy.}} = {dptr}{nname}"
|
&"\n {ndname}*{pragma} = {dptr}{nname}"
|
||||||
|
|
||||||
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
|
proc pStructCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int, nimState: NimState) =
|
||||||
if nimState.debug:
|
if nimState.debug:
|
||||||
|
|
@ -250,8 +266,9 @@ proc initGrammar(): Grammar =
|
||||||
if nimState.data.len == 1:
|
if nimState.data.len == 1:
|
||||||
nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}"
|
nimState.typeStr &= &"\n {nname}* {{.bycopy.}} = object{union}"
|
||||||
else:
|
else:
|
||||||
let importCDecl = genImportC(prefix & name, nname)
|
let
|
||||||
nimState.typeStr &= &"\n {nname}* {{.{importCDecl}, header: {nimState.currentHeader}, bycopy.}} = object{union}"
|
pragma = genPragma(nimState.genImportC(prefix & name, nname), "bycopy")
|
||||||
|
nimState.typeStr &= &"\n {nname}*{pragma} = object{union}"
|
||||||
|
|
||||||
var
|
var
|
||||||
i = fstart
|
i = fstart
|
||||||
|
|
@ -560,11 +577,14 @@ proc initGrammar(): Grammar =
|
||||||
pout = pout[0 .. ^3]
|
pout = pout[0 .. ^3]
|
||||||
|
|
||||||
if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname):
|
if fnname.nBl and nimState.identifiers.addNewIdentifer(fnname):
|
||||||
let ftyp = nimState.data[0].val.getIdentifier(nskType, fnname)
|
let
|
||||||
|
ftyp = nimState.data[0].val.getIdentifier(nskType, fnname)
|
||||||
|
pragma = genPragma(nimState.genImportC(fname, fnname))
|
||||||
|
|
||||||
if fptr.len != 0 or ftyp != "object":
|
if fptr.len != 0 or ftyp != "object":
|
||||||
nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.{genImportC(fname, fnname)}, header: {nimState.currentHeader}.}}"
|
nimState.procStr &= &"\nproc {fnname}*({pout}): {getPtrType(fptr&ftyp)}{pragma}"
|
||||||
else:
|
else:
|
||||||
nimState.procStr &= &"\nproc {fnname}*({pout}) {{.{genImportC(fname, fnname)}, header: {nimState.currentHeader}.}}"
|
nimState.procStr &= &"\nproc {fnname}*({pout}){pragma}"
|
||||||
))
|
))
|
||||||
|
|
||||||
proc initRegex(ast: ref Ast) =
|
proc initRegex(ast: ref Ast) =
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ extern "C" {
|
||||||
|
|
||||||
typedef uint8_t PRIMTYPE;
|
typedef uint8_t PRIMTYPE;
|
||||||
typedef PRIMTYPE CUSTTYPE;
|
typedef PRIMTYPE CUSTTYPE;
|
||||||
|
typedef CUSTTYPE _CCUSTTYPE_;
|
||||||
|
|
||||||
struct STRUCT0;
|
struct STRUCT0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ block:
|
||||||
var
|
var
|
||||||
pt: PRIMTYPE
|
pt: PRIMTYPE
|
||||||
ct: CUSTTYPE
|
ct: CUSTTYPE
|
||||||
|
cct: CCUSTTYPE
|
||||||
|
|
||||||
s0: STRUCT0
|
s0: STRUCT0
|
||||||
s1: STRUCT1
|
s1: STRUCT1
|
||||||
|
|
@ -64,6 +65,7 @@ var
|
||||||
|
|
||||||
pt = 3
|
pt = 3
|
||||||
ct = 4
|
ct = 4
|
||||||
|
cct = 5
|
||||||
|
|
||||||
s1.field1 = 5
|
s1.field1 = 5
|
||||||
s2.field1 = 6
|
s2.field1 = 6
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue