diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-14 03:20:23 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-14 03:20:23 -0500 |
| commit | f6e14f4b2b15b0ace8ed312252ae107f139bd33d (patch) | |
| tree | 34d844e1c4f1be2c95ef87e6363a76b3502bb40f /crates/sloth_vm/src/sloth_std/stdio.rs | |
| parent | 97b7cd10d2bec408cc237e13c61562c810d8fd29 (diff) | |
| download | sloth-f6e14f4b2b15b0ace8ed312252ae107f139bd33d.tar.gz | |
Terminal and file stuff
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/stdio.rs')
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/stdio.rs | 28 |
1 files changed, 27 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..75029bd 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) } @@ -30,6 +30,32 @@ pub const WRITE_FUNCTION: NativeFunction = NativeFunction { returns_value: false, }; +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, +}; + fn read(vm: &mut VM, _args: &[Primitive]) -> NativeFunctionResult { let mut line = String::new(); stdin() |
