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/sloth_std/misc.rs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 crates/sloth_vm/src/sloth_std/misc.rs (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') 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, +}; -- cgit v1.2.3 From af02ccd056754c60131d13d74b9fac0e23b2af31 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Tue, 18 Apr 2023 10:42:45 -0500 Subject: Added more documentation in the native functions --- crates/sloth_vm/src/sloth_std/file.rs | 12 ++++++++++-- crates/sloth_vm/src/sloth_std/misc.rs | 6 +++++- crates/sloth_vm/src/sloth_std/rand.rs | 11 +++++++++-- crates/sloth_vm/src/sloth_std/stdio.rs | 17 ++++++++++++++--- crates/sloth_vm/src/sloth_std/term.rs | 11 +++++++++-- crates/sloth_vm/src/sloth_std/time.rs | 5 ++++- 6 files changed, 51 insertions(+), 11 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/file.rs b/crates/sloth_vm/src/sloth_std/file.rs index 5761f09..dea914a 100644 --- a/crates/sloth_vm/src/sloth_std/file.rs +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -31,7 +31,11 @@ pub const FILE_READ: NativeFunction = NativeFunction { function: file_read, arity: 1, returns_value: true, - doc: None, + doc: Some( + "NativeFunction file_read: \n\targs: path (str)\n\tdesc: Returns the contents of a file \ + at \n\tExample: `var todo = file_read('/home/sloth/todo.txt'); # Assuming the \ + contents of todo.txt are 'Take a nap' then todo = 'Take a nap'`", + ), }; fn file_write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -71,5 +75,9 @@ pub const FILE_WRITE: NativeFunction = NativeFunction { function: file_write, arity: 2, returns_value: false, - doc: None, + doc: Some( + "NativeFunction file_write: \n\targs: path (str), content (str)\n\tdesc: Writes \ + to file at \n\tExample: `file_write('/home/sloth/todo.txt', 'Take a nap'); # \ + todo.txt now contains the string 'Take a nap'`", + ), }; diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 72be48f..54d6e37 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -35,5 +35,9 @@ pub const GET_DOC: NativeFunction = NativeFunction { function: get_doc, arity: 1, returns_value: true, - doc: None, + doc: Some( + "NativeFunction get_doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ + function with name \n\tExample: `var doc = get_doc('wait'); # Returns the \ + documentation of the 'wait' function to doc`", + ), }; diff --git a/crates/sloth_vm/src/sloth_std/rand.rs b/crates/sloth_vm/src/sloth_std/rand.rs index 93da0d7..1718c6d 100644 --- a/crates/sloth_vm/src/sloth_std/rand.rs +++ b/crates/sloth_vm/src/sloth_std/rand.rs @@ -16,7 +16,10 @@ pub const GEN_FUNCTION: NativeFunction = NativeFunction { function: gen, arity: 0, returns_value: true, - doc: None, + doc: Some( + "NativeFunction rand_gen:\n\tdesc: Returns a random number in the range `0.0 .. \ + 1.0`\n\tExample: `var num = rand_gen(); # num could be any number from 0.0 to 1.0`", + ), }; fn gen_range(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -37,5 +40,9 @@ pub const GEN_RANGE_FUNCTION: NativeFunction = NativeFunction { function: gen_range, arity: 2, returns_value: true, - doc: None, + doc: Some( + "NativeFunction rand_gen_range: \n\targs: min (int), max (int)\n\tdesc: Returns a random \ + numnber in the range .. \n\tExample: `var num = gen_range(20, 76); # num \ + could be any number from 20 to 76`", + ), }; diff --git a/crates/sloth_vm/src/sloth_std/stdio.rs b/crates/sloth_vm/src/sloth_std/stdio.rs index 160d012..f56b604 100644 --- a/crates/sloth_vm/src/sloth_std/stdio.rs +++ b/crates/sloth_vm/src/sloth_std/stdio.rs @@ -28,7 +28,10 @@ pub const WRITE_FUNCTION: NativeFunction = NativeFunction { function: write, arity: 1, returns_value: false, - doc: None, + doc: Some( + "NativeFunction write: \n\targs: string (str)\n\tdesc: Writes to the \ + terminal.\n\tExample: `write(\"I'm sleepy...\"); # Output: I'm sleepy...`", + ), }; fn writeln(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -55,7 +58,11 @@ pub const WRITELN_FUNCTION: NativeFunction = NativeFunction { function: writeln, arity: 1, returns_value: false, - doc: None, + doc: Some( + "NativeFunction writeln: \n\targs: string (str)\n\tdesc: Writes to the terminal \ + and starts a new line.\n\tExample: `writeln(\"I'm sleepy...\"); # Output: I'm \ + sleepy...\n # This is a new line`", + ), }; fn read(vm: &mut VM, _args: &[Primitive]) -> NativeFunctionResult { @@ -76,5 +83,9 @@ pub const READ_FUNCTION: NativeFunction = NativeFunction { function: read, arity: 0, returns_value: true, - doc: None, + doc: Some( + "NativeFunction read:\n\tdesc: Reads input from the terminal and returns what was \ + read.\n\tExample: `var input = read(); # Hello World input = 'Hello \ + World'`", + ), }; diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs index 41e6708..16fd86a 100644 --- a/crates/sloth_vm/src/sloth_std/term.rs +++ b/crates/sloth_vm/src/sloth_std/term.rs @@ -11,7 +11,10 @@ pub const TERM_CLEAR: NativeFunction = NativeFunction { }, arity: 0, returns_value: false, - doc: None, + doc: Some( + "NativeFunction term_clear: \n\tdesc: Clears the terminal\n\tExample: `term_clear(); # \ + Clears the terminal`", + ), }; fn term_setpos(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { @@ -30,5 +33,9 @@ pub const TERM_SETPOS: NativeFunction = NativeFunction { function: term_setpos, arity: 2, returns_value: false, - doc: None, + doc: Some( + "NativeFunction term_setpos: \n\targs: x (int), y (int)\n\tdesc: Sets the cursors \ + position to (, )\n\tExample: `term_setpos(5, 17); # Sets the position of the \ + cursor to (5, 17)`", + ), }; diff --git a/crates/sloth_vm/src/sloth_std/time.rs b/crates/sloth_vm/src/sloth_std/time.rs index d32d860..b27e0b5 100644 --- a/crates/sloth_vm/src/sloth_std/time.rs +++ b/crates/sloth_vm/src/sloth_std/time.rs @@ -22,5 +22,8 @@ pub const WAIT: NativeFunction = NativeFunction { function: wait, arity: 1, returns_value: false, - doc: Some("Use `wait(sec: int)` to sleep for `sec` time"), + doc: Some( + "NativeFunction wait: \n\targs: sec (int)\n\tdesc: Waits for seconds.\n\tExample: \ + `wait(10); # Waits 10 seconds`", + ), }; -- 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/misc.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 From 1466d51eed5767e92d3264289590770932ceba96 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 17:58:31 -0500 Subject: Fixed error with clippy --- crates/sloth_vm/src/sloth_std/misc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 4417e59..6994657 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -18,7 +18,7 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { }; let docs = NATIVE_LIBRARY - .get(fnc.name.clone()) + .get(fnc.name) .ok_or(native::Error::InvalidArgument)? .doc .ok_or(native::Error::InvalidArgument)? -- cgit v1.2.3 From 1cc0b13f3915a281aea109050b844f6cb38db71f Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:19:05 -0500 Subject: Change "get_doc" to "get$doc" --- crates/sloth_vm/src/sloth_std/misc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 6994657..dc6b8fd 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -36,8 +36,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, doc: Some( - "NativeFunction get_doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ - function with name \n\tExample: `var doc = get_doc('wait'); # Returns the \ + "NativeFunction get$doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ + function with name \n\tExample: `var doc = get$doc('wait'); # Returns the \ documentation of the 'wait' function to doc`", ), }; -- cgit v1.2.3 From 935f53cddfdc87dd0da0606595f4da1d4238c617 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:30:32 -0500 Subject: Doc stuff idk man --- crates/sloth_vm/src/sloth_std/misc.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 6994657..df8d452 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -17,13 +17,7 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { return Err(native::Error::InvalidArgument); }; - let docs = NATIVE_LIBRARY - .get(fnc.name) - .ok_or(native::Error::InvalidArgument)? - .doc - .ok_or(native::Error::InvalidArgument)? - .to_string(); - + let docs = fnc.doc.expect("Oopsie Poopsie the stringy no worky").to_string(); let object = Object::new(ObjectType::String(docs)); let ptr = vm.objects_mut().allocate(object); -- cgit v1.2.3 From a03609b894560780c7439168a20662516adbe100 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:31:09 -0500 Subject: Removed un needed import or whatever --- crates/sloth_vm/src/sloth_std/misc.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 8993f49..e0aa05c 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -1,4 +1,3 @@ -use super::NATIVE_LIBRARY; use crate::native::{self, NativeFunction, NativeFunctionResult}; use crate::value::{Object, ObjectType, Primitive}; use crate::VM; -- cgit v1.2.3 From e49358828d84d4bf2a2ec8ee9447c8f5a00f90bc Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:34:50 -0500 Subject: docs fixed --- crates/sloth_vm/src/sloth_std/misc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index e0aa05c..4476bfd 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -29,8 +29,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, doc: Some( - "NativeFunction get$doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ - function with name \n\tExample: `var doc = get$doc('wait'); # Returns the \ + "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ + function with name \n\tExample: `var doc = docs('wait'); # Returns the \ documentation of the 'wait' function to doc`", ), }; -- cgit v1.2.3 From d6b12291db70ea19d37d84226161f964400ae0b2 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:35:50 -0500 Subject: cargo-fmt lmao --- crates/sloth_vm/src/sloth_std/misc.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/misc.rs') diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 4476bfd..ca08d1d 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -16,7 +16,10 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { return Err(native::Error::InvalidArgument); }; - let docs = fnc.doc.expect("Oopsie Poopsie the stringy no worky").to_string(); + let docs = fnc + .doc + .expect("Oopsie Poopsie the stringy no worky") + .to_string(); let object = Object::new(ObjectType::String(docs)); let ptr = vm.objects_mut().allocate(object); @@ -29,8 +32,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, doc: Some( - "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ - function with name \n\tExample: `var doc = docs('wait'); # Returns the \ - documentation of the 'wait' function to doc`", + "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a function \ + with name \n\tExample: `var doc = docs('wait'); # Returns the documentation of the \ + 'wait' function to doc`", ), }; -- cgit v1.2.3