Zapp::Formula - Formula interpreter
version 0.005
my $f = Zapp::Formula->new; # [ call => [ var => 'UPPER' ], [ binop => '&', [ string => "hello " ], [ var => "name" ] ] ] my $tree = $f->parse( 'UPPER( "hello " & name )' ); # HELLO LEELA my $res = $f->eval( 'UPPER( "hello " & name )', { name => 'Leela' } ); # { greeting => "HELLO LEELA" } my $data = $f->resolve( { greeting => 'UPPER( "hello " & name )' }, { name => 'Leela' } );
This module parses and evaluates formulas. Formulas are strings that begin with one = and contain an expression. Formula expressions can contain strings, numbers, variables, binary operations, function calls, and array or hash literals.
=
my $tree = $f->parse( $formula )
Parse the given formula (without = prefix) and return an abstract syntax tree that can be evaluated.
my $value = $f->eval( $formula, $context );
Parse and execute the given formula (without = prefix) using the given context as values for any variables.
my $data = $f->resolve( $data, $context );
Resolve all formulas in the data structure $data and return a new data structure with the resolved values. Formulas are strings that begin with =. Use == to escape parsing.
$data
==
# { val => 1, str => '=num' } $f->resolve( { val => '=num', str => '==num' }, { num => 1 } );
Where possible, the formula syntax resembles the syntax from popular spreadsheet programs like Lotus 1-2-3, Excel, and Sheets, and Microsoft's Power Fx.
Strings are surrounded by double-quotes ("Professor Fisherprice Shpeekenshpell"). Double-quotes can be inserted into strings by adding a backslash ("The horse says \"Doctorate Denied\"").
"Professor Fisherprice Shpeekenshpell"
"The horse says \"Doctorate Denied\""
Numbers can be integers (312) or decimals (3.12). Negative numbers have - in front (-3.12).
312
3.12
-
-3.12
Variables start with a letter and can contain letters, numbers, and underscores (_). Variable values are defined in the context. Variables cannot be created by formulas (yet).
_
# Addition 1.2 + 3 -> 4.2 # Subtraction 4 - 2.3 -> 1.7 # Multiplication 2 * 3 -> 6 # Division 8 / 2 -> 4 # Exponentation 2 ^ 3 -> 8
# Concatenation "Hello, " & "World" -> "Hello, World"
# Equality 2 = 2 -> TRUE # Inequality 3 <> 3 -> FALSE # Less-/Greater-than 3 < 8 -> TRUE 3 > 8 -> FALSE # Less-/Greater-than-or-equal 3 <= 2 -> FALSE 3 >= 3 -> TRUE
Function calls start with the name of the function followed by empty parentheses or parentheses containing function parameters (expressions) separated by commas.
IF( name = "Leela", TRUE(), FALSE() )
See "FUNCTIONS" for a list of available functions.
Arrays begin with square brackets and contain expressions separated by commas.
[ name, name = "Leela", TRUE() ]
Get a value from an array using square brackets and the index of the item (0-based).
# Characters = [ "Fry", "Leela", "Bender" ] Characters[2] # Bender
Hashes begin with curly braces and contain key/value pairs separated by commas. Keys must be strings (for now) and are separated from values with colons.
{ "name": "Leela", "captain": TRUE() }
Get a value from a hash using dot followed by the key.
# Employees = { "Pilot": "Leela", "Cook": "Bender", "Person": "Fry" } Employees.Pilot # Leela
Zapp::Task, Zapp
=AND( <expression>... )
Returns TRUE if all expressions are true.
TRUE
=EVAL( <string> )
Evaluate the string as a formula and return the result. The string must not begin with an =.
=FALSE()
Returns a false value.
=IF( <expression>, <true_result>, <false_result> )
Evaluate the expression in expression and return true_result if the condition is true, or false_result if the condition is false.
expression
true_result
false_result
=IFS( <expression>, <result>, ..., <default_result> )
Evaluate each expression and return its corresponding result if the expression is true. Return default_result if no condition is true.
default_result
=NOT( <expression> )
Returns TRUE if the expression is true, FALSE otherwise.
FALSE
=OR( <expression>... )
Returns TRUE if one expression is true.
=TRUE()
Returns a true value.
=XOR( <expression>... )
Returns TRUE if one and only one expression is true.
Doug Bell <preaction@cpan.org>
This software is copyright (c) 2021 by Doug Bell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Zapp, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Zapp
CPAN shell
perl -MCPAN -e shell install Zapp
For more information on module installation, please visit the detailed CPAN module installation guide.