diff --git a/bf/fibonacci.bf b/bf/fibonacci.bf index ac429e9..85bdace 100644 --- a/bf/fibonacci.bf +++ b/bf/fibonacci.bf @@ -4,3 +4,6 @@ [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>> ]<<< ] +This program doesn't terminate; you will have to kill it. +Daniel B Cristofani (cristofdathevanetdotcom) +http://www.hevanet.com/cristofd/brainfuck/ diff --git a/bf/game_of_life.bf b/bf/game_of_life.bf new file mode 100644 index 0000000..e068233 --- /dev/null +++ b/bf/game_of_life.bf @@ -0,0 +1,38 @@ +[life.b -- John Horton Conway's Game of Life +(c) 2021 Daniel B. Cristofani +http://brainfuck.org/] + +>>>->+>+++++>(++++++++++)[[>>>+<<<-]>+++++>+>>+[<<+>>>>>+<<<-]<-]>>>>[ + [>>>+>+<<<<-]+++>>+[<+>>>+>+<<<-]>>[>[[>>>+<<<-]<]<<++>+>>>>>>-]<- +]+++>+>[[-]<+<[>+++++++++++++++++<-]<+]>>[ + [+++++++++.-------->>>]+[-<<<]>>>[>>,----------[>]<]<<[ + <<<[ + >--[<->>+>-<<-]<[[>>>]+>-[+>>+>-]+[<<<]<-]>++>[<+>-] + >[[>>>]+[<<<]>>>-]+[->>>]<-[++>]>[------<]>+++[<<<]> + ]< + ]>[ + -[+>>+>-]+>>+>>>+>[<<<]>->+>[ + >[->+>+++>>++[>>>]+++<<<++<<<++[>>>]>>>]<<<[>[>>>]+>>>] + <<<<<<<[<<++<+[-<<<+]->++>>>++>>>++<<<<]<<<+[-<<<+]+>->>->> + ]<<+<<+<<<+<<-[+<+<<-]+<+[ + ->+>[-<-<<[<<<]>[>>[>>>]<<+<[<<<]>-]] + <[<[<[<<<]>+>>[>>>]<<-]<[<<<]]>>>->>>[>>>]+> + ]>+[-<<[-]<]-[ + [>>>]<[<<[<<<]>>>>>+>[>>>]<-]>>>[>[>>>]<<<<+>[<<<]>>-]> + ]<<<<<<[---<-----[-[-[<->>+++<+++++++[-]]]]<+<+]> + ]>> +] + +[This program simulates the Game of Life cellular automaton. + +It duplicates the interface of the classic program at +http://www.linusakesson.net/programming/brainfuck/index.php, +but this program was written from scratch. + +Type e.g. "be" to toggle the fifth cell in the second row, "q" to quit, +or a bare linefeed to advance one generation. + +Grid wraps toroidally. Board size in parentheses in first line (2-166 work). + +This program is licensed under a Creative Commons Attribution-ShareAlike 4.0 +International License (http://creativecommons.org/licenses/by-sa/4.0/).] diff --git a/bf/helloworld.bf b/bf/hello_world.bf similarity index 100% rename from bf/helloworld.bf rename to bf/hello_world.bf diff --git a/lox/bf.lox b/lox/bf.lox index e43f8b9..dd99608 100644 --- a/lox/bf.lox +++ b/lox/bf.lox @@ -68,7 +68,7 @@ class Interpreter { } if (instruction == ",") { - + tape.current.data = promptAscii("> "); } if (instruction == "[" and tape.current.data == 0) { @@ -96,5 +96,5 @@ class Interpreter { } } -var interpreter = Interpreter("bf/helloworld.bf"); +var interpreter = Interpreter("bf/game_of_life.bf"); interpreter.run(); diff --git a/lox/read_ascii.lox b/lox/read_ascii.lox new file mode 100644 index 0000000..07e18b1 --- /dev/null +++ b/lox/read_ascii.lox @@ -0,0 +1,2 @@ +var a = promptAscii("> "); +print a; diff --git a/src/native_functions.rs b/src/native_functions.rs index 2e61e13..b33ab72 100644 --- a/src/native_functions.rs +++ b/src/native_functions.rs @@ -187,7 +187,7 @@ impl Callable for AsciiOut { struct PromptAscii; impl Callable for PromptAscii { fn name(&self) -> String { - "prompt_ascii".into() + "promptAscii".into() } fn arity(&self) -> usize { @@ -207,14 +207,17 @@ impl Callable for PromptAscii { .first() .ok_or(CallingError::CallFailed("arg not readable".into()))?; if let Value::String(prompt) = prompt { - print!("{prompt} "); - stdout().flush(); + print!("{prompt}"); + stdout() + .flush() + .map_err(|e| CallingError::CallFailed(e.to_string()))?; let mut buffer = [0; 1]; - stdin().read_exact(&mut buffer); + stdin() + .read_exact(&mut buffer) + .map_err(|e| CallingError::CallFailed(e.to_string()))?; - todo!() - // Ok(Value::Number(buffer[0] as char)) + Ok(Value::Number(OrderedFloat(buffer[0] as f64))) } else { Err(CallingError::CallFailed("prompt must be a string".into())) } @@ -241,7 +244,7 @@ pub fn all() -> Vec<(String, Value)> { Value::Callable((Rc::new(ReadFile {}), CallableType::Function)), ), ( - "prompt".into(), + "promptAscii".into(), Value::Callable((Rc::new(PromptAscii {}), CallableType::Function)), ), ]