aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-01-21 00:17:44 -0600
committerGanesh Viswanathan <dev@genotrance.com>2019-01-21 00:17:44 -0600
commit22d8181073621102cf32c56b08a29bd4dc4f459a (patch)
treebfbe455535a92f5a2e8ac021acc4fd7fe657761d
parent783cf70fbf0ac036289bc6d96294a916c1b063a3 (diff)
downloadnimterop-22d8181073621102cf32c56b08a29bd4dc4f459a.tar.gz
nimterop-22d8181073621102cf32c56b08a29bd4dc4f459a.zip
Fix #50 - ptr object to pointer
-rw-r--r--nimterop/ast.nim12
-rw-r--r--nimterop/getters.nim20
-rw-r--r--nimterop/grammar.nim7
-rw-r--r--tests/include/test.c4
-rw-r--r--tests/include/test.h4
-rw-r--r--tests/tnimterop_c.nim4
6 files changed, 32 insertions, 19 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 6498c93..8191ac6 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -1,4 +1,4 @@
-import sets, strformat, strutils, tables
+import sequtils, sets, strformat, strutils, tables
import regex
@@ -20,8 +20,9 @@ proc saveNodeData(node: TSNode): bool =
val = val.getType()
let
- pname = node.getPName()
- ppname = node.tsNodeParent().getPName()
+ pname = node.getPxName(1)
+ ppname = node.getPxName(2)
+ pppname = node.getPxName(3)
if node.tsNodePrevNamedSibling().tsNodeIsNull():
if pname == "pointer_declarator":
@@ -37,8 +38,9 @@ proc saveNodeData(node: TSNode): bool =
if node.tsNodeType() == "field_identifier" and
pname == "pointer_declarator" and
- ppname == "function_declarator":
- gStateRT.data.add(("function_declarator", ""))
+ ppname == "function_declarator" and
+ pppname == "pointer_declarator":
+ gStateRT.data.insert(("pointer_declarator", ""), gStateRT.data.len-1)
elif name in gExpressions:
if $node.tsNodeParent.tsNodeType() notin gExpressions:
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index cc5b0a2..921467e 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -283,16 +283,14 @@ proc getAstChildByName*(ast: ref Ast, name: string): ref Ast =
if name in ast.children[i].name.split("|"):
return ast.children[i]
-proc getPName*(node: TSNode): string =
- if not node.tsNodeIsNull():
- let
- nparent = node.tsNodeParent()
- if not nparent.tsNodeIsNull():
- return $nparent.tsNodeType()
+proc getPxName*(node: TSNode, offset: int): string =
+ var
+ np = node
+ count = 0
-proc isPName*(node: TSNode, name: string): bool =
- return node.getPName() == name
+ while not np.tsNodeIsNull() and count < offset:
+ np = np.tsNodeParent()
+ count += 1
-proc isPPName*(node: TSNode, name: string): bool =
- if node.getPName().len != 0:
- return node.tsNodeParent().isPName(name)
+ if count == offset and not np.tsNodeIsNull():
+ return $np.tsNodeType()
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 5e37393..692be01 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -137,7 +137,8 @@ proc initGrammar() =
if pout.len != 0 and pout[^1] == ',':
pout = pout[0 .. ^2]
- if typ != "object":
+
+ if tptr == "ptr " or typ != "object":
gStateRT.typeStr &= &" {name}* = proc({pout}): {getPtrType(tptr&typ)} {{.nimcall.}}\n"
else:
gStateRT.typeStr &= &" {name}* = proc({pout}) {{.nimcall.}}\n"
@@ -232,7 +233,7 @@ proc initGrammar() =
if pout.len != 0 and pout[^1] == ',':
pout = pout[0 .. ^2]
- if ftyp != "object":
+ if fptr == "ptr " or ftyp != "object":
gStateRT.typeStr &= &" {fname}*: proc({pout}): {getPtrType(fptr&ftyp)} {{.nimcall.}}\n"
else:
gStateRT.typeStr &= &" {fname}*: proc({pout}) {{.nimcall.}}\n"
@@ -445,7 +446,7 @@ proc initGrammar() =
pout = pout[0 .. ^2]
if gStateRT.procs.addNewIdentifer(fnname):
- if ftyp != "object":
+ if fptr == "ptr " or ftyp != "object":
gStateRT.procStr &= &"proc {fnname}*({pout}): {getPtrType(fptr&ftyp)} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n"
else:
gStateRT.procStr &= &"proc {fnname}*({pout}) {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n"
diff --git a/tests/include/test.c b/tests/include/test.c
index 3a59ffc..162645c 100644
--- a/tests/include/test.c
+++ b/tests/include/test.c
@@ -54,4 +54,8 @@ float test_call_param8(int *param1) {
*param1 = 5 * *param1;
return 1.0 * *param1;
+}
+
+void *test_call9() {
+ return NULL;
} \ No newline at end of file
diff --git a/tests/include/test.h b/tests/include/test.h
index f450814..6c62e2f 100644
--- a/tests/include/test.h
+++ b/tests/include/test.h
@@ -1,3 +1,5 @@
+#include <stddef.h>
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -81,6 +83,7 @@ typedef struct struct5 {
int (*tci)();
struct STRUCT1 (*tcp)(int);
float (*tcp8)(int *i);
+ void *(*tcv)();
} STRUCT5;
union UNION1 {
@@ -102,6 +105,7 @@ union UNION1 test_call_param5(float param1);
unsigned char test_call_param6(UNION2 param1);
int test_call_param7(union UNION1 param1);
float test_call_param8(int *param1);
+void *test_call9();
#ifdef __cplusplus
}
diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim
index e848950..2218256 100644
--- a/tests/tnimterop_c.nim
+++ b/tests/tnimterop_c.nim
@@ -64,8 +64,10 @@ s5.tci = test_call_int
s5.tcp = test_call_param
s5.tcp8 = test_call_param8
s51.tci = test_call_int
+s51.tcv = test_call9
check s5.tci() == 5
check s51.tci() == 5
+check s51.tcv() == nil
e = enum1
e2 = enum4
@@ -97,6 +99,8 @@ else:
check test_call_param8(addr i) == 25.0
check i == 25
+check test_call9() == nil
+
check e3 == enum7
check e4 == enum11