From 906a72a19516ff6b83e3200f9b1c7b432258ed44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Mon, 17 Jun 2024 21:06:49 +0500 Subject: [PATCH] Add "call" --- src/execute.rs | 4 ++++ src/instructions/modules.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) 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); + } +}