aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Yakimowich-Payne <jyapayne@gmail.com>2020-05-05 20:36:02 -0600
committergenotrance <dev@genotrance.com>2020-05-06 11:08:07 -0500
commitd5c15d0d3aebc1eced79faa0c1621351be56f60a (patch)
tree4251e315edd6c46c138404ad5f46eca4b70f6eff
parent87eef11ec17713c0eb6f5f17c419216efde79fa8 (diff)
downloadnimterop-d5c15d0d3aebc1eced79faa0c1621351be56f60a.tar.gz
nimterop-d5c15d0d3aebc1eced79faa0c1621351be56f60a.zip
Add multiple pointer support
-rw-r--r--nimterop/ast2.nim48
-rw-r--r--nimterop/comphelp.nim50
-rw-r--r--nimterop/exprparser.nim6
-rw-r--r--tests/include/tast2.h1
-rw-r--r--tests/tast2.nim1
5 files changed, 54 insertions, 52 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index fd70b46..4c06cc6 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -8,17 +8,6 @@ import "."/treesitter/api
import "."/[comphelp, exprparser, globals, getters, tshelp]
-proc getPtrType*(str: string): string =
- result = case str:
- of "cchar":
- "cstring"
- of "object":
- "pointer"
- of "FILE":
- "File"
- else:
- str
-
proc getOverrideOrSkip(gState: State, node: TSNode, origname: string, kind: NimSymKind): PNode =
# Check if symbol `origname` of `kind` and `origname` has any cOverride defined
# and use that if present
@@ -405,43 +394,6 @@ proc newXIdent(gState: State, node: TSNode, kind = nskType, fname = "", pragmas:
else:
gecho &"# $1 '{origname}' is duplicate, skipped" % getKeyword(kind)
-proc newPtrTree(gState: State, count: int, typ: PNode): PNode =
- # Create nkPtrTy tree depending on count
- #
- # Reduce by 1 if Nim type available for ptr X - e.g. ptr cchar = cstring
- result = typ
- var
- count = count
- if typ.kind == nkIdent:
- let
- tname = typ.ident.s
- ptname = getPtrType(tname)
- if tname != ptname:
- # If Nim type available, use that ident
- result = gState.getIdent(ptname, typ.info, exported = false)
- # One ptr reduced
- count -= 1
- if count > 0:
- # Nested nkPtrTy(typ) depending on count
- #
- # [ptr ...] typ
- #
- # nkPtrTy(
- # nkPtrTy(
- # typ
- # )
- # )
- var
- nresult = newNode(nkPtrTy)
- parent = nresult
- child: PNode
- for i in 1 ..< count:
- child = newNode(nkPtrTy)
- parent.add child
- parent = child
- parent.add result
- result = nresult
-
proc newArrayTree(gState: State, node: TSNode, typ, size: PNode = nil): PNode =
# Create nkBracketExpr tree depending on input
#
diff --git a/nimterop/comphelp.nim b/nimterop/comphelp.nim
index 1709f8b..18915b0 100644
--- a/nimterop/comphelp.nim
+++ b/nimterop/comphelp.nim
@@ -15,4 +15,52 @@ proc parseString*(gState: State, str: string): PNode =
str, gState.identCache, gState.config, errorHandler = handleError
)
except:
- decho getCurrentExceptionMsg() \ No newline at end of file
+ decho getCurrentExceptionMsg()
+
+proc getPtrType*(str: string): string =
+ result = case str:
+ of "cchar":
+ "cstring"
+ of "object":
+ "pointer"
+ of "FILE":
+ "File"
+ else:
+ str
+
+proc newPtrTree*(gState: State, count: int, typ: PNode): PNode =
+ # Create nkPtrTy tree depending on count
+ #
+ # Reduce by 1 if Nim type available for ptr X - e.g. ptr cchar = cstring
+ result = typ
+ var
+ count = count
+ if typ.kind == nkIdent:
+ let
+ tname = typ.ident.s
+ ptname = getPtrType(tname)
+ if tname != ptname:
+ # If Nim type available, use that ident
+ result = gState.getIdent(ptname, typ.info, exported = false)
+ # One ptr reduced
+ count -= 1
+ if count > 0:
+ # Nested nkPtrTy(typ) depending on count
+ #
+ # [ptr ...] typ
+ #
+ # nkPtrTy(
+ # nkPtrTy(
+ # typ
+ # )
+ # )
+ var
+ nresult = newNode(nkPtrTy)
+ parent = nresult
+ child: PNode
+ for i in 1 ..< count:
+ child = newNode(nkPtrTy)
+ parent.add child
+ parent = child
+ parent.add result
+ result = nresult \ No newline at end of file
diff --git a/nimterop/exprparser.nim b/nimterop/exprparser.nim
index e85310a..5372c2f 100644
--- a/nimterop/exprparser.nim
+++ b/nimterop/exprparser.nim
@@ -550,12 +550,12 @@ proc processTSNode(gState: State, node: TSNode, typeofNode: var PNode): PNode =
result = gState.parseString(node.val)
of "type_descriptor":
let pointerDecl = node.anyChildInTree("abstract_pointer_declarator")
+
if pointerDecl.isNil:
result = gState.processTSNode(node[0], typeofNode)
else:
- result = nkPtrTy.newTree(
- gState.processTSNode(node[0], typeofNode)
- )
+ let pointerCount = pointerDecl.getXCount("abstract_pointer_declarator")
+ result = gState.newPtrTree(pointerCount, gState.processTSNode(node[0], typeofNode))
of "sized_type_specifier", "primitive_type", "type_identifier":
# Input -> int, unsigned int, long int, etc
# Output -> cint, cuint, clong, etc
diff --git a/tests/include/tast2.h b/tests/include/tast2.h
index c7b85c7..0b1fca2 100644
--- a/tests/include/tast2.h
+++ b/tests/include/tast2.h
@@ -21,6 +21,7 @@ extern "C" {
#define COERCE2 645635634896 + 35436ul
#define BINEXPR ~(-(1u << !-1)) ^ (10 >> 1)
#define POINTEREXPR (int*)0
+#define POINTERPOINTERPOINTEREXPR (int***)0
#define BOOL true
#define MATHEXPR (1 + 2/3*20 - 100)
#define ANDEXPR (100 & 11000)
diff --git a/tests/tast2.nim b/tests/tast2.nim
index 4254a9d..6cdd606 100644
--- a/tests/tast2.nim
+++ b/tests/tast2.nim
@@ -151,6 +151,7 @@ assert SHL2 == (1.uint shl 2)
assert SHL3 == (1.uint shl 3)
assert typeof(POINTEREXPR) is (ptr cint)
+assert typeof(POINTERPOINTERPOINTEREXPR) is (ptr ptr ptr cint)
assert ALLSHL == (SHL1 or SHL2 or SHL3)