aboutsummaryrefslogtreecommitdiff
path: root/crates/sloth_vm/src/sloth_std/stdio.rs
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-04-27 18:42:28 -0500
committerGitHub <noreply@github.com>2023-04-27 18:42:28 -0500
commit86eab2dc7dc2f88e0f56d09ae3b5f36cf8d9706e (patch)
treed312fd5c0981df90aef29a4131703ae89f328268 /crates/sloth_vm/src/sloth_std/stdio.rs
parent448babe8090c2bcf177c23efd2257a926a1d07f7 (diff)
parentd6b12291db70ea19d37d84226161f964400ae0b2 (diff)
downloadsloth-86eab2dc7dc2f88e0f56d09ae3b5f36cf8d9706e.tar.gz
Merge pull request #5 from slothlang/standard-library
Standard Library
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/stdio.rs')
-rw-r--r--crates/sloth_vm/src/sloth_std/stdio.rs42
1 files changed, 41 insertions, 1 deletions
diff --git a/crates/sloth_vm/src/sloth_std/stdio.rs b/crates/sloth_vm/src/sloth_std/stdio.rs
index a743ad1..f56b604 100644
--- a/crates/sloth_vm/src/sloth_std/stdio.rs
+++ b/crates/sloth_vm/src/sloth_std/stdio.rs
@@ -18,7 +18,7 @@ fn write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult {
return Err(native::Error::InvalidArgument);
};
- println!("{str}");
+ print!("{str}");
Ok(Primitive::Empty)
}
@@ -28,6 +28,41 @@ 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 {
+ 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);
+ };
+
+ println!("{str}");
+
+ Ok(Primitive::Empty)
+}
+
+pub const WRITELN_FUNCTION: NativeFunction = NativeFunction {
+ name: "writeln",
+ 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 {
@@ -48,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'`",
+ ),
};