Compare commits
2 commits
62b78ddad2
...
f69ba75f21
| Author | SHA1 | Date | |
|---|---|---|---|
| f69ba75f21 | |||
| eeaf8042a5 |
6 changed files with 16 additions and 16 deletions
2
src/c_lowerer/mod.rs
Normal file
2
src/c_lowerer/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod declaration_transpiler;
|
||||||
|
pub mod statements_transpiler;
|
||||||
|
|
@ -1,3 +1 @@
|
||||||
pub mod declaration_transpiler;
|
pub mod transpiler;
|
||||||
pub mod statements_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,13 +24,11 @@ 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.collect_declaration(node)?;
|
self.lower_declarations_to_c_ir(node)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second pass: transpile function bodies
|
self.lower_function_bodies_to_c_ir(nodes)?;
|
||||||
self.transpile_function_bodies(nodes)?;
|
|
||||||
|
|
||||||
// Generate C code
|
// Generate C code
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
|
|
@ -39,12 +37,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\n");
|
output.push_str("#include <string.h>\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\n");
|
output.push_str(";\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate function declarations (prototypes)
|
// Generate function declarations (prototypes)
|
||||||
|
|
@ -70,7 +68,7 @@ impl Transpiler {
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_declaration(&mut self, node: &TypedASTNode) -> Result<(), String> {
|
fn lower_declarations_to_c_ir(&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)?;
|
||||||
|
|
@ -100,14 +98,15 @@ 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(_) => {}
|
||||||
// Other node types (traits, etc.) - handle later
|
TypedASTNodeKind::Impl(_) => {}
|
||||||
}
|
TypedASTNodeKind::Trait(_) => {}
|
||||||
|
TypedASTNodeKind::Use(_) => {}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transpile_function_bodies(&mut self, nodes: &[TypedASTNode]) -> Result<(), String> {
|
fn lower_function_bodies_to_c_ir(&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,6 +3,7 @@ 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