Implement new features #6

Merged
n3tael merged 9 commits from bedohswe/labast:master into master 2024-06-20 20:39:40 +00:00
5 changed files with 20 additions and 9 deletions
Showing only changes of commit fb1207bae5 - Show all commits

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
instructions, instructions,
stack::{Instruction, Stack, StackVec}, stack::{Instruction, Stack},
}; };
fn find_labels(stack: &mut 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; 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) { match instruction.name.chars().nth(0) {
Some('|') => { Some('|') => {
instruction.name = instruction.name.chars().skip(1).collect(); instruction.name = instruction.name.chars().skip(1).collect();
instruction.data = memory.pop(); instruction.ispiped = true;
} }
Some('/') => { Some('/') => {
instruction.name = instruction.name.chars().skip(1).collect(); instruction.name = instruction.name.chars().skip(1).collect();
memory.push(instruction.data); instruction.isdrained = true;
instruction.data = 0;
} }
_ => {} _ => {}
} }
@ -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) { while let Some(mut instruction) = stack.program.get_mut(stack.program_counter as usize) {
stack.program_counter += 1; 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('#') if instruction.name.chars().nth(0) == Some('#')
|| instruction.name.chars().nth(0) == Some('$') || instruction.name.chars().nth(0) == Some('$')

View File

@ -11,11 +11,10 @@ extern fn stack_pop_callback() -> u16 {
} }
#[no_mangle] #[no_mangle]
extern fn stack_push_callback(data: u16) -> i32 { extern fn stack_push_callback(data: u16){
unsafe { unsafe {
(*STACKPTR).memory.push(data) (*STACKPTR).memory.push(data)
}; };
return 0;
} }
#[no_mangle] #[no_mangle]

View File

@ -22,5 +22,7 @@ pub fn insert(program: &mut Vec<Instruction>, arg: String, data: u16) {
name: arg.to_string(), name: arg.to_string(),
arg: "\n".to_string(), arg: "\n".to_string(),
data, data,
ispiped: false,
isdrained: false
}); });
} }

View File

@ -48,7 +48,7 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
} }
if name != "" { if name != "" {
let inst = Instruction { name, arg, data }; let inst = Instruction { name, arg, data, ispiped: false, isdrained: false };
stack.program.push(inst); stack.program.push(inst);
} }
} }

View File

@ -7,6 +7,8 @@ pub struct Instruction {
pub name: String, pub name: String,
pub arg: String, pub arg: String,
pub data: u16, pub data: u16,
pub ispiped: bool,
pub isdrained: bool
} }
pub struct Stack { pub struct Stack {