Giter Club home page Giter Club logo

Comments (2)

fin-ger avatar fin-ger commented on August 16, 2024

Planning to compile to the following structure:

mod bottle_filler {
    #[derive(Debug, Clone)]
    pub enum State {
        Idle { remaining: f32 },
        Filling(f32),
        Empty,
    }
    
    #[allow(dead_code)]
    enum StateIdentifier {
        Idle,
        Filling,
        Empty,
    }
    pub struct Machine {
        current_state: StateIdentifier,
        idle: State,
        filling: State,
        empty: State,
    }
    impl Machine {
        pub fn new() -> Self {
            Self {
                current_state: StateIdentifier::Idle,
                idle: State::Idle {
                    remaining: <f32 as core::default::Default>::default(),
                },
                filling: State::Filling(<f32 as core::default::Default>::default()),
                empty: State::Empty,
            }
        }
        
        fn handle_fill_bottle(_from: Option<&State>, to: &mut State, volume: f32) {
            if let State::Filling(ref mut filling_volume) = to {
                *filling_volume = volume;
            }
        }
        
        fn handle_fuel_tank(_from: Option<&State>, to: &mut State) {
            if let State::Idle { ref mut remaining } = to {
                *remaining = 42.0;
            }
        }
        
        fn handle_bottle_full(from: Option<&State>, to: &mut State) {
            if let Some(State::Filling(ref volume)) = from {
                if let State::Idle { ref mut remaining } = to {
                    *remaining -= volume;
                }
            }
        }
        
        #[allow(unreachable_code)]
        pub fn fill(&mut self, volume: f32) -> bool {
            match self.current_state {
                StateIdentifier::Idle => {
                    {
                        let volume = volume;
                        if let State::Idle { remaining } = self.idle {
                            if (|| -> bool {
                                volume <= remaining
                            })() {
                                self.current_state = StateIdentifier::Filling;
                                Self::handle_fill_bottle(Some(&mut self.idle), &mut self.filling, volume);
                                return true;
                            }
                        }
                    }
                    
                    {
                        let volume = volume;
                        if let State::Idle { remaining } = self.idle {
                            if (|| -> bool {
                                volume > remaining
                            })() {
                                self.current_state = StateIdentifier::Empty;
                                return true;
                            }
                        }
                    }
                },
                
                _ => {},
            }
        
            false
        }
        
        #[allow(unreachable_code)]
        pub fn dump(&mut self) -> bool {
            match self.current_state {
                StateIdentifier::Idle => {
                    {
                        if let State::Idle { .. } = self.idle {
                            self.current_state = StateIdentifier::Empty;
                            return true;
                        }
                    }
                },
                
                _ => {},
            }
        
            false
        }
        
        #[allow(unreachable_code)]
        pub fn fuel(&mut self) -> bool {
            match self.current_state {
                StateIdentifier::Idle => {
                    {
                        if let State::Idle { .. } = self.idle {
                            self.current_state = StateIdentifier::Idle;
                            Self::handle_fuel_tank(None, &mut self.idle);
                            return true;
                        }
                    }
                },
                
                StateIdentifier::Empty => {
                    {
                        if let State::Empty = self.empty {
                            self.current_state = StateIdentifier::Idle;
                            Self::handle_fuel_tank(Some(&mut self.empty), &mut self.idle);
                            return true;
                        }
                    }
                }
                
                _ => {},
            }
        
            false
        }
        
        #[allow(unreachable_code)]
        pub fn full(&mut self) -> bool {
            match self.current_state {
                StateIdentifier::Filling => {
                    {
                        if let State::Filling(..) = self.filling {
                            self.current_state = StateIdentifier::Idle;
                            Self::handle_bottle_full(Some(&mut self.filling), &mut self.idle);
                            return true;
                        }
                    }
                },
                
                _ => {},
            }
        
            false
        }
    }
}

from declarative-state-machine.

fin-ger avatar fin-ger commented on August 16, 2024

Implemented data types for the new syntax and code generation from dummy data on transition-rework. Parsing will be the next step.

from declarative-state-machine.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.