From bddb011df4999f7ffeeddf6a4b66e2da6ab19ea0 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 15 Dec 2022 13:23:48 -0600 Subject: Initial language designs & lexer from crafting interpreters The very initial language designs I came up with for Sloth. Likely contains inconsistencies and definitely contains things that will be changed in the future. This is basically just a dump of every idea I've had for the language thus far. As for the lexer right now it is heavily based on the one from the Crafting Interpretrs book and doesn't yet parse Sloth grammar. --- tour/functions.sloth | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tour/functions.sloth (limited to 'tour/functions.sloth') diff --git a/tour/functions.sloth b/tour/functions.sloth new file mode 100644 index 0000000..e6c8b89 --- /dev/null +++ b/tour/functions.sloth @@ -0,0 +1,68 @@ +# Types can be inferred. +# If inferrence fails it will be set to "any" unless strict mode is on +pub fn add(lhs, rhs) { + let result = lhs + rhs; + return result; +} + +# ...or manually specified +pub fn mul(lhs: i32, rhs: i32) -> i32 { + let result = lhs * rhs; + return result; +} + +## Docstrings can be used with 2 pound signs +## +## lhs: Left hand side of subtraction +## rhs: Right hand side of subtraction +pub fn sub(lhs: i32, rhs: i32) -> i32 { + let result = lhs - rhs; + return result; +} + +## Fizzbuzz implementation from 1 through 100 +fn fizzbuzz() { + for x in 1..=100 { + let message = match (x % 5, x % 3) { + (0, 0) => "FizzBuzz", + (0, _) => "Fizz", + (_, 0) => "Buzz", + _ => x, + }; + + print(message); + } +} + +## Fizzbuzz implementation using a generator and a range passed into the function +## +## Generator functions are convenient ways to create iterators. Whatever the +## return type is will automatically be wrapped in an Iterator. In the following +## example the function return type would become `Iterator`. +## +## Unlike a normal function you use a yield statement which pauses the function +## call until the next element is requested. Return can still be used in a +## generator function, however it will be used to enact a full stop +generator fn fizzbuzz(range: Range) -> String { + for i in range { + yield match (i % 5, i % 3) { + (0, 0) => "FizzBuzz", + (0, _) => "Fizz", + (_, 0) => "Buzz", + _ => i, + }; + } +} + +fn print_fizzbuzz() { + for message in fizzbuzz(1..=100) { + print(message) + } +} + +pub fn splitting() { + # You are able to call .split and pass in anything that implements Into + "Ylc xsBDSv4e BL5m 1BgDSjv dbQj".split(' '); + "Ylc xsBDSv4e BL5m 1BgDSjv dbQj".split("DS"); + "Ylc xsBDSv4e BL5m 1BgDSjv dbQj".split(/[0-9A-F]{2}/); +} -- cgit v1.2.3