From f6e14f4b2b15b0ace8ed312252ae107f139bd33d Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Fri, 14 Apr 2023 03:20:23 -0500 Subject: Terminal and file stuff --- crates/sloth_vm/src/sloth_std/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates/sloth_vm/src/sloth_std/mod.rs') diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index 86611d7..b40fb9a 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -4,8 +4,10 @@ use once_cell::sync::Lazy; use crate::native::NativeFunction; +pub mod file; pub mod rand; pub mod stdio; +pub mod term; pub static NATIVE_LIBRARY: Lazy> = Lazy::new(|| { let mut map = HashMap::new(); @@ -16,9 +18,16 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n // stdio map.insert("write", stdio::WRITE_FUNCTION); + map.insert("writeln", stdio::WRITELN_FUNCTION); map.insert("read", stdio::READ_FUNCTION); + // term + map.insert("term$clear", term::TERM_CLEAR); + map.insert("term$setpos", term::TERM_SETPOS); + // filesystem + map.insert("file$read", file::FILE_READ); + map.insert("file$write", file::FILE_WRITE); map }); -- cgit v1.2.3 From 19fefeb732d559195edb01ebc36170c0cf9a0308 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Fri, 14 Apr 2023 04:57:53 -0500 Subject: eepy --- crates/sloth/src/main.rs | 2 +- crates/sloth_vm/src/sloth_std/mod.rs | 4 ++++ crates/sloth_vm/src/sloth_std/time.rs | 25 +++++++++++++++++++++++++ examples/mandelbrot.sloth | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 crates/sloth_vm/src/sloth_std/time.rs create mode 100644 examples/mandelbrot.sloth (limited to 'crates/sloth_vm/src/sloth_std/mod.rs') diff --git a/crates/sloth/src/main.rs b/crates/sloth/src/main.rs index 0d33e91..0eb4c24 100644 --- a/crates/sloth/src/main.rs +++ b/crates/sloth/src/main.rs @@ -38,5 +38,5 @@ fn main() { // println!("{:#?}", parser); let parsed = &parser.parse(); - println!("{:?}", parsed); + println!("{:#?}", parsed); } diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index b40fb9a..56e166c 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -8,6 +8,7 @@ pub mod file; pub mod rand; pub mod stdio; pub mod term; +pub mod time; pub static NATIVE_LIBRARY: Lazy> = Lazy::new(|| { let mut map = HashMap::new(); @@ -29,5 +30,8 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n map.insert("file$read", file::FILE_READ); map.insert("file$write", file::FILE_WRITE); + // time + map.insert("wait", time::WAIT); + map }); diff --git a/crates/sloth_vm/src/sloth_std/time.rs b/crates/sloth_vm/src/sloth_std/time.rs new file mode 100644 index 0000000..4df715e --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/time.rs @@ -0,0 +1,25 @@ +use std::{thread, time}; + +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::Primitive; +use crate::value::Primitive::Integer; +use crate::VM; + +fn wait(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let sec = args.get(0).cloned(); + + let Some(Integer(sec)) = sec else { + return Err(native::Error::InvalidArgument); + }; + + thread::sleep(time::Duration::from_secs(sec.try_into().unwrap())); + + Ok(Primitive::Empty) +} + +pub const WAIT: NativeFunction = NativeFunction { + name: "wait", + function: wait, + arity: 1, + returns_value: false, +}; diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth new file mode 100644 index 0000000..bc95e2f --- /dev/null +++ b/examples/mandelbrot.sloth @@ -0,0 +1,24 @@ +val size: int = 200; +val maxVal: float = 4.0; +val maxIter: int = 50; +val plane: float = 4.0; + +for x in 0 .. size { + for y in 0 .. size { + var cReal: float = (x * plane / size) - 2; + var cImg: float = (y * plane / size) - 2; + var zReal: float = 0; + var zImg: float = 0; + var count: float = 0; + while (zReal * zReal + zImg * zImg) <= maxVal && count < 4{ + var temp: float = (zReal * zReal) - (zImg * zImg) + cReal; + zImg = 2 * zReal * zImg + cImg; + zReal = temp; + count += 1; + } + if count == maxIter { + term_setpos(x, y); + print("*"); + } + } +} -- cgit v1.2.3 From b0ffef5124c88d52d81cbd2f8c96c019ce84ad47 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Tue, 18 Apr 2023 09:08:49 -0500 Subject: Need to write tests for it, but hopefully function docs work now --- crates/sloth_vm/src/native.rs | 1 + crates/sloth_vm/src/sloth_std/file.rs | 2 ++ crates/sloth_vm/src/sloth_std/misc.rs | 39 +++++++++++++++++++++++++++++++++ crates/sloth_vm/src/sloth_std/mod.rs | 3 +++ crates/sloth_vm/src/sloth_std/rand.rs | 2 ++ crates/sloth_vm/src/sloth_std/stdio.rs | 3 +++ crates/sloth_vm/src/sloth_std/term.rs | 2 ++ crates/sloth_vm/src/sloth_std/time.rs | 1 + examples/snake.sloth | 40 ++++++++++++++++++++++++++++++++++ 9 files changed, 93 insertions(+) create mode 100644 crates/sloth_vm/src/sloth_std/misc.rs create mode 100644 examples/snake.sloth (limited to 'crates/sloth_vm/src/sloth_std/mod.rs') diff --git a/crates/sloth_vm/src/native.rs b/crates/sloth_vm/src/native.rs index 8790cbd..fbd2626 100644 --- a/crates/sloth_vm/src/native.rs +++ b/crates/sloth_vm/src/native.rs @@ -15,4 +15,5 @@ pub struct NativeFunction { pub function: NativeFunctionInput, pub arity: u8, pub returns_value: bool, + pub doc: Option<&'static str>, } diff --git a/crates/sloth_vm/src/sloth_std/file.rs b/crates/sloth_vm/src/sloth_std/file.rs index b6c8adf..5761f09 100644 --- a/crates/sloth_vm/src/sloth_std/file.rs +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -31,6 +31,7 @@ pub const FILE_READ: NativeFunction = NativeFunction { function: file_read, arity: 1, returns_value: true, + doc: None, }; fn file_write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -70,4 +71,5 @@ pub const FILE_WRITE: NativeFunction = NativeFunction { function: file_write, arity: 2, returns_value: false, + doc: None, }; diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs new file mode 100644 index 0000000..72be48f --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -0,0 +1,39 @@ +use super::NATIVE_LIBRARY; +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::{Object, ObjectType, Primitive}; +use crate::VM; + +fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let Some(Primitive::Object(ptr)) = args.get(0).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let object = vm + .objects() + .get(ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(str) = &object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let docs = NATIVE_LIBRARY + .get(str.as_str()) + .ok_or(native::Error::InvalidArgument)? + .doc + .ok_or(native::Error::InvalidArgument)? + .to_string(); + + let object = Object::new(ObjectType::String(docs)); + let ptr = vm.objects_mut().allocate(object); + + Ok(Primitive::Object(ptr as u32)) +} + +pub const GET_DOC: NativeFunction = NativeFunction { + name: "get$doc", + function: get_doc, + arity: 1, + returns_value: true, + doc: None, +}; diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index 56e166c..982017d 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -5,6 +5,7 @@ use once_cell::sync::Lazy; use crate::native::NativeFunction; pub mod file; +pub mod misc; pub mod rand; pub mod stdio; pub mod term; @@ -27,6 +28,8 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n map.insert("term$setpos", term::TERM_SETPOS); // filesystem + // TODO: Make the files commands work by making a global file variable with + // certain permissions created by 'file.open' instead of just reading the file. map.insert("file$read", file::FILE_READ); map.insert("file$write", file::FILE_WRITE); diff --git a/crates/sloth_vm/src/sloth_std/rand.rs b/crates/sloth_vm/src/sloth_std/rand.rs index bae0606..93da0d7 100644 --- a/crates/sloth_vm/src/sloth_std/rand.rs +++ b/crates/sloth_vm/src/sloth_std/rand.rs @@ -16,6 +16,7 @@ pub const GEN_FUNCTION: NativeFunction = NativeFunction { function: gen, arity: 0, returns_value: true, + doc: None, }; fn gen_range(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -36,4 +37,5 @@ pub const GEN_RANGE_FUNCTION: NativeFunction = NativeFunction { function: gen_range, arity: 2, returns_value: true, + doc: None, }; diff --git a/crates/sloth_vm/src/sloth_std/stdio.rs b/crates/sloth_vm/src/sloth_std/stdio.rs index 75029bd..160d012 100644 --- a/crates/sloth_vm/src/sloth_std/stdio.rs +++ b/crates/sloth_vm/src/sloth_std/stdio.rs @@ -28,6 +28,7 @@ pub const WRITE_FUNCTION: NativeFunction = NativeFunction { function: write, arity: 1, returns_value: false, + doc: None, }; fn writeln(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -54,6 +55,7 @@ pub const WRITELN_FUNCTION: NativeFunction = NativeFunction { function: writeln, arity: 1, returns_value: false, + doc: None, }; fn read(vm: &mut VM, _args: &[Primitive]) -> NativeFunctionResult { @@ -74,4 +76,5 @@ pub const READ_FUNCTION: NativeFunction = NativeFunction { function: read, arity: 0, returns_value: true, + doc: None, }; diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs index cffa1e8..41e6708 100644 --- a/crates/sloth_vm/src/sloth_std/term.rs +++ b/crates/sloth_vm/src/sloth_std/term.rs @@ -11,6 +11,7 @@ pub const TERM_CLEAR: NativeFunction = NativeFunction { }, arity: 0, returns_value: false, + doc: None, }; fn term_setpos(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -29,4 +30,5 @@ pub const TERM_SETPOS: NativeFunction = NativeFunction { function: term_setpos, arity: 2, returns_value: false, + doc: None, }; diff --git a/crates/sloth_vm/src/sloth_std/time.rs b/crates/sloth_vm/src/sloth_std/time.rs index 4df715e..1c22294 100644 --- a/crates/sloth_vm/src/sloth_std/time.rs +++ b/crates/sloth_vm/src/sloth_std/time.rs @@ -22,4 +22,5 @@ pub const WAIT: NativeFunction = NativeFunction { function: wait, arity: 1, returns_value: false, + doc: None, }; diff --git a/examples/snake.sloth b/examples/snake.sloth new file mode 100644 index 0000000..c60819d --- /dev/null +++ b/examples/snake.sloth @@ -0,0 +1,40 @@ +var xPos = 0; +var yPos = 0; +# 0=right 1=down 2=left 3=up +var direction = 0; + +while true { + if direction == 0{ + var x = xPos + 1; + xPos = x; + } + if direction == 1 { + var y = yPos + 1; + yPos = y; + } + if direction == 2{ + var x = xPos - 1; + xPos = x; + } + if direction == 3 { + var y = yPos - 1; + yPos = y; + } + + var input = readln(); + if input == "w" && direction != 1 { + direction = 3; + } + if input == "a" && direction != 0 { + direction = 2; + } + if input == "s" && direction != 3 { + direction = 1; + } + if input == "d" && direction != 2 { + direction = 0; + } + + term_setpos(x, y); + print("*"); +} -- cgit v1.2.3 From fdaa289c2f55b31668ef0a6ec986cf459c834eb6 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 17:56:00 -0500 Subject: Applied changes requested by @CatDevz --- crates/sloth_vm/src/sloth_std/misc.rs | 8 ++++---- crates/sloth_vm/src/sloth_std/mod.rs | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/mod.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 54d6e37..4417e59 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -13,12 +13,12 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { .get(ptr as usize) .ok_or(native::Error::InvalidArgument)?; - let ObjectType::String(str) = &object.typ else { + let ObjectType::NativeFunction(fnc) = &object.typ else { return Err(native::Error::InvalidArgument); }; let docs = NATIVE_LIBRARY - .get(str.as_str()) + .get(fnc.name.clone()) .ok_or(native::Error::InvalidArgument)? .doc .ok_or(native::Error::InvalidArgument)? @@ -30,8 +30,8 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { Ok(Primitive::Object(ptr as u32)) } -pub const GET_DOC: NativeFunction = NativeFunction { - name: "get$doc", +pub const DOCS: NativeFunction = NativeFunction { + name: "docs", function: get_doc, arity: 1, returns_value: true, diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index 982017d..ff761a6 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -36,5 +36,8 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n // time map.insert("wait", time::WAIT); + // doc + map.insert("docs", misc::DOCS); + map }); -- cgit v1.2.3