aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm
diff options
context:
space:
mode:
Diffstat (limited to 'crates/sloth_vm')
-rw-r--r--crates/sloth_vm/src/native.rs1
-rw-r--r--crates/sloth_vm/src/sloth_std/file.rs10
-rw-r--r--crates/sloth_vm/src/sloth_std/misc.rs43
-rw-r--r--crates/sloth_vm/src/sloth_std/mod.rs3
-rw-r--r--crates/sloth_vm/src/sloth_std/rand.rs9
-rw-r--r--crates/sloth_vm/src/sloth_std/stdio.rs14
-rw-r--r--crates/sloth_vm/src/sloth_std/term.rs9
-rw-r--r--crates/sloth_vm/src/sloth_std/time.rs4
8 files changed, 93 insertions, 0 deletions
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..dea914a 100644
--- a/crates/sloth_vm/src/sloth_std/file.rs
+++ b/crates/sloth_vm/src/sloth_std/file.rs
@@ -31,6 +31,11 @@ pub const FILE_READ: NativeFunction = NativeFunction {
function: file_read,
arity: 1,
returns_value: true,
+ doc: Some(
+ "NativeFunction file_read: \n\targs: path (str)\n\tdesc: Returns the contents of a file \
+ at <path>\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 {
@@ -70,4 +75,9 @@ pub const FILE_WRITE: NativeFunction = NativeFunction {
function: file_write,
arity: 2,
returns_value: false,
+ doc: Some(
+ "NativeFunction file_write: \n\targs: path (str), content (str)\n\tdesc: Writes <content> \
+ to file at <path>\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
new file mode 100644
index 0000000..54d6e37
--- /dev/null
+++ b/crates/sloth_vm/src/sloth_std/misc.rs
@@ -0,0 +1,43 @@
+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: Some(
+ "NativeFunction get_doc: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \
+ function with name <str>\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/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<HashMap<&'static str, NativeFunction>> = 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..1718c6d 100644
--- a/crates/sloth_vm/src/sloth_std/rand.rs
+++ b/crates/sloth_vm/src/sloth_std/rand.rs
@@ -16,6 +16,10 @@ pub const GEN_FUNCTION: NativeFunction = NativeFunction {
function: gen,
arity: 0,
returns_value: true,
+ 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 {
@@ -36,4 +40,9 @@ pub const GEN_RANGE_FUNCTION: NativeFunction = NativeFunction {
function: gen_range,
arity: 2,
returns_value: true,
+ doc: Some(
+ "NativeFunction rand_gen_range: \n\targs: min (int), max (int)\n\tdesc: Returns a random \
+ numnber in the range <min> .. <max>\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 75029bd..f56b604 100644
--- a/crates/sloth_vm/src/sloth_std/stdio.rs
+++ b/crates/sloth_vm/src/sloth_std/stdio.rs
@@ -28,6 +28,10 @@ pub const WRITE_FUNCTION: NativeFunction = NativeFunction {
function: write,
arity: 1,
returns_value: false,
+ doc: Some(
+ "NativeFunction write: \n\targs: string (str)\n\tdesc: Writes <string> to the \
+ terminal.\n\tExample: `write(\"I'm sleepy...\"); # Output: I'm sleepy...`",
+ ),
};
fn writeln(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult {
@@ -54,6 +58,11 @@ pub const WRITELN_FUNCTION: NativeFunction = NativeFunction {
function: writeln,
arity: 1,
returns_value: false,
+ doc: Some(
+ "NativeFunction writeln: \n\targs: string (str)\n\tdesc: Writes <string> 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 {
@@ -74,4 +83,9 @@ pub const READ_FUNCTION: NativeFunction = NativeFunction {
function: read,
arity: 0,
returns_value: true,
+ doc: Some(
+ "NativeFunction read:\n\tdesc: Reads input from the terminal and returns what was \
+ read.\n\tExample: `var input = read(); # Hello World <execute code> input = 'Hello \
+ World'`",
+ ),
};
diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs
index cffa1e8..16fd86a 100644
--- a/crates/sloth_vm/src/sloth_std/term.rs
+++ b/crates/sloth_vm/src/sloth_std/term.rs
@@ -11,6 +11,10 @@ pub const TERM_CLEAR: NativeFunction = NativeFunction {
},
arity: 0,
returns_value: false,
+ 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 {
@@ -29,4 +33,9 @@ pub const TERM_SETPOS: NativeFunction = NativeFunction {
function: term_setpos,
arity: 2,
returns_value: false,
+ doc: Some(
+ "NativeFunction term_setpos: \n\targs: x (int), y (int)\n\tdesc: Sets the cursors \
+ position to (<x>, <y>)\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 4df715e..b27e0b5 100644
--- a/crates/sloth_vm/src/sloth_std/time.rs
+++ b/crates/sloth_vm/src/sloth_std/time.rs
@@ -22,4 +22,8 @@ pub const WAIT: NativeFunction = NativeFunction {
function: wait,
arity: 1,
returns_value: false,
+ doc: Some(
+ "NativeFunction wait: \n\targs: sec (int)\n\tdesc: Waits for <sec> seconds.\n\tExample: \
+ `wait(10); # Waits 10 seconds`",
+ ),
};