diff options
| author | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-14 04:57:53 -0500 |
|---|---|---|
| committer | nic-gaffney <gaffney_nic@protonmail.com> | 2023-04-14 04:57:53 -0500 |
| commit | 19fefeb732d559195edb01ebc36170c0cf9a0308 (patch) | |
| tree | 681bcb8d9d507024d15814032d3fbb1674677ddc | |
| parent | f6e14f4b2b15b0ace8ed312252ae107f139bd33d (diff) | |
| download | sloth-19fefeb732d559195edb01ebc36170c0cf9a0308.tar.gz | |
eepy
| -rw-r--r-- | crates/sloth/src/main.rs | 2 | ||||
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/mod.rs | 4 | ||||
| -rw-r--r-- | crates/sloth_vm/src/sloth_std/time.rs | 25 | ||||
| -rw-r--r-- | examples/mandelbrot.sloth | 24 |
4 files changed, 54 insertions, 1 deletions
diff --git a/crates/sloth/src/main.rs b/crates/sloth/src/main.rs index 0d33e91..0eb4c24 100644 --- a/crates/sloth/src/main.rs +++ b/crates/sloth/src/main.rs @@ -38,5 +38,5 @@ fn main() { // println!("{:#?}", parser); let parsed = &parser.parse(); - println!("{:?}", parsed); + println!("{:#?}", parsed); } diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index b40fb9a..56e166c 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -8,6 +8,7 @@ pub mod file; pub mod rand; pub mod stdio; pub mod term; +pub mod time; pub static NATIVE_LIBRARY: Lazy<HashMap<&'static str, NativeFunction>> = Lazy::new(|| { let mut map = HashMap::new(); @@ -29,5 +30,8 @@ pub static NATIVE_LIBRARY: Lazy<HashMap<&'static str, NativeFunction>> = Lazy::n map.insert("file$read", file::FILE_READ); map.insert("file$write", file::FILE_WRITE); + // time + map.insert("wait", time::WAIT); + map }); diff --git a/crates/sloth_vm/src/sloth_std/time.rs b/crates/sloth_vm/src/sloth_std/time.rs new file mode 100644 index 0000000..4df715e --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/time.rs @@ -0,0 +1,25 @@ +use std::{thread, time}; + +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::Primitive; +use crate::value::Primitive::Integer; +use crate::VM; + +fn wait(_vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let sec = args.get(0).cloned(); + + let Some(Integer(sec)) = sec else { + return Err(native::Error::InvalidArgument); + }; + + thread::sleep(time::Duration::from_secs(sec.try_into().unwrap())); + + Ok(Primitive::Empty) +} + +pub const WAIT: NativeFunction = NativeFunction { + name: "wait", + function: wait, + arity: 1, + returns_value: false, +}; diff --git a/examples/mandelbrot.sloth b/examples/mandelbrot.sloth new file mode 100644 index 0000000..bc95e2f --- /dev/null +++ b/examples/mandelbrot.sloth @@ -0,0 +1,24 @@ +val size: int = 200; +val maxVal: float = 4.0; +val maxIter: int = 50; +val plane: float = 4.0; + +for x in 0 .. size { + for y in 0 .. size { + var cReal: float = (x * plane / size) - 2; + var cImg: float = (y * plane / size) - 2; + var zReal: float = 0; + var zImg: float = 0; + var count: float = 0; + while (zReal * zReal + zImg * zImg) <= maxVal && count < 4{ + var temp: float = (zReal * zReal) - (zImg * zImg) + cReal; + zImg = 2 * zReal * zImg + cImg; + zReal = temp; + count += 1; + } + if count == maxIter { + term_setpos(x, y); + print("*"); + } + } +} |
