Skip to content
Snippets Groups Projects
Commit 81a13dd1 authored by MarvelousAnything's avatar MarvelousAnything
Browse files

Possible solution for state trait object saftey issue.

parent f0e4d42c
Branches
No related tags found
No related merge requests found
use crate::callable::Callable; use crate::callable::Callable;
use crate::instruction::Instruction; use crate::instruction::Instruction;
use crate::tvm::Tvm;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame { pub struct Frame {
...@@ -16,3 +17,13 @@ pub enum FrameData { ...@@ -16,3 +17,13 @@ pub enum FrameData {
Instruction(Instruction, Vec<i32>), Instruction(Instruction, Vec<i32>),
Primitive(i32), Primitive(i32),
} }
pub trait FrameEvaluator {
fn eval_frame(&mut self, frame: Frame);
}
impl FrameEvaluator for Tvm {
fn eval_frame(&mut self, frame: Frame) {
println!("Evaluating frame: {:?}", frame);
}
}
\ No newline at end of file
...@@ -3,10 +3,8 @@ use crate::callable::Callable; ...@@ -3,10 +3,8 @@ use crate::callable::Callable;
use crate::frame::Frame; use crate::frame::Frame;
use crate::tvm::Tvm; use crate::tvm::Tvm;
pub trait State : Debug + Clone { pub trait State : Debug {
fn pause(&mut self); fn tick(&mut self, tvm: &mut Tvm) -> StateResult;
fn resume(&mut self);
fn tick(&mut self) -> StateResult;
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
...@@ -26,13 +24,55 @@ pub enum TvmState { ...@@ -26,13 +24,55 @@ pub enum TvmState {
Halted, Halted,
} }
impl TvmState {
pub fn to_state(&self) -> Box<dyn State> {
match self {
TvmState::Waiting => Box::new(states::Waiting),
TvmState::Paused => Box::new(states::Paused),
TvmState::Call(callable) => Box::new(states::Call { callable: callable.clone() }),
TvmState::Eval(frame, pc) => Box::new(states::Eval { frame: frame.clone(), pc: *pc }),
TvmState::FrameEval(frame) => Box::new(states::FrameEval { frame: frame.clone() }),
TvmState::Halted => Box::new(states::Halted),
}
}
pub fn is_waiting(&self) -> bool {
matches!(self, TvmState::Waiting)
}
pub fn is_paused(&self) -> bool {
matches!(self, TvmState::Paused)
}
pub fn is_call(&self) -> bool {
matches!(self, TvmState::Call(_))
}
pub fn is_eval(&self) -> bool {
matches!(self, TvmState::Eval(_, _))
}
pub fn is_frame_eval(&self) -> bool {
matches!(self, TvmState::FrameEval(_))
}
pub fn is_halted(&self) -> bool {
matches!(self, TvmState::Halted)
}
}
pub trait Stateful : Debug { pub trait Stateful : Debug {
fn get_state(&self) -> TvmState; fn get_state(&self) -> TvmState;
fn set_state(&mut self, state: TvmState); fn set_state(&mut self, state: TvmState);
fn get_ticks(&self) -> usize; fn get_ticks(&self) -> usize;
fn increment_ticks(&mut self); fn increment_ticks(&mut self);
fn previous_state(&self) -> Option<TvmState>; fn previous_state(&self) -> Option<TvmState>;
fn pause(&mut self);
fn resume(&mut self);
fn tick(&mut self);
fn get_last_result(&self) -> Option<StateResult>;
fn is_paused(&self) -> bool; fn is_paused(&self) -> bool;
fn handle_result(&mut self, result: StateResult);
} }
impl Stateful for Tvm { impl Stateful for Tvm {
...@@ -57,8 +97,95 @@ impl Stateful for Tvm { ...@@ -57,8 +97,95 @@ impl Stateful for Tvm {
self.previous_state.clone() self.previous_state.clone()
} }
fn pause(&mut self) {
if !self.is_paused() {
self.set_state(TvmState::Paused);
}
}
fn resume(&mut self) {
if self.is_paused() {
self.set_state(self.previous_state().unwrap());
}
}
fn tick(&mut self) {
}
fn get_last_result(&self) -> Option<StateResult> {
todo!()
}
fn is_paused(&self) -> bool { fn is_paused(&self) -> bool {
self.state == TvmState::Paused self.state == TvmState::Paused
} }
fn handle_result(&mut self, result: StateResult) {
println!("Handling result: {:?}", result);
}
}
pub mod states {
use super::*;
#[derive(Debug, Clone)]
pub struct Waiting;
#[derive(Debug, Clone)]
pub struct Paused;
#[derive(Debug, Clone)]
pub struct Call {
pub callable: Callable,
}
#[derive(Debug, Clone)]
pub struct Eval {
pub frame: Frame,
pub pc: usize,
}
#[derive(Debug, Clone)]
pub struct FrameEval {
pub frame: Frame,
}
#[derive(Debug, Clone)]
pub struct Halted;
impl State for Waiting {
// Tick should do nothing.
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
StateResult::Continue
}
}
impl State for Paused {
// Tick should do nothing.
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
StateResult::Continue
}
} }
impl State for Call {
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
unimplemented!()
}
}
impl State for Eval {
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
unimplemented!()
}
}
impl State for FrameEval {
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
unimplemented!()
}
}
impl State for Halted {
fn tick(&mut self, tvm: &mut Tvm) -> StateResult {
unimplemented!()
}
}
}
use crate::frame::Frame; use crate::frame::Frame;
use crate::state::{TvmState}; use crate::state::{StateResult, TvmState};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Tvm { pub struct Tvm {
...@@ -10,6 +10,7 @@ pub struct Tvm { ...@@ -10,6 +10,7 @@ pub struct Tvm {
pub state: TvmState, pub state: TvmState,
pub ticks: usize, pub ticks: usize,
pub previous_state: Option<TvmState>, pub previous_state: Option<TvmState>,
pub last_result: Option<StateResult>,
} }
impl Default for Tvm { impl Default for Tvm {
...@@ -22,6 +23,7 @@ impl Default for Tvm { ...@@ -22,6 +23,7 @@ impl Default for Tvm {
state: TvmState::Waiting, state: TvmState::Waiting,
ticks: 0, ticks: 0,
previous_state: None, previous_state: None,
last_result: None,
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment