initial commit

This commit is contained in:
n3taeli 2024-02-05 14:13:44 +02:00
parent a005400477
commit 317fa500b8
Signed by: n3tael
GPG Key ID: F305925762F035A8
24 changed files with 78 additions and 7 deletions

View File

@ -11,4 +11,9 @@ publish = false
[profile.dev] [profile.dev]
overflow-checks = false overflow-checks = false
[profile.release]
strip = true
lto = true
panic = "abort"
[dependencies] [dependencies]

View File

@ -2,4 +2,4 @@
A zero-dependencies Labaski interpreter written in Rust. Fully supports [Labashki specs 1.4.1](./SPECIFICATION.md). A zero-dependencies Labaski interpreter written in Rust. Fully supports [Labashki specs 1.4.1](./SPECIFICATION.md).
## Examples ## Examples
See `example-scripts` directory. See `scripts` directory.

View File

@ -1,5 +1,9 @@
@echo off @echo off
echo Building for Windows...
cargo build --release cargo build --release
echo Building for Linux...
cargo zigbuild --release --target x86_64-unknown-linux-gnu cargo zigbuild --release --target x86_64-unknown-linux-gnu
echo Copy to bin...
copy target\release\labast.exe bin\ /Y copy target\release\labast.exe bin\ /Y
copy target\x86_64-unknown-linux-gnu\release\labast bin\ /Y copy target\x86_64-unknown-linux-gnu\release\labast bin\ /Y
echo Done!

4
scripts/std/rev.lb Normal file
View File

@ -0,0 +1,4 @@
; Labashki standard library
; Written by Labashki developer - aeris
ARGS 0

4
scripts/std/rot.lb Normal file
View File

@ -0,0 +1,4 @@
; Labashki standard library
; Written by Labashki developer - aeris
ARGS 3

View File

@ -0,0 +1,17 @@
; Labashki standard library
; Written by Labashki developer - aeris
MAXSIZE
ARGS 1 ; Насколько сварить носок
SUB
@ 1
JNZ 2
QUIT
@ 2
#EXPR -
PUSH -1
_UNIX_RANDOM
SWAP
JMP 1

2
scripts/vareniynosok.lb Normal file
View File

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

View File

@ -49,6 +49,7 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
"MEOW" => instructions::meow::meow(&mut stack.memory), "MEOW" => instructions::meow::meow(&mut stack.memory),
"DUMP" => instructions::dump::dump(&mut stack.memory), "DUMP" => instructions::dump::dump(&mut stack.memory),
"SIZE" => instructions::size::size(&mut stack.memory), "SIZE" => instructions::size::size(&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),
@ -72,6 +73,9 @@ pub fn execute(stack: &mut Stack, mut origin_stack: Option<&mut Stack>, mod_name
"ARGS" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data), "ARGS" => instructions::args::args(&mut stack.memory, &mut origin_stack, &instruction.data),
"#EXEC" => instructions::exec::exec(stack, &instruction.arg.clone(), &mod_name), "#EXEC" => instructions::exec::exec(stack, &instruction.arg.clone(), &mod_name),
#[cfg(target_family = "unix")]
"_UNIX_RANDOM" => instructions::_unix_random::_unix_random(&mut stack.memory),
// ? // ?
"NOP" => continue, "NOP" => continue,
"EXIT" => instructions::exit::exit(&instruction.data), "EXIT" => instructions::exit::exit(&instruction.data),

View File

@ -0,0 +1,14 @@
use std::fs::File;
use std::io::Read;
use crate::RunError;
#[cfg(target_family = "unix")]
pub fn _unix_random(memory: &mut Vec<u16>) {
let mut rng = File::open("/dev/urandom").unwrap();
let mut buffer = [0u8; 1];
rng.read_exact(&mut buffer).unwrap();
let data = &memory.pop().expect(&format!("{}", RunError::MemoryEmpty));
memory.push((buffer[0] as u16) % (*data - 1));
}

View File

@ -1,7 +1,7 @@
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("msg").memory; let origin_memory = &mut origin_stack.as_mut().expect(&format!("{}", RunError::RequestArgsInMainModule)).memory;
if *origin_memory == *memory { if *origin_memory == *memory {
eprintln!("{}", RunError::RequestArgsInMainModule); eprintln!("{}", RunError::RequestArgsInMainModule);

View File

@ -1,4 +1,9 @@
pub fn dump(memory: &mut Vec<u16>) { pub fn dump(memory: &mut Vec<u16>) {
if memory.is_empty() {
println!("Stack is empty.");
return;
}
for (i, entry) in memory.iter().enumerate() { for (i, entry) in memory.iter().enumerate() {
println!("{0}: {1}", i, entry); println!("{0}: {1}", i, entry);
} }

View File

@ -6,7 +6,7 @@ pub fn jnz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_count
std::process::exit(2); std::process::exit(2);
} }
if memory.pop() != Some(0) { if memory.last() != Some(&0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16; *program_counter = (labels[*data as usize].unwrap() - 1) as u16;
} }
} }

View File

@ -6,7 +6,7 @@ pub fn jz(memory: &mut Vec<u16>, labels: &mut [Option<i16>; 256], program_counte
std::process::exit(2); std::process::exit(2);
} }
if memory.pop() == Some(0) { if memory.last() == Some(&0) {
*program_counter = (labels[*data as usize].unwrap() - 1) as u16; *program_counter = (labels[*data as usize].unwrap() - 1) as u16;
} }
} }

View File

@ -0,0 +1,3 @@
pub fn maxsize(memory: &mut Vec<u16>) {
memory.push(65535);
}

View File

@ -19,4 +19,7 @@ pub mod exec;
pub mod args; pub mod args;
pub mod expr; pub mod expr;
pub mod size; pub mod size;
pub mod exit; pub mod exit;
pub mod maxsize;
#[cfg(target_family = "unix")]
pub mod _unix_random;

View File

@ -24,7 +24,13 @@ pub fn parse(stack: &mut Stack, file_content: &str) {
if command.len() >= 2 { if command.len() >= 2 {
match name.chars().nth(0) { match name.chars().nth(0) {
Some('#') => arg = command[1].to_string(), Some('#') => arg = command[1].to_string(),
_ => data = command[1].parse().expect(&format!("{}", ParseError::DataNotAUInt(command[0].to_string(), i + 1))), _ => {
if command[1] == "-1" { // required for _UNIX_RANDOM instruction
data = 65535;
} else {
data = command[1].parse().expect(&format!("{}", ParseError::DataNotAUInt(command[0].to_string(), i + 1)))
}
}
} }
} }