Compare commits
4 commits
master
...
fix_pointe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c763fc4d3 | ||
|
|
baaba7881b | ||
|
|
0ff18278f0 | ||
|
|
e69aafa40d |
5 changed files with 75 additions and 51 deletions
|
|
@ -8,17 +8,6 @@ import "."/treesitter/api
|
||||||
|
|
||||||
import "."/[comphelp, exprparser, globals, getters, tshelp]
|
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 =
|
proc getOverrideOrSkip(gState: State, node: TSNode, origname: string, kind: NimSymKind): PNode =
|
||||||
# Check if symbol `origname` of `kind` and `origname` has any cOverride defined
|
# Check if symbol `origname` of `kind` and `origname` has any cOverride defined
|
||||||
# and use that if present
|
# and use that if present
|
||||||
|
|
@ -117,7 +106,7 @@ proc newConstDef(gState: State, node: TSNode, fname = "", fval = ""): PNode =
|
||||||
if not maybeTyNode.isNil:
|
if not maybeTyNode.isNil:
|
||||||
let name = maybeTyNode.getName()
|
let name = maybeTyNode.getName()
|
||||||
case name
|
case name
|
||||||
of "type_descriptor", "sized_type_specifier":
|
of "type_descriptor", "sized_type_specifier", "primitive_type", "type_identifier":
|
||||||
discard
|
discard
|
||||||
else:
|
else:
|
||||||
# Can't do gState.parseCExpression(root) here for some reason?
|
# Can't do gState.parseCExpression(root) here for some reason?
|
||||||
|
|
@ -405,43 +394,6 @@ proc newXIdent(gState: State, node: TSNode, kind = nskType, fname = "", pragmas:
|
||||||
else:
|
else:
|
||||||
gecho &"# $1 '{origname}' is duplicate, skipped" % getKeyword(kind)
|
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 =
|
proc newArrayTree(gState: State, node: TSNode, typ, size: PNode = nil): PNode =
|
||||||
# Create nkBracketExpr tree depending on input
|
# Create nkBracketExpr tree depending on input
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,52 @@ proc parseString*(gState: State, str: string): PNode =
|
||||||
str, gState.identCache, gState.config, errorHandler = handleError
|
str, gState.identCache, gState.config, errorHandler = handleError
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
decho getCurrentExceptionMsg()
|
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
|
||||||
|
|
@ -548,7 +548,26 @@ proc processTSNode(gState: State, node: TSNode, typeofNode: var PNode): PNode =
|
||||||
# Input -> true, false
|
# Input -> true, false
|
||||||
# Output -> true, false
|
# Output -> true, false
|
||||||
result = gState.parseString(node.val)
|
result = gState.parseString(node.val)
|
||||||
of "type_descriptor", "sized_type_specifier":
|
of "type_descriptor":
|
||||||
|
# Input => int*
|
||||||
|
# (type_descriptor 1 2 4 "int*"
|
||||||
|
# (type_identifier 1 2 3 "int")
|
||||||
|
# (abstract_pointer_declarator 1 3 1 "*")
|
||||||
|
# )
|
||||||
|
#
|
||||||
|
# Output => ptr int
|
||||||
|
#
|
||||||
|
# nkPtrTy(
|
||||||
|
# nkIdent("int")
|
||||||
|
# )
|
||||||
|
let pointerDecl = node.anyChildInTree("abstract_pointer_declarator")
|
||||||
|
|
||||||
|
if pointerDecl.isNil:
|
||||||
|
result = gState.processTSNode(node[0], typeofNode)
|
||||||
|
else:
|
||||||
|
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
|
# Input -> int, unsigned int, long int, etc
|
||||||
# Output -> cint, cuint, clong, etc
|
# Output -> cint, cuint, clong, etc
|
||||||
let ty = getType(node.val)
|
let ty = getType(node.val)
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ extern "C" {
|
||||||
#define COERCE 645635634896ull + 35436
|
#define COERCE 645635634896ull + 35436
|
||||||
#define COERCE2 645635634896 + 35436ul
|
#define COERCE2 645635634896 + 35436ul
|
||||||
#define BINEXPR ~(-(1u << !-1)) ^ (10 >> 1)
|
#define BINEXPR ~(-(1u << !-1)) ^ (10 >> 1)
|
||||||
|
#define POINTEREXPR (int*)0
|
||||||
|
#define POINTERPOINTERPOINTEREXPR (int***)0
|
||||||
#define BOOL true
|
#define BOOL true
|
||||||
#define MATHEXPR (1 + 2/3*20 - 100)
|
#define MATHEXPR (1 + 2/3*20 - 100)
|
||||||
#define ANDEXPR (100 & 11000)
|
#define ANDEXPR (100 & 11000)
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,9 @@ assert SHL1 == (1.uint shl 1)
|
||||||
assert SHL2 == (1.uint shl 2)
|
assert SHL2 == (1.uint shl 2)
|
||||||
assert SHL3 == (1.uint shl 3)
|
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)
|
assert ALLSHL == (SHL1 or SHL2 or SHL3)
|
||||||
|
|
||||||
assert A0 is object
|
assert A0 is object
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue