diff --git a/src/c_lowerer/mod.rs b/src/c_lowerer/mod.rs deleted file mode 100644 index 72605cf..0000000 --- a/src/c_lowerer/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod declaration_transpiler; -pub mod statements_transpiler; \ No newline at end of file diff --git a/src/c_lowerer/declaration_transpiler.rs b/src/codegen/declaration_transpiler.rs similarity index 100% rename from src/c_lowerer/declaration_transpiler.rs rename to src/codegen/declaration_transpiler.rs diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index ed9dfaf..7014f92 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -1 +1,3 @@ -pub mod transpiler; \ No newline at end of file +pub mod declaration_transpiler; +pub mod statements_transpiler; +pub mod transpiler; diff --git a/src/c_lowerer/statements_transpiler.rs b/src/codegen/statements_transpiler.rs similarity index 100% rename from src/c_lowerer/statements_transpiler.rs rename to src/codegen/statements_transpiler.rs diff --git a/src/codegen/transpiler.rs b/src/codegen/transpiler.rs index dccc5bd..c7392ef 100644 --- a/src/codegen/transpiler.rs +++ b/src/codegen/transpiler.rs @@ -1,8 +1,8 @@ use crate::ast::*; use crate::c_ir::*; +use crate::codegen::declaration_transpiler::DeclarationTranspiler; +use crate::codegen::statements_transpiler::StatementsTranspiler; use std::collections::HashMap; -use crate::c_lowerer::declaration_transpiler::DeclarationTranspiler; -use crate::c_lowerer::statements_transpiler::StatementsTranspiler; pub struct Transpiler { structs: HashMap, @@ -24,11 +24,13 @@ impl Transpiler { } pub fn transpile_program(&mut self, nodes: &[TypedASTNode]) -> Result { + // First pass: collect declarations for node in nodes { - self.lower_declarations_to_c_ir(node)?; + self.collect_declaration(node)?; } - self.lower_function_bodies_to_c_ir(nodes)?; + // Second pass: transpile function bodies + self.transpile_function_bodies(nodes)?; // Generate C code let mut output = String::new(); @@ -37,12 +39,12 @@ impl Transpiler { output.push_str("#include \n"); output.push_str("#include \n"); output.push_str("#include \n"); - output.push_str("#include \n"); + output.push_str("#include \n\n"); // Generate struct declarations for struct_decl in self.structs.values() { output.push_str(&self.generate_struct_decl(struct_decl)); - output.push_str(";\n"); + output.push_str(";\n\n"); } // Generate function declarations (prototypes) @@ -68,7 +70,7 @@ impl Transpiler { Ok(output) } - fn lower_declarations_to_c_ir(&mut self, node: &TypedASTNode) -> Result<(), String> { + fn collect_declaration(&mut self, node: &TypedASTNode) -> Result<(), String> { match &node.kind { TypedASTNodeKind::Struct(s) => { let struct_decl = self.decl_transpiler.transpile_struct(s)?; @@ -98,15 +100,14 @@ impl Transpiler { // For externs, we might need to add function prototypes // But for now, skip as they're handled differently } - TypedASTNodeKind::Load(_) => {} - TypedASTNodeKind::Impl(_) => {} - TypedASTNodeKind::Trait(_) => {} - TypedASTNodeKind::Use(_) => {} + _ => { + // Other node types (traits, etc.) - handle later + } } Ok(()) } - fn lower_function_bodies_to_c_ir(&mut self, nodes: &[TypedASTNode]) -> Result<(), String> { + fn transpile_function_bodies(&mut self, nodes: &[TypedASTNode]) -> Result<(), String> { for node in nodes { match &node.kind { TypedASTNodeKind::Function(f) => { diff --git a/src/lib.rs b/src/lib.rs index 8260c34..dc1f045 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ pub const EXTENSION: &str = ".sui"; pub mod ast; pub mod c_ir; pub mod codegen; -pub mod c_lowerer; pub mod lambda_lower; pub mod lexer; pub mod monomorphize;