Implement new features #6
@ -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('$')
|
||||
|
@ -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]
|
||||
|
@ -22,5 +22,7 @@ pub fn insert(program: &mut Vec<Instruction>, arg: String, data: u16) {
|
||||
name: arg.to_string(),
|
||||
arg: "\n".to_string(),
|
||||
data,
|
||||
ispiped: false,
|
||||
isdrained: false
|
||||
});
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user