line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Language::Expr; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2016-07-03'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.29'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
82394
|
use 5.010001; |
|
6
|
|
|
|
|
17
|
|
7
|
6
|
|
|
6
|
|
24
|
use strict; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
112
|
|
8
|
6
|
|
|
6
|
|
23
|
use warnings; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
1136
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
5
|
|
|
5
|
1
|
5821
|
my $class = shift; |
12
|
5
|
|
|
|
|
28
|
bless {}, $class; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub get_compiler { |
16
|
3
|
|
|
3
|
1
|
9
|
my ($self, $name) = @_; |
17
|
3
|
|
|
|
|
8
|
my $mod = "Language::Expr::Compiler::$name"; |
18
|
3
|
|
|
|
|
17
|
(my $mod_pm = "$mod.pm") =~ s!::!/!g; |
19
|
3
|
|
|
|
|
1620
|
require $mod_pm; |
20
|
3
|
|
|
|
|
22
|
$mod->new(); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub get_interpreter { |
24
|
4
|
|
|
4
|
1
|
10
|
my ($self, $name) = @_; |
25
|
4
|
|
|
|
|
12
|
my $mod = "Language::Expr::Interpreter::$name"; |
26
|
4
|
|
|
|
|
27
|
(my $mod_pm = "$mod.pm") =~ s!::!/!g; |
27
|
4
|
|
|
|
|
2076
|
require $mod_pm; |
28
|
4
|
|
|
|
|
31
|
$mod->new(); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
1; |
32
|
|
|
|
|
|
|
# ABSTRACT: Simple minilanguage for use in expression |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
__END__ |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=pod |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=encoding UTF-8 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 NAME |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Language::Expr - Simple minilanguage for use in expression |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 VERSION |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
This document describes version 0.29 of Language::Expr (from Perl distribution Language-Expr), released on 2016-07-03. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 SYNOPSIS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use Language::Expr; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $le = Language::Expr->new; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# convert Expr to string Perl code |
55
|
|
|
|
|
|
|
say $le->get_compiler('perl')->compile('1 ^^ 2'); => # "(1 xor 2)" |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# convert Expr to JavaScript |
58
|
|
|
|
|
|
|
say $le->get_compiler('js')->compile('1 . 2'); # => "'' + 1 + 2" |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# evaluate Expr using the default interpreter |
61
|
|
|
|
|
|
|
say $le->get_interpreter('default')->eval('1 + 2'); # => 3 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# enumerate variables |
64
|
|
|
|
|
|
|
my $vars = $le->enum_vars('$a*$a + sqr($b)'); # => ['a', 'b'] |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Language::Expr defines a simple, Perl-like expression minilanguage. It supports |
69
|
|
|
|
|
|
|
mathematical and string operators, arrays, hashes, variables, and functions. See |
70
|
|
|
|
|
|
|
L<Language::Expr::Manual::Syntax> for description of the language syntax. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This distribution consists of the language parser (L<Language::Expr::Parser>), |
73
|
|
|
|
|
|
|
some interpreters (Language::Expr::Interpreter::*), and some compilers |
74
|
|
|
|
|
|
|
(Language::Expr::Compiler::*). |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 KNOWN BUGS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Due to possible bugs in Perl's RE engine or Regexp::Grammars or my |
79
|
|
|
|
|
|
|
grammar, some syntax errors will cause further parsing to |
80
|
|
|
|
|
|
|
fail. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 METHODS |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 new() |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 get_compiler($name) => obj |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Get compiler named C<$name>, e.g. C<perl>, C<js>. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 get_interpreter($name) => obj |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Get compiler named C<$name>, e.g. C<default>, C<var_enumer>, C<dummy>. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 FAQ |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 Why yet another simplistic (restricted, etc) language? Why not just Perl? |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
When first adding expression support to L<Data::Schema> (now L<Data::Sah>), I |
101
|
|
|
|
|
|
|
want a language that is simple enough so I can easily convert it to Perl, PHP, |
102
|
|
|
|
|
|
|
JavaScript, and others. I do not need a fully-fledged programming language. In |
103
|
|
|
|
|
|
|
fact, Expr is not even Turing-complete, it does not support assignment or loops. |
104
|
|
|
|
|
|
|
Nor does it allow function definition (though it allows anonymous function in |
105
|
|
|
|
|
|
|
grep/map/usort). Instead, I just need some basic stuffs like |
106
|
|
|
|
|
|
|
mathematical/string/logical operators, arrays, hashes, functions, |
107
|
|
|
|
|
|
|
map/grep/usort. This language will mostly be used inside templates and schemas. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 Why don't you use Language::Farnsworth, or Math::Expression, or Math::Expression::Evaluator, or $FOO? |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
I need several compilers and interpreters (some even with different semantics), |
112
|
|
|
|
|
|
|
so it's easier to start with a simple parser of my own. And of course there is |
113
|
|
|
|
|
|
|
personal preference of language syntax. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 What is the difference between a compiler and interpreter? |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
An interpreter evaluates expression as it is being parsed, while a compiler |
118
|
|
|
|
|
|
|
generates a complete Perl (or whatever) code first. Thus, if you $le->eval() |
119
|
|
|
|
|
|
|
repeatedly using the interpreter mode (setting $le->interpreted(1)), you will |
120
|
|
|
|
|
|
|
repeatedly parse the expression each time. This can be one or more orders of |
121
|
|
|
|
|
|
|
magnitude slower compared to compiling into Perl once and then directly |
122
|
|
|
|
|
|
|
executing the Perl code repeatedly. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Note that if you use $le->eval() using the default compiler mode, you do not |
125
|
|
|
|
|
|
|
reap the benefits of compilation because the expression will be compiled each |
126
|
|
|
|
|
|
|
time you call $le->eval(). To save the compilation result, use $le->compile() or |
127
|
|
|
|
|
|
|
$le->perl() and compile the Perl code yourself using Perl's eval(). |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 I want different syntax for (variables, foo operator, etc)! |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Create your own language :-) Fork this distribution and start |
132
|
|
|
|
|
|
|
modifying the Language::Expr::Parser module. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 How to show details of errors in expression? |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
This is a TODO item. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 HOMEPAGE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Language-Expr>. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SOURCE |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Source repository is at L<https://github.com/sharyanto/perl-Language-Expr>. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 BUGS |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Language-Expr> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
151
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
152
|
|
|
|
|
|
|
feature. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SEE ALSO |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Syntax reference: L<Language::Expr::Manual::Syntax> |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Modules that are using Language::Expr: L<Data::Sah>, L<Data::Template::Expr> |
159
|
|
|
|
|
|
|
(not yet released). |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Other related modules: L<Math::Expression>, L<Math::Expression::Evaluator>, |
162
|
|
|
|
|
|
|
L<Language::Farnsworth> |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 AUTHOR |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This software is copyright (c) 2016 by perlancar@cpan.org. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
173
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |