From ba8c57984bc2e947c1f8a1c22c3df398188959a5 Mon Sep 17 00:00:00 2001 From: Ganesh Viswanathan Date: Mon, 3 Dec 2018 16:17:13 -0600 Subject: Support for unsigned, function params with pointers --- nimterop/ast.nim | 8 +++++++- nimterop/getters.nim | 7 ++++++- 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" )) -- cgit v1.2.3