Skip to content

Commit e97abba

Browse files
committed
fix(codegen): resolve top-level const refs in ExprVar (#73)
ExprVar name lookup fell through to UnboundVariable after checking locals and variant_tags, never reaching func_indices where TopConst bindings are stored (negative sentinel: global_idx = -(k+1)). Add a GlobalGet fallback so const identifiers used inside fn bodies compile correctly. check already passed; compile now passes too.
1 parent 0c128f0 commit e97abba

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

lib/codegen.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,13 @@ let rec gen_expr (ctx : context) (expr : expr) : (context * instr list) result =
436436
UnboundVariable even though the parser accepts it. *)
437437
begin match List.assoc_opt id.name ctx.variant_tags with
438438
| Some tag -> Ok (ctx, [I32Const (Int32.of_int tag)])
439-
| None -> Error (UnboundVariable id.name)
439+
| None ->
440+
(* Top-level const bindings are stored in func_indices with a
441+
negative sentinel: actual global index = -(k+1). *)
442+
begin match List.assoc_opt id.name ctx.func_indices with
443+
| Some k when k < 0 -> Ok (ctx, [GlobalGet (-(k + 1))])
444+
| _ -> Error (UnboundVariable id.name)
445+
end
440446
end
441447
end
442448

0 commit comments

Comments
 (0)