From 0eb3af2c47caf802d7ae7ac914e32e1a9d7d4571 Mon Sep 17 00:00:00 2001 From: n3taeli Date: Tue, 6 Feb 2024 22:39:44 +0200 Subject: [PATCH] =?UTF-8?q?labast=20v1.0.4=20*=20=D0=98=D1=81=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=B1=D0=B0=D0=B3=20c=20"quit?= =?UTF-8?q?"=20*=20=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20"band",=20"bnand",=20"bor",=20"bxor"=20(=D1=81=D0=BF?= =?UTF-8?q?=D0=B0=D1=81=D0=B8=D0=B1=D0=BE=20=D0=92=D0=BB=D0=B0=D0=B4=D1=83?= =?UTF-8?q?)=20*=20=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=20"args"=20(=D1=8D=D1=82=D0=BE=20=D0=B2=D0=BE=D0=BE=D0=B1?= =?UTF-8?q?=D1=89=D0=B5=20=D0=BD=D0=B5=20=D0=BD=D0=B0=D0=B4=D0=BE=20=D0=B1?= =?UTF-8?q?=D1=8B=D0=BB=D0=BE=20=D0=BA=D0=B0=D0=BA=20=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=D1=81=D1=8F,=20=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B7=D0=B0=D1=82=D0=BE=20=D0=BA=D0=BE=D0=B4=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=20=D0=B1=D0=BE=D0=BB=D0=B5=D0=B5=20=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D0=B0=D0=B1=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=BC)?= =?UTF-8?q?=20*=20=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=20=D0=B1=D0=B0=D0=B3=20=D1=81=20=D0=BF=D0=B0=D1=80=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 5 +++- scripts/esolangs-program-forms/hello-world.lb | 1 + .../random-number-generator.lb | 4 ++++ .../esolangs-program-forms/truth-machine.lb | 12 ++++++++++ scripts/std/print.lb | 16 +++++++++++++ scripts/vareniynosok.lb | 2 -- src/execute.rs | 6 +---- src/instructions/args.rs | 23 ++++++++----------- src/instructions/band.rs | 14 ++++------- src/instructions/bnand.rs | 14 ++++------- src/instructions/bor.rs | 14 ++++------- src/instructions/bxor.rs | 14 ++++------- src/instructions/exec.rs | 1 - src/parse.rs | 4 ++-- .../notepad++.xml | 0 17 files changed, 68 insertions(+), 66 deletions(-) create mode 100644 scripts/esolangs-program-forms/hello-world.lb create mode 100644 scripts/esolangs-program-forms/random-number-generator.lb create mode 100644 scripts/esolangs-program-forms/truth-machine.lb create mode 100644 scripts/std/print.lb delete mode 100644 scripts/vareniynosok.lb rename npp_labaski.xml => syntax-highlighting/notepad++.xml (100%) diff --git a/Cargo.lock b/Cargo.lock index b88f3be..8930960 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "labast" -version = "1.0.3" +version = "1.0.4" dependencies = [ "fastrand", "pico-args", diff --git a/Cargo.toml b/Cargo.toml index b85db34..ae95f2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "labast" -version = "1.0.3" +version = "1.0.4" authors = [ "n3tael" ] edition = "2021" description = "A zero-dependencies Labaski interpreter written in Rust." diff --git a/README.md b/README.md index f50517d..9b29a5c 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,7 @@ A Labaski interpreter written in Rust. Fully supports Labashki specs 1.12. ## Examples -See `scripts` directory. \ No newline at end of file +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)) \ No newline at end of file diff --git a/scripts/esolangs-program-forms/hello-world.lb b/scripts/esolangs-program-forms/hello-world.lb new file mode 100644 index 0000000..fc112e2 --- /dev/null +++ b/scripts/esolangs-program-forms/hello-world.lb @@ -0,0 +1 @@ +$println Hello, world! \ No newline at end of file diff --git a/scripts/esolangs-program-forms/random-number-generator.lb b/scripts/esolangs-program-forms/random-number-generator.lb new file mode 100644 index 0000000..61a7e88 --- /dev/null +++ b/scripts/esolangs-program-forms/random-number-generator.lb @@ -0,0 +1,4 @@ +; https://esolangs.org/wiki/User:XKCD_Random_Number + +push 4 +meow \ No newline at end of file diff --git a/scripts/esolangs-program-forms/truth-machine.lb b/scripts/esolangs-program-forms/truth-machine.lb new file mode 100644 index 0000000..c117399 --- /dev/null +++ b/scripts/esolangs-program-forms/truth-machine.lb @@ -0,0 +1,12 @@ +; Truth-machine on Labashki + +@ 0 + scan + kjnz 1 + meow + quit + +@ 1 + push 1 + meow + jmp 1 \ No newline at end of file diff --git a/scripts/std/print.lb b/scripts/std/print.lb new file mode 100644 index 0000000..8152dff --- /dev/null +++ b/scripts/std/print.lb @@ -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 \ No newline at end of file diff --git a/scripts/vareniynosok.lb b/scripts/vareniynosok.lb deleted file mode 100644 index 4e3a3d7..0000000 --- a/scripts/vareniynosok.lb +++ /dev/null @@ -1,2 +0,0 @@ -size -#exec std\unix\ВАРЁНЫЙНОСОК.lb \ No newline at end of file diff --git a/src/execute.rs b/src/execute.rs index 79f9416..7b4fa6b 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -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(); } } \ No newline at end of file diff --git a/src/instructions/args.rs b/src/instructions/args.rs index a80bec1..c0958f4 100644 --- a/src/instructions/args.rs +++ b/src/instructions/args.rs @@ -1,21 +1,18 @@ use crate::{errors::RunError, stack::Stack}; pub fn args(memory: &mut Vec, 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))); - } } \ No newline at end of file diff --git a/src/instructions/band.rs b/src/instructions/band.rs index 88e34a4..111acd4 100644 --- a/src/instructions/band.rs +++ b/src/instructions/band.rs @@ -1,14 +1,8 @@ use crate::errors::RunError; pub fn band(memory: &mut Vec) { - 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); -} \ No newline at end of file + memory.push(a & b); +} diff --git a/src/instructions/bnand.rs b/src/instructions/bnand.rs index 19fb826..5e40052 100644 --- a/src/instructions/bnand.rs +++ b/src/instructions/bnand.rs @@ -1,14 +1,8 @@ use crate::errors::RunError; pub fn bnand(memory: &mut Vec) { - 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); -} \ No newline at end of file + memory.push(!(a & b)); +} diff --git a/src/instructions/bor.rs b/src/instructions/bor.rs index 69fd8ec..9cf8942 100644 --- a/src/instructions/bor.rs +++ b/src/instructions/bor.rs @@ -1,14 +1,8 @@ use crate::errors::RunError; pub fn bor(memory: &mut Vec) { - 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); -} \ No newline at end of file + memory.push(a | b); +} diff --git a/src/instructions/bxor.rs b/src/instructions/bxor.rs index fc43407..e16c576 100644 --- a/src/instructions/bxor.rs +++ b/src/instructions/bxor.rs @@ -1,14 +1,8 @@ use crate::errors::RunError; pub fn bxor(memory: &mut Vec) { - 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); -} \ No newline at end of file + memory.push(a ^ b); +} diff --git a/src/instructions/exec.rs b/src/instructions/exec.rs index 7476731..ebf8b39 100644 --- a/src/instructions/exec.rs +++ b/src/instructions/exec.rs @@ -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 { diff --git a/src/parse.rs b/src/parse.rs index bde2e41..437317d 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -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(); diff --git a/npp_labaski.xml b/syntax-highlighting/notepad++.xml similarity index 100% rename from npp_labaski.xml rename to syntax-highlighting/notepad++.xml