line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::SymbolicX::Calculator; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22418
|
use 5.006; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
25
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
843
|
use Params::Util qw/_INSTANCE/; |
|
1
|
|
|
|
|
5656
|
|
|
1
|
|
|
|
|
80
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
998
|
use Math::Symbolic (); |
|
1
|
|
|
|
|
149912
|
|
|
1
|
|
|
|
|
28
|
|
10
|
1
|
|
|
1
|
|
1161
|
use Math::Symbolic::Custom::Transformation; |
|
1
|
|
|
|
|
25551
|
|
|
1
|
|
|
|
|
76
|
|
11
|
|
|
|
|
|
|
require Math::SymbolicX::Calculator::Command; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
11
|
use vars qw/$Identifier_Regex/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
392
|
|
16
|
|
|
|
|
|
|
$Identifier_Regex = qr/[a-zA-Z][a-zA-Z_0-9]*/; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=encoding utf8 |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Math::SymbolicX::Calculator - A representation of a Symbolic Calculator |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# You probably want to use on of the interfaces instead such as |
27
|
|
|
|
|
|
|
# Math::SymbolicX::Calculator::Interface::Shell |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Math::SymbolicX::Calculator; |
30
|
|
|
|
|
|
|
my $calc = Math::SymbolicX::Calculator->new(); |
31
|
|
|
|
|
|
|
my $cmd = $calc->new_command(...); |
32
|
|
|
|
|
|
|
# ... |
33
|
|
|
|
|
|
|
$calc->execute($cmd); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 DESCRIPTION |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
This class represents the state of a symbolic calculator. It is mainly |
38
|
|
|
|
|
|
|
a glorified state hash of variables and their contents. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
It can execute commands which are represented by |
41
|
|
|
|
|
|
|
L objects and which operate |
42
|
|
|
|
|
|
|
on the symbol table on some way. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Any slot of the symbol table may either contain a L tree |
45
|
|
|
|
|
|
|
or a L object. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head2 new |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Returns a new Calculator object. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
58
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
59
|
0
|
|
0
|
|
|
|
my $class = ref($proto)||$proto; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $self = { |
62
|
|
|
|
|
|
|
stash => {}, |
63
|
|
|
|
|
|
|
history => [], |
64
|
|
|
|
|
|
|
}; |
65
|
0
|
|
|
|
|
|
bless $self => $class; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $self; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 new_command |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
This method is a short-cut to |
74
|
|
|
|
|
|
|
L's C method and |
75
|
|
|
|
|
|
|
creates a new command object which can be executed using the |
76
|
|
|
|
|
|
|
Calculator object. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub new_command { |
81
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
82
|
0
|
|
|
|
|
|
return Math::SymbolicX::Calculator::Command->new(@_); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 execute |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Executes the command given as first argument. The command should be a |
88
|
|
|
|
|
|
|
L object. Returns any |
89
|
|
|
|
|
|
|
return values of the command's execution. (This may be a list!) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub execute { |
94
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
95
|
0
|
|
|
|
|
|
my $cmd = shift; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my @output = $cmd->_execute($self); |
98
|
0
|
|
|
|
|
|
return @output; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 stash |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Accesses the symbol table hash with the symbol name given as first |
104
|
|
|
|
|
|
|
argument. Valid symbol names match the regex C[A-Za-z][A-Za-z0-9_]*/>. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
(This is read only.) |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub stash { |
111
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
112
|
0
|
|
|
|
|
|
my $sym = shift; |
113
|
0
|
|
|
|
|
|
return $self->{stash}{$sym}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 get_transformation |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
First argument must be a symbol name. Accesses the Calculator symbol table |
119
|
|
|
|
|
|
|
to fetch a transformation from it that is saved as the symbol. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
If the smybol table contains a transformation in the specified slot, |
122
|
|
|
|
|
|
|
that transformation is returned. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
If it contains a formula, it manufactures a transformation from that |
125
|
|
|
|
|
|
|
formula which amounts to replacing the specified symbol with |
126
|
|
|
|
|
|
|
the formula. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
If an error occurrs, an error message will be returned instead of |
129
|
|
|
|
|
|
|
a C object. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub get_transformation { |
134
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
135
|
0
|
|
|
|
|
|
my $sym = shift; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $obj = $self->stash($sym); |
138
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if (_INSTANCE($obj, 'Math::Symbolic::Custom::Transformation')) { |
|
|
0
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
return $obj; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
elsif (ref($obj) =~ /^Math::Symbolic::/) { |
143
|
|
|
|
|
|
|
# insertion of the form |
144
|
|
|
|
|
|
|
# "bar = baz^2; foo = bar+2; foo =~ bar;" ==> foo==baz^2+2 |
145
|
0
|
|
|
|
|
|
my $trafo; |
146
|
0
|
|
|
|
|
|
eval { |
147
|
0
|
|
|
|
|
|
$trafo = Math::Symbolic::Custom::Transformation->new( $sym, $obj ); |
148
|
|
|
|
|
|
|
}; |
149
|
0
|
0
|
0
|
|
|
|
if ($@ or not defined $trafo) { |
150
|
0
|
0
|
|
|
|
|
my $error = "Invalid transformation: '$sym -> $obj' " . ($@?" Error: $@":""); |
151
|
0
|
|
|
|
|
|
return($error); |
152
|
|
|
|
|
|
|
} |
153
|
0
|
|
|
|
|
|
return $trafo; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
else { |
156
|
0
|
|
|
|
|
|
my $error = "Invalid or undefined symbol '$sym'"; |
157
|
0
|
|
|
|
|
|
return($error); |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
die "Sanity check"; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__END__ |