aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGanesh Viswanathan <dev@genotrance.com>2019-10-16 17:13:27 -0500
committerGanesh Viswanathan <dev@genotrance.com>2019-10-16 17:13:27 -0500
commit48955b3aa140ee7a903e5bf6bb6e36527a0a8359 (patch)
tree52de8fd21e9fbce6dd4cb38cf8d6b3f86ddb4266
parent531843a82a5efe59c15a675d9c3c8c5f28d4cab0 (diff)
downloadnimterop-48955b3aa140ee7a903e5bf6bb6e36527a0a8359.tar.gz
nimterop-48955b3aa140ee7a903e5bf6bb6e36527a0a8359.zip
Partial fix for #139
-rw-r--r--nimterop/ast.nim15
-rw-r--r--nimterop/globals.nim2
-rw-r--r--nimterop/grammar.nim49
-rw-r--r--tests/include/test.c4
-rw-r--r--tests/include/test.h1
-rw-r--r--tests/tnimterop_c.nim5
6 files changed, 58 insertions, 18 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim
index 240d5fd..b5176fb 100644
--- a/nimterop/ast.nim
+++ b/nimterop/ast.nim
@@ -86,15 +86,30 @@ proc searchAstForNode(ast: ref Ast, node: TSNode, nimState: NimState): bool =
ast.getAstChildByName($nodeChild.tsNodeType())
else:
ast
+
+ if nimState.gState.debug:
+ nimState.nodeBranch.add $node.tsNodeType()
+ echo "#" & spaces(nimState.nodeBranch.len) & nimState.nodeBranch[^1]
+
if not searchAstForNode(astChild, nodeChild, nimState):
+ if nimState.gState.debug:
+ echo "#" & spaces(nimState.nodeBranch.len) & &" {$nodeChild.tsNodeType()} unexpected"
+ discard nimState.nodeBranch.pop()
flag = false
break
+ if nimState.gState.debug:
+ discard nimState.nodeBranch.pop()
+ if nimState.nodeBranch.len == 0:
+ echo ""
+
if flag:
return node.saveNodeData(nimState)
else:
+ echo "#" & spaces(nimState.nodeBranch.len+1) & $node.tsNodeType()
return node.saveNodeData(nimState)
elif node.getTSNodeNamedChildCountSansComments() == 0:
+ echo "#" & spaces(nimState.nodeBranch.len+1) & $node.tsNodeType()
return node.saveNodeData(nimState)
proc searchAst(root: TSNode, astTable: AstTable, nimState: NimState) =
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 32a461c..8fd8fe9 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -72,6 +72,8 @@ type
data*: seq[tuple[name, val: string]]
+ nodeBranch*: seq[string]
+
var
gStateCT {.compiletime, used.} = new(State)
diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim
index 8ddd8c1..bd9a851 100644
--- a/nimterop/grammar.nim
+++ b/nimterop/grammar.nim
@@ -43,6 +43,19 @@ proc initGrammar(): Grammar =
)
"""
+ arrGrammar = &"""
+ (array_declarator!
+ (pointer_declarator!
+ (pointer_declarator!
+ (type_identifier)
+ )
+ (type_identifier)
+ )
+ (type_identifier|identifier)
+ (identifier|number_literal)
+ )
+ """
+
paramListGrammar = &"""
(parameter_list
(parameter_declaration*
@@ -52,10 +65,13 @@ proc initGrammar(): Grammar =
(type_qualifier?)
(pointer_declarator!
(type_qualifier?)
+ {arrGrammar}
(identifier|type_identifier)
)
+ {arrGrammar}
(identifier|type_identifier)
)
+ {arrGrammar}
(abstract_pointer_declarator?
(abstract_pointer_declarator?)
)
@@ -77,20 +93,7 @@ proc initGrammar(): Grammar =
)
"""
- arrGrammar = &"""
- (array_declarator!
- (pointer_declarator!
- (pointer_declarator!
- (type_identifier)
- )
- (type_identifier)
- )
- (type_identifier)
- (identifier|number_literal)
- )
- """
-
- template funcParamCommon(fname, pname, ptyp, pptr, pout, count, i: untyped): untyped =
+ template funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen: untyped): untyped =
ptyp = nimState.getIdentifier(nimState.data[i].val, nskType, fname).getType()
pptr = ""
@@ -106,7 +109,14 @@ proc initGrammar(): Grammar =
count += 1
i += 1
- if pptr.len != 0 or ptyp != "object":
+ if i < nimState.data.len and nimState.data[i].name in ["identifier", "number_literal"]:
+ flen = nimState.data[i].val
+ if nimState.data[i].name == "identifier":
+ flen = nimState.getIdentifier(flen, nskConst, fname)
+
+ pout &= &"{pname}: array[{flen}, {getPtrType(pptr&ptyp)}], "
+ i += 1
+ elif pptr.len != 0 or ptyp != "object":
pout &= &"{pname}: {getPtrType(pptr&ptyp)}, "
# typedef int X
@@ -171,12 +181,13 @@ proc initGrammar(): Grammar =
fname = nname
pout, pname, ptyp, pptr = ""
count = 1
+ flen = ""
while i < nimState.data.len:
if nimState.data[i].name == "function_declarator":
break
- funcParamCommon(fname, pname, ptyp, pptr, pout, count, i)
+ funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]
@@ -277,6 +288,7 @@ proc initGrammar(): Grammar =
ftyp, fname: string
fptr = ""
aptr = ""
+ flen = ""
while i < nimState.data.len-fend:
fptr = ""
aptr = ""
@@ -323,7 +335,7 @@ proc initGrammar(): Grammar =
if nimState.data[i].name == "field_declaration":
break
- funcParamCommon(fname, pname, ptyp, pptr, pout, count, i)
+ funcParamCommon(fname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]
@@ -575,13 +587,14 @@ proc initGrammar(): Grammar =
fnname = nimState.getIdentifier(fname, nskProc)
pout, pname, ptyp, pptr = ""
count = 1
+ flen = ""
i += 1
while i < nimState.data.len:
if nimState.data[i].name == "function_declarator":
break
- funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i)
+ funcParamCommon(fnname, pname, ptyp, pptr, pout, count, i, flen)
if pout.len != 0 and pout[^2 .. ^1] == ", ":
pout = pout[0 .. ^3]
diff --git a/tests/include/test.c b/tests/include/test.c
index 8990df9..18e444f 100644
--- a/tests/include/test.c
+++ b/tests/include/test.c
@@ -64,6 +64,10 @@ void **test_call10(int **param1) {
return NULL;
}
+char *test_array_param(int arr[5]) {
+ return NULL;
+}
+
void multiline1(void) {}
void *multiline2(void) {
diff --git a/tests/include/test.h b/tests/include/test.h
index df48cd0..d062277 100644
--- a/tests/include/test.h
+++ b/tests/include/test.h
@@ -119,6 +119,7 @@ int test_call_param7(union UNION1 param1);
float test_call_param8(int *param1);
void *test_call9();
void **test_call10(int **param1);
+char *test_array_param(int arr[5]);
// Issue #58
void
diff --git a/tests/tnimterop_c.nim b/tests/tnimterop_c.nim
index 1c30387..a289303 100644
--- a/tests/tnimterop_c.nim
+++ b/tests/tnimterop_c.nim
@@ -178,3 +178,8 @@ check TDEFL_OUT_BUF_SIZE == 85196
check TDEFL_BOGUS_1 == 2
check TDEFL_BOGUS_2 == 1024
check TDEFL_BOGUS_3 == (85196 / 2).int
+
+var
+ arr: array[5, cint]
+
+check test_array_param(arr) == nil