Skip to content

Commit e70fd02

Browse files
committed
Fixed GOTO to use correct line index then added INPUT, APPEND_VALUE, CLEAR_VALUE, ADD, SUB, NL, CLS, and WAIT
1 parent e52693a commit e70fd02

File tree

5 files changed

+157
-4
lines changed

5 files changed

+157
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cargo-features = ["edition2024"]
22

33
[package]
44
name = "floor-basic"
5-
version = "0.1.0"
5+
version = "0.1.1"
66
edition = "2024"
77

88
[dependencies]

src/executor/commands.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use lazy_static::lazy_static;
22
use std::collections::HashMap;
33
use std::io::{self, Write};
44
use std::sync::Mutex;
5+
use std::thread;
6+
use std::time::Duration;
57

68
lazy_static! {
79
static ref LATEST_STORED_VALUE: Mutex<String> = Mutex::new("undefined".to_string());
@@ -52,3 +54,79 @@ pub fn PX(_: &[&str]) {
5254
pub fn GOTO(_: &[&str]) {
5355
// This command is handled directly, as some values cannot be passed here.
5456
}
57+
58+
// OPCODE: 0x05
59+
// Takes text-based input from the console, then assigns the value to the current value.
60+
pub fn INPUT(_: &[&str]) {
61+
let mut stored_value = LATEST_STORED_VALUE.lock().unwrap();
62+
let mut input: String = String::new();
63+
64+
print!("> ");
65+
io::stdout().flush().unwrap();
66+
io::stdin()
67+
.read_line(&mut input)
68+
.expect("Could not read your input!");
69+
*stored_value = input.trim().to_string();
70+
}
71+
72+
// OPCODE: 0x06
73+
// Appends a text-based value to the currently-stored value.
74+
pub fn APPEND_VALUE(args: &[&str]) {
75+
let mut stored_value = LATEST_STORED_VALUE.lock().unwrap();
76+
stored_value.push_str(&args.join(" "));
77+
}
78+
79+
// OPCODE: 0x07
80+
// Clears the currently-stored value
81+
pub fn CLEAR_VALUE(_: &[&str]) {
82+
let mut stored_value = LATEST_STORED_VALUE.lock().unwrap();
83+
*stored_value = "undefined".to_string();
84+
}
85+
86+
// OPCODE: 0x08
87+
// Adds a number-based value to the currently-stored value.
88+
pub fn ADD(args: &[&str]) {
89+
if let Ok(addend) = args.join(" ").parse::<i32>() {
90+
let mut stored_value = LATEST_STORED_VALUE.lock().unwrap();
91+
if let Ok(current) = stored_value.parse::<i32>() {
92+
*stored_value = (current + addend).to_string();
93+
}
94+
}
95+
}
96+
97+
// OPCODE: 0x09
98+
// Subtracts a number-based value from the currently-stored value.
99+
pub fn SUB(args: &[&str]) {
100+
if let Ok(subtrahend) = args.join(" ").parse::<i32>() {
101+
let mut stored_value = LATEST_STORED_VALUE.lock().unwrap();
102+
if let Ok(current) = stored_value.parse::<i32>() {
103+
*stored_value = (current - subtrahend).to_string();
104+
}
105+
}
106+
}
107+
108+
// OPCODE: 0x0A
109+
// Prints a new line to the console.
110+
pub fn NL(_: &[&str]) {
111+
println!();
112+
}
113+
114+
// OPCODE: 0x0B
115+
// Clears the console.
116+
pub fn CLS(_: &[&str]) {
117+
print!("\x1B[2J\x1B[1;1H");
118+
io::stdout().flush().unwrap();
119+
}
120+
121+
// OPCODE: 0x0C
122+
// Waits/sleeps a specific amount of milliseconds, based on the currently-stored value.
123+
pub fn WAIT(_: &[&str]) {
124+
let stored_value = LATEST_STORED_VALUE.lock().unwrap();
125+
if let Ok(ms) = stored_value.parse::<u64>() {
126+
thread::sleep(Duration::from_millis(ms));
127+
} else {
128+
println!(
129+
"When running the wait command, the currently-stored value must be in milliseconds!"
130+
);
131+
}
132+
}

src/executor/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ pub fn execute(line: &str) {
99
valid_commands.insert("SETVALUE", commands::SETVALUE);
1010
valid_commands.insert("PRINT", commands::PRINT);
1111
valid_commands.insert("PX", commands::PX);
12+
valid_commands.insert("INPUT", commands::INPUT);
13+
valid_commands.insert("APPEND_VALUE", commands::APPEND_VALUE);
14+
valid_commands.insert("CLEAR_VALUE", commands::CLEAR_VALUE);
15+
valid_commands.insert("ADD", commands::ADD);
16+
valid_commands.insert("SUB", commands::SUB);
17+
valid_commands.insert("NL", commands::NL);
18+
valid_commands.insert("CLS", commands::CLS);
19+
valid_commands.insert("WAIT", commands::WAIT);
1220

1321
if let Some(action) = valid_commands.get(cmd) {
1422
action(&parts[1..]);

src/main.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,82 @@ fn handle_fbin(buffer: Vec<u8>) -> Result<()> {
5151
let target: usize = buffer[i] as usize;
5252
i += 1;
5353

54-
if target < buffer.len() {
55-
i = target;
54+
let adjusted: usize = target.saturating_sub(1);
55+
56+
if adjusted < buffer.len() {
57+
i = adjusted;
5658
continue;
5759
} else {
5860
println!("GOTO target {} out of bounds", target);
5961
break;
6062
}
6163
}
6264

65+
// INPUT
66+
// Takes text-based input from the console, then assigns the value to the current value.
67+
0x05 => executor::execute("INPUT"),
68+
69+
// APPEND_VALUE
70+
// Appends a text-based value to the currently-stored value.
71+
0x06 => {
72+
let mut arg_bytes: Vec<u8> = Vec::new();
73+
while i < buffer.len() && buffer[i] != 0x00 {
74+
arg_bytes.push(buffer[i]);
75+
i += 1;
76+
}
77+
i += 1;
78+
if let Ok(arg_str) = String::from_utf8(arg_bytes) {
79+
executor::execute(&format!("APPEND_VALUE {}", arg_str));
80+
}
81+
continue;
82+
}
83+
84+
// CLEAR_VALUE
85+
// Clears the currently-stored value
86+
0x07 => executor::execute("CLEAR_VALUE"),
87+
88+
// ADD
89+
// Adds a number-based value to the currently-stored value.
90+
0x08 => {
91+
let mut arg_bytes: Vec<u8> = Vec::new();
92+
while i < buffer.len() && buffer[i] != 0x00 {
93+
arg_bytes.push(buffer[i]);
94+
i += 1;
95+
}
96+
i += 1;
97+
if let Ok(arg_str) = String::from_utf8(arg_bytes) {
98+
executor::execute(&format!("ADD {}", arg_str));
99+
}
100+
continue;
101+
}
102+
103+
// SUB
104+
// Subtracts a number-based value from the currently-stored value.
105+
0x09 => {
106+
let mut arg_bytes: Vec<u8> = Vec::new();
107+
while i < buffer.len() && buffer[i] != 0x00 {
108+
arg_bytes.push(buffer[i]);
109+
i += 1;
110+
}
111+
i += 1;
112+
if let Ok(arg_str) = String::from_utf8(arg_bytes) {
113+
executor::execute(&format!("SUB {}", arg_str));
114+
}
115+
continue;
116+
}
117+
118+
// NL
119+
// Prints a new line to the console.
120+
0x0A => executor::execute("NL"),
121+
122+
// CLS
123+
// Clears the console.
124+
0x0B => executor::execute("CLS"),
125+
126+
// WAIT
127+
// Waits/sleeps a specific amount of milliseconds, based on the currently-stored value.
128+
0x0C => executor::execute("WAIT"),
129+
63130
// Other(s)
64131
_ => {}
65132
}

0 commit comments

Comments
 (0)