Fix qualifiers, preproc only literals, remove _, struct field issue, print preproc output

This commit is contained in:
Ganesh Viswanathan 2018-12-04 16:46:16 -06:00
commit 8d9bc5adbd
4 changed files with 27 additions and 13 deletions

View file

@ -19,6 +19,9 @@ proc execCmd(cmd:string)=
task test, "Test": task test, "Test":
execCmd "nim c -r tests/tnimterop_c.nim" execCmd "nim c -r tests/tnimterop_c.nim"
execCmd "nim cpp -r tests/tnimterop_cpp.nim" execCmd "nim cpp -r tests/tnimterop_cpp.nim"
when defined(windows):
execCmd "nim c -r tests/tmath.nim"
execCmd "nim cpp -r tests/tmath.nim"
task installWithDeps, "install dependencies": task installWithDeps, "install dependencies":
for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter", for a in ["http://github.com/genotrance/nimtreesitter?subdir=treesitter",

View file

@ -17,13 +17,18 @@ proc getType*(str: string): string =
return "object" return "object"
result = str.strip(chars={'_'}). result = str.strip(chars={'_'}).
replace("unsigned ", "u"). multiReplace([
("unsigned ", "cu"),
("double ", "cdouble"),
("long ", "clong"),
]).
replace(re"([u]?int[\d]+)_t", "$1") replace(re"([u]?int[\d]+)_t", "$1")
if result == "uchar": case result:
result = "cuchar" of "long":
elif result == "double": result = "clong"
result = "cdouble" of "double":
result = "cdouble"
proc getLit*(str: string): string = proc getLit*(str: string): string =
if str.contains(re"^[\-]?[\d]+$") or if str.contains(re"^[\-]?[\d]+$") or

View file

@ -15,7 +15,7 @@ proc initGrammar() =
proc () {.closure, locks: 0.} = proc () {.closure, locks: 0.} =
let let
name = gStateRT.data[0].val.getIdentifier() name = gStateRT.data[0].val.getIdentifier()
val = gStateRT.data[1].val val = gStateRT.data[1].val.getLit()
if name notin gStateRT.consts and val.nBl: if name notin gStateRT.consts and val.nBl:
gStateRT.consts.add(name) gStateRT.consts.add(name)
@ -44,7 +44,7 @@ proc initGrammar() =
proc () {.closure, locks: 0.} = proc () {.closure, locks: 0.} =
var var
name = gStateRT.data[1].val.getIdentifier() name = gStateRT.data[1].val.getIdentifier()
typ = gStateRT.data[0].val typ = gStateRT.data[0].val.getIdentifier()
if name notin gStateRT.types: if name notin gStateRT.types:
gStateRT.types.add(name) gStateRT.types.add(name)
@ -58,11 +58,14 @@ proc initGrammar() =
gStateRT.types.add(nname) gStateRT.types.add(nname)
gStateRT.typeStr &= &" {nname}* {{.importc: \"{prefix}{name}\", header: {gStateRT.currentHeader}, bycopy.}} = object\n" gStateRT.typeStr &= &" {nname}* {{.importc: \"{prefix}{name}\", header: {gStateRT.currentHeader}, bycopy.}} = object\n"
for i in fstart .. gStateRT.data.len-fend: var
i = fstart
while i < gStateRT.data.len-fend:
let let
ftyp = gStateRT.data[i].val ftyp = gStateRT.data[i].val.getIdentifier()
fname = gStateRT.data[i+1].val.getIdentifier() fname = gStateRT.data[i+1].val.getIdentifier()
gStateRT.typeStr &= &" {fname}*: {ftyp}\n" gStateRT.typeStr &= &" {fname}*: {ftyp}\n"
i += 2
# struct X {} # struct X {}
gStateRT.grammar.add((""" gStateRT.grammar.add(("""
@ -86,7 +89,7 @@ proc initGrammar() =
) )
""", """,
proc () {.closure, locks: 0.} = proc () {.closure, locks: 0.} =
pStructCommon(gStateRT.data[0].val, 1, 2, "struct ") pStructCommon(gStateRT.data[0].val, 1, 0, "struct ")
)) ))
# typedef struct X {} # typedef struct X {}
@ -113,7 +116,7 @@ proc initGrammar() =
) )
""", """,
proc () {.closure, locks: 0.} = proc () {.closure, locks: 0.} =
pStructCommon(gStateRT.data[^1].val, 0, 3) pStructCommon(gStateRT.data[^1].val, 0, 1)
)) ))
proc pEnumCommon(name: string, fstart, fend: int, prefix="") = proc pEnumCommon(name: string, fstart, fend: int, prefix="") =
@ -231,7 +234,7 @@ proc initGrammar() =
""", """,
proc () {.closure, locks: 0.} = proc () {.closure, locks: 0.} =
let let
ftyp = gStateRT.data[0].val ftyp = gStateRT.data[0].val.getIdentifier()
fname = gStateRT.data[1].val fname = gStateRT.data[1].val
fnname = fname.getIdentifier() fnname = fname.getIdentifier()
@ -242,13 +245,14 @@ proc initGrammar() =
if gStateRT.data.len > 2: if gStateRT.data.len > 2:
while i < gStateRT.data.len-1: while i < gStateRT.data.len-1:
let let
ptyp = gStateRT.data[i].val ptyp = gStateRT.data[i].val.getIdentifier()
pname = gStateRT.data[i+1].val.getIdentifier() pname = gStateRT.data[i+1].val.getIdentifier()
pout &= &"{pname}: {ptyp}," pout &= &"{pname}: {ptyp},"
i += 2 i += 2
if pout.len != 0 and pout[^1] == ',': if pout.len != 0 and pout[^1] == ',':
pout = pout[0 .. ^2] pout = pout[0 .. ^2]
gStateRT.procs.add(fnname)
if ftyp != "object": if ftyp != "object":
gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n"
else: else:

View file

@ -100,6 +100,8 @@ proc process(path: string) =
printLisp(root) printLisp(root)
elif gStateRT.pnim: elif gStateRT.pnim:
printNim(path, root) printNim(path, root)
elif gStateRT.preprocess:
echo gStateRT.code
proc main( proc main(
mode = modeDefault, mode = modeDefault,