本帖最后由 nemon 于 2024-8-8 18:39 编辑
上次说到,用rust改写出一段可以运行出helloWorld的代码(https://bbs.eeworld.com.cn/thread-1289540-1-1.html)。
很明显,功能太单一了,BF的程序都写死在里面了,根本没有啥用。所以这次要改的更有用一些。
首先,要能够读入程序文件名作为参数,那么就要能够在执行程序的时候接受一个命令行参数。
这个功能这样实现,首先要引入这个东东:
use std::env::args;
然后,用一行就可以读到命令行参数:
let prog_file_path_name = args().nth(1).expect("请提供文件名");
考虑到有可能没有传入文件名作为参数,所以这里加了expect来提示这种错误。
然后要解决的,是从文件中读取内容。这次引入的要多一个:
use std::fs;
use std::io;
然后用1行把整个文件读到字符串变量里。由于如果读文件出现问题,会出现io错误,所以main函数声明也要改一改:
fn main() -> io::Result<()> {
// 从命令行参数获取文件名
let prog_file_path_name = args().nth(1).expect("请提供文件名");
//读取文件
let file_contents = fs::read_to_string(prog_file_path_name).expect("文件名有误");
//println!("文件内容:\n{}", file_contents);
//TODO:解释执行文件
Ok(())
}
然后,把上一篇的处理过程另写一个函数,并接受字符串作为输入。
完成的代码如下:
use std::env::args;
use std::fs;
use std::io;
//读入文件的主函数
fn main() -> io::Result<()> {
// 从命令行参数获取文件名
let prog_file_path_name = args().nth(1).expect("请提供文件名");
//读取文件
let file_contents = fs::read_to_string(prog_file_path_name).expect("文件名有误");
//println!("文件内容:\n{}", file_contents);
//解释执行文件
explain_and_run(file_contents);
Ok(())
}
// 解释执行文件
fn explain_and_run(prog_contents : String) {
let s_prog = String::from(prog_contents);
let length = s_prog.len();
let mut i = 0;
let mut memory: [u8; 32] = [0; 32];
let mut position_in_memory = 0;
let mut call_stack: [u32; 32] = [0; 32];
let mut position_in_stack = 0;
while i < length {
let mut c = &s_prog[i..(i + 1)];
match c {
">" => {
//println!("{}:{}",c,"指针加一");
position_in_memory += 1;
}
"<" => {
//println!("{}:{}",c,"指针减一");
position_in_memory -= 1;
}
"+" => {
//println!("{}:{}",c,"指针指向的字节的值加一");
memory[position_in_memory] = memory[position_in_memory] + 1;
}
"-" => {
//println!("{}:{}",c,"指针指向的字节的值减一");
memory[position_in_memory] -= 1;
}
"." => {
//println!("{}:{}",c,"输出指针指向的单元内容(ASCⅡ码)");
let _ch = std::char::from_u32(memory[position_in_memory] as u32).unwrap();
print!("{}", _ch);
}
"," => {
let mut input = String::new();
let _ = std::io::stdin().read_line(&mut input).expect("无法读取输入");
let char_input = input.chars().next().expect("没有输入字符");
memory[position_in_memory] = char_input as u8 ;
}
"[" => {
//println!("{}:{}",c,"如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处");
if memory[position_in_memory] == 0 {
position_in_stack += 1;
call_stack[position_in_stack] = i as u32;
let mut count = 1;
while count > 0 {
i += 1;
c = &s_prog[i..(i + 1)];
match c {
"[" => {
count += 1;
}
"]" => {
count -= 1;
}
&_ => count = count,
}
}
} else {
position_in_stack += 1;
call_stack[position_in_stack] = i as u32;
}
}
"]" => {
//println!("{}:{}",c,"如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处");
if memory[position_in_memory] != 0 {
i = call_stack[position_in_stack] as usize;
} else {
position_in_stack -= 1;
}
}
_ => println!("非法字符 {}", c),
}
// 循环体
i += 1;
}
}
想省事的同学可以直接下载:
cargo build 之后就可以用 cargo run example.bf的形式来执行BF语言写的程序了。
这里附赠helloWorld的bf程序:
以及用cargo build --release生成的exe:
到此BrainRust的功能已经完成,下一篇,我们将引入ust的面型对象风格。