@@ -803,7 +803,7 @@ impl ZyntaxRuntime {
803803 ///
804804 /// This performs the lowering pass to convert the TypedAST to HIR,
805805 /// which can then be compiled to machine code.
806- fn lower_typed_program ( & self , program : zyntax_typed_ast:: TypedProgram ) -> RuntimeResult < HirModule > {
806+ fn lower_typed_program ( & self , mut program : zyntax_typed_ast:: TypedProgram ) -> RuntimeResult < HirModule > {
807807 use zyntax_compiler:: lowering:: { LoweringContext , LoweringConfig } ;
808808 use zyntax_typed_ast:: { AstArena , InternedString , TypeRegistry } ;
809809
@@ -814,12 +814,13 @@ impl ZyntaxRuntime {
814814 // Process extern declarations to register opaque types (needs &mut)
815815 self . process_extern_declarations_mut ( & program, & mut type_registry) ?;
816816
817+ // Process imports to load stdlib traits and impls BEFORE lowering
818+ // This must happen before wrapping type_registry in Arc since it needs &mut
819+ self . process_imports_for_traits ( & mut program, & mut type_registry) ?;
820+
817821 // Wrap in Arc for sharing
818822 let type_registry = std:: sync:: Arc :: new ( type_registry) ;
819823
820- // Process imports to load stdlib traits and impls before lowering
821- self . process_imports_for_traits ( & program, & type_registry) ?;
822-
823824 let mut lowering_ctx = LoweringContext :: new (
824825 module_name,
825826 type_registry. clone ( ) ,
@@ -848,32 +849,68 @@ impl ZyntaxRuntime {
848849 /// their trait definitions and implementations in the TypeRegistry.
849850 fn process_imports_for_traits (
850851 & self ,
851- program : & zyntax_typed_ast:: TypedProgram ,
852- type_registry : & std :: sync :: Arc < zyntax_typed_ast:: TypeRegistry > ,
852+ program : & mut zyntax_typed_ast:: TypedProgram ,
853+ type_registry : & mut zyntax_typed_ast:: TypeRegistry ,
853854 ) -> RuntimeResult < ( ) > {
854855 use zyntax_typed_ast:: typed_ast:: TypedDeclaration ;
855856
856- // Collect all import declarations
857+ // Collect imports to process (can't mutate while iterating)
858+ let mut imports_to_process = Vec :: new ( ) ;
857859 for decl in & program. declarations {
858860 if let TypedDeclaration :: Import ( import) = & decl. node {
859- // Get module name (for simple imports like "import prelude", it's a single identifier)
860861 let module_name = import. module_path
861862 . first ( )
862863 . and_then ( |s| s. resolve_global ( ) )
863864 . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
865+ imports_to_process. push ( module_name) ;
866+ }
867+ }
864868
865- log:: debug!( "Processing import: {}" , module_name) ;
869+ // Process each import
870+ for module_name in imports_to_process {
871+ log:: debug!( "Processing import: {}" , module_name) ;
872+
873+ // Try to resolve the import using our import resolvers
874+ if let Ok ( Some ( source) ) = self . resolve_import ( & module_name) {
875+ log:: debug!( "Resolved import '{}', parsing module..." , module_name) ;
876+
877+ // Find a grammar to parse the imported module
878+ // Try each registered grammar until one succeeds
879+ let mut parsed_program = None ;
880+ for ( _lang_name, grammar) in & self . grammars {
881+ match grammar. parse ( & source) {
882+ Ok ( imported_program) => {
883+ parsed_program = Some ( imported_program) ;
884+ break ;
885+ }
886+ Err ( _) => continue ,
887+ }
888+ }
866889
867- // Try to resolve the import using our import resolvers
868- if let Ok ( Some ( source) ) = self . resolve_import ( & module_name) {
869- log:: debug!( "Resolved import '{}', parsing module..." , module_name) ;
890+ if let Some ( mut imported_program) = parsed_program {
891+ log:: info!( "Parsed stdlib module '{}': {} declarations" ,
892+ module_name, imported_program. declarations. len( ) ) ;
893+
894+ // First, process extern declarations from the imported module
895+ // to register opaque types in the type registry
896+ if let Err ( e) = self . process_extern_declarations_mut ( & imported_program, type_registry) {
897+ log:: warn!( "Failed to process extern declarations from '{}': {}" , module_name, e) ;
898+ }
870899
871- // TODO: Parse the imported module and extract traits/impls
872- // For now, just log that we found it
873- log:: info!( "Found stdlib module '{}' ({} bytes)" , module_name, source. len( ) ) ;
900+ // Merge declarations from imported module into main program
901+ // Filter out the import declarations themselves to avoid circular imports
902+ for imported_decl in imported_program. declarations . drain ( ..) {
903+ if !matches ! ( imported_decl. node, TypedDeclaration :: Import ( _) ) {
904+ program. declarations . push ( imported_decl) ;
905+ }
906+ }
907+
908+ log:: debug!( "Merged declarations from '{}'" , module_name) ;
874909 } else {
875- log:: warn!( "Could not resolve import: {} " , module_name) ;
910+ log:: warn!( "Failed to parse imported module '{}' with any registered grammar " , module_name) ;
876911 }
912+ } else {
913+ log:: warn!( "Could not resolve import: {}" , module_name) ;
877914 }
878915 }
879916
0 commit comments