Files
@ fcf8c30ed335
Branch filter:
Location: CSY/reowolf/src/protocol/mod.rs - annotation
fcf8c30ed335
12.2 KiB
application/rls-services+xml
WIP on fixing type inferencer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | 3f236d37dad1 814be01095d9 06f259bf8031 4c9116f4253b 13bac4f35619 814be01095d9 06f259bf8031 aaeaf5986496 06f259bf8031 814be01095d9 814be01095d9 729feec4d37a 814be01095d9 814be01095d9 6c04a99de862 d3835a56401d d3835a56401d 6c04a99de862 6c04a99de862 6c04a99de862 6c04a99de862 6c04a99de862 06f259bf8031 06f259bf8031 a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 d3835a56401d d600dd299dde 44a98be4e4b4 06f259bf8031 06f259bf8031 0fb83f27a238 06f259bf8031 03af3095927d 4c9116f4253b 44a98be4e4b4 44a98be4e4b4 4c9116f4253b cecf94fdb875 cecf94fdb875 175721d796d7 44a98be4e4b4 44a98be4e4b4 06f259bf8031 44a98be4e4b4 06f259bf8031 d23010006486 06f259bf8031 06f259bf8031 44a98be4e4b4 9774ef9fe888 44a98be4e4b4 33ea10021de4 33ea10021de4 33ea10021de4 33ea10021de4 9774ef9fe888 9774ef9fe888 9774ef9fe888 9774ef9fe888 9774ef9fe888 06f259bf8031 9774ef9fe888 9774ef9fe888 9774ef9fe888 9774ef9fe888 06f259bf8031 4c9116f4253b 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 935120e93ef1 44a98be4e4b4 935120e93ef1 3af8b6eaec1b 935120e93ef1 44a98be4e4b4 935120e93ef1 3af8b6eaec1b 3af8b6eaec1b fdaf709bf0ca fdaf709bf0ca fdaf709bf0ca fdaf709bf0ca 3af8b6eaec1b 44a98be4e4b4 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b fdaf709bf0ca fdaf709bf0ca fdaf709bf0ca 3af8b6eaec1b fdaf709bf0ca 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 4776bcc45b33 4bfd6d133687 06f259bf8031 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 0fb83f27a238 06f259bf8031 06f259bf8031 3af8b6eaec1b 3af8b6eaec1b 3af8b6eaec1b 44a98be4e4b4 06f259bf8031 06f259bf8031 44a98be4e4b4 4c9116f4253b 44a98be4e4b4 cecf94fdb875 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 06f259bf8031 06f259bf8031 6717437470eb 5e11ebce9bc0 5e11ebce9bc0 6717437470eb 5e11ebce9bc0 5e11ebce9bc0 a226385adc2d 06f259bf8031 06f259bf8031 ac7f49602455 ac7f49602455 ac7f49602455 a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 4c9116f4253b 44a98be4e4b4 cecf94fdb875 44a98be4e4b4 44a98be4e4b4 44a98be4e4b4 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 44a98be4e4b4 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 44a98be4e4b4 06f259bf8031 5e11ebce9bc0 a226385adc2d dedf89df1602 44a98be4e4b4 06f259bf8031 dedf89df1602 44a98be4e4b4 06f259bf8031 a226385adc2d a226385adc2d a226385adc2d dedf89df1602 44a98be4e4b4 a226385adc2d dedf89df1602 44a98be4e4b4 a226385adc2d a226385adc2d a226385adc2d 06f259bf8031 dedf89df1602 06f259bf8031 dedf89df1602 dedf89df1602 06f259bf8031 dedf89df1602 dedf89df1602 06f259bf8031 a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 44a98be4e4b4 06f259bf8031 06f259bf8031 06f259bf8031 fd175763a0f7 06f259bf8031 a226385adc2d 06f259bf8031 44a98be4e4b4 06f259bf8031 a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 dedf89df1602 dedf89df1602 175721d796d7 44a98be4e4b4 44a98be4e4b4 dedf89df1602 dedf89df1602 44a98be4e4b4 06f259bf8031 175721d796d7 44a98be4e4b4 dedf89df1602 5e11ebce9bc0 5e11ebce9bc0 dedf89df1602 dedf89df1602 02eb59c6fd66 dedf89df1602 dedf89df1602 02eb59c6fd66 5e11ebce9bc0 5e11ebce9bc0 5e11ebce9bc0 dedf89df1602 5e11ebce9bc0 44a98be4e4b4 5e11ebce9bc0 5e11ebce9bc0 5e11ebce9bc0 5e11ebce9bc0 175721d796d7 44a98be4e4b4 8642f7a7bf01 5e11ebce9bc0 5e11ebce9bc0 5e11ebce9bc0 5e11ebce9bc0 44a98be4e4b4 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 175721d796d7 44a98be4e4b4 44a98be4e4b4 dedf89df1602 dedf89df1602 a226385adc2d a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 06f259bf8031 175721d796d7 44a98be4e4b4 44a98be4e4b4 dedf89df1602 dedf89df1602 dedf89df1602 dedf89df1602 dedf89df1602 06f259bf8031 a226385adc2d 06f259bf8031 06f259bf8031 06f259bf8031 6938bcacc41e 6938bcacc41e 6938bcacc41e 6938bcacc41e 6938bcacc41e 6ec2e0261a03 6938bcacc41e 6938bcacc41e 6938bcacc41e 6938bcacc41e 6938bcacc41e 6938bcacc41e a226385adc2d | mod arena;
// mod ast;
mod eval;
pub(crate) mod inputsource;
pub(crate) mod input_source2;
// mod lexer;
mod parser;
#[cfg(test)] mod tests;
// TODO: Remove when not benchmarking
pub(crate) mod ast;
pub(crate) mod ast_printer;
pub(crate) mod lexer;
lazy_static::lazy_static! {
/// Conveniently-provided protocol description initialized with a zero-length PDL string.
/// Exposed to minimize repeated initializations of this common protocol description.
pub static ref TRIVIAL_PD: std::sync::Arc<ProtocolDescription> = {
std::sync::Arc::new(ProtocolDescription::parse(b"").unwrap())
};
}
use crate::common::*;
use crate::protocol::ast::*;
use crate::protocol::eval::*;
use crate::protocol::inputsource::*;
use crate::protocol::parser::*;
/// Description of a protocol object, used to configure new connectors.
#[repr(C)]
pub struct ProtocolDescription {
heap: Heap,
source: InputSource,
root: RootId,
}
#[derive(Debug, Clone)]
pub(crate) struct ComponentState {
prompt: Prompt,
}
pub(crate) enum EvalContext<'a> {
Nonsync(&'a mut NonsyncProtoContext<'a>),
Sync(&'a mut SyncProtoContext<'a>),
// None,
}
//////////////////////////////////////////////
impl std::fmt::Debug for ProtocolDescription {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "(An opaque protocol description)")
}
}
impl ProtocolDescription {
// TODO: Allow for multi-file compilation
pub fn parse(buffer: &[u8]) -> Result<Self, String> {
// TODO: @fixme, keep code compilable, but needs support for multiple
// input files.
let source = InputSource::from_buffer(buffer).unwrap();
let mut parser = Parser::new();
parser.feed(source).expect("failed to lex source");
if let Err(err) = parser.parse() {
println!("ERROR:\n{}", err);
return Err(format!("{}", err))
}
debug_assert_eq!(parser.modules.len(), 1, "only supporting one module here for now");
let root = parser.modules[0].root_id;
return Ok(ProtocolDescription { heap: parser.heap, source: parser.modules[0].source.clone(), root });
}
pub(crate) fn component_polarities(
&self,
identifier: &[u8],
) -> Result<Vec<Polarity>, AddComponentError> {
use AddComponentError::*;
let h = &self.heap;
let root = &h[self.root];
let def = root.get_definition_ident(h, identifier);
if def.is_none() {
return Err(NoSuchComponent);
}
let def = &h[def.unwrap()];
if !def.is_component() {
return Err(NoSuchComponent);
}
for ¶m in def.parameters().iter() {
let param = &h[param];
let parser_type = &h[param.parser_type];
match parser_type.variant {
ParserTypeVariant::Input(_) | ParserTypeVariant::Output(_) => continue,
_ => {
return Err(NonPortTypeParameters);
}
}
}
let mut result = Vec::new();
for ¶m in def.parameters().iter() {
let param = &h[param];
let parser_type = &h[param.parser_type];
if let ParserTypeVariant::Input(_) = parser_type.variant {
result.push(Polarity::Getter)
} else if let ParserTypeVariant::Output(_) = parser_type.variant {
result.push(Polarity::Putter)
} else {
unreachable!()
}
}
Ok(result)
}
// expects port polarities to be correct
pub(crate) fn new_component(&self, identifier: &[u8], ports: &[PortId]) -> ComponentState {
let mut args = Vec::new();
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))),
}
}
let h = &self.heap;
let root = &h[self.root];
let def = root.get_definition_ident(h, identifier).unwrap();
ComponentState { prompt: Prompt::new(h, def, &args) }
}
}
impl ComponentState {
pub(crate) fn nonsync_run<'a: 'b, 'b>(
&'a mut self,
context: &'b mut NonsyncProtoContext<'b>,
pd: &'a ProtocolDescription,
) -> NonsyncBlocker {
let mut context = EvalContext::Nonsync(context);
loop {
let result = self.prompt.step(&pd.heap, &mut context);
match result {
// In component definitions, there are no return statements
Ok(_) => unreachable!(),
Err(cont) => match cont {
EvalContinuation::Stepping => continue,
EvalContinuation::Inconsistent => return NonsyncBlocker::Inconsistent,
EvalContinuation::Terminal => return NonsyncBlocker::ComponentExit,
EvalContinuation::SyncBlockStart => return NonsyncBlocker::SyncBlockStart,
// Not possible to end sync block if never entered one
EvalContinuation::SyncBlockEnd => unreachable!(),
EvalContinuation::NewComponent(definition_id, args) => {
// Look up definition (TODO for now, assume it is a definition)
let h = &pd.heap;
let init_state = ComponentState { prompt: Prompt::new(h, definition_id, &args) };
context.new_component(&args, init_state);
// Continue stepping
continue;
}
// Outside synchronous blocks, no fires/get/put happens
EvalContinuation::BlockFires(_) => unreachable!(),
EvalContinuation::BlockGet(_) => unreachable!(),
EvalContinuation::Put(_, _) => unreachable!(),
},
}
}
}
pub(crate) fn sync_run<'a: 'b, 'b>(
&'a mut self,
context: &'b mut SyncProtoContext<'b>,
pd: &'a ProtocolDescription,
) -> SyncBlocker {
let mut context = EvalContext::Sync(context);
loop {
let result = self.prompt.step(&pd.heap, &mut context);
match result {
// Inside synchronous blocks, there are no return statements
Ok(_) => unreachable!(),
Err(cont) => match cont {
EvalContinuation::Stepping => continue,
EvalContinuation::Inconsistent => return SyncBlocker::Inconsistent,
// First need to exit synchronous block before definition may end
EvalContinuation::Terminal => unreachable!(),
// No nested synchronous blocks
EvalContinuation::SyncBlockStart => unreachable!(),
EvalContinuation::SyncBlockEnd => return SyncBlocker::SyncBlockEnd,
// Not possible to create component in sync block
EvalContinuation::NewComponent(_, _) => unreachable!(),
EvalContinuation::BlockFires(port) => match port {
Value::Output(OutputValue(port)) => {
return SyncBlocker::CouldntCheckFiring(port);
}
Value::Input(InputValue(port)) => {
return SyncBlocker::CouldntCheckFiring(port);
}
_ => unreachable!(),
},
EvalContinuation::BlockGet(port) => match port {
Value::Output(OutputValue(port)) => {
return SyncBlocker::CouldntReadMsg(port);
}
Value::Input(InputValue(port)) => {
return SyncBlocker::CouldntReadMsg(port);
}
_ => unreachable!(),
},
EvalContinuation::Put(port, message) => {
let value;
match port {
Value::Output(OutputValue(port_value)) => {
value = port_value;
}
Value::Input(InputValue(port_value)) => {
value = port_value;
}
_ => unreachable!(),
}
let payload;
match message {
Value::Message(MessageValue(None)) => {
// Putting a null message is inconsistent
return SyncBlocker::Inconsistent;
}
Value::Message(MessageValue(Some(buffer))) => {
// Create a copy of the payload
payload = buffer;
}
_ => unreachable!(),
}
return SyncBlocker::PutMsg(value, payload);
}
},
}
}
}
}
impl EvalContext<'_> {
// fn random(&mut self) -> LongValue {
// match self {
// // EvalContext::None => unreachable!(),
// EvalContext::Nonsync(_context) => todo!(),
// EvalContext::Sync(_) => unreachable!(),
// }
// }
fn new_component(&mut self, args: &[Value], init_state: ComponentState) -> () {
match self {
// EvalContext::None => unreachable!(),
EvalContext::Nonsync(context) => {
let mut moved_ports = HashSet::new();
for arg in args.iter() {
match arg {
Value::Output(OutputValue(port)) => {
moved_ports.insert(*port);
}
Value::Input(InputValue(port)) => {
moved_ports.insert(*port);
}
_ => {}
}
}
context.new_component(moved_ports, init_state)
}
EvalContext::Sync(_) => unreachable!(),
}
}
fn new_channel(&mut self) -> [Value; 2] {
match self {
// EvalContext::None => unreachable!(),
EvalContext::Nonsync(context) => {
let [from, to] = context.new_port_pair();
let from = Value::Output(OutputValue(from));
let to = Value::Input(InputValue(to));
return [from, to];
}
EvalContext::Sync(_) => unreachable!(),
}
}
fn fires(&mut self, port: Value) -> Option<Value> {
match self {
// EvalContext::None => unreachable!(),
EvalContext::Nonsync(_) => unreachable!(),
EvalContext::Sync(context) => match port {
Value::Output(OutputValue(port)) => context.is_firing(port).map(Value::from),
Value::Input(InputValue(port)) => context.is_firing(port).map(Value::from),
_ => unreachable!(),
},
}
}
fn get(&mut self, port: Value) -> Option<Value> {
match self {
// EvalContext::None => unreachable!(),
EvalContext::Nonsync(_) => unreachable!(),
EvalContext::Sync(context) => match port {
Value::Output(OutputValue(port)) => {
context.read_msg(port).map(Value::receive_message)
}
Value::Input(InputValue(port)) => {
context.read_msg(port).map(Value::receive_message)
}
_ => unreachable!(),
},
}
}
fn did_put(&mut self, port: Value) -> bool {
match self {
EvalContext::Nonsync(_) => unreachable!("did_put in nonsync context"),
EvalContext::Sync(context) => match port {
Value::Output(OutputValue(port)) => {
context.did_put_or_get(port)
},
Value::Input(_) => unreachable!("did_put on input port"),
_ => unreachable!("did_put on non-port value")
}
}
}
}
|