diff options
| author | Cody <cody@codyq.dev> | 2023-06-07 03:28:40 -0500 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-06-07 03:28:40 -0500 |
| commit | 6f6613419f1511c5637c9f69b3caa5ae838270b9 (patch) | |
| tree | e203d6cdc0eb2140ae6f0a430e76f2992de66bec /crates/sloth_vm/src/sloth_std/stdio.rs | |
| parent | 25c5ccb29a6f2387a04bfb5d50874e00084c15d6 (diff) | |
| download | sloth-6f6613419f1511c5637c9f69b3caa5ae838270b9.tar.gz | |
Moving over from a VM interpreter to natively compiled w/ LLVM
Diffstat (limited to 'crates/sloth_vm/src/sloth_std/stdio.rs')
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/stdio.rs | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/crates/sloth_vm/src/sloth_std/stdio.rs b/crates/sloth_vm/src/sloth_std/stdio.rs deleted file mode 100644 index f56b604..0000000 --- a/crates/sloth_vm/src/sloth_std/stdio.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::io::{stdin, BufRead}; - -use crate::native::{self, NativeFunction, NativeFunctionResult}; -use crate::value::{Object, ObjectType, Primitive}; -use crate::VM; - -fn write(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); - }; - - print!("{str}"); - - Ok(Primitive::Empty) -} - -pub const WRITE_FUNCTION: NativeFunction = NativeFunction { - name: "write", - 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 { - let mut line = String::new(); - stdin() - .lock() - .read_line(&mut line) - .map_err(|it| native::Error::Unknown(it.to_string()))?; - - let object = Object::new(ObjectType::String(line)); - let ptr = vm.objects_mut().allocate(object); - - Ok(Primitive::Object(ptr as u32)) -} - -pub const READ_FUNCTION: NativeFunction = NativeFunction { - name: "read", - 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'`", - ), -}; |
