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
//! A rather slow [`Boolfuck`] interpreter, in case you want to help me make this more efficient go to the [corresponding github page]. //! //! For tips/tutorials concerning [`Boolfuck`] visit the official homepage. It is also possible to translate [`Brainfuck`] in [`Boolfuck`] code with this crate. //! //! # Details //! //! * Input is taken from the console and the `end of line` character is removed, //! because now programs written with `int getchar()`*( a function in `c`)* in mind work correctly.([`The Lost Kingdom`] for example) //! This causes problems with other programs which rely on `null terminated` strings or the `end of line` character. //! //! * The commands `,` and `;` both work in little-endian order. //! //! * This crate is a **a lot** faster when run with `--release`! //! //! //! # Remarks //! //! * *If you have created a better [`boolfuck`] interpreter I would not mind to transfer this name to you.* //! [`Boolfuck`]:http://samuelhughes.com/boof/ //! [`Brainfuck`]:https://en.wikipedia.org/wiki/Brainfuck //! [`The Lost Kingdom`]:http://web.archive.org/web/20111031121638/http://jonripley.com/i-fiction/games/LostKingdomBF.html //! [corresponding github page]:https://github.com/Nijaitchy/boolfuck/ mod token; mod program; pub use token::Token as Token; pub use token::ToString as ToString; pub use token::ToToken as ToToken; use program::Program; /// A simple struct which contains `boolfuck` code and can run that code. /// /// # Examples /// /// ```rust,no_run /// use boolfuck::Boolfuck; /// /// let program = Boolfuck::new(",>,>,>,>,>,>,>,<<<<<<<;>;>;>;>;>;>;>;"); /// /// program.run(false); /// ``` pub struct Boolfuck { source: Vec<Token>, } impl Boolfuck { /// Creates a new instance of `Boolfuck` taking a `&str` as input, this string is used as the source code. /// /// # Panic /// /// In case there are unclosed brackets the program `panics`! /// /// # Examples /// /// ```rust,no_run /// use boolfuck::Boolfuck; /// /// let program = Boolfuck::new(",>,>,>,>,>,>,>,<<<<<<<;>;>;>;>;>;>;>;"); /// /// program.run(false); /// ``` pub fn new(source: &str) -> Self { Boolfuck { source: source.to_token(), } } /// Creates a new instance of `Boolfuck` taking a string of `Brainfuck` code as input, this string gets converted to `Boolfuck`. /// /// This is completely unoptimized! /// /// # Panic /// /// In case there are unclosed brackets the program `panics`! /// /// # Examples /// /// ```rust,no_run /// use boolfuck::Boolfuck; /// /// let program = Boolfuck::from_brainfuck(",."); /// /// program.run(false); /// ``` pub fn from_brainfuck(source: &str) -> Self { let mut boolfuck_source = String::new(); for character in source.chars() { match character { '+' => boolfuck_source.push_str(">[>]+<[+<]>>>>>>>>>[+]<<<<<<<<<"), '-' => boolfuck_source.push_str(">>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+]<<<<<<<<<"), '<' => boolfuck_source.push_str("<<<<<<<<<"), '>' => boolfuck_source.push_str(">>>>>>>>>"), ',' => boolfuck_source.push_str(">,>,>,>,>,>,>,>,<<<<<<<<"), '.' => boolfuck_source.push_str(">;>;>;>;>;>;>;>;<<<<<<<<"), '[' => boolfuck_source.push_str(">>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>[+<<<<<<<<[>]+<[+<]"), ']' => boolfuck_source.push_str(">>>>>>>>>+<<<<<<<<+[>+]<[<]>>>>>>>>>]<[+<]"), _ => (), } } Self::new(&boolfuck_source) } /// Runs the code saved inside of the struct, in case present is true the value of all cells is shown after the program has finished. /// /// # Examples /// /// ```rust,no_run /// use boolfuck::Boolfuck; /// /// let program = Boolfuck::new(",>,>,>,>,>,>,>,<<<<<<<;>;>;>;>;>;>;>;"); /// /// program.run(true); /// ``` pub fn run(&self, present: bool) { Program::new(&self.source).run(present); } /// Returns the representation of `Boolfuck` code used in this struct, this code can be converted to string using `to_string()` /// /// and then get used a a input for another `Boolfuck` instance. /// /// # Examples /// /// ```rust,no_run /// use boolfuck::Boolfuck; /// use boolfuck::ToString; /// /// let program = Boolfuck::new(",>,>,>,>,>,>,>,<<<<<<<;>;>;>;>;>;>;>;"); /// /// let program = Boolfuck::new(&program.get_tokens().to_string()); /// /// program.run(true); /// ``` pub fn get_tokens(&self) -> &Vec<Token> { &self.source } } impl std::fmt::Debug for Boolfuck { /// Displays the used source code. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { let mut output = String::new(); for token in self.source.iter() { use Token::*; match *token { MoveLeft => output.push('<'), MoveRight => output.push('>'), BracketLeft => output.push('['), BracketRight => output.push(']'), Read => output.push(','), Write => output.push(';'), Flip => output.push('+'), EOF => (), } } write!(f, "Boolfuck: {{ {} }}", output) } } #[cfg(test)] mod tests { #[test] fn it_works() { } }