[UNVERIFIED] update to spec 1.10, parser rewrite.

This commit is contained in:
2024-02-06 01:48:44 +02:00
parent cff855b9cb
commit f2b551a586
14 changed files with 182 additions and 70 deletions
+26
View File
@@ -0,0 +1,26 @@
use crate::{errors::RunError, stack::Instruction};
pub fn insert(program: &mut Vec<Instruction>, arg: &String, data: &u16) {
let args: Vec<char> = arg.chars().collect();
if args[0] == '#' || args[0] == '$' {
eprintln!("{}", RunError::AttemptToInsertHashOrStringInst);
std::process::exit(2);
}
if args[0] == '\n' {
eprintln!("{}", RunError::AttemptToInsertEmptyConst);
std::process::exit(2);
}
if args[0] == '@' || args[1] == ' ' {
eprintln!("{}", RunError::AttemptToInsertALabel);
std::process::exit(2);
}
program.push(Instruction {
name: arg.to_string(),
arg: "\n".to_string(),
data: *data
});
}
+1 -1
View File
@@ -6,7 +6,7 @@ pub fn jnz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_count
std::process::exit(2);
}
if memory.last() != Some(&0) {
if memory.pop() != Some(0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
}
}
+1 -1
View File
@@ -6,7 +6,7 @@ pub fn jz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counte
std::process::exit(2);
}
if memory.last() == Some(&0) {
if memory.pop() == Some(0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
}
}
+12
View File
@@ -0,0 +1,12 @@
use crate::errors::RunError;
pub fn kjnz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
if labels[*data as usize].is_none() {
eprintln!("{}", RunError::UnknownLabel(*data));
std::process::exit(2);
}
if memory.last() != Some(&0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
}
}
+12
View File
@@ -0,0 +1,12 @@
use crate::errors::RunError;
pub fn kjz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counter: &mut u16, data: &u16) {
if labels[*data as usize].is_none() {
eprintln!("{}", RunError::UnknownLabel(*data));
std::process::exit(2);
}
if memory.last() == Some(&0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16;
}
}
+5 -1
View File
@@ -32,4 +32,8 @@ pub mod band;
pub mod bor;
pub mod bxor;
pub mod bnand;
pub mod not;
pub mod not;
pub mod kjnz;
pub mod kjz;
pub mod print;
pub mod insert;
+3
View File
@@ -0,0 +1,3 @@
pub fn print(arg: &String) {
println!("{}", arg);
}