assembler updates

This commit is contained in:
2025-06-17 23:48:06 +01:00
parent 87fbd6c362
commit b0670d1e6c
3 changed files with 268 additions and 161 deletions
+60
View File
@@ -0,0 +1,60 @@
use std::path::PathBuf;
use crate::{
AssembleError,
model::{Module, Node, Opcode, Symbol, Token},
quick_hash,
};
pub fn resolve_dependencies(mut nodes: Vec<Node>) -> Result<Vec<Node>, AssembleError> {
// first we get a list of imports
let mut dependencies = Vec::new();
for node in &nodes {
if let Opcode::Include = node.1 {
// we want the path, and the name
let name = if let Token::Symbol(name) = node.2.get(0).unwrap() {
name.name.clone()
} else {
unreachable!()
}; //node.2.get(0).unwrap()
let path = if let Token::StringLit(path) = node.2.get(1).unwrap() {
path
} else {
unreachable!()
};
let hash = quick_hash(&PathBuf::from(path).canonicalize().unwrap());
dependencies.push((name, hash));
}
}
let mut changes = Vec::<(u32, u32, Symbol)>::new();
// now we resolve the symbols on all the nodes
// we need to check all operands for unresolved signals
for (i, node) in nodes.clone().iter().enumerate() {
let Node(_, _, operands) = node;
for (j, token) in operands.iter().enumerate() {
if let Token::Symbol(symbol) = token {
for d in &dependencies {
if let Module::Unresolved(name) = symbol.module.clone() {
if name != d.0 {
continue;
}
let symbol = Symbol {
name: symbol.name.clone(),
module: Module::Resolved(d.1),
};
changes.push((i as u32, j as u32, symbol));
}
}
}
}
}
for (i, j, symbol) in changes {
nodes[i as usize].2[j as usize] = Token::Symbol(symbol);
}
Ok(nodes)
}