Compare commits

..

No commits in common. "f69ba75f21ad39e22db277a328695a3394a7226f" and "62b78ddad27aeac9e7525d7320d7b15aea262c78" have entirely different histories.

6 changed files with 16 additions and 16 deletions

View file

@ -1,2 +0,0 @@
pub mod declaration_transpiler;
pub mod statements_transpiler;

View file

@ -1 +1,3 @@
pub mod transpiler;
pub mod declaration_transpiler;
pub mod statements_transpiler;
pub mod transpiler;

View file

@ -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<String, CStructDecl>,
@ -24,11 +24,13 @@ impl Transpiler {
}
pub fn transpile_program(&mut self, nodes: &[TypedASTNode]) -> Result<String, String> {
// 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 <stdio.h>\n");
output.push_str("#include <stdlib.h>\n");
output.push_str("#include <stdbool.h>\n");
output.push_str("#include <string.h>\n");
output.push_str("#include <string.h>\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) => {

View file

@ -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;