update 1.0.2

This commit is contained in:
2024-02-05 17:13:32 +02:00
parent 317fa500b8
commit a6fbe841f1
27 changed files with 351 additions and 196 deletions
+71 -29
View File
@@ -1,4 +1,6 @@
use crate::{instructions, stack::Stack};
use std::io::Read;
use crate::{errors::RunError, instructions, stack::{Instruction, Stack}};
fn find_labels(stack: &mut Stack) {
while stack.program_counter < (stack.program.len() as u16) {
@@ -29,62 +31,102 @@ fn find_labels(stack: &mut Stack) {
}
}
fn find_prefix_operators(instruction: &mut Instruction, memory: &mut Vec<u16>) {
match instruction.name.chars().nth(0) {
Some('|') => {
instruction.name = instruction.name.chars().skip(1).collect();
instruction.data = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
},
Some('^') => {
instruction.name = instruction.name.chars().skip(1).collect();
//dbg!(&instruction);
if instruction.name.chars().nth(0) != Some('#') {
println!("{0} {1}", instruction.name, instruction.data.to_string());
} else if instruction.arg.is_empty() {
println!("{}", instruction.name);
} else {
println!("{0} {1}", instruction.name, instruction.arg);
}
}
_ => {}
}
}
pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name: &String) {
find_labels(stack);
while let Some(instruction) = stack.program.get(stack.program_counter as usize) {
while let Some(mut instruction) = stack.program.get_mut(stack.program_counter as usize) {
stack.program_counter += 1;
find_prefix_operators(&mut instruction, &mut stack.memory);
if instruction.name.starts_with('@') {
continue;
}
match instruction.name.as_str() {
// Stack operations
"PUSH" => instructions::push::push(&mut stack.memory, &instruction.data),
"PICK" => instructions::pick::pick(&mut stack.memory, &instruction.data),
"POP" => instructions::pop::pop(&mut stack.memory),
"DUP" => instructions::dup::dup(&mut stack.memory),
"SWAP" => instructions::swap::swap(&mut stack.memory),
"MEOW" => instructions::meow::meow(&mut stack.memory),
"DUMP" => instructions::dump::dump(&mut stack.memory),
"SIZE" => instructions::size::size(&mut stack.memory),
"MAXSIZE" => instructions::maxsize::maxsize(&mut stack.memory),
"push" => instructions::push::push(&mut stack.memory, &instruction.data),
"pick" => instructions::pick::pick(&mut stack.memory, &instruction.data),
"pop" => instructions::pop::pop(&mut stack.memory),
"dup" => instructions::dup::dup(&mut stack.memory),
"swap" => instructions::swap::swap(&mut stack.memory),
"meow" => instructions::meow::meow(&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),
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
// Labels
"JMP" => instructions::jmp::jmp(&mut stack.labels, &mut stack.program_counter, &instruction.data),
"JNZ" => instructions::jnz::jnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
"JZ" => instructions::jz::jz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
"jmp" => instructions::jmp::jmp(&mut stack.labels, &mut stack.program_counter, &instruction.data),
"jnz" => instructions::jnz::jnz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
"jz" => instructions::jz::jz(&mut stack.memory, &mut stack.labels, &mut stack.program_counter, &instruction.data),
// Math operations
"ADD" => instructions::add::add(&mut stack.memory),
"SUB" => instructions::sub::sub(&mut stack.memory),
"MUL" => instructions::mul::mul(&mut stack.memory),
"DIV" => instructions::div::div(&mut stack.memory),
"add" => instructions::add::add(&mut stack.memory),
"sub" => instructions::sub::sub(&mut stack.memory),
"mul" => instructions::mul::mul(&mut stack.memory),
"div" => instructions::div::div(&mut stack.memory),
"mod" => instructions::modulo::modulo(&mut stack.memory),
"and" => instructions::and::and(&mut stack.memory),
"or" => instructions::or::or(&mut stack.memory),
"xor" => instructions::xor::xor(&mut stack.memory),
"nand" => instructions::nand::nand(&mut stack.memory),
"band" => instructions::band::band(&mut stack.memory),
"bor" => instructions::bor::bor(&mut stack.memory),
"bxor" => instructions::bxor::bxor(&mut stack.memory),
"bnand" => instructions::bnand::bnand(&mut stack.memory),
"not" => instructions::not::not(&mut stack.memory),
// Console operations
"PUTC" => instructions::putc::putc(&mut stack.memory),
"GETC" => instructions::getc::getc(&mut stack.memory),
"SCAN" => instructions::scan::scan(&mut stack.memory),
"putc" => instructions::putc::putc(&mut stack.memory),
"getc" => instructions::getc::getc(&mut stack.memory),
"scan" => instructions::scan::scan(&mut stack.memory),
// Modules
"ARGS" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data),
"#EXEC" => instructions::exec::exec(stack, &instruction.arg.clone(), &mod_name),
"args" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data),
"#exec" => {
let arg = instruction.arg.clone();
instructions::exec::exec(stack, &arg, &mod_name);
}
#[cfg(target_family = "unix")]
"_UNIX_RANDOM" => instructions::_unix_random::_unix_random(&mut stack.memory),
"_unix_random" => instructions::_unix_random::_unix_random(&mut stack.memory),
// ?
"NOP" => continue,
"EXIT" => instructions::exit::exit(&instruction.data),
"QUIT" => std::process::exit(0),
"nop" => continue,
"exit" => instructions::exit::exit(&instruction.data),
"quit" => std::process::exit(0),
_ => {
eprintln!("Unknown instruction: {}", instruction.name);
std::process::exit(2);
}
}
std::io::stdin().bytes();
}
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn and(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if a && b {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn band(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if a & b {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn bnand(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if !(a & b) {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn bor(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if a | b {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn bxor(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if a ^ b {
1
} else {
0
};
memory.push(c);
}
+10
View File
@@ -23,3 +23,13 @@ pub mod exit;
pub mod maxsize;
#[cfg(target_family = "unix")]
pub mod _unix_random;
pub mod modulo;
pub mod and;
pub mod or;
pub mod xor;
pub mod nand;
pub mod band;
pub mod bor;
pub mod bxor;
pub mod bnand;
pub mod not;
+8
View File
@@ -0,0 +1,8 @@
use crate::errors::RunError;
pub fn modulo(memory: &mut Vec<u16>) {
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
memory.push(b % a);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn nand(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if !(a && b) {
1
} else {
0
};
memory.push(c);
}
+13
View File
@@ -0,0 +1,13 @@
use crate::errors::RunError;
pub fn not(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if !a {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn or(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if a || b {
1
} else {
0
};
memory.push(c);
}
+14
View File
@@ -0,0 +1,14 @@
use crate::errors::RunError;
pub fn xor(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
let c: u16 = if !a != !b {
1
} else {
0
};
memory.push(c);
}
+25 -4
View File
@@ -1,4 +1,4 @@
use std::{env, fs};
use std::{env, fs, io::{BufRead, Write}};
use errors::RunError;
use execute::execute;
use parse::parse;
@@ -12,12 +12,33 @@ mod errors;
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {0} [FILE]", args[0]);
if args.get(2) == Some(&String::from("-h"))
|| args.get(2) == Some(&String::from("--help")) {
eprintln!("Usage: {0} [file]", args[0]);
return;
}
if args.len() < 2 {
let mut stack = Stack::new();
let mut line = String::new();
loop {
line.clear();
stack.program.clear();
let stdin = std::io::stdin();
print!("> ");
std::io::stdout().flush().expect("Cannot flush the buffer.");
stdin.lock().read_line(&mut line).unwrap();
parse(&mut stack, &line.trim());
execute(&mut stack, None, &"inline".to_string());
}
}
let mut stack = Stack::new();
let lines = fs::read_to_string(&args[1]).expect(&format!("{}", RunError::FailToReadFile));