Add "call"

This commit is contained in:
bʰedoh₂ swé 2024-06-17 21:06:49 +05:00
parent 065df34eb1
commit 906a72a195
2 changed files with 28 additions and 0 deletions

View File

@ -145,6 +145,10 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
let arg = instruction.arg.clone();
instructions::modules::exec(stack, arg, mod_name.clone())
},
"call" => {
let arg = instruction.arg.clone();
instructions::modules::call(stack, arg)
},
"native" => {
let arg = instruction.arg.clone();
instructions::native::native(stack, arg)

View File

@ -34,3 +34,27 @@ pub fn exec(mut stack: &mut Stack, arg: String, mod_name: String) {
stack.memory.push(*item);
}
}
pub fn call(mut stack: &mut Stack, mod_name: String) {
let len: u16 = stack.memory.pop();
let mut rev: String = String::new();
for _i in 0..len {
rev.push(stack.memory.pop() as u8 as char);
}
let tocall: String = rev.chars().rev().collect::<String>();
if mod_name == tocall {
eprintln!("{}", RunError::ExecuteItself(tocall.to_string()));
std::process::exit(2);
}
let lines = fs::read_to_string(&tocall).expect(&format!("{}", RunError::FailToReadFile));
let mut temp_stack = Stack::new(stack.memory.size());
parse(&mut temp_stack, &lines);
execute(&mut temp_stack, Some(&mut stack), tocall.clone());
for item in temp_stack.memory.iter() {
stack.memory.push(*item);
}
}