diff --git a/bootstrap/src/compiler.rs b/bootstrap/src/compiler.rs index 65992d145e..d3e1d13f0c 100644 --- a/bootstrap/src/compiler.rs +++ b/bootstrap/src/compiler.rs @@ -13,6 +13,7 @@ use std::default::Default; #[derive(Debug, Clone, PartialEq)] pub enum NodeKind { Module, + UseDecl, ConstDecl, EnumDecl, StructDecl, @@ -721,14 +722,36 @@ impl Parser { fn parse_module_body(&mut self, module: &mut Node) -> Result<(), String> { while self.current.kind != TokenKind::Eof && self.current.kind != TokenKind::RBrace { - // [BUG 5 FIX] Skip use/using statements + // Parse use/using statements into UseDecl nodes if self.current.kind == TokenKind::KwUse || self.current.kind == TokenKind::KwUsing { - while self.current.kind != TokenKind::Semicolon && self.current.kind != TokenKind::Eof { + self.advance(); // consume 'use'/'using' + // Collect the full path: e.g. "base::types" or just "datalog_solve" + let mut full_path = String::new(); + if self.current.kind == TokenKind::Ident { + full_path.push_str(&self.current.lexeme); self.advance(); + // Parse :: separated segments + while self.current.kind == TokenKind::Colon { + self.advance(); // first : + if self.current.kind == TokenKind::Colon { + self.advance(); // second : + } + full_path.push_str("::"); + if self.current.kind == TokenKind::Ident { + full_path.push_str(&self.current.lexeme); + self.advance(); + } + } } if self.current.kind == TokenKind::Semicolon { self.advance(); } + // Extract the last segment as the import name + let import_name = full_path.rsplit("::").next().unwrap_or(&full_path).to_string(); + let mut use_node = Node::new(NodeKind::UseDecl); + use_node.name = import_name; // e.g. "types" + use_node.value = full_path; // e.g. "base::types" + module.children.push(use_node); continue; } @@ -1626,13 +1649,28 @@ impl Codegen { } else { "unknown" }; - self.write_line(&format!("// Generated from t27 spec: {}", module_name)); + self.write_line(&format!("// Generated from t27 spec: {} (module name)", module_name)); self.write_line("// DO NOT EDIT — generated by t27c"); self.write_line("// phi^2 + 1/phi^2 = 3 | TRINITY"); self.write_line(""); + // Emit @import for UseDecl nodes first + let mut has_imports = false; + for decl in &ast.children { + if decl.kind == NodeKind::UseDecl { + self.write_line(&format!("const {} = @import(\"{}.zig\");", decl.name, decl.name)); + has_imports = true; + } + } + if has_imports { + self.write_line(""); + } + + // Emit other declarations for decl in &ast.children { - self.gen_decl(decl); + if decl.kind != NodeKind::UseDecl { + self.gen_decl(decl); + } } }