From 50abe9765ab6276ecb96ba651ee189c9971b568b Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 30 Nov 2023 11:07:52 -0500 Subject: [PATCH] Implement random_float and random_boolean --- src/built_in_functions/mod.rs | 4 +++- src/built_in_functions/random.rs | 28 ++++++++++++++++++++++++++++ tree-sitter-dust/corpus/yield.txt | 12 ++++-------- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/built_in_functions/mod.rs b/src/built_in_functions/mod.rs index fbb3fe1..8be8f65 100644 --- a/src/built_in_functions/mod.rs +++ b/src/built_in_functions/mod.rs @@ -7,7 +7,7 @@ mod output; mod random; mod r#type; -pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 11] = [ +pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 13] = [ &assert::Assert, &assert::AssertEqual, &data_formats::FromJson, @@ -17,6 +17,8 @@ pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 11] = [ &fs::Append, &output::Output, &random::Random, + &random::RandomBoolean, + &random::RandomFloat, &random::RandomInteger, &r#type::Type, ]; diff --git a/src/built_in_functions/random.rs b/src/built_in_functions/random.rs index 03660e6..94bd097 100644 --- a/src/built_in_functions/random.rs +++ b/src/built_in_functions/random.rs @@ -32,3 +32,31 @@ impl BuiltInFunction for RandomInteger { Ok(Value::Integer(random())) } } + +pub struct RandomFloat; + +impl BuiltInFunction for RandomFloat { + fn name(&self) -> &'static str { + "random_float" + } + + fn run(&self, arguments: &[Value], _context: &Map) -> Result { + Error::expect_argument_amount(self, 0, arguments.len())?; + + Ok(Value::Float(random())) + } +} + +pub struct RandomBoolean; + +impl BuiltInFunction for RandomBoolean { + fn name(&self) -> &'static str { + "random_boolean" + } + + fn run(&self, arguments: &[Value], _context: &Map) -> Result { + Error::expect_argument_amount(self, 0, arguments.len())?; + + Ok(Value::Boolean(random())) + } +} diff --git a/tree-sitter-dust/corpus/yield.txt b/tree-sitter-dust/corpus/yield.txt index 7889ccd..b653d76 100644 --- a/tree-sitter-dust/corpus/yield.txt +++ b/tree-sitter-dust/corpus/yield.txt @@ -13,8 +13,7 @@ Simple Yield (expression (value (integer))) - (expression - (identifier)))))) + (identifier))))) ================================================================================ Yield Chain @@ -34,9 +33,6 @@ x -> (foo) -> (bar) -> (abc) (yield (expression (identifier)) - (expression - (identifier)))) - (expression - (identifier)))) - (expression - (identifier)))))) + (identifier))) + (identifier))) + (identifier)))))