Skip to content

Commit

Permalink
Add array type to documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed May 24, 2022
1 parent 2ec5d73 commit f29d66e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ true || false

// Strings are also supported
concat("foo", "bar")

// Arrays can be composed of any combination of types
[10, 12] + [1.2, 1.3]
2 * [10, 5] // Operations can also be applied between scalar values and arrays
[false, 0, true] == true // An array evaluates to true if any element is true
```

Beyond the simpler operators, the following operations are supported:
Expand Down Expand Up @@ -181,6 +186,10 @@ value = pi * e * tau
f(x) = 2*x**2 + 3*x + 5
f(2.3)

// Functions work well with arrays
sum(a) = element(a, 0) + ( len(a)>1 ? sum(dequeue(a)) : 0 )
sum([10, 10, 11])

// Recursive functions work too!
factorial(x) = x==0 ? 1 : (x * factorial(x - 1) )
factorial(5)
Expand Down Expand Up @@ -219,6 +228,9 @@ tan(r), cos(r), sin(r), atan(r), acos(r), asin(r), tanh(r), cosh(r), sinh(r)
ln(n) | log10(n) | log(n, base)
sqrt(n) | root(n, base)

// Typecasting
bool(n) | array(n) | int(n) | float(n)

// RNG functions
choose("argument 1", 2, 3.0, ...) | rand() | rand(min, max)

Expand Down
4 changes: 2 additions & 2 deletions src/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,14 @@ const INT : DecoratorDefinition = DecoratorDefinition {
const BOOL : DecoratorDefinition = DecoratorDefinition {
name: &["bool", "boolean"],
description: "Format a number as a boolean",
argument: ExpectedTypes::IntOrFloat,
argument: ExpectedTypes::Any,
handler: |_, input| Ok(Value::Boolean(input.as_bool()).as_string())
};

const ARRAY : DecoratorDefinition = DecoratorDefinition {
name: &["array"],
description: "Format a number as an array",
argument: ExpectedTypes::Array,
argument: ExpectedTypes::Any,
handler: |_, input| Ok(Value::Array(input.as_array()).as_string())
};

Expand Down
50 changes: 50 additions & 0 deletions src/functions/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,50 @@ use super::{FunctionDefinition, FunctionArgument, FunctionTable};
use crate::value::{Value, IntegerType};
use crate::errors::*;

const BOOL : FunctionDefinition = FunctionDefinition {
name: "bool",
description: "Returns a value as a boolean",
arguments: || vec![
FunctionArgument::new_required("n", ExpectedTypes::Any),
],
handler: |_, args: &[Value]| {
Ok(Value::Boolean(args[0].as_bool()))
}
};

const ARRAY : FunctionDefinition = FunctionDefinition {
name: "array",
description: "Returns a value as an array",
arguments: || vec![
FunctionArgument::new_required("n", ExpectedTypes::Any),
],
handler: |_, args: &[Value]| {
Ok(Value::Array(args[0].as_array()))
}
};

const INT : FunctionDefinition = FunctionDefinition {
name: "int",
description: "Returns a value as an integer",
arguments: || vec![
FunctionArgument::new_required("n", ExpectedTypes::IntOrFloat),
],
handler: |_, args: &[Value]| {
Ok(Value::Integer(args[0].as_int().unwrap()))
}
};

const FLOAT : FunctionDefinition = FunctionDefinition {
name: "float",
description: "Returns a value as a float",
arguments: || vec![
FunctionArgument::new_required("n", ExpectedTypes::IntOrFloat),
],
handler: |_, args: &[Value]| {
Ok(Value::Float(args[0].as_float().unwrap()))
}
};

const MIN : FunctionDefinition = FunctionDefinition {
name: "min",
description: "Returns the smallest numeric value from the supplied arguments",
Expand Down Expand Up @@ -153,6 +197,12 @@ const ROOT : FunctionDefinition = FunctionDefinition {

/// Register string functions
pub fn register_functions(table: &mut FunctionTable) {
// Typecasting
table.register(BOOL);
table.register(ARRAY);
table.register(INT);
table.register(FLOAT);

// Rounding functions
table.register(MIN);
table.register(MAX);
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn bool_expression_handler(token: &mut Token, _state: &mut ParserState) -> O
Rule::le => token.set_value(Value::Boolean(l.as_string() <= r.as_string())),
_ => {}
}
} else if l.is_bool() && r.is_bool() {
} else if l.is_bool() || r.is_bool() {
match token.child(i+1).unwrap().rule() {
Rule::lt => token.set_value(Value::Boolean(!l.as_bool() & r.as_bool())),
Rule::gt => token.set_value(Value::Boolean(l.as_bool() & !r.as_bool())),
Expand Down
5 changes: 5 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,11 @@ mod test_token {
token_does_value_equal("true == false", Value::Boolean(false), &mut state);
token_does_value_equal("true == false != true", Value::Boolean(true), &mut state);

// arrays
token_does_value_equal("[10, 12] + [1.2, 1.3]", Value::Array(vec![Value::Float(11.2), Value::Float(13.3)]), &mut state);
token_does_value_equal("2 * [10, 5]", Value::Array(vec![Value::Integer(20), Value::Integer(10)]), &mut state);
token_does_value_equal("[false, 0, true] == true", Value::Boolean(true), &mut state);

// Function
let t = Token::new("5+5\nfn(x, y) = x * y\n5+5", &mut state).unwrap();
assert_eq!("10\nx * y\n10", t.text);
Expand Down

0 comments on commit f29d66e

Please sign in to comment.