Fix #50 - ptr object to pointer

This commit is contained in:
Ganesh Viswanathan 2019-01-21 00:17:44 -06:00
commit 22d8181073
6 changed files with 32 additions and 19 deletions

View file

@ -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:

View file

@ -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()

View file

@ -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"

View file

@ -54,4 +54,8 @@ float test_call_param8(int *param1) {
*param1 = 5 * *param1;
return 1.0 * *param1;
}
void *test_call9() {
return NULL;
}

View file

@ -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
}

View file

@ -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