diff --git a/src/execute.rs b/src/execute.rs index 7564096..ba5e3d2 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -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) diff --git a/src/instructions/modules.rs b/src/instructions/modules.rs index e869135..3611d4c 100644 --- a/src/instructions/modules.rs +++ b/src/instructions/modules.rs @@ -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::(); + + 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); + } +}