Changeset - 935120e93ef1
[Not reviewed]
0 3 0
Hans-Dieter Hiep - 5 years ago 2020-02-05 17:27:11
hdh@cwi.nl
Rework setup of protocol description
3 files changed with 26 insertions and 47 deletions:
0 comments (0 inline, 0 general)
src/protocol/ast.rs
Show inline comments
 
@@ -1217,12 +1217,20 @@ impl Root {
 
            if h[h[def].identifier()] == h[id] {
 
                return Some(def);
 
            }
 
        }
 
        None
 
    }
 
    pub fn get_definition_ident(&self, h: &Heap, id: &[u8]) -> Option<DefinitionId> {
 
        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<DeclarationId> {
 
        for &decl in self.declarations.iter() {
 
            if h[h[decl].identifier()] == h[id] {
 
                return Some(decl);
 
            }
 
        }
src/protocol/mod.rs
Show inline comments
 
@@ -12,14 +12,13 @@ use crate::protocol::inputsource::*;
 
use crate::protocol::parser::*;
 
use std::hint::unreachable_unchecked;
 

	
 
pub struct ProtocolDescriptionImpl {
 
    heap: Heap,
 
    source: InputSource,
 
    root: RootId,
 
    main: ComponentId,
 
    root: RootId
 
}
 

	
 
impl std::fmt::Debug for ProtocolDescriptionImpl {
 
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
 
        write!(f, "Protocol")
 
    }
 
@@ -31,17 +30,13 @@ impl ProtocolDescription for ProtocolDescriptionImpl {
 
    fn parse(buffer: &[u8]) -> Result<Self, String> {
 
        let mut heap = Heap::new();
 
        let mut source = InputSource::from_buffer(buffer).unwrap();
 
        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<u8> = Vec::new();
 
                err.write(&source, &mut vec).unwrap();
 
                Err(String::from_utf8_lossy(&vec).to_string())
 
            }
 
@@ -61,18 +56,29 @@ impl ProtocolDescription for ProtocolDescriptionImpl {
 
    //         } else {
 
    //             unreachable!()
 
    //         }
 
    //     }
 
    //     result
 
    // }
 
    fn new_main_component(&self, ports: &[Key]) -> ComponentStateImpl {
 
    fn new_main_component(&self, identifier: &[u8], ports: &[(Polarity, Key)]) -> Result<ComponentStateImpl, NewMainErr> {
 
        // 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) }
 
    }
 
}
 

	
src/protocol/parser.rs
Show inline comments
 
@@ -1781,46 +1781,12 @@ impl Visitor for SelectableExpressions {
 
        } else {
 
            Ok(())
 
        }
 
    }
 
}
 

	
 
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 &param 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,
 
}
 

	
 
impl<'a> Parser<'a> {
 
    pub fn new(source: &'a mut InputSource) -> Self {
 
@@ -1841,13 +1807,12 @@ impl<'a> Parser<'a> {
 
        LinkStatements::new().visit_protocol_description(h, pd)?;
 
        BuildLabels::new().visit_protocol_description(h, pd)?;
 
        ResolveLabels::new().visit_protocol_description(h, pd)?;
 
        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)
 
    }
 
}
 

	
 
#[cfg(test)]
 
mod tests {
0 comments (0 inline, 0 general)