dev/asset-management (#6)
Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #6
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -70,8 +97,8 @@ impl Codegen {
|
||||
}
|
||||
}
|
||||
|
||||
// Find tick function (should be in the first program, which is the entry)
|
||||
let mut tick_fn_name = None;
|
||||
// Find frame function (should be in the first program, which is the entry)
|
||||
let mut frame_fn_name = None;
|
||||
if let Some((_, _, entry_program)) = programs.first() {
|
||||
for item in &entry_program.body {
|
||||
let f_opt = match item {
|
||||
@@ -88,8 +115,8 @@ impl Codegen {
|
||||
|
||||
if let Some(f) = f_opt {
|
||||
if let Some(ident) = &f.id {
|
||||
if ident.name == "tick" {
|
||||
tick_fn_name = Some(ident.name.to_string());
|
||||
if ident.name == "frame" {
|
||||
frame_fn_name = Some(ident.name.to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -97,11 +124,45 @@ impl Codegen {
|
||||
}
|
||||
}
|
||||
|
||||
let tick_fn_name = tick_fn_name.ok_or_else(|| anyhow!("export function tick() not found in entry file"))?;
|
||||
let frame_fn_name = frame_fn_name.ok_or_else(|| anyhow!("export function frame() not found in entry file"))?;
|
||||
|
||||
// Entry point: loop calling tick
|
||||
// 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(tick_fn_name), Operand::U32(0)], Span::default());
|
||||
self.emit_op(OpCode::Call, vec![Operand::Label(frame_fn_name), Operand::U32(0)], Span::default());
|
||||
self.emit_op(OpCode::Pop, vec![], Span::default());
|
||||
self.emit_op(OpCode::FrameSync, vec![], Span::default());
|
||||
self.emit_op(OpCode::Jmp, vec![Operand::Label("entry".to_string())], 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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user