Compare commits
No commits in common. "f69ba75f21ad39e22db277a328695a3394a7226f" and "62b78ddad27aeac9e7525d7320d7b15aea262c78" have entirely different histories.
f69ba75f21
...
62b78ddad2
6 changed files with 16 additions and 16 deletions
|
|
@ -1,2 +0,0 @@
|
||||||
pub mod declaration_transpiler;
|
|
||||||
pub mod statements_transpiler;
|
|
||||||
|
|
@ -1 +1,3 @@
|
||||||
|
pub mod declaration_transpiler;
|
||||||
|
pub mod statements_transpiler;
|
||||||
pub mod transpiler;
|
pub mod transpiler;
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::ast::*;
|
use crate::ast::*;
|
||||||
use crate::c_ir::*;
|
use crate::c_ir::*;
|
||||||
|
use crate::codegen::declaration_transpiler::DeclarationTranspiler;
|
||||||
|
use crate::codegen::statements_transpiler::StatementsTranspiler;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use crate::c_lowerer::declaration_transpiler::DeclarationTranspiler;
|
|
||||||
use crate::c_lowerer::statements_transpiler::StatementsTranspiler;
|
|
||||||
|
|
||||||
pub struct Transpiler {
|
pub struct Transpiler {
|
||||||
structs: HashMap<String, CStructDecl>,
|
structs: HashMap<String, CStructDecl>,
|
||||||
|
|
@ -24,11 +24,13 @@ impl Transpiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn transpile_program(&mut self, nodes: &[TypedASTNode]) -> Result<String, String> {
|
pub fn transpile_program(&mut self, nodes: &[TypedASTNode]) -> Result<String, String> {
|
||||||
|
// First pass: collect declarations
|
||||||
for node in nodes {
|
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
|
// Generate C code
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
|
|
@ -37,12 +39,12 @@ impl Transpiler {
|
||||||
output.push_str("#include <stdio.h>\n");
|
output.push_str("#include <stdio.h>\n");
|
||||||
output.push_str("#include <stdlib.h>\n");
|
output.push_str("#include <stdlib.h>\n");
|
||||||
output.push_str("#include <stdbool.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
|
// Generate struct declarations
|
||||||
for struct_decl in self.structs.values() {
|
for struct_decl in self.structs.values() {
|
||||||
output.push_str(&self.generate_struct_decl(struct_decl));
|
output.push_str(&self.generate_struct_decl(struct_decl));
|
||||||
output.push_str(";\n");
|
output.push_str(";\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate function declarations (prototypes)
|
// Generate function declarations (prototypes)
|
||||||
|
|
@ -68,7 +70,7 @@ impl Transpiler {
|
||||||
Ok(output)
|
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 {
|
match &node.kind {
|
||||||
TypedASTNodeKind::Struct(s) => {
|
TypedASTNodeKind::Struct(s) => {
|
||||||
let struct_decl = self.decl_transpiler.transpile_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
|
// For externs, we might need to add function prototypes
|
||||||
// But for now, skip as they're handled differently
|
// But for now, skip as they're handled differently
|
||||||
}
|
}
|
||||||
TypedASTNodeKind::Load(_) => {}
|
_ => {
|
||||||
TypedASTNodeKind::Impl(_) => {}
|
// Other node types (traits, etc.) - handle later
|
||||||
TypedASTNodeKind::Trait(_) => {}
|
}
|
||||||
TypedASTNodeKind::Use(_) => {}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
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 {
|
for node in nodes {
|
||||||
match &node.kind {
|
match &node.kind {
|
||||||
TypedASTNodeKind::Function(f) => {
|
TypedASTNodeKind::Function(f) => {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ pub const EXTENSION: &str = ".sui";
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod c_ir;
|
pub mod c_ir;
|
||||||
pub mod codegen;
|
pub mod codegen;
|
||||||
pub mod c_lowerer;
|
|
||||||
pub mod lambda_lower;
|
pub mod lambda_lower;
|
||||||
pub mod lexer;
|
pub mod lexer;
|
||||||
pub mod monomorphize;
|
pub mod monomorphize;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue