diff --git a/src/execute.rs b/src/execute.rs index ba5e3d2..83c3345 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -1,6 +1,6 @@ use crate::{ instructions, - stack::{Instruction, Stack, StackVec}, + stack::{Instruction, Stack}, }; fn find_labels(stack: &mut Stack) { @@ -28,18 +28,17 @@ fn find_labels(stack: &mut Stack) { stack.program_counter = stack.labels[0].unwrap_or(0) as u16; } -fn find_prefix_operators(instruction: &mut Instruction, memory: &mut StackVec) { +fn find_prefix_operators(instruction: &mut Instruction) { match instruction.name.chars().nth(0) { Some('|') => { instruction.name = instruction.name.chars().skip(1).collect(); - instruction.data = memory.pop(); + instruction.ispiped = true; } Some('/') => { instruction.name = instruction.name.chars().skip(1).collect(); - memory.push(instruction.data); - instruction.data = 0; + instruction.isdrained = true; } _ => {} } @@ -51,7 +50,16 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name 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); + find_prefix_operators(&mut instruction); + + if instruction.ispiped { + instruction.data = stack.memory.pop(); + } + + if instruction.isdrained { + stack.memory.push(instruction.data); + instruction.data = 0; + } if instruction.name.chars().nth(0) == Some('#') || instruction.name.chars().nth(0) == Some('$') diff --git a/src/instructions/native.rs b/src/instructions/native.rs index 20e7c11..f295a01 100644 --- a/src/instructions/native.rs +++ b/src/instructions/native.rs @@ -11,11 +11,10 @@ extern fn stack_pop_callback() -> u16 { } #[no_mangle] -extern fn stack_push_callback(data: u16) -> i32 { +extern fn stack_push_callback(data: u16){ unsafe { (*STACKPTR).memory.push(data) }; - return 0; } #[no_mangle] diff --git a/src/instructions/self_modify.rs b/src/instructions/self_modify.rs index ec75b49..be8c31d 100644 --- a/src/instructions/self_modify.rs +++ b/src/instructions/self_modify.rs @@ -22,5 +22,7 @@ pub fn insert(program: &mut Vec, arg: String, data: u16) { name: arg.to_string(), arg: "\n".to_string(), data, + ispiped: false, + isdrained: false }); } diff --git a/src/parse.rs b/src/parse.rs index 6617ce3..deae48a 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -48,7 +48,7 @@ pub fn parse(stack: &mut Stack, file_content: &str) { } if name != "" { - let inst = Instruction { name, arg, data }; + let inst = Instruction { name, arg, data, ispiped: false, isdrained: false }; stack.program.push(inst); } } diff --git a/src/stack.rs b/src/stack.rs index 601be6b..ae371b5 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -7,6 +7,8 @@ pub struct Instruction { pub name: String, pub arg: String, pub data: u16, + pub ispiped: bool, + pub isdrained: bool } pub struct Stack {