forked from n3tael/labast
update v1.0.3
* fixed _unix_random bug * add custom instruction "random" * update to spec 1.12 * more cli options
This commit is contained in:
+17
-12
@@ -102,12 +102,14 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
|
||||
"kjz" => instructions::kjz::kjz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
|
||||
|
||||
// Input/Output
|
||||
"putc" => instructions::putc::putc(&mut stack.memory),
|
||||
"getc" => instructions::getc::getc(&mut stack.memory),
|
||||
"scan" => instructions::scan::scan(&mut stack.memory),
|
||||
"meow" => instructions::meow::meow(&mut stack.memory),
|
||||
"#print" => instructions::print::print(&instruction.arg),
|
||||
"$print" => instructions::print::print(&instruction.arg),
|
||||
"putc" => instructions::putc::putc(&mut stack.memory),
|
||||
"getc" => instructions::getc::getc(&mut stack.memory),
|
||||
"scan" => instructions::scan::scan(&mut stack.memory),
|
||||
"meow" => instructions::meow::meow(&mut stack.memory),
|
||||
"#print" => instructions::print::print(&instruction.arg),
|
||||
"$print" => instructions::print::print(&instruction.arg),
|
||||
"#println" => instructions::println::println(&instruction.arg),
|
||||
"$println" => instructions::println::println(&instruction.arg),
|
||||
|
||||
// Modules
|
||||
"args" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data),
|
||||
@@ -126,18 +128,21 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
|
||||
},
|
||||
|
||||
// Special
|
||||
"dump" => instructions::dump::dump(&mut stack.memory),
|
||||
"size" => instructions::size::size(&mut stack.memory),
|
||||
"dump" => instructions::dump::dump(&mut stack.memory),
|
||||
"size" => instructions::size::size(&mut stack.memory),
|
||||
"maxsize" => instructions::maxsize::maxsize(&mut stack.memory),
|
||||
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
|
||||
"nop" => continue,
|
||||
"quit" => std::process::exit(0),
|
||||
"exit" => instructions::exit::exit(&instruction.data),
|
||||
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
|
||||
"nop" => continue,
|
||||
"quit" => std::process::exit(0),
|
||||
"exit" => instructions::exit::exit(&instruction.data),
|
||||
|
||||
// Platform-specific
|
||||
#[cfg(target_family = "unix")]
|
||||
"_unix_random" => instructions::_unix_random::_unix_random(&mut stack.memory),
|
||||
|
||||
// Labast-specific
|
||||
"random" => instructions::random::random(&mut stack.memory),
|
||||
|
||||
_ => {
|
||||
eprintln!("Unknown instruction: {}", instruction.name);
|
||||
std::process::exit(2);
|
||||
|
||||
@@ -6,9 +6,9 @@ use crate::RunError;
|
||||
pub fn _unix_random(memory: &mut Vec<u16>) {
|
||||
let mut rng = File::open("/dev/urandom").unwrap();
|
||||
|
||||
let mut buffer = [0u8; 1];
|
||||
let mut buffer = [0u8; 2];
|
||||
rng.read_exact(&mut buffer).unwrap();
|
||||
|
||||
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
memory.push((buffer[0] as u16) % (*data - 1));
|
||||
memory.push((u16::from_ne_bytes(buffer)) % (*data - 1));
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
pub fn maxsize(memory: &mut Vec<u16>) {
|
||||
memory.push(65535);
|
||||
memory.push(u16::MAX);
|
||||
}
|
||||
@@ -36,4 +36,6 @@ pub mod not;
|
||||
pub mod kjnz;
|
||||
pub mod kjz;
|
||||
pub mod print;
|
||||
pub mod insert;
|
||||
pub mod println;
|
||||
pub mod insert;
|
||||
pub mod random;
|
||||
@@ -1,3 +1,3 @@
|
||||
pub fn print(arg: &String) {
|
||||
println!("{}", arg);
|
||||
print!("{}", arg);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub fn println(arg: &String) {
|
||||
println!("{}", arg);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
use fastrand;
|
||||
use crate::RunError;
|
||||
|
||||
pub fn random(memory: &mut Vec<u16>) {
|
||||
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
memory.push(fastrand::u16(..) % (*data - 1));
|
||||
}
|
||||
+54
-13
@@ -1,4 +1,5 @@
|
||||
use std::{env, fs, io::{BufRead, Write}};
|
||||
use std::{fs, io::{BufRead, Write}};
|
||||
|
||||
use errors::RunError;
|
||||
use execute::execute;
|
||||
use parse::parse;
|
||||
@@ -10,17 +11,28 @@ mod execute;
|
||||
mod instructions;
|
||||
mod errors;
|
||||
|
||||
const HELP: &str = "\
|
||||
labast - Labaski interpreter written in Rust.
|
||||
|
||||
usage: labast [options] [ file.lb ]
|
||||
|
||||
options:
|
||||
-h, --help prints help information
|
||||
-v, --version prints version
|
||||
";
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let args = match parse_args() {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}.", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if args.get(2) == Some(&String::from("-h"))
|
||||
|| args.get(2) == Some(&String::from("--help")) {
|
||||
eprintln!("Usage: {0} [file]", args[0]);
|
||||
return;
|
||||
}
|
||||
let mut stack = Stack::new();
|
||||
|
||||
if args.len() < 2 {
|
||||
let mut stack = Stack::new();
|
||||
if args.input.is_none() {
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
@@ -37,12 +49,41 @@ fn main() {
|
||||
parse(&mut stack, &line.trim());
|
||||
execute(&mut stack, None, &"inline".to_string());
|
||||
}
|
||||
} else {
|
||||
let input = &args.input.expect("error: no input file");
|
||||
let lines = fs::read_to_string(&input).expect(&format!("{}", RunError::FailToReadFile));
|
||||
|
||||
parse(&mut stack, &lines);
|
||||
execute(&mut stack, None, &String::from(input.file_name().expect("Failed to read file name.").to_str().expect("Failed to convert file name to string")));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AppArgs {
|
||||
input: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<AppArgs, pico_args::Error> {
|
||||
let mut pargs = pico_args::Arguments::from_env();
|
||||
|
||||
if pargs.contains(["-h", "--help"]) {
|
||||
print!("{}", HELP);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
let mut stack = Stack::new();
|
||||
let lines = fs::read_to_string(&args[1]).expect(&format!("{}", RunError::FailToReadFile));
|
||||
if pargs.contains(["-v", "--version"]) {
|
||||
println!("labast version {}", option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"));
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
parse(&mut stack, &lines);
|
||||
let args = AppArgs {
|
||||
input: pargs.opt_free_from_str()?,
|
||||
};
|
||||
|
||||
execute(&mut stack, None, &args[1]);
|
||||
let remaining = pargs.finish();
|
||||
if !remaining.is_empty() {
|
||||
eprintln!("Warning: unused arguments left: {:?}.", remaining);
|
||||
}
|
||||
|
||||
return Ok(args);
|
||||
}
|
||||
Reference in New Issue
Block a user