@@ -8,7 +8,7 @@ import v.ast
88pub fn (mut t Transformer) array_init (mut node ast.ArrayInit) ast.Expr {
99 // For JS and Go generate array init using their syntax
1010 // if t.pref.backend !in [.c, .native] {
11- if ! t.pref.new_transform || node.is_fixed {
11+ if ! t.pref.new_transform || node.is_fixed { // || t.file.mod.name != 'main' {
1212 for mut expr in node.exprs {
1313 expr = t.expr (mut expr)
1414 }
@@ -127,6 +127,72 @@ pub fn (mut t Transformer) array_init(mut node ast.ArrayInit) ast.Expr {
127127 // }
128128}
129129
130+ pub fn (mut t Transformer) index_expr (mut node ast.IndexExpr) ast.Expr {
131+ // if t.pref.new_transform {
132+ // println(t.file.mod)
133+ // }
134+ t.check_safe_array (mut node)
135+ node.left = t.expr (mut node.left)
136+ node.index = t.expr (mut node.index)
137+ node.or_expr = t.expr (mut node.or_expr) as ast.OrExpr
138+ // if true { //!t.pref.new_transform { //|| t.file.mod.name != 'main' {
139+ if ! t.pref.new_transform { // || t.file.mod.name != 'main' {
140+ return node
141+ }
142+ // Recursively transform the left side (array/map) and the index
143+ node.left = t.expr (mut node.left)
144+ node.index = t.expr (mut node.index)
145+ // Only apply transformation if it is an array (not a fixed array, map, or option)
146+ if node.is_array && ! node.is_farray && ! node.is_option && ! node.is_map {
147+ // Transformation: x[i] => (*(T*)builtin__array_get(x, i))
148+ mut left_expr := node.left
149+ mut left_type := node.left_type
150+ // If the array is a pointer/reference (e.g. &[]int), we must dereference it
151+ // because builtin__array_get expects the array struct by value.
152+ if left_type.is_ptr () {
153+ left_expr = ast.PrefixExpr{
154+ op: .mul
155+ right: left_expr
156+ pos: node.pos
157+ }
158+ // The type of the argument becomes the value type (remove pointer)
159+ left_type = left_type.set_nr_muls (0 )
160+ }
161+ // Create the call: builtin__array_get(x, i)
162+ call_expr := ast.CallExpr{
163+ name: 'builtin__array_get'
164+ mod: 'builtin'
165+ args: [
166+ ast.CallArg{
167+ expr: left_expr
168+ typ: left_type
169+ },
170+ ast.CallArg{
171+ expr: node.index
172+ typ: ast.int_type
173+ },
174+ ]
175+ return_type: ast.voidptr_type
176+ }
177+ // Create the pointer type T* to cast the result (voidptr) to.
178+ // node.typ is the element type T.
179+ ptr_typ := node.typ.ref ()
180+ // Create the cast: (T*)builtin__array_get(...)
181+ cast_expr := ast.CastExpr{
182+ expr: call_expr
183+ typ: ptr_typ
184+ typname: t.table.get_type_name (ptr_typ)
185+ }
186+ // Dereference the result to get the element: *...
187+ return ast.PrefixExpr{
188+ op: .mul
189+ right: cast_expr
190+ pos: node.pos
191+ }
192+ }
193+ return node
194+ }
195+
130196pub fn (mut t Transformer) find_new_array_len (node ast.AssignStmt) {
131197 if ! t.pref.is_prod {
132198 return
0 commit comments