labast v1.0.4

* Исправлен баг c "quit"
* Исправлены "band", "bnand", "bor", "bxor" (спасибо Владу)
* Переписан "args" (это вообще не надо было как оказывается, но зато код стал более читабельным)
* Исправлен баг с парсером
This commit is contained in:
n3taeli 2024-02-06 22:39:44 +02:00
parent 4fb06f8f34
commit 0eb3af2c47
Signed by: n3tael
GPG Key ID: F305925762F035A8
17 changed files with 68 additions and 66 deletions

2
Cargo.lock generated
View File

@ -10,7 +10,7 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]] [[package]]
name = "labast" name = "labast"
version = "1.0.3" version = "1.0.4"
dependencies = [ dependencies = [
"fastrand", "fastrand",
"pico-args", "pico-args",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "labast" name = "labast"
version = "1.0.3" version = "1.0.4"
authors = [ "n3tael" ] authors = [ "n3tael" ]
edition = "2021" edition = "2021"
description = "A zero-dependencies Labaski interpreter written in Rust." description = "A zero-dependencies Labaski interpreter written in Rust."

View File

@ -3,3 +3,6 @@ A Labaski interpreter written in Rust. Fully supports Labashki specs 1.12.
## Examples ## Examples
See `scripts` directory. See `scripts` directory.
## Syntax highlighting
* Notepad++: `syntax-highlighting/notepad++.xml` ([how to install](https://stackoverflow.com/questions/4913834/how-do-you-add-syntax-highlighting-for-less-in-notepad))

View File

@ -0,0 +1 @@
$println Hello, world!

View File

@ -0,0 +1,4 @@
; https://esolangs.org/wiki/User:XKCD_Random_Number
push 4
meow

View File

@ -0,0 +1,12 @@
; Truth-machine on Labashki
@ 0
scan
kjnz 1
meow
quit
@ 1
push 1
meow
jmp 1

16
scripts/std/print.lb Normal file
View File

@ -0,0 +1,16 @@
;
; BSD Zero Clause License
;
; Copyright (C) 2024 by Bedohswe
;
; Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@ 0
args 1
kjz 1
putc
jmp 0
@ 1
quit

View File

@ -1,2 +0,0 @@
size
#exec std\unix\ВАРЁНЫЙНОСОК.lb

View File

@ -1,5 +1,3 @@
use std::io::Read;
use crate::{errors::RunError, instructions, stack::{Instruction, Stack}}; use crate::{errors::RunError, instructions, stack::{Instruction, Stack}};
fn find_labels(stack: &mut 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), "maxsize" => instructions::maxsize::maxsize(&mut stack.memory),
"#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg), "#expr" => instructions::expr::expr(&mut stack.memory, &instruction.arg),
"nop" => continue, "nop" => continue,
"quit" => std::process::exit(0), "quit" => break,
"exit" => instructions::exit::exit(&instruction.data), "exit" => instructions::exit::exit(&instruction.data),
// Platform-specific // 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::process::exit(2);
} }
} }
std::io::stdin().bytes();
} }
} }

View File

@ -1,21 +1,18 @@
use crate::{errors::RunError, stack::Stack}; use crate::{errors::RunError, stack::Stack};
pub fn args(memory: &mut Vec<u16>, origin_stack: &mut Option<&mut Stack>, data: &u16) { 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); eprintln!("{}", RunError::RequestArgsInMainModule);
std::process::exit(2); 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)));
}
} }

View File

@ -1,14 +1,8 @@
use crate::errors::RunError; use crate::errors::RunError;
pub fn band(memory: &mut Vec<u16>) { pub fn band(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let c: u16 = if a & b { memory.push(a & b);
1
} else {
0
};
memory.push(c);
} }

View File

@ -1,14 +1,8 @@
use crate::errors::RunError; use crate::errors::RunError;
pub fn bnand(memory: &mut Vec<u16>) { pub fn bnand(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let c: u16 = if !(a & b) { memory.push(!(a & b));
1
} else {
0
};
memory.push(c);
} }

View File

@ -1,14 +1,8 @@
use crate::errors::RunError; use crate::errors::RunError;
pub fn bor(memory: &mut Vec<u16>) { pub fn bor(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let c: u16 = if a | b { memory.push(a | b);
1
} else {
0
};
memory.push(c);
} }

View File

@ -1,14 +1,8 @@
use crate::errors::RunError; use crate::errors::RunError;
pub fn bxor(memory: &mut Vec<u16>) { pub fn bxor(memory: &mut Vec<u16>) {
let a: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let a: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let b: bool = memory.pop().expect(&format!("{}", RunError::MemoryEmpty)) == 1; let b: u16 = memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
let c: u16 = if a ^ b { memory.push(a ^ b);
1
} else {
0
};
memory.push(c);
} }

View File

@ -11,7 +11,6 @@ pub fn exec(mut stack: &mut Stack, arg: &String, mod_name: &String) {
let mut temp_stack = Stack::new(); let mut temp_stack = Stack::new();
parse(&mut temp_stack, &lines); parse(&mut temp_stack, &lines);
execute(&mut temp_stack, Some(&mut stack), &arg.clone()); execute(&mut temp_stack, Some(&mut stack), &arg.clone());
for item in temp_stack.memory { for item in temp_stack.memory {

View File

@ -14,8 +14,8 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
} }
let command: [String; 2] = inst_line.split_once(char::is_whitespace) let command: [String; 2] = inst_line.split_once(char::is_whitespace)
.map_or_else(|| [inst_line.to_string(), "".to_string()], .map_or_else(|| [inst_line.trim().to_string(), "".to_string()],
|(first, second)| [first.to_string(), second.to_string()]); |(first, second)| [first.trim().to_string(), second.trim().to_string()]);
let name: String; let name: String;
let mut arg: String = String::new(); let mut arg: String = String::new();