Compare commits
2 commits
4102f5a351
...
c3a73e3104
Author | SHA1 | Date | |
---|---|---|---|
c3a73e3104 | |||
b686a670d2 |
2 changed files with 33 additions and 31 deletions
|
@ -1,5 +1,4 @@
|
||||||
(print sum of 123 and 45)
|
(print sum of 2 numbers)
|
||||||
|
|
||||||
o
|
o
|
||||||
> 1230 B
|
> 1230 B
|
||||||
vo A-+
|
vo A-+
|
||||||
|
@ -17,8 +16,7 @@ v> * 1 o ^
|
||||||
F -R0 * 4 B
|
F -R0 * 4 B
|
||||||
*| # < -A8 v
|
*| # < -A8 v
|
||||||
| # <
|
| # <
|
||||||
#|
|
#-| v <
|
||||||
-| v <
|
|
||||||
| *-|
|
| *-|
|
||||||
F +B * ^
|
F +B * ^
|
||||||
#< \F----
|
#< \F----
|
||||||
|
|
58
src/main.rs
58
src/main.rs
|
@ -1,4 +1,4 @@
|
||||||
use std::{env, fs, io::stdin, process::exit};
|
use std::{env, fs, io::{stdin, stdout, Write}, process::exit};
|
||||||
|
|
||||||
use owo_colors::OwoColorize;
|
use owo_colors::OwoColorize;
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ struct Machine {
|
||||||
input: Vec<u8>,
|
input: Vec<u8>,
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
output: Vec<u8>,
|
output: Vec<u8>,
|
||||||
state: State,
|
|
||||||
steps: usize,
|
steps: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,13 +78,6 @@ enum Direction {
|
||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq)]
|
|
||||||
enum State {
|
|
||||||
#[default]
|
|
||||||
Running,
|
|
||||||
EndOfProgram,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<_> = env::args().collect();
|
let args: Vec<_> = env::args().collect();
|
||||||
if args.len() <= 1 {
|
if args.len() <= 1 {
|
||||||
|
@ -109,9 +101,11 @@ fn main() {
|
||||||
|
|
||||||
let mut interpreter = Machine::new(&source, input_data);
|
let mut interpreter = Machine::new(&source, input_data);
|
||||||
print!("\x1B[2J"); // clear screen
|
print!("\x1B[2J"); // clear screen
|
||||||
|
interpreter.show();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
interpreter.show();
|
print!(":> ");
|
||||||
|
stdout().flush().unwrap();
|
||||||
let mut input_text = String::new();
|
let mut input_text = String::new();
|
||||||
stdin().read_line(&mut input_text).unwrap();
|
stdin().read_line(&mut input_text).unwrap();
|
||||||
let action: Vec<_> = input_text.trim().split_ascii_whitespace().collect();
|
let action: Vec<_> = input_text.trim().split_ascii_whitespace().collect();
|
||||||
|
@ -122,18 +116,35 @@ fn main() {
|
||||||
.unwrap_or(&input_text)
|
.unwrap_or(&input_text)
|
||||||
.as_bytes()[6..],
|
.as_bytes()[6..],
|
||||||
);
|
);
|
||||||
|
interpreter.print_input();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const HELP_TEXT:&str = r#"\n | step
|
||||||
|
- step once
|
||||||
|
step [n]
|
||||||
|
- step n times
|
||||||
|
input [text]
|
||||||
|
- append text to the input buffer
|
||||||
|
dbg | debug
|
||||||
|
- debug print interpreter state
|
||||||
|
q | quit | exit
|
||||||
|
- exit to terminal
|
||||||
|
"#;
|
||||||
match action.as_slice() {
|
match action.as_slice() {
|
||||||
[] | ["step"] => interpreter.step_once(),
|
[] | ["step"] => {
|
||||||
|
interpreter.step_once();
|
||||||
|
interpreter.show();
|
||||||
|
}
|
||||||
["dbg" | "debug"] => {
|
["dbg" | "debug"] => {
|
||||||
dbg!(&interpreter);
|
dbg!(&interpreter);
|
||||||
}
|
}
|
||||||
["step", num] => _ = num.parse().map(|n| interpreter.step(n)),
|
["step", num] => _ = num.parse().map(|n| interpreter.step(n)),
|
||||||
["run"] => interpreter.run(),
|
|
||||||
["q" | "exit" | "quit"] => break,
|
["q" | "exit" | "quit"] => break,
|
||||||
|
["h" | "help" | "?"] => {
|
||||||
_ => println!("{}", "unrecognised command".red()),
|
println!("{}", HELP_TEXT.green());
|
||||||
|
}
|
||||||
|
_ => println!("{}", "unrecognised command (try 'help')".red()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,23 +159,12 @@ impl Machine {
|
||||||
input,
|
input,
|
||||||
input_index: 0,
|
input_index: 0,
|
||||||
output: Vec::new(),
|
output: Vec::new(),
|
||||||
state: State::default(),
|
|
||||||
steps: 0,
|
steps: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self) {
|
|
||||||
while self.state == State::Running {
|
|
||||||
self.step_once();
|
|
||||||
self.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn step(&mut self, n: usize) {
|
fn step(&mut self, n: usize) {
|
||||||
for _ in 0..n {
|
for _ in 0..n {
|
||||||
if self.state != State::Running {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
self.step_once();
|
self.step_once();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,7 +230,7 @@ impl Machine {
|
||||||
print!("{}", c.cyan());
|
print!("{}", c.cyan());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
print!(" :: ");
|
print!(".");
|
||||||
for (val, dir) in marbles_on_row {
|
for (val, dir) in marbles_on_row {
|
||||||
print!("{}", format!("{}{} ", dir.char(), val).magenta())
|
print!("{}", format!("{}{} ", dir.char(), val).magenta())
|
||||||
}
|
}
|
||||||
|
@ -239,10 +239,14 @@ impl Machine {
|
||||||
if !self.output.is_empty() {
|
if !self.output.is_empty() {
|
||||||
println!("output: '{}'", String::from_utf8_lossy(&self.output));
|
println!("output: '{}'", String::from_utf8_lossy(&self.output));
|
||||||
}
|
}
|
||||||
|
self.print_input();
|
||||||
|
println!("steps: {}", self.steps);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_input(&self){
|
||||||
if !self.input.is_empty() {
|
if !self.input.is_empty() {
|
||||||
println!("input: '{}'", String::from_utf8_lossy(&self.input));
|
println!("input: '{}'", String::from_utf8_lossy(&self.input));
|
||||||
}
|
}
|
||||||
println!("steps: {}", self.steps);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn step_once(&mut self) {
|
fn step_once(&mut self) {
|
||||||
|
|
Loading…
Reference in a new issue