fixing array indexing on pointers

This commit is contained in:
Irmen de Jong
2025-07-29 23:41:38 +02:00
parent c39d570b72
commit c4c5636a81
4 changed files with 17 additions and 14 deletions

View File

@@ -111,7 +111,7 @@ class IRCodeGen(
if('.' !in node.name) {
// there is 1 case where the identifier is not scoped: if it's the value field name after a pointer array indexing.
val expr = node.parent as? PtBinaryExpression
if (expr?.operator != "." || expr.right !== node || expr.left !is PtArrayIndexer || !expr.left.type.isPointer)
if (expr?.operator != "." || expr.right !== node || expr.left !is PtArrayIndexer || (!expr.left.type.isPointer && !expr.left.type.isStructInstance))
require('.' in node.name) { "node $node name is not scoped: ${node.name}" }
}
}

View File

@@ -241,16 +241,11 @@ class BinaryExpression(
} else if (leftIndexer != null && rightIdentifier.nameInSource.size == 1) {
// ARRAY[x].NAME --> maybe it's a pointer dereference
val dt = leftIndexer.inferType(program).getOrUndef()
if (dt.isPointer) {
dt.dereference().subType
} else null
if (dt.isPointer) dt.dereference().subType else dt.subType
} else if (leftExpr != null) {
// SOMEEXPRESSION . NAME
val leftDt = leftExpr.inferType(program)
if (leftDt.isPointer)
leftDt.getOrUndef().subType
else
null
if (leftDt.isPointer) leftDt.getOrUndef().subType else null
} else null
if (struct == null)
InferredTypes.unknown()
@@ -1733,9 +1728,12 @@ class ArrayIndexedPtrDereference(
val arrayIdentifier = chain.map { it.first }
val symbol = definingScope.lookup(arrayIdentifier) as? VarDecl
if(symbol!=null) {
require(symbol.datatype.isArray)
if(symbol.datatype.isArray)
return InferredTypes.knownFor(symbol.datatype.sub!!)
else if(symbol.datatype.isPointer)
return InferredTypes.knownFor(symbol.datatype.dereference())
}
}
// too hard to determine the type....?
return InferredTypes.unknown()

View File

@@ -1,8 +1,9 @@
TODO
====
fix ^^Node nodes / cx16.r0L = nodes[2].weight
don't write pointer types into P8IR files, just write uword as the type? (actually breaks the VARIABLESWITHINIT now that zp vars get initialized to 0 again; all the pointer examples won't compile anymore)
fix ^^Node nodes / cx16.r0L = nodes[2].weight (TODO("IR datatype for struct instances")
fix bool bb2 = bptr[2]^^ ... peekbool(bptr[2]) gives a arg 1 type error... just omit peekbool() here?
fix countries[2]^^ = 0 compiler crash
fix passing array of structptrs to subroutine , arg type mismatches
disallow ^^str

View File

@@ -1,12 +1,16 @@
main {
sub start() {
struct Node {
ubyte weight
}
^^Node nodes
nodes^^.zzz = 99
cx16.r0L = nodes^^.zzz
cx16.r0L = nodes[2].value
^^Node[10] array
^^bool bptr
bool bb1 = bptr[2]
bool bb2 = bptr[2]^^ ; TODO fix this ... peekbool(bptr[2]) gives a arg 1 type error... just omit peekbool() here?
cx16.r0L = array[2].weight
cx16.r1L = nodes[2].weight ; TODO implement support for this one
}
}