Fix #41
This commit is contained in:
parent
dd234ab7ef
commit
47bc610c9a
4 changed files with 33 additions and 6 deletions
|
|
@ -109,7 +109,14 @@ proc getUniqueIdentifier*(existing: HashSet[string], prefix = ""): string =
|
|||
return name & $count
|
||||
|
||||
proc addNewIdentifer*(existing: var HashSet[string], name: string): bool =
|
||||
return not existing.containsOrIncl(name.replace("_", "").toLowerAscii)
|
||||
let
|
||||
nimName =
|
||||
if existing == gStateRT.types:
|
||||
name[0] & name[1 .. ^1].replace("_", "").toLowerAscii
|
||||
else:
|
||||
name.replace("_", "").toLowerAscii
|
||||
|
||||
return not existing.containsOrIncl(nimName)
|
||||
|
||||
proc getPtrType*(str: string): string =
|
||||
result = case str:
|
||||
|
|
|
|||
|
|
@ -171,8 +171,14 @@ proc initGrammar() =
|
|||
let
|
||||
nchild = $node.tsNodeNamedChild(i).tsNodeType()
|
||||
if nchild != "comment":
|
||||
if nchild == "union_specifier":
|
||||
union = " {.union.}"
|
||||
case nchild:
|
||||
of "struct_specifier":
|
||||
if fstart == 1:
|
||||
prefix = "struct "
|
||||
of "union_specifier":
|
||||
if fstart == 1:
|
||||
prefix = "union "
|
||||
union = " {.union.}"
|
||||
break
|
||||
|
||||
if gStateRT.types.addNewIdentifer(nname):
|
||||
|
|
@ -238,6 +244,15 @@ proc initGrammar() =
|
|||
gStateRT.typeStr &= &" {fname}*: {getPtrType(fptr&ftyp)}\n"
|
||||
i += 1
|
||||
|
||||
if node.tsNodeType() == "type_definition" and
|
||||
gStateRT.data[^1].name == "type_identifier":
|
||||
let
|
||||
dname = gStateRT.data[^1].val
|
||||
ndname = gStateRT.data[^1].val.getIdentifier()
|
||||
|
||||
if gStateRT.types.addNewIdentifer(ndname):
|
||||
gStateRT.typeStr &= &" {ndname}* {{.importc: \"{dname}\", header: {gStateRT.currentHeader}, bycopy.}} = {nname}\n"
|
||||
|
||||
let
|
||||
fieldGrammar = &"""
|
||||
(field_identifier!)
|
||||
|
|
@ -296,9 +311,11 @@ proc initGrammar() =
|
|||
if gStateRT.data.len > 1 and
|
||||
gStateRT.data[0].name == "type_identifier" and
|
||||
gStateRT.data[1].name != "field_identifier":
|
||||
offset = 1
|
||||
|
||||
pStructCommon(ast, node, gStateRT.data[^1].val, offset, 1)
|
||||
offset = 1
|
||||
pStructCommon(ast, node, gStateRT.data[0].val, offset, 1)
|
||||
else:
|
||||
pStructCommon(ast, node, gStateRT.data[^1].val, offset, 1)
|
||||
))
|
||||
|
||||
proc pEnumCommon(ast: ref Ast, node: TSNode, name: string, fstart, fend: int) =
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ typedef struct {
|
|||
ENUM4 *field5[TEST_INT];
|
||||
} STRUCT4;
|
||||
|
||||
typedef struct STRUCT5 {
|
||||
typedef struct struct5 {
|
||||
int (*tci)();
|
||||
struct STRUCT1 (*tcp)(int);
|
||||
float (*tcp8)(int *i);
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ var
|
|||
s3: STRUCT3
|
||||
s4: STRUCT4
|
||||
s5: STRUCT5
|
||||
s51: struct5
|
||||
|
||||
e: ENUM
|
||||
e2: ENUM2 = enum5
|
||||
|
|
@ -62,7 +63,9 @@ else: # TODO: what's `defined(cpp)` for c ?
|
|||
s5.tci = test_call_int
|
||||
s5.tcp = test_call_param
|
||||
s5.tcp8 = test_call_param8
|
||||
s51.tci = test_call_int
|
||||
check s5.tci() == 5
|
||||
check s51.tci() == 5
|
||||
|
||||
e = enum1
|
||||
e2 = enum4
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue