diff options
| author | Ganesh Viswanathan <dev@genotrance.com> | 2018-12-03 16:17:13 -0600 |
|---|---|---|
| committer | Ganesh Viswanathan <dev@genotrance.com> | 2018-12-03 16:17:13 -0600 |
| commit | ba8c57984bc2e947c1f8a1c22c3df398188959a5 (patch) | |
| tree | 729728e221ec289b9dca106c727b1e8caded6cea | |
| parent | 33956c36ee837603ed94f94b528a3bbb6bd51890 (diff) | |
| download | nimterop-ba8c57984bc2e947c1f8a1c22c3df398188959a5.tar.gz nimterop-ba8c57984bc2e947c1f8a1c22c3df398188959a5.zip | |
Support for unsigned, function params with pointers
| -rw-r--r-- | nimterop/ast.nim | 8 | ||||
| -rw-r--r-- | nimterop/getters.nim | 7 | ||||
| -rw-r--r-- | nimterop/grammar.nim | 35 |
3 files changed, 44 insertions, 6 deletions
diff --git a/nimterop/ast.nim b/nimterop/ast.nim index cce07c3..ca4a563 100644 --- a/nimterop/ast.nim +++ b/nimterop/ast.nim @@ -12,6 +12,7 @@ const gAtoms = @[ "number_literal", "preproc_arg", "primitive_type", + "sized_type_specifier", "type_identifier" ] @@ -21,7 +22,10 @@ proc saveNodeData(node: TSNode): bool = var val = node.getNodeVal() - if name == "primitive_type": + if name == "primitive_type" and node.tsNodeParent.tsNodeType() == "sized_type_specifier": + return true + + if name in ["primitive_type", "sized_type_specifier"]: val = val.getType() if node.tsNodeParent().tsNodeType() == "pointer_declarator": @@ -36,6 +40,8 @@ proc searchAstForNode(ast: ref Ast, node: TSNode): bool = let childNames = node.getTSNodeNamedChildNames().join() + if ast.isNil: + return if ast.children.len != 0: let rstr = ast.getRegexForAstChildren() diff --git a/nimterop/getters.nim b/nimterop/getters.nim index ff8a361..02732e5 100644 --- a/nimterop/getters.nim +++ b/nimterop/getters.nim @@ -13,7 +13,12 @@ proc getIdentifier*(str: string): string = result = str.strip(chars={'_'}) proc getType*(str: string): string = - result = str.strip(chars={'_'}).replace(re"([u]?int[\d]+)_t", "$1").replace(re"^void$", "object") + if str == "void": + return "object" + + result = str.strip(chars={'_'}). + replace("unsigned ", "u"). + replace(re"([u]?int[\d]+)_t", "$1") proc getLit*(str: string): string = if str.contains(re"^[\-]?[\d]+$") or diff --git a/nimterop/grammar.nim b/nimterop/grammar.nim index 4e819d9..1fc45c2 100644 --- a/nimterop/grammar.nim +++ b/nimterop/grammar.nim @@ -15,7 +15,7 @@ proc initGrammar() = name = gStateRT.data[0].val.getIdentifier() val = gStateRT.data[1].val - if name notin gStateRT.consts: + if name notin gStateRT.consts and val.nBl: gStateRT.consts.add(name) gStateRT.constStr &= &" {name}* = {val}\n" )) @@ -27,6 +27,9 @@ proc initGrammar() = gStateRT.grammar.add((""" (type_definition (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) @@ -66,6 +69,9 @@ proc initGrammar() = (field_declaration_list (field_declaration+ (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) @@ -88,6 +94,9 @@ proc initGrammar() = (field_declaration_list (field_declaration+ (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) @@ -163,6 +172,9 @@ proc initGrammar() = gStateRT.grammar.add((""" (declaration (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) @@ -171,13 +183,19 @@ proc initGrammar() = (parameter_list (parameter_declaration* (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) (enum_specifier? (type_identifier) ) - (identifier) + (identifier?) + (pointer_declarator? + (identifier) + ) ) ) ) @@ -187,13 +205,19 @@ proc initGrammar() = (parameter_list (parameter_declaration* (primitive_type|type_identifier?) + (sized_type_specifier? + (primitive_type) + ) (struct_specifier? (type_identifier) ) (enum_specifier? (type_identifier) ) - (identifier) + (identifier?) + (pointer_declarator? + (identifier) + ) ) ) ) @@ -220,7 +244,10 @@ proc initGrammar() = if pout.len != 0 and pout[^1] == ',': pout = pout[0 .. ^2] - gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" + if ftyp != "object": + gStateRT.procStr &= &"proc {fnname}({pout}): {ftyp} {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" + else: + gStateRT.procStr &= &"proc {fnname}({pout}) {{.importc: \"{fname}\", header: {gStateRT.currentHeader}.}}\n" )) |
