forked from n3tael/labast
labast v1.0.4
* Исправлен баг c "quit" * Исправлены "band", "bnand", "bor", "bxor" (спасибо Владу) * Переписан "args" (это вообще не надо было как оказывается, но зато код стал более читабельным) * Исправлен баг с парсером
This commit is contained in:
+1
-5
@@ -1,5 +1,3 @@
|
||||
use std::io::Read;
|
||||
|
||||
use crate::{errors::RunError, instructions, stack::{Instruction, Stack}};
|
||||
|
||||
fn find_labels(stack: &mut Stack) {
|
||||
@@ -133,7 +131,7 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
|
||||
"maxsize" => instructions::maxsize::maxsize(&mut stack.memory),
|
||||
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
|
||||
"nop" => continue,
|
||||
"quit" => std::process::exit(0),
|
||||
"quit" => break,
|
||||
"exit" => instructions::exit::exit(&instruction.data),
|
||||
|
||||
// Platform-specific
|
||||
@@ -148,7 +146,5 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
|
||||
std::process::exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
std::io::stdin().bytes();
|
||||
}
|
||||
}
|
||||
+10
-13
@@ -1,21 +1,18 @@
|
||||
use crate::{errors::RunError, stack::Stack};
|
||||
|
||||
pub fn args(memory: &mut Vec<u16>, origin_stack: &mut Option<&mut Stack>, data: &u16) {
|
||||
let origin_memory = &mut origin_stack.as_mut().expect(&format!("{}", RunError::RequestArgsInMainModule)).memory;
|
||||
if let Some(origin_stack) = origin_stack.as_mut() {
|
||||
let mut data = *data;
|
||||
|
||||
if *origin_memory == *memory {
|
||||
if data == 0 {
|
||||
data = origin_stack.memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
}
|
||||
|
||||
for _ in 0..data {
|
||||
memory.push(origin_stack.memory.pop().expect(&format!("{}", RunError::MemoryEmpty)));
|
||||
}
|
||||
} else {
|
||||
eprintln!("{}", RunError::RequestArgsInMainModule);
|
||||
std::process::exit(2);
|
||||
}
|
||||
|
||||
let quanity: u16;
|
||||
if *data == 0 {
|
||||
quanity = origin_memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
} else {
|
||||
quanity = *data;
|
||||
}
|
||||
|
||||
for _ in 0..quanity {
|
||||
memory.push(origin_memory.pop().expect(&format!("{}", RunError::MemoryEmpty)));
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,8 @@
|
||||
use crate::errors::RunError;
|
||||
|
||||
pub fn band(memory: &mut Vec<u16>) {
|
||||
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
|
||||
let c: u16 = if a & b {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
memory.push(c);
|
||||
}
|
||||
memory.push(a & b);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
use crate::errors::RunError;
|
||||
|
||||
pub fn bnand(memory: &mut Vec<u16>) {
|
||||
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
|
||||
let c: u16 = if !(a & b) {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
memory.push(c);
|
||||
}
|
||||
memory.push(!(a & b));
|
||||
}
|
||||
|
||||
+4
-10
@@ -1,14 +1,8 @@
|
||||
use crate::errors::RunError;
|
||||
|
||||
pub fn bor(memory: &mut Vec<u16>) {
|
||||
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
|
||||
let c: u16 = if a | b {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
memory.push(c);
|
||||
}
|
||||
memory.push(a | b);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
use crate::errors::RunError;
|
||||
|
||||
pub fn bxor(memory: &mut Vec<u16>) {
|
||||
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1;
|
||||
let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
|
||||
|
||||
let c: u16 = if a ^ b {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
memory.push(c);
|
||||
}
|
||||
memory.push(a ^ b);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ pub fn exec(mut stack: &mut Stack, arg: &String, mod_name: &String) {
|
||||
let mut temp_stack = Stack::new();
|
||||
|
||||
parse(&mut temp_stack, &lines);
|
||||
|
||||
execute(&mut temp_stack, Some(&mut stack), &arg.clone());
|
||||
|
||||
for item in temp_stack.memory {
|
||||
|
||||
+2
-2
@@ -14,8 +14,8 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
|
||||
}
|
||||
|
||||
let command: [String; 2] = inst_line.split_once(char::is_whitespace)
|
||||
.map_or_else(|| [inst_line.to_string(), "".to_string()],
|
||||
|(first, second)| [first.to_string(), second.to_string()]);
|
||||
.map_or_else(|| [inst_line.trim().to_string(), "".to_string()],
|
||||
|(first, second)| [first.trim().to_string(), second.trim().to_string()]);
|
||||
|
||||
let name: String;
|
||||
let mut arg: String = String::new();
|
||||
|
||||
Reference in New Issue
Block a user