33import ast
44from dataclasses import dataclass , field
55
6+ from coretrace_python .frontend .imports import ImportBindings , collect_imports
7+ from coretrace_python .frontend .symbols import SymbolId
68from coretrace_python .ir .model import (
79 BasicBlock ,
810 BinaryOp ,
1820 Return ,
1921 SourceLocation ,
2022 StoreLocal ,
23+ Symbol ,
2124 UnaryOp ,
2225 Value ,
26+ ValueInstruction ,
2327)
2428
2529
@@ -60,6 +64,7 @@ class LoweringError(Exception):
6064@dataclass
6165class _FunctionLowerer :
6266 filename : str
67+ imports : ImportBindings
6368 next_value_id : int = 0
6469 locals : dict [str , Value ] = field (default_factory = dict )
6570 block : BasicBlock = field (default_factory = lambda : BasicBlock ("entry" ))
@@ -86,12 +91,28 @@ def new_value(self) -> Value:
8691 self .next_value_id += 1
8792 return value
8893
89- def emit (self , instruction ) :
94+ def emit (self , instruction : ValueInstruction ) -> Value :
9095 self .block .instructions .append (instruction )
9196 return instruction .result
9297
98+ def emit_effect (self , instruction : StoreLocal | Return ) -> None :
99+ self .block .instructions .append (instruction )
100+
101+ def imported_symbol (self , node : ast .expr ) -> SymbolId | None :
102+ if isinstance (node , ast .Name ):
103+ if node .id in self .locals or node .id in self .parameters :
104+ return None
105+ return self .imports .resolve (node .id )
106+ if isinstance (node , ast .Attribute ):
107+ parent = self .imported_symbol (node .value )
108+ return parent .attribute (node .attr ) if parent is not None else None
109+ return None
110+
93111 def expression (self , node : ast .expr ) -> Value :
94112 location = self .location (node )
113+ imported_symbol = self .imported_symbol (node )
114+ if imported_symbol is not None :
115+ return self .emit (Symbol (self .new_value (), location , imported_symbol ))
95116 if isinstance (node , ast .Name ):
96117 if node .id in self .locals :
97118 return self .emit (LoadLocal (self .new_value (), location , node .id ))
@@ -159,12 +180,12 @@ def statement(self, node: ast.stmt) -> None:
159180 self .fail (node , "only assignment to one local name is supported" )
160181 name = node .targets [0 ].id
161182 value = self .expression (node .value )
162- self .emit (StoreLocal (None , self .location (node ), name , value ))
183+ self .emit_effect (StoreLocal (None , self .location (node ), name , value ))
163184 self .locals [name ] = value
164185 return
165186 if isinstance (node , ast .Return ):
166187 value = self .expression (node .value ) if node .value is not None else None
167- self .emit (Return (None , self .location (node ), value ))
188+ self .emit_effect (Return (None , self .location (node ), value ))
168189 return
169190 if isinstance (node , ast .Expr ):
170191 self .expression (node .value )
@@ -190,10 +211,13 @@ def function(self, node: ast.FunctionDef | ast.AsyncFunctionDef) -> FunctionIR:
190211
191212
192213def lower_module (tree : ast .Module , filename : str = "<unknown>" ) -> ModuleIR :
214+ imports = collect_imports (tree , filename )
193215 functions : list [FunctionIR ] = []
194216 for statement in tree .body :
195217 if isinstance (statement , (ast .FunctionDef , ast .AsyncFunctionDef )):
196- functions .append (_FunctionLowerer (filename ).function (statement ))
218+ functions .append (_FunctionLowerer (filename , imports ).function (statement ))
219+ elif isinstance (statement , (ast .Import , ast .ImportFrom )):
220+ continue
197221 elif isinstance (statement , ast .Expr ) and isinstance (statement .value , ast .Constant ):
198222 # Permit module docstrings.
199223 continue
0 commit comments