feat(ir): implement resolve functionality (untested)

This commit is contained in:
multisn8 2024-01-19 00:45:01 +01:00
parent d8e08459a0
commit 29269e2fcd
Signed by untrusted user: multisamplednight
GPG key ID: 6D525AA147CBDAE2
8 changed files with 203 additions and 101 deletions

View file

@ -21,11 +21,12 @@ pub struct GraphIr {
/// See [`crate::GraphIr::instructions`], just that a simple number is used for the ID instead
/// of a proper span.
pub(crate) instructions: Map<usize, instruction::Kind>,
/// See [`crate::GraphIr::edges`]. RON wants you to type the set as if it were a list.
/// See [`crate::GraphIr::edges`], the forward edges.
/// RON wants you to type the set as if it were a list.
pub(crate) edges: Map<Socket, Set<Socket>>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct Socket {
/// ID of the instruction this socket is on.
pub(crate) on: usize,
@ -36,7 +37,8 @@ impl From<Socket> for id::Socket {
fn from(source: Socket) -> Self {
Self {
belongs_to: (id::Instruction(Span {
range: source.on..=source.on,
from: source.on,
to: source.on,
})),
idx: id::SocketIdx(source.idx),
}
@ -50,10 +52,9 @@ impl From<GraphIr> for crate::GraphIr {
instructions: source
.instructions
.into_iter()
.map(|(id, kind)| (id::Instruction(Span { range: id..=id }), kind))
.map(|(id, kind)| (id::Instruction(Span { from: id, to: id }), kind))
.collect(),
edges: type_edges(source.edges),
// same as above, but also reverse the mapping
rev_edges: reverse_and_type_edges(edges),
}
}
@ -70,7 +71,7 @@ fn type_edges(edges: Map<Socket, Set<Socket>>) -> Map<id::Output, Set<id::Input>
.collect()
}
fn reverse_and_type_edges(edges: Map<Socket, Set<Socket>>) -> Map<id::Input, Set<id::Output>> {
fn reverse_and_type_edges(edges: Map<Socket, Set<Socket>>) -> Map<id::Input, id::Output> {
edges
.into_iter()
.fold(Map::new(), |mut rev_edges, (output, inputs)| {
@ -78,7 +79,10 @@ fn reverse_and_type_edges(edges: Map<Socket, Set<Socket>>) -> Map<id::Input, Set
for input in inputs {
let input = id::Input(input.into());
rev_edges.entry(input).or_default().insert(output.clone());
let previous = rev_edges.insert(input, output.clone());
if let Some(previous) = previous {
panic!("two or more outputs referred to the same input {previous:#?} — handle me better later with a TryFrom impl");
}
}
rev_edges