99from .binary_ops import handle_binary_op
1010from .expr_pass import eval_expr , handle_expr
1111
12- local_var_metadata : dict [str | Any , Any ] = {}
1312logger = logging .getLogger (__name__ )
1413
1514
@@ -57,10 +56,9 @@ def handle_assign(
5756 if isinstance (target , ast .Attribute ):
5857 # struct field assignment
5958 field_name = target .attr
60- if var_name in local_sym_tab and var_name in local_var_metadata :
61- struct_type = local_var_metadata [var_name ]
59+ if var_name in local_sym_tab :
60+ struct_type = local_sym_tab [var_name ]. metadata
6261 struct_info = structs_sym_tab [struct_type ]
63-
6462 if field_name in struct_info .fields :
6563 field_ptr = struct_info .gep (
6664 builder , local_sym_tab [var_name ][0 ], field_name
@@ -93,16 +91,20 @@ def handle_assign(
9391 elif isinstance (rval , ast .Constant ):
9492 if isinstance (rval .value , bool ):
9593 if rval .value :
96- builder .store (ir .Constant (ir .IntType (1 ), 1 ), local_sym_tab [var_name ][0 ])
94+ builder .store (
95+ ir .Constant (ir .IntType (1 ), 1 ), local_sym_tab [var_name ].var
96+ )
9797 else :
98- builder .store (ir .Constant (ir .IntType (1 ), 0 ), local_sym_tab [var_name ][0 ])
98+ builder .store (
99+ ir .Constant (ir .IntType (1 ), 0 ), local_sym_tab [var_name ].var
100+ )
99101 print (f"Assigned constant { rval .value } to { var_name } " )
100102 elif isinstance (rval .value , int ):
101103 # Assume c_int64 for now
102104 # var = builder.alloca(ir.IntType(64), name=var_name)
103105 # var.align = 8
104106 builder .store (
105- ir .Constant (ir .IntType (64 ), rval .value ), local_sym_tab [var_name ][ 0 ]
107+ ir .Constant (ir .IntType (64 ), rval .value ), local_sym_tab [var_name ]. var
106108 )
107109 # local_sym_tab[var_name] = var
108110 print (f"Assigned constant { rval .value } to { var_name } " )
@@ -118,7 +120,7 @@ def handle_assign(
118120 global_str .global_constant = True
119121 global_str .initializer = str_const
120122 str_ptr = builder .bitcast (global_str , ir .PointerType (ir .IntType (8 )))
121- builder .store (str_ptr , local_sym_tab [var_name ][ 0 ] )
123+ builder .store (str_ptr , local_sym_tab [var_name ]. var )
122124 print (f"Assigned string constant '{ rval .value } ' to { var_name } " )
123125 else :
124126 print ("Unsupported constant type" )
@@ -136,13 +138,13 @@ def handle_assign(
136138 # var = builder.alloca(ir_type, name=var_name)
137139 # var.align = ir_type.width // 8
138140 builder .store (
139- ir .Constant (ir_type , rval .args [0 ].value ), local_sym_tab [var_name ][0 ]
141+ ir .Constant (ir_type , rval .args [0 ].value ),
142+ local_sym_tab [var_name ].var ,
140143 )
141144 print (
142145 f"Assigned { call_type } constant "
143146 f"{ rval .args [0 ].value } to { var_name } "
144147 )
145- # local_sym_tab[var_name] = var
146148 elif HelperHandlerRegistry .has_handler (call_type ):
147149 # var = builder.alloca(ir.IntType(64), name=var_name)
148150 # var.align = 8
@@ -154,10 +156,8 @@ def handle_assign(
154156 local_sym_tab ,
155157 map_sym_tab ,
156158 structs_sym_tab ,
157- local_var_metadata ,
158159 )
159- builder .store (val [0 ], local_sym_tab [var_name ][0 ])
160- # local_sym_tab[var_name] = var
160+ builder .store (val [0 ], local_sym_tab [var_name ].var )
161161 print (f"Assigned constant { rval .func .id } to { var_name } " )
162162 elif call_type == "deref" and len (rval .args ) == 1 :
163163 print (f"Handling deref assignment { ast .dump (rval )} " )
@@ -174,18 +174,15 @@ def handle_assign(
174174 print ("Failed to evaluate deref argument" )
175175 return
176176 print (f"Dereferenced value: { val } , storing in { var_name } " )
177- builder .store (val [0 ], local_sym_tab [var_name ][0 ])
178- # local_sym_tab[var_name] = var
177+ builder .store (val [0 ], local_sym_tab [var_name ].var )
179178 print (f"Dereferenced and assigned to { var_name } " )
180179 elif call_type in structs_sym_tab and len (rval .args ) == 0 :
181180 struct_info = structs_sym_tab [call_type ]
182181 ir_type = struct_info .ir_type
183182 # var = builder.alloca(ir_type, name=var_name)
184183 # Null init
185- builder .store (ir .Constant (ir_type , None ), local_sym_tab [var_name ][0 ])
186- local_var_metadata [var_name ] = call_type
184+ builder .store (ir .Constant (ir_type , None ), local_sym_tab [var_name ].var )
187185 print (f"Assigned struct { call_type } to { var_name } " )
188- # local_sym_tab[var_name] = var
189186 else :
190187 print (f"Unsupported assignment call type: { call_type } " )
191188 elif isinstance (rval .func , ast .Attribute ):
@@ -208,12 +205,10 @@ def handle_assign(
208205 local_sym_tab ,
209206 map_sym_tab ,
210207 structs_sym_tab ,
211- local_var_metadata ,
212208 )
213209 # var = builder.alloca(ir.IntType(64), name=var_name)
214210 # var.align = 8
215- builder .store (val [0 ], local_sym_tab [var_name ][0 ])
216- # local_sym_tab[var_name] = var
211+ builder .store (val [0 ], local_sym_tab [var_name ].var )
217212 else :
218213 print ("Unsupported assignment call structure" )
219214 else :
@@ -237,7 +232,7 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
237232 return None
238233 elif isinstance (cond , ast .Name ):
239234 if cond .id in local_sym_tab :
240- var = local_sym_tab [cond .id ][ 0 ]
235+ var = local_sym_tab [cond .id ]. var
241236 val = builder .load (var )
242237 if val .type != ir .IntType (1 ):
243238 # Convert nonzero values to true, zero to false
@@ -352,7 +347,6 @@ def process_stmt(
352347):
353348 print (f"Processing statement: { ast .dump (stmt )} " )
354349 if isinstance (stmt , ast .Expr ):
355- print (local_var_metadata )
356350 handle_expr (
357351 func ,
358352 module ,
@@ -361,7 +355,6 @@ def process_stmt(
361355 local_sym_tab ,
362356 map_sym_tab ,
363357 structs_sym_tab ,
364- local_var_metadata ,
365358 )
366359 elif isinstance (stmt , ast .Assign ):
367360 handle_assign (
@@ -677,7 +670,8 @@ def _expr_type(e):
677670 if found_type is None :
678671 found_type = t
679672 elif found_type != t :
680- raise ValueError (f"Conflicting return types:{ found_type } vs { t } " )
673+ raise ValueError (f"Conflicting return types:{
674+ found_type } vs { t } " )
681675 return found_type or "None"
682676
683677
0 commit comments