new music on color-square

This commit is contained in:
Nilton Constantino
2026-01-22 13:28:39 +00:00
parent 9704274707
commit f7b60c2688
20 changed files with 978 additions and 551 deletions
@@ -18,8 +18,10 @@ pub struct Codegen {
pub symbols: Vec<Symbol>,
instructions: Vec<(Asm, bool)>, // (Asm, has_symbol)
locals: HashMap<String, u32>,
globals: HashMap<String, u32>,
constant_pool: Vec<ConstantPoolEntry>,
next_local: u32,
next_global: u32,
label_count: u32,
}
@@ -31,8 +33,10 @@ impl Codegen {
symbols: Vec::new(),
instructions: Vec::new(),
locals: HashMap::new(),
globals: HashMap::new(),
constant_pool: vec![ConstantPoolEntry::Null], // Index 0 is always Null
next_local: 0,
next_global: 0,
label_count: 0,
}
}
@@ -52,7 +56,7 @@ impl Codegen {
}
pub fn compile_programs(&mut self, programs: Vec<(String, String, &Program)>) -> Result<Vec<u8>> {
// First pass: collect all functions and their indices
// First pass: collect all functions and global variables
let mut all_functions = Vec::new();
for (file, source, program) in &programs {
for item in &program.body {
@@ -63,6 +67,29 @@ impl Codegen {
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::FunctionDeclaration(f)) = &decl.declaration {
all_functions.push((file.clone(), source.clone(), f.as_ref()));
} else if let Some(Declaration::VariableDeclaration(var)) = &decl.declaration {
for decl in &var.declarations {
if let BindingPattern::BindingIdentifier(ident) = &decl.id {
let name = ident.name.to_string();
if !self.globals.contains_key(&name) {
let id = self.next_global;
self.globals.insert(name, id);
self.next_global += 1;
}
}
}
}
}
Statement::VariableDeclaration(var) => {
for decl in &var.declarations {
if let BindingPattern::BindingIdentifier(ident) = &decl.id {
let name = ident.name.to_string();
if !self.globals.contains_key(&name) {
let id = self.next_global;
self.globals.insert(name, id);
self.next_global += 1;
}
}
}
}
_ => {}
@@ -99,6 +126,40 @@ impl Codegen {
let frame_fn_name = frame_fn_name.ok_or_else(|| anyhow!("export function frame() not found in entry file"))?;
// Initialize globals
for (file, source, program) in &programs {
self.file_name = file.clone();
self.source_text = source.clone();
for item in &program.body {
let var_opt = match item {
Statement::VariableDeclaration(var) => Some(var.as_ref()),
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::VariableDeclaration(var)) = &decl.declaration {
Some(var.as_ref())
} else {
None
}
}
_ => None,
};
if let Some(var) = var_opt {
for decl in &var.declarations {
if let BindingPattern::BindingIdentifier(ident) = &decl.id {
let name = ident.name.to_string();
let id = *self.globals.get(&name).unwrap();
if let Some(init) = &decl.init {
self.compile_expr(init)?;
} else {
self.emit_op(OpCode::PushI32, vec![Operand::I32(0)], decl.span);
}
self.emit_op(OpCode::SetGlobal, vec![Operand::U32(id)], decl.span);
}
}
}
}
}
// Entry point: loop calling frame
self.emit_label("entry".to_string());
self.emit_op(OpCode::Call, vec![Operand::Label(frame_fn_name), Operand::U32(0)], Span::default());
@@ -221,7 +282,7 @@ impl Codegen {
if val.fract() == 0.0 && val >= i32::MIN as f64 && val <= i32::MAX as f64 {
self.emit_op(OpCode::PushI32, vec![Operand::I32(val as i32)], n.span);
} else {
self.emit_op(OpCode::PushI64, vec![Operand::I64(val as i64)], n.span);
self.emit_op(OpCode::PushF64, vec![Operand::F64(val)], n.span);
}
}
Expression::BooleanLiteral(b) => {
@@ -238,6 +299,8 @@ impl Codegen {
let name = ident.name.to_string();
if let Some(&id) = self.locals.get(&name) {
self.emit_op(OpCode::GetLocal, vec![Operand::U32(id)], ident.span);
} else if let Some(&id) = self.globals.get(&name) {
self.emit_op(OpCode::GetGlobal, vec![Operand::U32(id)], ident.span);
} else {
return Err(anyhow!("Undefined variable: {} at {:?}", name, ident.span));
}
@@ -249,6 +312,9 @@ impl Codegen {
if let Some(&id) = self.locals.get(&name) {
self.emit_op(OpCode::SetLocal, vec![Operand::U32(id)], assign.span);
self.emit_op(OpCode::GetLocal, vec![Operand::U32(id)], assign.span);
} else if let Some(&id) = self.globals.get(&name) {
self.emit_op(OpCode::SetGlobal, vec![Operand::U32(id)], assign.span);
self.emit_op(OpCode::GetGlobal, vec![Operand::U32(id)], assign.span);
} else {
return Err(anyhow!("Undefined variable: {} at {:?}", name, ident.span));
}