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/file.rs | 73 ++++++++++++++++++++++++++++++++++ crates/sloth_vm/src/sloth_std/mod.rs | 9 +++++ crates/sloth_vm/src/sloth_std/stdio.rs | 28 ++++++++++++- crates/sloth_vm/src/sloth_std/term.rs | 32 +++++++++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 crates/sloth_vm/src/sloth_std/file.rs create mode 100644 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 new file mode 100644 index 0000000..b6c8adf --- /dev/null +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -0,0 +1,73 @@ +use std::fs; + +use crate::native::{self, NativeFunction, NativeFunctionResult}; +use crate::value::{Object, ObjectType, Primitive}; +use crate::VM; + +fn file_read(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 contents = fs::read_to_string(str).expect("IO Error: Failed to read file!"); + + let object = Object::new(ObjectType::String(contents)); + let ptr = vm.objects_mut().allocate(object); + + Ok(Primitive::Object(ptr as u32)) +} + +pub const FILE_READ: NativeFunction = NativeFunction { + name: "file$read", + function: file_read, + arity: 1, + returns_value: true, +}; + +fn file_write(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { + let Some(Primitive::Object(path_ptr)) = args.get(0).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let path_object = vm + .objects() + .get(path_ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(path) = &path_object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let Some(Primitive::Object(content_ptr)) = args.get(1).cloned() else { + return Err(native::Error::InvalidArgument); + }; + + let content_object = vm + .objects() + .get(content_ptr as usize) + .ok_or(native::Error::InvalidArgument)?; + + let ObjectType::String(content) = &content_object.typ else { + return Err(native::Error::InvalidArgument); + }; + + let _ = fs::write(path, content); + + Ok(Primitive::Empty) +} + +pub const FILE_WRITE: NativeFunction = NativeFunction { + name: "file$write", + function: file_write, + arity: 2, + returns_value: false, +}; diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index 86611d7..b40fb9a 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -4,8 +4,10 @@ use once_cell::sync::Lazy; use crate::native::NativeFunction; +pub mod file; pub mod rand; pub mod stdio; +pub mod term; pub static NATIVE_LIBRARY: Lazy> = Lazy::new(|| { let mut map = HashMap::new(); @@ -16,9 +18,16 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n // stdio map.insert("write", stdio::WRITE_FUNCTION); + map.insert("writeln", stdio::WRITELN_FUNCTION); map.insert("read", stdio::READ_FUNCTION); + // term + map.insert("term$clear", term::TERM_CLEAR); + map.insert("term$setpos", term::TERM_SETPOS); + // filesystem + map.insert("file$read", file::FILE_READ); + map.insert("file$write", file::FILE_WRITE); map }); 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() 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 19fefeb732d559195edb01ebc36170c0cf9a0308 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Fri, 14 Apr 2023 04:57:53 -0500 Subject: eepy --- crates/sloth/src/main.rs | 2 +- crates/sloth_vm/src/sloth_std/mod.rs | 4 ++++ crates/sloth_vm/src/sloth_std/time.rs | 25 +++++++++++++++++++++++++ examples/mandelbrot.sloth | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 crates/sloth_vm/src/sloth_std/time.rs create mode 100644 examples/mandelbrot.sloth 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> = Lazy::new(|| { let mut map = HashMap::new(); @@ -29,5 +30,8 @@ pub static NATIVE_LIBRARY: Lazy> = 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("*"); + } + } +} -- 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 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 38b564380a6362a82a0e5269b1e7e3d3635900e7 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Tue, 18 Apr 2023 09:15:35 -0500 Subject: Added small doc for wait command --- crates/sloth_vm/src/sloth_std/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sloth_vm/src/sloth_std/time.rs b/crates/sloth_vm/src/sloth_std/time.rs index 1c22294..d32d860 100644 --- a/crates/sloth_vm/src/sloth_std/time.rs +++ b/crates/sloth_vm/src/sloth_std/time.rs @@ -22,5 +22,5 @@ pub const WAIT: NativeFunction = NativeFunction { function: wait, arity: 1, returns_value: false, - doc: None, + doc: Some("Use `wait(sec: int)` to sleep for `sec` time"), }; -- 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(-) 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 448babe8090c2bcf177c23efd2257a926a1d07f7 Mon Sep 17 00:00:00 2001 From: Cody Date: Tue, 25 Apr 2023 19:37:44 -0500 Subject: Added Nix package --- flake.nix | 70 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/flake.nix b/flake.nix index 77fc725..ce3b001 100644 --- a/flake.nix +++ b/flake.nix @@ -1,4 +1,6 @@ { + description = "slothlang"; + inputs = { nixpkgs.url = "github:nixos/nixpkgs/master"; flake-utils.url = "github:numtide/flake-utils"; @@ -10,29 +12,51 @@ }; outputs = { self, nixpkgs, flake-utils, rust-overlay, ... }: - flake-utils.lib.eachSystem - [ "x86_64-linux" ] - (system: - let - overlays = [ (import rust-overlay) ]; - pkgs = import nixpkgs { - inherit system overlays; - }; - in - rec - { - devShell = pkgs.mkShell rec { - buildInputs = with pkgs; [ - (rust-bin.nightly."2023-02-10".default.override { - extensions = [ "rust-src" "rust-analyzer" ]; - targets = [ "wasm32-unknown-unknown" ]; - }) + flake-utils.lib.eachDefaultSystem (system: + let + overlays = [ (import rust-overlay) ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + + rustStable = pkgs.rust-bin.stable.latest.default; + rustNightly = pkgs.rust-bin.nightly."2023-02-10".default; + + rustPlatform = pkgs.makeRustPlatform { + cargo = rustStable; + rustc = rustStable; + }; + in + with pkgs; + { + packages.default = rustPlatform.buildRustPackage rec { + pname = "sloth"; + version = "0.1.0"; + src = ./.; + + # FIXME: Tests do not run in release mode + checkType = "debug"; + cargoLock = { + lockFile = ./Cargo.lock; + }; - cargo-deny - cargo-release - ]; + meta = with lib; { + description = "The Sloth programming language"; + homepage = "https://slothlang.tech"; + license = with licenses; [ mit asl20 ]; + }; + }; + devShells.default = mkShell { + buildInputs = [ + (rustNightly.override { + extensions = [ "rust-src" "rust-analyzer" ]; + targets = [ "wasm32-unknown-unknown" ]; + }) - LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath buildInputs; - }; - }); + cargo-deny + cargo-release + ]; + }; + } + ); } -- cgit v1.2.3 From 757e804671ef3dcb2e0f1295e385780b5feae2ca Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 17:18:11 -0500 Subject: Standard library finished for now --- crates/sloth/src/parser/stmt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth/src/parser/stmt.rs b/crates/sloth/src/parser/stmt.rs index 2c48da8..7c468ef 100644 --- a/crates/sloth/src/parser/stmt.rs +++ b/crates/sloth/src/parser/stmt.rs @@ -313,7 +313,7 @@ mod tests { #[test] fn basic_statement_a() { - let lexer = Lexer::new("var test_a = 5 + 3;"); + let lexer = Lexer::new("var test_a: int = 5 + 3;"); let tokens = lexer.collect_vec(); let expected_ast = Stmt::DefineVariable { @@ -323,7 +323,7 @@ mod tests { lhs: (Box::new(Expr::Literal(Literal::Integer(5)))), rhs: (Box::new(Expr::Literal(Literal::Integer(3)))), }), - typ: (None), + typ: Some("int".to_string()), }; let mut parser = AstParser::new(tokens); -- cgit v1.2.3 From fdaa289c2f55b31668ef0a6ec986cf459c834eb6 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 17:56:00 -0500 Subject: Applied changes requested by @CatDevz --- crates/sloth_vm/src/sloth_std/misc.rs | 8 ++++---- crates/sloth_vm/src/sloth_std/mod.rs | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 54d6e37..4417e59 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -13,12 +13,12 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { .get(ptr as usize) .ok_or(native::Error::InvalidArgument)?; - let ObjectType::String(str) = &object.typ else { + let ObjectType::NativeFunction(fnc) = &object.typ else { return Err(native::Error::InvalidArgument); }; let docs = NATIVE_LIBRARY - .get(str.as_str()) + .get(fnc.name.clone()) .ok_or(native::Error::InvalidArgument)? .doc .ok_or(native::Error::InvalidArgument)? @@ -30,8 +30,8 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { Ok(Primitive::Object(ptr as u32)) } -pub const GET_DOC: NativeFunction = NativeFunction { - name: "get$doc", +pub const DOCS: NativeFunction = NativeFunction { + name: "docs", function: get_doc, arity: 1, returns_value: true, diff --git a/crates/sloth_vm/src/sloth_std/mod.rs b/crates/sloth_vm/src/sloth_std/mod.rs index 982017d..ff761a6 100644 --- a/crates/sloth_vm/src/sloth_std/mod.rs +++ b/crates/sloth_vm/src/sloth_std/mod.rs @@ -36,5 +36,8 @@ pub static NATIVE_LIBRARY: Lazy> = Lazy::n // time map.insert("wait", time::WAIT); + // doc + map.insert("docs", misc::DOCS); + map }); -- cgit v1.2.3 From 1466d51eed5767e92d3264289590770932ceba96 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 17:58:31 -0500 Subject: Fixed error with clippy --- crates/sloth_vm/src/sloth_std/misc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 4417e59..6994657 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -18,7 +18,7 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { }; let docs = NATIVE_LIBRARY - .get(fnc.name.clone()) + .get(fnc.name) .ok_or(native::Error::InvalidArgument)? .doc .ok_or(native::Error::InvalidArgument)? -- cgit v1.2.3 From f0d121688d9d65ee855602eeaf60ed5c2bc1436f Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:14:52 -0500 Subject: Change "file_read" to "file$read" --- crates/sloth_vm/src/sloth_std/file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/file.rs b/crates/sloth_vm/src/sloth_std/file.rs index dea914a..ade1b2b 100644 --- a/crates/sloth_vm/src/sloth_std/file.rs +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -32,8 +32,8 @@ pub const FILE_READ: NativeFunction = NativeFunction { arity: 1, returns_value: true, 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 \ + "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'`", ), }; -- cgit v1.2.3 From 2367b44b4e3806ba6ae03c1299577caebeb6f7bd Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:16:07 -0500 Subject: Change "file_write" to "file$write" --- crates/sloth_vm/src/sloth_std/file.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/file.rs b/crates/sloth_vm/src/sloth_std/file.rs index ade1b2b..b0b476a 100644 --- a/crates/sloth_vm/src/sloth_std/file.rs +++ b/crates/sloth_vm/src/sloth_std/file.rs @@ -76,8 +76,8 @@ pub const FILE_WRITE: NativeFunction = NativeFunction { arity: 2, returns_value: false, 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'); # \ + "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'`", ), }; -- cgit v1.2.3 From 1cc0b13f3915a281aea109050b844f6cb38db71f Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:19:05 -0500 Subject: Change "get_doc" to "get$doc" --- crates/sloth_vm/src/sloth_std/misc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 6994657..dc6b8fd 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -36,8 +36,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, 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 \ + "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`", ), }; -- cgit v1.2.3 From 935f53cddfdc87dd0da0606595f4da1d4238c617 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:30:32 -0500 Subject: Doc stuff idk man --- crates/sloth_vm/src/sloth_std/misc.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 6994657..df8d452 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -17,13 +17,7 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { return Err(native::Error::InvalidArgument); }; - let docs = NATIVE_LIBRARY - .get(fnc.name) - .ok_or(native::Error::InvalidArgument)? - .doc - .ok_or(native::Error::InvalidArgument)? - .to_string(); - + let docs = fnc.doc.expect("Oopsie Poopsie the stringy no worky").to_string(); let object = Object::new(ObjectType::String(docs)); let ptr = vm.objects_mut().allocate(object); -- cgit v1.2.3 From a03609b894560780c7439168a20662516adbe100 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:31:09 -0500 Subject: Removed un needed import or whatever --- crates/sloth_vm/src/sloth_std/misc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 8993f49..e0aa05c 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -1,4 +1,3 @@ -use super::NATIVE_LIBRARY; use crate::native::{self, NativeFunction, NativeFunctionResult}; use crate::value::{Object, ObjectType, Primitive}; use crate::VM; -- cgit v1.2.3 From c0cbf40f54c80fc51348d1a4e00833f5957c5e6d Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:32:13 -0500 Subject: Update crates/sloth_vm/src/sloth_std/rand.rs --- crates/sloth_vm/src/sloth_std/rand.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/rand.rs b/crates/sloth_vm/src/sloth_std/rand.rs index 1718c6d..818b616 100644 --- a/crates/sloth_vm/src/sloth_std/rand.rs +++ b/crates/sloth_vm/src/sloth_std/rand.rs @@ -17,8 +17,8 @@ pub const GEN_FUNCTION: NativeFunction = NativeFunction { arity: 0, returns_value: true, 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`", + "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`", ), }; -- cgit v1.2.3 From 8a0eee45254f86f8fe50acda1a2a187c877a4701 Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Thu, 27 Apr 2023 18:32:58 -0500 Subject: Update crates/sloth_vm/src/sloth_std/rand.rs --- crates/sloth_vm/src/sloth_std/rand.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/rand.rs b/crates/sloth_vm/src/sloth_std/rand.rs index 818b616..870cca1 100644 --- a/crates/sloth_vm/src/sloth_std/rand.rs +++ b/crates/sloth_vm/src/sloth_std/rand.rs @@ -41,8 +41,8 @@ pub const GEN_RANGE_FUNCTION: NativeFunction = NativeFunction { arity: 2, returns_value: true, 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 \ + "NativeFunction rand$gen_range: \n\targs: min (int), max (int)\n\tdesc: Returns a random \ + numnber in the range .. \n\tExample: `var num = rand$gen_range(20, 76); # num \ could be any number from 20 to 76`", ), }; -- 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(-) 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(-) 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 From e49358828d84d4bf2a2ec8ee9447c8f5a00f90bc Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:34:50 -0500 Subject: docs fixed --- crates/sloth_vm/src/sloth_std/misc.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index e0aa05c..4476bfd 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -29,8 +29,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, 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 \ + "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ + function with name \n\tExample: `var doc = docs('wait'); # Returns the \ documentation of the 'wait' function to doc`", ), }; -- cgit v1.2.3 From d6b12291db70ea19d37d84226161f964400ae0b2 Mon Sep 17 00:00:00 2001 From: nic-gaffney Date: Thu, 27 Apr 2023 18:35:50 -0500 Subject: cargo-fmt lmao --- crates/sloth_vm/src/sloth_std/misc.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/sloth_vm/src/sloth_std/misc.rs b/crates/sloth_vm/src/sloth_std/misc.rs index 4476bfd..ca08d1d 100644 --- a/crates/sloth_vm/src/sloth_std/misc.rs +++ b/crates/sloth_vm/src/sloth_std/misc.rs @@ -16,7 +16,10 @@ fn get_doc(vm: &mut VM, args: &[Primitive]) -> NativeFunctionResult { return Err(native::Error::InvalidArgument); }; - let docs = fnc.doc.expect("Oopsie Poopsie the stringy no worky").to_string(); + let docs = fnc + .doc + .expect("Oopsie Poopsie the stringy no worky") + .to_string(); let object = Object::new(ObjectType::String(docs)); let ptr = vm.objects_mut().allocate(object); @@ -29,8 +32,8 @@ pub const DOCS: NativeFunction = NativeFunction { arity: 1, returns_value: true, doc: Some( - "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a \ - function with name \n\tExample: `var doc = docs('wait'); # Returns the \ - documentation of the 'wait' function to doc`", + "NativeFunction docs: \n\targs: name (str)\n\tdesc: Returns documentaiton on a function \ + with name \n\tExample: `var doc = docs('wait'); # Returns the documentation of the \ + 'wait' function to doc`", ), }; -- cgit v1.2.3 From fcb49524c0cff86c66d3a2c11ac8277fadb76bad Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 27 Apr 2023 22:32:42 -0500 Subject: Added .envrc --- .envrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a4726a --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use_nix -- cgit v1.2.3 From 99a08356c50190f783213ac99fc288712122f60f Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 27 Apr 2023 22:34:39 -0500 Subject: Remove .vscode --- .vscode/launch.json | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 961f091..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": "0.1.0", - "configurations": [ - { - "name": "Run Extension", - "type": "extensionHost", - "request": "launch", - "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}/ext"] - } - ] -} -- cgit v1.2.3 From a2185236bc6555c9ce96de0326d4c6a622469e13 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 4 May 2023 03:48:18 -0500 Subject: Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5507c5b..b7793f3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Sloth - Blazingly Slow™ -Sloth is an interpreted high level language written in Rust. The syntax is meant to be close to Python but with sprinkles of Rust. +Sloth is an interpreted high level language written in Rust. ## Build To build sloth is easy, just run `cargo build` and you will have your own version of sloth! -- cgit v1.2.3 From e92ee3d43d9101e34ab6727441dc9567b8f80d72 Mon Sep 17 00:00:00 2001 From: Cody Date: Wed, 24 May 2023 00:18:31 -0500 Subject: Some changes --- Cargo.lock | 9 +++++++-- Cargo.toml | 1 + crates/sloth/Cargo.toml | 1 + crates/sloth/src/main.rs | 2 +- crates/sloth/src/parser/ast.rs | 5 +++++ crates/sloth/src/parser/expr.rs | 2 +- crates/sloth_asm/Cargo.toml | 8 ++++++++ crates/sloth_asm/src/lib.rs | 1 + flake.nix | 1 + 9 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 crates/sloth_asm/Cargo.toml create mode 100644 crates/sloth_asm/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b7ccbe0..c46664e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.141" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "once_cell" @@ -111,9 +111,14 @@ name = "sloth" version = "0.1.0" dependencies = [ "itertools", + "libc", "thiserror", ] +[[package]] +name = "sloth_asm" +version = "0.1.0" + [[package]] name = "sloth_bytecode" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 57e0bc4..118519e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "crates/sloth", + "crates/sloth_asm", "crates/sloth_bytecode", "crates/sloth_vm", ] diff --git a/crates/sloth/Cargo.toml b/crates/sloth/Cargo.toml index 2ee6919..1b7a9fc 100644 --- a/crates/sloth/Cargo.toml +++ b/crates/sloth/Cargo.toml @@ -7,4 +7,5 @@ edition.workspace = true [dependencies] itertools = "0.10.5" +libc = "0.2.142" thiserror = "1.0.40" diff --git a/crates/sloth/src/main.rs b/crates/sloth/src/main.rs index 0eb4c24..8fab058 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/src/parser/ast.rs b/crates/sloth/src/parser/ast.rs index 3c8cdeb..12dad13 100644 --- a/crates/sloth/src/parser/ast.rs +++ b/crates/sloth/src/parser/ast.rs @@ -24,6 +24,7 @@ pub enum BinaryOp { LogOr, Range, } + #[derive(Debug, PartialEq)] pub enum UnaryOp { Not, @@ -31,6 +32,7 @@ pub enum UnaryOp { BWComp, } + #[derive(Debug, PartialEq)] pub enum Literal { Integer(i128), @@ -41,6 +43,7 @@ pub enum Literal { Regex(String), List(Vec), } + #[derive(Debug, PartialEq)] pub enum Expr { Grouping(Box), @@ -61,11 +64,13 @@ pub enum Expr { Literal(Literal), Lambda, // TODO: Lambda } + #[derive(PartialEq, Debug)] pub struct FuncArgs { pub name: String, pub typ: Option, } + #[derive(PartialEq, Debug)] pub enum Stmt { ExprStmt(Expr), diff --git a/crates/sloth/src/parser/expr.rs b/crates/sloth/src/parser/expr.rs index ad35d20..8036552 100644 --- a/crates/sloth/src/parser/expr.rs +++ b/crates/sloth/src/parser/expr.rs @@ -156,7 +156,7 @@ impl<'a> AstParser<'a> { // Binary expressions in order of precedence from lowest to highest. binary_expr!(logical_or , logical_and , (TokenType::PipePipe)); binary_expr!(logical_and , range , (TokenType::AmpAmp)); - binary_expr!(range , equality , (TokenType::DotDot)); + binary_expr!(range , equality , (TokenType::DotDot)); binary_expr!(equality , comparison , (TokenType::BangEq | TokenType::EqEq)); binary_expr!(comparison , bitwise_shifting, (TokenType::Lt | TokenType::Gt | TokenType::LtEq | TokenType::GtEq)); binary_expr!(bitwise_shifting, additive , (TokenType::LtLt | TokenType::GtGt)); diff --git a/crates/sloth_asm/Cargo.toml b/crates/sloth_asm/Cargo.toml new file mode 100644 index 0000000..b3ae934 --- /dev/null +++ b/crates/sloth_asm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sloth_asm" + +license.workspace = true +version.workspace = true +edition.workspace = true + +[dependencies] diff --git a/crates/sloth_asm/src/lib.rs b/crates/sloth_asm/src/lib.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/crates/sloth_asm/src/lib.rs @@ -0,0 +1 @@ + diff --git a/flake.nix b/flake.nix index ce3b001..4223f11 100644 --- a/flake.nix +++ b/flake.nix @@ -53,6 +53,7 @@ targets = [ "wasm32-unknown-unknown" ]; }) + cargo-watch cargo-deny cargo-release ]; -- cgit v1.2.3