diff --git a/src/common.rs b/src/common.rs index 64de8c8bb1838be6eff9cab9b8d8880326a6f4de..0775b8a025f6c237bd804aa78aa222baf3ffebfe 100644 --- a/src/common.rs +++ b/src/common.rs @@ -44,6 +44,7 @@ pub enum Polarity { #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone, Debug)] pub struct Key(u64); +#[derive(Eq, PartialEq, Copy, Clone, Debug)] pub enum MainComponentErr { NoSuchComponent, NonPortTypeParameters, diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index edbec35536ec14480927e1b087da5d2dba29363b..69d540318eac690a91df6e15f6ff48f3776251d5 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -42,43 +42,59 @@ impl ProtocolDescription for ProtocolDescriptionImpl { } } } - // fn main_interface_polarities(&self) -> Vec { - // let def = &self.heap[self.main]; - // let mut result = Vec::new(); - // for ¶m in def.parameters().iter() { - // let param = &self.heap[param]; - // let type_annot = &self.heap[param.type_annotation]; - // let ptype = &type_annot.the_type.primitive; - // if ptype == &PrimitiveType::Input { - // result.push(Polarity::Getter) - // } else if ptype == &PrimitiveType::Output { - // result.push(Polarity::Putter) - // } else { - // unreachable!() - // } - // } - // result - // } - fn new_main_component(&self, identifier: &[u8], ports: &[(Polarity, Key)]) -> Result { - // Find symbol with identifier - let def = self.heap[self.root].get_definition_ident(&heap, identifier); + fn component_polarities(&self, identifier: &[u8]) -> Result, MainComponentErr> { + let h = &self.heap; + let root = &h[self.root]; + let def = root.get_definition_ident(h, identifier); if def.is_none() { - return Err(NewMainErr::NoSuchComponent); + return Err(MainComponentErr::NoSuchComponent); } - let def = &self.heap[def.unwrap()]; + let def = &h[def.unwrap()]; if !def.is_component() { - return Err(NewMainErr::NoSuchComponent); + return Err(MainComponentErr::NoSuchComponent); } - let main = def.as_component().this(); - // + for ¶m in def.parameters().iter() { + let param = &h[param]; + let type_annot = &h[param.type_annotation]; + if type_annot.the_type.array { + return Err(MainComponentErr::NonPortTypeParameters); + } + match type_annot.the_type.primitive { + PrimitiveType::Input | PrimitiveType::Output => continue, + _ => { + return Err(MainComponentErr::NonPortTypeParameters); + } + } + } + let mut result = Vec::new(); + for ¶m in def.parameters().iter() { + let param = &h[param]; + let type_annot = &h[param.type_annotation]; + let ptype = &type_annot.the_type.primitive; + if ptype == &PrimitiveType::Input { + result.push(Polarity::Getter) + } else if ptype == &PrimitiveType::Output { + result.push(Polarity::Putter) + } else { + unreachable!() + } + } + Ok(result) + } + fn new_main_component(&self, identifier: &[u8], ports: &[Key]) -> ComponentStateImpl { let mut args = Vec::new(); - for &(x, y) in ports.iter() { - match x { - Polarity::Getter => args.push(Value::Input(InputValue(y))), - Polarity::Putter => args.push(Value::Output(OutputValue(y))), + for (&x, y) in ports.iter().zip(self.component_polarities(identifier).unwrap()) { + match y { + Polarity::Getter => args.push(Value::Input(InputValue(x))), + Polarity::Putter => args.push(Value::Output(OutputValue(x))) } } - ComponentStateImpl { prompt: Prompt::new(&self.heap, self.main.upcast(), &args) } + let h = &self.heap; + let root = &h[self.root]; + let def = root.get_definition_ident(h, identifier).unwrap(); + ComponentStateImpl { + prompt: Prompt::new(h, def, &args) + } } }