Changeset - 3af8b6eaec1b
[Not reviewed]
0 2 0
Hans-Dieter Hiep - 5 years ago 2020-02-05 17:47:22
hdh@cwi.nl
Implement change to protocol description trait interface
2 files changed with 47 insertions and 30 deletions:
0 comments (0 inline, 0 general)
src/common.rs
Show inline comments
 
@@ -41,12 +41,13 @@ pub enum Polarity {
 
    Getter, // input port (from the perspective of the component)
 
}
 

	
 
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Copy, Clone, Debug)]
 
pub struct Key(u64);
 

	
 
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
 
pub enum MainComponentErr {
 
    NoSuchComponent,
 
    NonPortTypeParameters,
 
}
 
pub trait ProtocolDescription: Sized {
 
    type S: ComponentState<D = Self>;
src/protocol/mod.rs
Show inline comments
 
@@ -39,49 +39,65 @@ impl ProtocolDescription for ProtocolDescriptionImpl {
 
                let mut vec: Vec<u8> = Vec::new();
 
                err.write(&source, &mut vec).unwrap();
 
                Err(String::from_utf8_lossy(&vec).to_string())
 
            }
 
        }
 
    }
 
    // fn main_interface_polarities(&self) -> Vec<Polarity> {
 
    //     let def = &self.heap[self.main];
 
    //     let mut result = Vec::new();
 
    //     for &param 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<ComponentStateImpl, NewMainErr> {
 
        // Find symbol with identifier
 
        let def = self.heap[self.root].get_definition_ident(&heap, identifier);
 
    fn component_polarities(&self, identifier: &[u8]) -> Result<Vec<Polarity>, 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 &param 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 &param 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)
 
        }
 
    }
 
}
 

	
 
#[derive(Debug, Clone)]
 
pub struct ComponentStateImpl {
 
    prompt: Prompt,
0 comments (0 inline, 0 general)