diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-21 13:01:26 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2019-01-21 13:01:26 -0600 |
| commit | f9d0514ad453077f13073562f393921fe49c8ec2 (patch) | |
| tree | 4385b975c225ab0e7ee8172f03757f18c2230624 | |
| parent | a8028a62ecaf642bb2cff06f7533dcd31e843fb5 (diff) | |
| download | nimterop-f9d0514ad453077f13073562f393921fe49c8ec2.tar.gz nimterop-f9d0514ad453077f13073562f393921fe49c8ec2.zip | |
Fix #53 - array size expressions
| -rw-r--r-- | nimterop/getters.nim | 8 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 17 | ||||
| -rw-r--r-- | tests/include/test.h | 2 |
3 files changed, 14 insertions, 13 deletions
diff --git a/nimterop/getters.nim b/nimterop/getters.nim index 921467e..e50e04d 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -294,3 +294,11 @@ 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 ") + ]) diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 979b9f4..28b08d1 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -213,9 +213,9 @@ proc initGrammar() = i += 1 fname = gStateRT.data[i].val.getIdentifier() - if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in ["identifier", "number_literal"]: + if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in gEnumVals: let - flen = gStateRT.data[i+1].val.getIdentifier() + flen = gStateRT.data[i+1].val.getNimExpression() gStateRT.typeStr &= &" {fname}*: {aptr}array[{flen}, {getPtrType(fptr&ftyp)}]\n" i += 2 elif i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name == "function_declarator": @@ -265,7 +265,7 @@ proc initGrammar() = (pointer_declarator (field_identifier) ) - (identifier|number_literal) + (^$1+) ) (function_declarator+ (pointer_declarator @@ -273,7 +273,7 @@ proc initGrammar() = ) {paramListGrammar} ) - """ + """ % gEnumVals.join("|") fieldListGrammar = &""" (field_declaration_list? @@ -347,14 +347,7 @@ proc initGrammar() = if gStateRT.consts.addNewIdentifer(fname): if i+1 < gStateRT.data.len-fend and gStateRT.data[i+1].name in gEnumVals: - gStateRT.data[i+1].val = gStateRT.data[i+1].val.multiReplace([ - (" ", ""), - ("<<", " shl "), (">>", " shr "), - ("^", " xor "), ("&", " and "), ("|", " or "), - ("~", " not ") - ]) - - gStateRT.constStr &= &" {fname}* = ({gStateRT.data[i+1].val}).{nname}\n" + gStateRT.constStr &= &" {fname}* = ({gStateRT.data[i+1].val.getNimExpression()}).{nname}\n" try: count = gStateRT.data[i+1].val.parseInt() + 1 except: diff --git a/tests/include/test.h b/tests/include/test.h index 06eb18e..a8a5b74 100644 --- a/tests/include/test.h +++ b/tests/include/test.h @@ -78,7 +78,7 @@ typedef struct { int field2[TEST_INT]; enum ENUM field3[TEST_INT]; int *field4[TEST_INT]; - ENUM4 *field5[TEST_INT]; + ENUM4 *field5[TEST_INT+TEST_INT]; } STRUCT4; typedef struct struct5 { |
