Fix execAction, enum expressions, union cleanup, no forceClean

This commit is contained in:
Ganesh Viswanathan 2019-10-14 22:55:01 -05:00
commit e293172cf2
5 changed files with 61 additions and 16 deletions

View file

@ -45,7 +45,7 @@ proc execAction*(cmd: string, retry = 0, nostderr = false): string =
sleep(500)
result = execAction(cmd, retry = retry - 1)
else:
doAssert true, "Command failed: " & $(ret, nostderr) & "\ncmd: " & ccmd & "\nresult:\n" & result
doAssert false, "Command failed: " & $(ret, nostderr) & "\ncmd: " & ccmd & "\nresult:\n" & result
proc findExe*(exe: string): string =
## Find the specified executable using the `which`/`where` command - supported
@ -148,6 +148,7 @@ proc getProjectCacheDir*(name: string, forceClean = true): string =
result = getNimteropCacheDir() / name
if forceClean and compileOption("forceBuild"):
echo "# Removing " & result
rmDir(result)
proc extractZip*(zipfile, outdir: string) =

View file

@ -332,12 +332,54 @@ proc getPxName*(node: TSNode, offset: int): string =
if count == offset and not np.tsNodeIsNull():
return $np.tsNodeType()
proc getNimExpression*(expr: string): string =
return expr.multiReplace([
(" ", ""),
("<<", " shl "), (">>", " shr "),
("^", " xor "), ("&", " and "), ("|", " or "),
("~", " not "), ("\n", " "), ("\r", "")
proc getNimExpression*(nimState: NimState, expr: string): string =
var
clean = expr.multiReplace([("\n", " "), ("\r", "")])
ident = ""
gen = ""
hex = false
for i in 0 .. clean.len:
if i != clean.len:
if clean[i] == '_' and ident.len == 0:
gen = $clean[i]
elif clean[i] in IdentChars:
if clean[i] in Digits and ident.len == 0:
gen = $clean[i]
elif clean[i] in HexDigits and hex == true:
gen = $clean[i]
elif i > 0 and i < clean.len-1 and clean[i] in ['x', 'X'] and
clean[i-1] == '0' and clean[i+1] in HexDigits:
gen = $clean[i]
hex = true
else:
ident &= clean[i]
hex = false
else:
gen = (block:
if (i == 0 or clean[i-1] != '\'') or
(i == clean.len - 1 or clean[i+1] != '\''):
case clean[i]
of '^': " xor "
of '&': " and "
of '|': " or "
of '~': " not "
else: $clean[i]
else:
$clean[i]
)
hex = false
if i == clean.len or gen.len != 0:
if ident.len != 0:
ident = nimState.getIdentifier(ident, nskConst)
result &= ident
ident = ""
result &= gen
gen = ""
result = result.multiReplace([
("<<", " shl "), (">>", " shr ")
])
proc getSplitComma*(joined: seq[string]): seq[string] =

View file

@ -238,7 +238,7 @@ proc initGrammar(): Grammar =
prefix = "struct "
of "union_specifier":
prefix = "union "
union = " {.union.}"
union = ", union"
of "type_definition":
if node.getTSNodeNamedChildCountSansComments() != 0:
for i in 0 .. node.tsNodeNamedChildCount()-1:
@ -252,23 +252,25 @@ proc initGrammar(): Grammar =
of "union_specifier":
if fstart == 1:
prefix = "union "
union = " {.union.}"
union = ", union"
break
if nname.nBl and nimState.addNewIdentifer(nname):
if nimState.data.len == 1:
nimState.typeStr &= &"{nimState.getComments()}\n {nname}* {{.bycopy.}} = object{union}"
nimState.typeStr &= &"{nimState.getComments()}\n {nname}* {{.bycopy{union}.}} = object"
else:
var
pragmas: seq[string] = @[]
if nimState.gState.dynlib.len == 0:
pragmas.add nimState.getImportC(prefix & name, nname)
pragmas.add "bycopy"
if union.len != 0:
pragmas.add "union"
let
pragma = nimState.getPragma(pragmas)
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = object{union}"
nimState.typeStr &= &"{nimState.getComments()}\n {nname}*{pragma} = object"
var
i = fstart
@ -283,7 +285,7 @@ proc initGrammar(): Grammar =
continue
if nimState.data[i].name notin ["field_identifier", "pointer_declarator", "array_pointer_declarator"]:
ftyp = nimState.data[i].val.getType()
ftyp = nimState.getIdentifier(nimState.data[i].val, nskType, nname).getType()
i += 1
while i < nimState.data.len-fend and "pointer" in nimState.data[i].name:
@ -299,7 +301,7 @@ proc initGrammar(): Grammar =
if i+1 < nimState.data.len-fend and nimState.data[i+1].name in gEnumVals:
let
flen = nimState.data[i+1].val.getNimExpression()
flen = nimState.getNimExpression(nimState.data[i+1].val)
nimState.typeStr &= &"{nimState.getComments()}\n {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]"
i += 2
elif i+1 < nimState.data.len-fend and nimState.data[i+1].name == "bitfield_clause":
@ -461,7 +463,7 @@ proc initGrammar(): Grammar =
if i+1 < nimState.data.len-fend and
nimState.data[i+1].name in gEnumVals:
if fname.nBl and nimState.addNewIdentifer(fname):
nimState.constStr &= &"{nimState.getComments()}\n {fname}* = ({nimState.data[i+1].val.getNimExpression()}).{nname}"
nimState.constStr &= &"{nimState.getComments()}\n {fname}* = ({nimState.getNimExpression(nimState.data[i+1].val)}).{nname}"
try:
count = nimState.data[i+1].val.parseInt() + 1
except:

View file

@ -3,7 +3,7 @@ import os
import "."/build
const
cacheDir* = getProjectCacheDir("nimterop")
cacheDir* = getProjectCacheDir("nimterop", forceClean = false)
proc nimteropRoot*(): string =
currentSourcePath.parentDir.parentDir

View file

@ -87,7 +87,7 @@ typedef struct {
int field2[TEST_INT];
enum ENUM field3[TEST_INT];
int *field4[TEST_INT];
ENUM4 *field5[TEST_INT+TEST_INT];
ENUM4 *field5[TEST_INT + TEST_INT];
int field6 : 1;
} STRUCT4;