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/term.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 crates/sloth_vm/src/sloth_std/term.rs (limited to 'crates/sloth_vm/src/sloth_std/term.rs') diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs new file mode 100644 index 0000000..cffa1e8 --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/term.rs @@ -0,0 +1,32 @@ +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::Primitive; +use crate::value::Primitive::Integer; +use crate::VM; + +pub const TERM_CLEAR: NativeFunction = NativeFunction { + name: "term$clear", + function: |_vm, _args| { + print!("\x1b[2J\x1b[H"); + Ok(Primitive::Empty) + }, + arity: 0, + returns_value: false, +}; + +fn term_setpos(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let x = args.get(0).cloned(); + let y = args.get(1).cloned(); + + let (Some(Integer(x)), Some(Integer(y))) = (x, y) else { + return Err(native::Error::InvalidArgument); + }; + print!("\x1b[{x};{y}H"); + Ok(Primitive::Empty) +} + +pub const TERM_SETPOS: NativeFunction = NativeFunction { + name: "term$setpos", + function: term_setpos, + arity: 2, + returns_value: false, +}; -- 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/term.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 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/term.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 33e941220effaad90f6460b7b163d0e870d8a8cb Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:33:29 -0500 Subject: Update crates/sloth_vm/src/sloth_std/term.rs --- crates/sloth_vm/src/sloth_std/term.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/sloth_vm/src/sloth_std/term.rs') diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs index 16fd86a..c9b85da 100644 --- a/crates/sloth_vm/src/sloth_std/term.rs +++ b/crates/sloth_vm/src/sloth_std/term.rs @@ -12,7 +12,7 @@ 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(); # \ + "NativeFunction term$clear: \n\tdesc: Clears the terminal\n\tExample: `term$clear(); # \ Clears the terminal`", ), }; -- cgit v1.2.3 From 422fe0a9bdc2eac76cc1512a1708ade73e57cf68 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:33:50 -0500 Subject: Update crates/sloth_vm/src/sloth_std/term.rs --- crates/sloth_vm/src/sloth_std/term.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/sloth_vm/src/sloth_std/term.rs') diff --git a/crates/sloth_vm/src/sloth_std/term.rs b/crates/sloth_vm/src/sloth_std/term.rs index c9b85da..f61321c 100644 --- a/crates/sloth_vm/src/sloth_std/term.rs +++ b/crates/sloth_vm/src/sloth_std/term.rs @@ -34,8 +34,8 @@ pub const TERM_SETPOS: NativeFunction = NativeFunction { arity: 2, returns_value: false, 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 \ + "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)`", ), }; -- cgit v1.2.3