diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-10-14 22:55:01 -0500 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-10-14 22:55:01 -0500 |
| commit | e293172cf29d45f6c85e982b5ba9df57cb208bbc (patch) | |
| tree | 92a95c60b5b458b5bfecb8fc930fd6ae78940de8 | |
| parent | 3e9dc2fb0fd6257fd86897c1b13f10ed2a5279b4 (diff) | |
| download | nimterop-e293172cf29d45f6c85e982b5ba9df57cb208bbc.tar.gz nimterop-e293172cf29d45f6c85e982b5ba9df57cb208bbc.zip | |
Fix execAction, enum expressions, union cleanup, no forceClean
| -rw-r--r-- | nimterop/build.nim | 3 | ||||
| -rw-r--r-- | nimterop/getters.nim | 54 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 16 | ||||
| -rw-r--r-- | nimterop/paths.nim | 2 | ||||
| -rw-r--r-- | tests/include/test.h | 2 |
5 files changed, 61 insertions, 16 deletions
diff --git a/nimterop/build.nim b/nimterop/build.nim index 81e7445..f3f1b48 100644 --- a/nimterop/build.nim +++ b/nimterop/build.nim @@ -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) = diff --git a/nimterop/getters.nim b/nimterop/getters.nim index d08b318..62dc132 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -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] = diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 57cbbdd..8ddd8c1 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -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: diff --git a/nimterop/paths.nim b/nimterop/paths.nim index d55276e..0ae461f 100644 --- a/nimterop/paths.nim +++ b/nimterop/paths.nim @@ -3,7 +3,7 @@ import os import "."/build const - cacheDir* = getProjectCacheDir("nimterop") + cacheDir* = getProjectCacheDir("nimterop", forceClean = false) proc nimteropRoot*(): string = currentSourcePath.parentDir.parentDir diff --git a/tests/include/test.h b/tests/include/test.h index cb4dd59..df48cd0 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -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; |
