diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2022-10-11 09:50:36 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2022-10-11 09:50:36 -0300 |
commit | e8a11468fefd81c2d9181ca15bceaf71e09a5345 (patch) | |
tree | 252b7bf0043ebc4cd4dd589c79a298d47a686730 | |
parent | dcf5620842edb8a4632cde794c8e511e075d3662 (diff) |
Initial support for empty and simple patterns
-rw-r--r-- | src/pattern.rs | 4 | ||||
-rw-r--r-- | src/vm/de.rs | 148 | ||||
-rw-r--r-- | src/vm/mod.rs | 26 |
3 files changed, 111 insertions, 67 deletions
diff --git a/src/pattern.rs b/src/pattern.rs index a0c0ec3..6795481 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -42,12 +42,12 @@ impl<O: Serialize> Pattern<O> { De: Deserialize<'de>, { let mut err = Default::default(); - let frames = Default::default(); + let mut frames = Default::default(); //let mut output = Default::default(); let interp = vm::Interpreter::new( &self.consts, &mut err, - &frames, + &mut frames, //&mut output, ); let (pack, obj) = vm::Packer::new(interp, MAX_CALLS).deserialize(der)?; diff --git a/src/vm/de.rs b/src/vm/de.rs index 640b1aa..3290ab0 100644 --- a/src/vm/de.rs +++ b/src/vm/de.rs @@ -38,11 +38,11 @@ pub(crate) struct Packer<'pat, 'state, O: Serialize> { } struct FramesMut<'packer, 'pat> { - frames: std::cell::RefMut<'packer, Vec<Frame<'pat>>>, + frames: &'packer mut Vec<Frame<'pat>>, } struct Frames<'packer, 'pat> { - frames: std::cell::Ref<'packer, Vec<Frame<'pat>>>, + frames: &'packer Vec<Frame<'pat>>, } impl<'packer, 'pat> FramesMut<'packer, 'pat> { @@ -52,9 +52,47 @@ impl<'packer, 'pat> FramesMut<'packer, 'pat> { fn iter_active_mut<'a>(&'a mut self) -> impl Iterator<Item=&'a mut Frame<'pat>> where 'packer: 'a { self.iter_mut().filter(|frame| { - frame.matches + frame.matches() }) } + + /// Steps the VM into the next operation. + fn step_in(&mut self) { + self.iter_mut().for_each(|frame| { + if let Some(ref mut overstep) = frame.overstep { + *overstep += 1; + } else { + if !frame.next() { + frame.overstep = Some(1); + } else if matches!( + frame.op(), + PatternElement::ValueSubtree { .. }, + ) { + todo!() + } + } + }); + } + + fn step_out<'de>(&mut self, pack: Pack<'pat, 'de>) -> Pack<'pat, 'de> { + self.iter_mut().for_each(|frame| { + if let Some(ref mut overstep) = frame.overstep { + if *overstep > 0 { + *overstep -= 1; + } + } else { + if let Some(0) = frame.overstep { + frame.overstep = None; + } + if frame.overstep.is_none() { + if frame.prev() { + todo!(); + } + } + } + }); + pack + } } impl<'packer, 'pat> Frames<'packer, 'pat> { @@ -64,7 +102,7 @@ impl<'packer, 'pat> Frames<'packer, 'pat> { fn iter_active<'a>(&'a self) -> impl Iterator<Item=&'a Frame<'pat>> where 'packer: 'a { self.iter().filter(|frame| { - frame.matches + frame.matches() }) } } @@ -84,13 +122,13 @@ impl<'pat, 'state, 'de, O: Serialize> Packer<'pat, 'state, O> { fn frames_mut(&mut self) -> FramesMut<'_, 'pat> { FramesMut { - frames: self.interp.frames.borrow_mut(), + frames: &mut *self.interp.frames, } } fn frames(&mut self) -> Frames<'_, 'pat> { Frames { - frames: self.interp.frames.borrow(), + frames: &*self.interp.frames, } } } @@ -115,7 +153,7 @@ impl<'pat, 'state, 'de, O: Serialize> Packer<'pat, 'state, O> { // override it in the relevant Pattern call. impl<'pat, 'state, 'de, O> serde::de::DeserializeSeed<'de> -for Packer<'pat, 'state, O> +for &mut Packer<'pat, 'state, O> where O: Serialize, { @@ -127,11 +165,7 @@ where where D: serde::Deserializer<'de> { - self.frames_mut().iter_mut().for_each(|frame| { - if !frame.next() { - frame.matches = false; - } - }); + self.frames_mut().step_in(); let pat = self.interp.pat; let target_type = self.frames().iter_active().fold( Type::IgnoredAny, @@ -159,54 +193,62 @@ where }, ); match target_type { - Type::Any => deserializer.deserialize_any(self), - Type::IgnoredAny => deserializer.deserialize_ignored_any(self), - Type::Bool => deserializer.deserialize_bool(self), - Type::I8 => deserializer.deserialize_i8(self), - Type::I16 => deserializer.deserialize_i16(self), - Type::I32 => deserializer.deserialize_i32(self), - Type::I64 => deserializer.deserialize_i64(self), - Type::I128 => deserializer.deserialize_i128(self), - Type::U8 => deserializer.deserialize_u8(self), - Type::U16 => deserializer.deserialize_u16(self), - Type::U32 => deserializer.deserialize_u32(self), - Type::U64 => deserializer.deserialize_u64(self), - Type::U128 => deserializer.deserialize_u128(self), - Type::F32 => deserializer.deserialize_f32(self), - Type::F64 => deserializer.deserialize_f64(self), - Type::Char => deserializer.deserialize_char(self), + Type::Any => deserializer.deserialize_any(&mut *self), + Type::IgnoredAny => { + deserializer.deserialize_ignored_any(&mut *self) + }, + Type::Bool => deserializer.deserialize_bool(&mut *self), + Type::I8 => deserializer.deserialize_i8(&mut *self), + Type::I16 => deserializer.deserialize_i16(&mut *self), + Type::I32 => deserializer.deserialize_i32(&mut *self), + Type::I64 => deserializer.deserialize_i64(&mut *self), + Type::I128 => deserializer.deserialize_i128(&mut *self), + Type::U8 => deserializer.deserialize_u8(&mut *self), + Type::U16 => deserializer.deserialize_u16(&mut *self), + Type::U32 => deserializer.deserialize_u32(&mut *self), + Type::U64 => deserializer.deserialize_u64(&mut *self), + Type::U128 => deserializer.deserialize_u128(&mut *self), + Type::F32 => deserializer.deserialize_f32(&mut *self), + Type::F64 => deserializer.deserialize_f64(&mut *self), + Type::Char => deserializer.deserialize_char(&mut *self), Type::Str if !self.collecting => { - deserializer.deserialize_str(self) + deserializer.deserialize_str(&mut *self) + }, + Type::Str | Type::String => { + deserializer.deserialize_string(&mut *self) }, - Type::Str | Type::String => deserializer.deserialize_string(self), Type::Bytes if !self.collecting => { - deserializer.deserialize_bytes(self) + deserializer.deserialize_bytes(&mut *self) }, Type::Bytes | Type::ByteBuf => { - deserializer.deserialize_byte_buf(self) + deserializer.deserialize_byte_buf(&mut *self) + }, + Type::Option => deserializer.deserialize_option(&mut *self), + Type::Unit => deserializer.deserialize_unit(&mut *self), + Type::Seq => deserializer.deserialize_seq(&mut *self), + Type::Map => deserializer.deserialize_map(&mut *self), + Type::Identifier => { + deserializer.deserialize_identifier(&mut *self) + }, + Type::Tuple(len) => { + deserializer.deserialize_tuple(len, &mut *self) }, - Type::Option => deserializer.deserialize_option(self), - Type::Unit => deserializer.deserialize_unit(self), - Type::Seq => deserializer.deserialize_seq(self), - Type::Map => deserializer.deserialize_map(self), - Type::Identifier => deserializer.deserialize_identifier(self), - Type::Tuple(len) => deserializer.deserialize_tuple(len, self), Type::UnitStruct(name) => { - deserializer.deserialize_unit_struct(name, self) + deserializer.deserialize_unit_struct(name, &mut *self) }, Type::NewtypeStruct(name) => { - deserializer.deserialize_newtype_struct(name, self) + deserializer.deserialize_newtype_struct(name, &mut *self) }, Type::TupleStruct { name, len } => { - deserializer.deserialize_tuple_struct(name, len, self) + deserializer.deserialize_tuple_struct(name, len, &mut *self) }, Type::Struct { name, fields } => { - deserializer.deserialize_struct(name, fields, self) + deserializer.deserialize_struct(name, fields, &mut *self) }, Type::Enum { name, variants } => { - deserializer.deserialize_enum(name, variants, self) + deserializer.deserialize_enum(name, variants, &mut *self) }, - } + }.map(|(pack, obj)| (self.frames_mut().step_out(pack), obj)) } } @@ -238,7 +280,7 @@ macro_rules! vs { } impl<'pat, 'state, 'de, O> serde::de::Visitor<'de> -for Packer<'pat, 'state, O> +for &mut Packer<'pat, 'state, O> where O: Serialize, { @@ -548,8 +590,8 @@ mod tests { fn test_broken() { let consts = PatternConstants::<()>::default(); let mut err = Default::default(); - let frames = Default::default(); - let interp = Interpreter::new(&consts, &mut err, &frames); + let mut frames = Default::default(); + let interp = Interpreter::new(&consts, &mut err, &mut frames); let _ = Packer::new(interp, MAX_CALLS); } @@ -558,8 +600,8 @@ mod tests { let mut consts = PatternConstants::<()>::default(); consts.protos.push(Vec::new()); let mut err = Default::default(); - let frames = Default::default(); - let interp = Interpreter::new(&consts, &mut err, &frames); + let mut frames = Default::default(); + let interp = Interpreter::new(&consts, &mut err, &mut frames); let _ = Packer::new(interp, MAX_CALLS); } @@ -569,8 +611,8 @@ mod tests { consts.protos.push(Vec::new()); let mut der = JsonDeserializer::from_str("{}"); let mut err = Default::default(); - let frames = Default::default(); - let interp = Interpreter::new(&consts, &mut err, &frames); + let mut frames = Default::default(); + let interp = Interpreter::new(&consts, &mut err, &mut frames); let pack = Packer::new(interp, MAX_CALLS).deserialize(&mut der).unwrap(); } @@ -588,8 +630,8 @@ mod tests { ]); let mut der = JsonDeserializer::from_str("3"); let mut err = Default::default(); - let frames = Default::default(); - let interp = Interpreter::new(&consts, &mut err, &frames); + let mut frames = Default::default(); + let interp = Interpreter::new(&consts, &mut err, &mut frames); let packed = Packer::new(interp, MAX_CALLS).deserialize(&mut der); let (pack, obj) = packed.unwrap(); assert!(obj.is_none()); diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 190fa3d..8122778 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -355,9 +355,9 @@ pub(crate) struct Interpreter<'pat, 'state, O: Serialize> { /// The pattern currently being processed. pat: &'pat PatternConstants<O>, /// The error override (if any). - error: &'state Cell<Option<crate::errors::MatchError>>, + error: &'state mut Option<crate::errors::MatchError>, /// The current interpreter frames. - frames: &'state RefCell<Vec<Frame<'pat>>>, + frames: &'state mut Vec<Frame<'pat>>, ///// The final output. //output: &'state Cell<Pack<'pat, 'de>>, } @@ -367,8 +367,8 @@ pub(crate) struct Frame<'pat> { ops: &'pat [PatternElement], /// The instruction index being processed. iar: Option<usize>, - /// Whether this frame matches. - matches: bool, + /// How many steps this frame has failed to match. + overstep: Option<usize>, ///// Elements collected while processing this frame? //path: Pack<'pat, 'de>, } @@ -377,21 +377,19 @@ impl<'pat, 'state, O: Serialize> Interpreter<'pat, 'state, O> { pub(crate) fn new( pat: &'pat PatternConstants<O>, error: &'state mut Option<crate::errors::MatchError>, - frames: &'state RefCell<Vec<Frame<'pat>>>, + frames: &'state mut Vec<Frame<'pat>>, //output: &'state mut Pack<'pat, 'de>, ) -> Self { - let mut mut_frames = frames.borrow_mut(); - debug_assert!(mut_frames.is_empty()); - mut_frames.push(Frame { + debug_assert!(frames.is_empty()); + frames.push(Frame { ops: &pat.protos[0], iar: None, - matches: true, + overstep: None, //path: Default::default(), }); - drop(mut_frames); Self { pat: pat, - error: Cell::from_mut(error), + error: error, frames: frames, //output: Cell::from_mut(output), } @@ -463,10 +461,14 @@ impl<'pat> Frame<'pat> { /// /// Panics if called on a non-matching frame or if iteration hasn't begun. fn op(&self) -> PatternElement { - assert!(self.matches, "op() called on non-matching frame"); + assert!(self.matches(), "op() called on non-matching frame"); self.ops[self.iar.expect("ops[iar]")] } + fn matches(&self) -> bool { + self.overstep.is_none() + } + /// Rewinds the instruction address register. /// /// # Returns |