From 935120e93ef14a3368f653213df6a8337aeb5069 2020-02-05 17:27:11 From: Hans-Dieter Hiep Date: 2020-02-05 17:27:11 Subject: [PATCH] Rework setup of protocol description --- diff --git a/src/protocol/ast.rs b/src/protocol/ast.rs index 8de88d47194be4ef92063ac95d3e4139b4ee6db1..17a9f472f295125a32ff768347ad64352b79e20b 100644 --- a/src/protocol/ast.rs +++ b/src/protocol/ast.rs @@ -1220,6 +1220,14 @@ impl Root { } None } + pub fn get_definition_ident(&self, h: &Heap, id: &[u8]) -> Option { + for &def in self.definitions.iter() { + if h[h[def].identifier()].ident() == id { + return Some(def); + } + } + None + } pub fn get_declaration(&self, h: &Heap, id: IdentifierId) -> Option { for &decl in self.declarations.iter() { if h[h[decl].identifier()] == h[id] { diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 97986eb6333fd7cc9a11578f68d3aa8fac07799d..edbec35536ec14480927e1b087da5d2dba29363b 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -15,8 +15,7 @@ use std::hint::unreachable_unchecked; pub struct ProtocolDescriptionImpl { heap: Heap, source: InputSource, - root: RootId, - main: ComponentId, + root: RootId } impl std::fmt::Debug for ProtocolDescriptionImpl { @@ -34,11 +33,7 @@ impl ProtocolDescription for ProtocolDescriptionImpl { let mut parser = Parser::new(&mut source); match parser.parse(&mut heap) { Ok(root) => { - // Find main definition (grammar rule ensures this exists) - let sym = heap.get_external_identifier(b"main"); - let def = heap[root].get_definition(&heap, sym.upcast()).unwrap(); - let main = heap[def].as_component().this(); - return Ok(ProtocolDescriptionImpl { heap, source, root, main }); + return Ok(ProtocolDescriptionImpl { heap, source, root }); } Err(err) => { let mut vec: Vec = Vec::new(); @@ -64,12 +59,23 @@ impl ProtocolDescription for ProtocolDescriptionImpl { // } // result // } - fn new_main_component(&self, ports: &[Key]) -> ComponentStateImpl { + 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); + if def.is_none() { + return Err(NewMainErr::NoSuchComponent); + } + let def = &self.heap[def.unwrap()]; + if !def.is_component() { + return Err(NewMainErr::NoSuchComponent); + } + let main = def.as_component().this(); + // let mut args = Vec::new(); - for (&x, y) in ports.iter().zip(self.main_interface_polarities()) { - match y { - Polarity::Getter => args.push(Value::Input(InputValue(x))), - Polarity::Putter => args.push(Value::Output(OutputValue(x))), + for &(x, y) in ports.iter() { + match x { + Polarity::Getter => args.push(Value::Input(InputValue(y))), + Polarity::Putter => args.push(Value::Output(OutputValue(y))), } } ComponentStateImpl { prompt: Prompt::new(&self.heap, self.main.upcast(), &args) } diff --git a/src/protocol/parser.rs b/src/protocol/parser.rs index 098dc481f9808d9f4ef97d12548e388c1fd405b5..bd82d1282afe155610322f311a1f2afa6117fe39 100644 --- a/src/protocol/parser.rs +++ b/src/protocol/parser.rs @@ -1784,40 +1784,6 @@ impl Visitor for SelectableExpressions { } } -struct CheckMainComponent {} - -impl CheckMainComponent { - fn new() -> Self { - CheckMainComponent {} - } - fn visit_protocol_description(&mut self, h: &mut Heap, root: RootId) -> VisitorResult { - let sym = h.get_external_identifier(b"main"); - let root = &h[root]; - let def = root.get_definition(h, sym.upcast()); - if def.is_none() { - return Err(ParseError::new(root.position, "Missing main definition")); - } - let def = &h[def.unwrap()]; - if !def.is_component() { - return Err(ParseError::new(def.position(), "Main definition must be a component")); - } - 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(ParseError::new(type_annot.position, "Illegal type")); - } - match type_annot.the_type.primitive { - PrimitiveType::Input | PrimitiveType::Output => continue, - _ => { - return Err(ParseError::new(type_annot.position, "Illegal type")); - } - } - } - Ok(()) - } -} - pub struct Parser<'a> { source: &'a mut InputSource, } @@ -1844,7 +1810,6 @@ impl<'a> Parser<'a> { AssignableExpressions::new().visit_protocol_description(h, pd)?; IndexableExpressions::new().visit_protocol_description(h, pd)?; SelectableExpressions::new().visit_protocol_description(h, pd)?; - CheckMainComponent::new().visit_protocol_description(h, pd)?; Ok(pd) } }