Language::Expr - Simple minilanguage for use in expression
version 0.06
use Language::Expr; my $le = new Language::Expr; $le->var('a' => 1, 'b' => 2); $le->func(sqr => sub { $_[0] ** 2 }, rand => sub {rand()}); # evaluate expression say $le->eval('$a + sqr($b)'); # 5 # enumerate variables say $le->enum_vars('$a*$a + sqr($b)'); # ['a', 'b']
Language::Expr defines a simple, Perl-like expression minilanguage. It supports mathematical and string operators, arrays, hashes, variables, and functions. See Language::Expr::Manual::Syntax for description of the language syntax.
The language is very simple. The parser is just around 120 lines long.
This distribution consists of the language parser (Language::Expr::Parser), some interpreters (Language::Expr::Interpreter::*), and some compilers (Language::Expr::Compiler::*).
Whether to use the interpreter. By default is 0 (use the compiler, which means Language::Expr expression will be compiled to Perl code first before executed).
The Language::Expr::Interpreter::Default instance.
The Language::Expr::Compiler::Perl instance.
The Language::Expr::Interpreter::VarEnumer instance.
Construct a new Language::Expr object.
Define variables.
Define functions. Dies if function is defined multiple times.
Evaluate expression in STR and return the result. Will die if there is a parsing or runtime error. By default it uses the compiler unless you set interpreted to 1.
interpreted
Enumerate variables mentioned in expression STR. Return empty arrayref if no variables are mentioned.
When first adding expression support to Data::Schema, I want a language that is simple enough so I can easily convert it to Perl, PHP, JavaScript, and others. I do not need a fully-fledged programming languag. In fact, Expr is not even Turing-complete, it does not support assignment or loops. Nor does it allow function definition (though it allows anonymous function in grep/map/usort). Instead, I just need some basic stuffs like mathematical/string/logical operators, arrays, hashes, functions, map/grep/usort. This language will mostly be used inside templates and schemas.
I need several compilers and interpreters (some even with different semantics), so that it's easier to start with a simple parser of my own. And of course there are personal preference of language syntax.
Create your own language :-) Fork this distribution and start modifying the Language::Expr::Parser module.
I personally am not having problem with performance. In fact, Regexp::Grammmars should be much faster than Parse::RecDescent. If you need faster parsing speed you can take a look at reimplementing the parser using Parse::Yapp, Parse::Eyapp, etc.
If you are having performance runtime problem, try switching from using the interpreter to using one of the available compilers.
This is a TODO item.
use Language::Expr::Compiler::Perl; my $plc = Language::Expr::Compiler::Perl; print $plc->perl('1 ^^ 2'); # prints '1 xor 2'
See Language::Expr::Compiler::Perl for more details.
use Language::Expr::Compiler::JS; my $jsc = Language::Expr::Compiler::JS; print $jsc->js('map({$_**2}, [1, 2, 3])'); # prints '[1, 2, 3].map(function(_){ Math.pow(_, 2) })'
See Language::Expr::Compiler::JS for more details.
Due to possible bugs in Perl's RE engine or Regexp::Grammars or my grammar, some syntax errors will cause further parsing to fail. Variable interpolation inside double quoted strings also doesn't work yet (segfaults).
Syntax reference: Language::Expr::Manual::Syntax
Modules that are using Language::Expr: Data::Schema, Data::Template::Expr.
Other related modules: Math::Expression, Math::Expression::Evaluator, Language::Farnsworth
Steven Haryanto <stevenharyanto@gmail.com>
This software is copyright (c) 2010 by Steven Haryanto.
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 Language::Expr, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Language::Expr
CPAN shell
perl -MCPAN -e shell install Language::Expr
For more information on module installation, please visit the detailed CPAN module installation guide.