blib/lib/Language/Farnsworth/Evaluate.pm

filter: Statementstmt 66.9% 283 / 423 Branchbran 69.2% 151 / 218 Conditioncond 58.3% 21 / 36 MC/DCmcdc 52.3% 11 / 21 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 4 totaltotal 67.3% 489 / 726 CC 129 SCAR 63.8
CC
129
Cov
67%
evalbranch
833.2
makevalue
17.3
eval
7.0
CRAP 588.6
« blib/lib/Language/Farnsworth/Value/Lambda.pm ↑ Summary blib/lib/Language/Farnsworth/Parser.pm »
1 #!/usr/bin/perl
2  
3 package Language::Farnsworth::Evaluate;
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Language::Farnsworth::FunctionDispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Language::Farnsworth::Variables;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Language::Farnsworth::Units;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Language::Farnsworth::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Language::Farnsworth::Value::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Language::Farnsworth::Value::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Language::Farnsworth::Value::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Language::Farnsworth::Value::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Language::Farnsworth::Value::Undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Language::Farnsworth::Value::Lambda;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Language::Farnsworth::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Language::Farnsworth::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Language::Farnsworth::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use Math::Pari;# ':hex'; #why not? because it fucks up so fucking badly that fuck isn't a strong enough word
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub new
28 314 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
29 314 my $class = shift;
30 314 my $self = {};
31 314 bless $self;
32  
33 314 my %opts = (@_);
34  
35 314 ▶ if (ref($opts{funcs}) eq "Language::Farnsworth::FunctionDispatch")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'funcs'} eq 'Language::Farnsworth::FunctionDispatch') { } 310 4 314
36 {
37 310 $self->{funcs} = $opts{funcs};
38 }
39 else
40 {
41 4 $self->{funcs} = new Language::Farnsworth::FunctionDispatch();
42 }
43  
44 314 ▶ if (ref($opts{vars}) eq "Language::Farnsworth::Variables")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'vars'} eq 'Language::Farnsworth::Variables') { } 310 4 314
45 {
46 310 $self->{vars} = $opts{vars};
47 }
48 else
49 {
50 4 $self->{vars} = new Language::Farnsworth::Variables();
51 }
52  
53 314 ▶ if (ref($opts{units}) eq "Language::Farnsworth::Units")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'units'} eq 'Language::Farnsworth::Units') { } 310 4 314
54 {
55 310 $self->{units} = $opts{units};
56 }
57 else
58 {
59 4 $self->{units} = new Language::Farnsworth::Units();
60 }
61  
62 314 ▶ if (ref($opts{parser}) eq "Language::Farnsworth::Parser")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'parser'} eq 'Language::Farnsworth::Parser') { } 310 4 314
63 {
64 310 $self->{parser} = $opts{parser};
65 }
66 else
67 {
68 4 $self->{parser} = new Language::Farnsworth::Parser();
69 }
70  
71 314 $self->{dumpbranches} = 0;
72  
73 314 return $self;
74 }
75  
76 sub DESTROY
77 183 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
78 183 debug 2,"SCOPE DIE: $_[0]";
79 }
80  
81 sub eval
82 197 ▶{
Subroutine eval: called CC=7 SCAR=19.5
Pod undocumented
83 197 my $self = shift;
84 197 my $code = shift; #i should probably take an array, so i can use arrays of things, but that'll be later
85  
86 197 $code =~ s/^\s*//;
87 197 $code =~ s/\s*$//;
88  
89 197 my $tree = $self->{parser}->parse($code); #should i catch the exceptions here? dunno
90  
91 196 debug 3, Dumper($tree);
92  
93 196 my $ret = eval{$self->evalbranch($tree)};
94
95 #capture return[] at minimum level
96 196 ▶ if ($@ && $@->isa("Language::Farnsworth::Error")&&$@->isreturn())
Branch: 3 branches
BranchTrueFalseTotal
if ($@ and $@->isa('Language::Farnsworth::Error') and $@->isreturn) { } 1 195 196
elsif ($@ and $@->isa('Language::Farnsworth::Error')) { } 6 189 195
elsif ($@) { } 1 188 189
Condition100%6/6
$@ and $@->isa("Language::Farnsworth::Error") and $@->isreturn
!ll&&!rl&&r
18961
$@ and $@->isa("Language::Farnsworth::Error")
!ll&&!rl&&r
18817
MC/DC100%3/3
$@ and $@->isa("Language::Farnsworth::Error") and $@->isreturn
$@ $@->isa("Language::Farnsworth::Error") $@->isreturn
Truth table100%4/4
A and B and C
A= $@B= $@->isa("Language::Farnsworth::Error")C= $@->isreturn
ABCresult
0XX0
10X0
1100
1111
97 {
98 1 return $@->getmsg();
99 }
100 ▶ elsif ($@ && $@->isa("Language::Farnsworth::Error"))
Condition100%3/3
$@ and $@->isa("Language::Farnsworth::Error")
!ll&&!rl&&r
18816
MC/DC100%2/2
$@ and $@->isa("Language::Farnsworth::Error")
$@ $@->isa("Language::Farnsworth::Error")
Truth table100%3/3
A and B
A= $@B= $@->isa("Language::Farnsworth::Error")
ABresult
0X0
100
111
101 {
102 6 return $@;
103 }
104 elsif ($@)
105 {
106 1 error EPERL, $@;
107 }
108 else
109 {
110 188 return $ret;
111 }
112 }
113  
114 #evaluate a single branch
115 sub evalbranch
116 21757 ▶{
Subroutine evalbranch: called CC=107 SCAR=67.3
Pod undocumented
117 21757 my $self = shift;
118  
119 21757 my $branch = shift;
120 21757 my $type = ref($branch); #this'll grab what kind from the bless on the tree
121  
122 21757 my $return; #to make things simpler later on
123  
124 #print Data::Dumper->Dump([$branch],["BRANCH"]);
125  
126 21757 ▶ if ($type eq "Add")
Branch: 49 branches
BranchTrueFalseTotal
if ($type eq 'Add') { } 65 21692 21757
elsif ($type eq 'Sub') { } 24 21668 21692
elsif ($type eq 'Mul') { } 7271 14397 21668
elsif ($type eq 'Div') { } 2795 11602 14397
elsif ($type eq 'Conforms') { } 7 11595 11602
elsif ($type eq 'Mod') { } 0 11595 11595
elsif ($type eq 'Pow') { } 978 10617 11595
elsif ($type eq 'And') { } 4 10613 10617
elsif ($type eq 'Or') { } 0 10613 10613
elsif ($type eq 'Xor') { } 0 10613 10613
elsif ($type eq 'Not') { } 0 10613 10613
elsif ($type eq 'Gt') { } 15 10598 10613
elsif ($type eq 'Lt') { } 7 10591 10598
elsif ($type eq 'Ge') { } 20 10571 10591
elsif ($type eq 'Le') { } 0 10571 10571
elsif ($type eq 'Compare') { } 47 10524 10571
elsif ($type eq 'Eq') { } 4 10520 10524
elsif ($type eq 'Ne') { } 0 10520 10520
elsif ($type eq 'Ternary') { } 18 10502 10520
elsif ($type eq 'If') { } 4 10498 10502
elsif ($type eq 'Store') { } 59 10439 10498
elsif ($type eq 'StoreAdd') { } 0 10439 10439
elsif ($type eq 'StoreSub') { } 0 10439 10439
elsif ($type eq 'StoreDiv') { } 0 10439 10439
elsif ($type eq 'StoreMul') { } 0 10439 10439
elsif ($type eq 'StoreMod') { } 0 10439 10439
elsif ($type eq 'StorePow') { } 0 10439 10439
elsif ($type eq 'PreInc') { } 0 10439 10439
elsif ($type eq 'PostInc') { } 0 10439 10439
elsif ($type eq 'PreDec') { } 0 10439 10439
elsif ($type eq 'PostDec') { } 0 10439 10439
elsif ($type eq 'DeclareVar') { } 36 10403 10439
elsif ($type eq 'DeclareFunc') { } 2 10401 10403
elsif ($type eq 'FuncDef') { } 116 10285 10401
elsif ($type eq 'Lambda') { } 28 10257 10285
elsif ($type eq 'LambdaCall') { } 47 10210 10257
elsif ($type eq 'Array' or $type eq 'SubArray') { } 219 9991 10210
elsif ($type eq 'ArgArray') { } 0 9991 9991
elsif ($type eq 'ArrayFetch') { } 32 9959 9991
elsif ($type eq 'ArrayStore') { } 0 9959 9959
elsif ($type eq 'While') { } 5 9954 9959
elsif ($type eq 'Stmt') { } 322 9632 9954
elsif ($type eq 'Paren') { } 347 9285 9632
elsif ($type eq 'SetDisplay') { } 0 9285 9285
elsif ($type eq 'UnitDef') { } 8680 605 9285
elsif ($type eq 'DefineDimen') { } 36 569 605
elsif ($type eq 'DefineCombo') { } 224 345 569
elsif ($type eq 'SetPrefix' or $type eq 'SetPrefixAbrv') { } 340 5 345
elsif ($type eq 'Trans') { } 0 5 5
127 {
128 65 my $a = $self->makevalue($branch->[0]);
129 65 my $b = $self->makevalue($branch->[1]);
130 65 $return = $a + $b;
131 }
132 elsif ($type eq "Sub")
133 {
134 24 my $a = $self->makevalue($branch->[0]);
135 24 my $b = $self->makevalue($branch->[1]);
136 24 $return = $a - $b;
137 }
138 elsif ($type eq "Mul")
139 {
140 7271 ▶ if ((ref($branch->[0]) eq "Fetch") && (ref($branch->[1]) eq "Array") && ($branch->[2] eq "imp"))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $branch->[0] eq 'Fetch' and ref $branch->[1] eq 'Array' and $branch->[2] eq 'imp') { } 100 7171 7271
Condition83%5/6
ref $branch->[0] eq "Fetch" and ref $branch->[1] eq "Array" and $branch->[2] eq "imp"
!ll&&!rl&&r
71710100
ref $branch->[0] eq "Fetch" and ref $branch->[1] eq "Array"
!ll&&!rl&&r
6642529100
MC/DC66%2/3
ref $branch->[0] eq "Fetch" and ref $branch->[1] eq "Array" and $branch->[2] eq "imp"
ref $branch->[0] eq "Fetch" ref $branch->[1] eq "Array" $branch->[2] eq "imp"
Truth table75%3/4
A and B and C
A= ref $branch->[0] eq "Fetch"B= ref $branch->[1] eq "Array"C= $branch->[2] eq "imp"
ABCresult
0XX0
10X0
1100
1111
141 {
142 #we've got a new style function call!
143 100 my $a = $branch->[0][0]; #grab the function name
144 100 my $b = $self->makevalue($branch->[1]);
145  
146 # print STDERR "----------------FUNCCALL! $a\n";
147 # print STDERR "$self";
148 # print Dumper($a, $b);
149
150 100 ▶ if ($self->{funcs}->isfunc($a)) #check if there is a func $a
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'funcs'}->isfunc($a)) { } 96 4 100
151 { #$return = $self->{funcs}->callfunc($self, $name, $args, $branch);
152 96 $return = $self->{funcs}->callfunc($self, $a, $b, $branch);
153 }
154 else #otherwise we try to
155 {
156 4 $a = $self->makevalue($branch->[0]); #evaluate it, since it wasn't a function
157
158 4 $return = $a * $b; #do the multiplication
159 }
160 }
161 else
162 {
163 7171 my $a = $self->makevalue($branch->[0]);
164 7171 my $b = $self->makevalue($branch->[1]);
165  
166 #print "-----------SUBMULT!\n";
167 #print Dumper($a,$b);
168  
169 7171 $return = $a * $b;
170 }
171 }
172 elsif ($type eq "Div")
173 {
174 2795 my $a = $self->makevalue($branch->[0]);
175 2795 my $b = $self->makevalue($branch->[1]);
176 #print Dumper($a, $b);
177 2795 $return = $a / $b;
178 }
179 elsif ($type eq "Conforms")
180 {
181 7 my $a = $self->makevalue($branch->[0]);
182 7 my $b = $self->makevalue($branch->[1]);
183 7 $return = new Language::Farnsworth::Value::Boolean($a->conforms($b));
184 }
185 elsif ($type eq "Mod")
186 {
187 0 my $a = $self->makevalue($branch->[0]);
188 0 my $b = $self->makevalue($branch->[1]);
189 0 $return = $a % $b;
190 }
191 elsif ($type eq "Pow")
192 {
193 978 my $a = $self->makevalue($branch->[0]);
194 978 my $b = $self->makevalue($branch->[1]);
195 978 $return = $a ** $b;
196 }
197 elsif ($type eq "And")
198 {
199 4 my $a = $self->makevalue($branch->[0]);
200  
201 4 ▶ if ($a->bool())
Branch: 1 branch
BranchTrueFalseTotal
if ($a->bool) { } 3 1 4
202 {
203 3 my $b = $self->makevalue($branch->[1]);
204 3 ▶ $return = $a && $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a && $b ? : 3 0 3
Condition33%1/3
$a and $b
!ll&&!rl&&r
030
MC/DC0%0/2
$a and $b
$a $b
Truth table33%1/3
A and B
A= $aB= $b
ABresult
0X0
100
111
205 3 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
206 }
207 else
208 {
209 1 $return = Language::Farnsworth::Value::Boolean->new(0); #make sure its the right type
210 }
211 }
212 elsif ($type eq "Or")
213 {
214 0 my $a = $self->makevalue($branch->[0]);
215  
216 0 ▶ if ($a->bool())
Branch: 1 branch
BranchTrueFalseTotal
if ($a->bool) { } 0 0 0
217 {
218 0 $return = Language::Farnsworth::Value::Boolean->new(1); #make sure its the right type
219 }
220 else
221 {
222 0 my $b = $self->makevalue($branch->[1]);
223 0 ▶ $return = $a || $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a || $b ? : 0 0 0
Condition0%0/3
$a or $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$a or $b
$a $b
Truth table0%0/3
A or B
A= $aB= $b
ABresult
1X1
011
000
224 0 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
225 }
226 }
227 elsif ($type eq "Xor")
228 {
229 0 my $a = $self->makevalue($branch->[0]);
230 0 my $b = $self->makevalue($branch->[1]);
231 0 ▶ $return = $a->bool() ^ $b->bool() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a->bool ^ $b->bool ? : 0 0 0
232 0 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
233 }
234 elsif ($type eq "Not")
235 {
236 0 my $a = $self->makevalue($branch->[0]);
237 0 ▶ $return = $a->bool() ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$a->bool ? : 0 0 0
238 0 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
239 }
240 elsif ($type eq "Gt")
241 {
242 15 my $a = $self->makevalue($branch->[0]);
243 15 my $b = $self->makevalue($branch->[1]);
244 15 ▶ $return = ($a > $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 6 9 15
245 15 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
246 }
247 elsif ($type eq "Lt")
248 {
249 7 my $a = $self->makevalue($branch->[0]);
250 7 my $b = $self->makevalue($branch->[1]);
251 7 ▶ $return = $a < $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 0 7 7
252 7 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
253 }
254 elsif ($type eq "Ge")
255 {
256 20 my $a = $self->makevalue($branch->[0]);
257 20 my $b = $self->makevalue($branch->[1]);
258 20 ▶ $return = $a >= $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a >= $b ? : 16 4 20
259 20 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
260 }
261 elsif ($type eq "Le")
262 {
263 0 my $a = $self->makevalue($branch->[0]);
264 0 my $b = $self->makevalue($branch->[1]);
265 0 ▶ $return = $a <= $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a <= $b ? : 0 0 0
266 0 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
267 }
268 elsif ($type eq "Compare")
269 {
270 47 my $a = $self->makevalue($branch->[0]);
271 47 my $b = $self->makevalue($branch->[1]);
272 47 $return = $a <=> $b;
273 47 $return = Language::Farnsworth::Value::Pari->new($return); #make sure its the right type
274 }
275 elsif ($type eq "Eq")
276 {
277 4 my $a = $self->makevalue($branch->[0]);
278 4 my $b = $self->makevalue($branch->[1]);
279 4 ▶ $return = $a == $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 3 1 4
280 4 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
281 }
282 elsif ($type eq "Ne")
283 {
284 0 my $a = $self->makevalue($branch->[0]);
285 0 my $b = $self->makevalue($branch->[1]);
286 0 ▶ $return = $a != $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a != $b ? : 0 0 0
287 0 $return = Language::Farnsworth::Value::Boolean->new($return); #make sure its the right type
288 }
289 elsif ($type eq "Ternary")
290 {
291 #turing completeness FTW
292 18 my $left = $self->makevalue($branch->[0]);
293 #$left = $left->bool() != new Language::Farnsworth::Value::Pari(0, $left->{dimen}); #shouldn't need it anymore, since i got ->bool working
294 18 ▶ $return = $left ? $self->makevalue($branch->[1]) : $self->makevalue($branch->[2]);
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 3 15 18
295 }
296 elsif ($type eq "If")
297 {
298 #turing completeness FTW
299 4 my $left = $self->makevalue($branch->[0]);
300 #$left = $left != new Language::Farnsworth::Value(0, $left->{dimen});
301
302 4 ▶ if ($left)
Branch: 1 branch
BranchTrueFalseTotal
if ($left) { } 3 1 4
303 {
304 3 $return = $self->makevalue($branch->[1]);
305 }
306 else
307 {
308 1 $return = $self->makevalue($branch->[2]);
309 }
310 }
311 elsif ($type eq "Store")
312 {
313 59 my $value = $self->makevalue($branch->[1]);
314 59 my $lvalue = $self->makevalue($branch->[0]);
315 59 $return = $value; #make stores evaluate to the value on the right
316 #$self->{vars}->setvar($name, $value);
317 59 $lvalue->{stored}++; #testing
318 59 my $cloned = $value->clone();
319 #warn "SETTING VALUES";
320 #warn Data::Dumper->Dump([$lvalue, $lvalue->getref(), $value, $cloned], [qw($lvalue \$ref $value $cloned)]);
321 59 $cloned->setref(\$cloned);
322 59 debug 6, "---STORE---\n",Data::Dumper->Dump([$lvalue, $value, $cloned, $lvalue->getref()],[qw(lvalue value cloned lvalref)]);
323 59 ${$lvalue->{_ref}} = $cloned;
324 59 debug 6, "---STORE---\n",Data::Dumper->Dump([$lvalue, $value, $cloned, $lvalue->getref()],[qw(lvalue value cloned lvalref)]);
325 # eval {
326 # my $rrval = $self->makevalue($branch->[0]);
327 # debug 2, Data::Dumper->Dump([$rrval, $lvalue, $cloned], [qw(rrval lvalue cloned)]);
328 # }; #keep it from killing things ahead of time
329 # undef $@;
330 }
331 elsif ($type eq "StoreAdd")
332 {
333 0 my $value = $self->makevalue($branch->[1]);
334 0 my $lvalue = $self->makevalue($branch->[0]);
335
336 0 my $cloned = $value->clone();
337 0 $return = (${$lvalue->getref()} = $lvalue + $cloned);
338 }
339 elsif ($type eq "StoreSub")
340 {
341 0 my $value = $self->makevalue($branch->[1]);
342 0 my $lvalue = $self->makevalue($branch->[0]);
343
344 0 my $cloned = $value->clone();
345 0 $return = (${$lvalue->getref()} = $lvalue - $cloned);
346 }
347 elsif ($type eq "StoreDiv")
348 {
349 0 my $value = $self->makevalue($branch->[1]);
350 0 my $lvalue = $self->makevalue($branch->[0]);
351
352 0 my $cloned = $value->clone();
353 0 $return = (${$lvalue->getref()} = $lvalue / $cloned);
354 }
355 elsif ($type eq "StoreMul")
356 {
357 0 my $value = $self->makevalue($branch->[1]);
358 0 my $lvalue = $self->makevalue($branch->[0]);
359
360 0 my $cloned = $value->clone();
361 0 $return = (${$lvalue->getref()} = $lvalue * $cloned);
362 }
363 elsif ($type eq "StoreMod")
364 {
365 0 my $value = $self->makevalue($branch->[1]);
366 0 my $lvalue = $self->makevalue($branch->[0]);
367
368 0 my $cloned = $value->clone();
369 0 $return = (${$lvalue->getref()} = $lvalue % $cloned);
370 }
371 elsif ($type eq "StorePow")
372 {
373 0 my $value = $self->makevalue($branch->[1]);
374 0 my $lvalue = $self->makevalue($branch->[0]);
375
376 0 my $cloned = $value->clone();
377 0 $return = (${$lvalue->getref()} = $lvalue ** $cloned);
378 }
379 elsif ($type eq "PreInc")
380 {
381 0 my $lvalue = $self->makevalue($branch->[0]);
382 0 $return = (${$lvalue->getref()} = $lvalue+VALUE_ONE());
383 }
384 elsif ($type eq "PostInc")
385 {
386 0 my $lvalue = $self->makevalue($branch->[0]);
387 0 my $val = $lvalue->clone();
388 0 ${$lvalue->getref()} = $val+VALUE_ONE();
389  
390 0 $return = $val;
391 }
392 elsif ($type eq "PreDec")
393 {
394 0 my $lvalue = $self->makevalue($branch->[0]);
395 0 $return = (${$lvalue->getref()} = $lvalue-VALUE_ONE());
396 }
397 elsif ($type eq "PostDec")
398 {
399 0 my $lvalue = $self->makevalue($branch->[0]);
400 0 my $val = $lvalue->clone();
401 0 ${$lvalue->getref()} = $val-VALUE_ONE();
402  
403 0 $return = $val;
404 }
405 elsif ($type eq "DeclareVar")
406 {
407 36 my $name = $branch->[0];
408 36 my $value;
409 #print "\n\n DECLARING $name\n";
410 #print Dumper($branch);
411  
412 36 ▶ if (defined($branch->[1]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $branch->[1]) { } 31 5 36
413 {
414 31 $value = $self->makevalue($branch->[1]);
415 }
416 else
417 {
418 5 $value = $self->makevalue(bless [0], 'Num');
419 }
420  
421 36 $return = $value; #make stores evaluate to the value on the right
422 36 $self->{vars}->declare($name, $value);
423 }
424 elsif ($type eq "DeclareFunc")
425 {
426 #print Dumper($branch);
427 2 my $name = $branch->[0];
428 2 my $lambda = $self->makevalue($branch->[1]);
429  
430 #should i allow constants? if i do i'll have to handle them differently, for now it'll be an error
431 2 ▶ error "Right side of function declaration for '$name' did not evaluate to a lambda" unless ($lambda->istype("Lambda"));
Branch: 1 branch
BranchTrueFalseTotal
unless $lambda->istype("Lambda") 0 2 2
432  
433 2 $self->{funcs}->addfunclamb($name, $lambda);
434 2 $return = $lambda;
435 }
436 elsif ($type eq "FuncDef")
437 {
438 #print Dumper($branch);
439 116 my $name = $branch->[0];
440 116 my $args = $branch->[1];
441 116 my $value = $branch->[2]; #not really a value, but in fact the tree to run for the function
442  
443 116 my $nvars = new Language::Farnsworth::Variables($self->{vars}); #lamdbas get their own vars
444 116 my %nopts = (vars => $nvars, funcs => $self->{funcs}, units => $self->{units}, parser => $self->{parser});
445 116 my $scope = $self->new(%nopts);
446  
447 116 my $vargs;
448  
449 116 for my $arg (@$args)
450 {
451 171 my $reference = $arg->[3];
452 171 my $constraint = $arg->[2];
453 171 my $default = $arg->[1];
454 171 my $name = $arg->[0]; #name
455  
456 171 ▶ if (defined($default))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 6 165 171
457 {
458 6 $default = $self->makevalue($default); #should be right
459 }
460  
461 171 ▶ if (defined($constraint))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constraint) 89 82 171
462 {
463 #print Dumper($constraint);
464 89 $constraint = $self->makevalue($constraint); #should be right
465 #print Dumper($constraint);
466 }
467  
468 171 push @$vargs, [$name, $default, $constraint, $reference];
469 }
470  
471 116 $self->{funcs}->addfunc($name, $vargs, $value, $scope);
472 116 $return = undef; #cause an error should someone manage to make it parse other than the way i think it should be
473 }
474 elsif ($type eq "Lambda")
475 {
476 28 my $args = $branch->[0];
477 28 my $code = $branch->[1];
478  
479 #print "==========LAMBDA==========\n";
480 #print Data::Dumper->Dump([$args,$code], ["args", "code"]);
481  
482 28 my $nvars = new Language::Farnsworth::Variables($self->{vars}); #lamdbas get their own vars
483 28 my %nopts = (vars => $nvars, funcs => $self->{funcs}, units => $self->{units}, parser => $self->{parser});
484 28 my $scope = $self->new(%nopts);
485  
486 #this should probably get a function in Language::Farnsworth::FunctionDispatch
487 28 my $vargs;
488  
489 28 for my $arg (@$args)
490 {
491 34 my $reference = $arg->[3];
492 34 my $constraint = $arg->[2];
493 34 my $default = $arg->[1];
494 34 my $name = $arg->[0]; #name
495  
496 # if ($reference)
497 # {
498 # #we've got a reference for lambdas!
499 # error "Passing arguments by reference for lambdas is unsupported at this time";
500 # }
501  
502 34 ▶ if (defined($default))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 3 31 34
503 {
504 3 $default = $self->makevalue($default); #should be right
505 }
506  
507 34 ▶ if (defined($constraint))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constraint) 1 33 34
508 {
509 #print Dumper($constraint);
510 1 $constraint = $self->makevalue($constraint); #should be right
511 #print Dumper($constraint);
512 }
513  
514 34 push @$vargs, [$name, $default, $constraint, $reference];
515 }
516  
517 28 $return = new Language::Farnsworth::Value::Lambda($scope, $vargs, $code, $branch);
518 }
519 elsif ($type eq "LambdaCall") #still used in ONE place, sort[] in Standard.pm, i need to get the code to use mult but that's being a pita and i'm done trying to do it at the moment
520 {
521 47 my $left = $self->makevalue($branch->[0]);
522 47 my $right = $self->makevalue($branch->[1]);
523  
524 47 ▶ error "Right side of lamdbda call must evaluate to a Lambda\n" unless $right->istype("Lambda");
Branch: 1 branch
BranchTrueFalseTotal
unless $right->istype("Lambda") 0 47 47
525  
526 #need $args to be an array
527 47 ▶ my $args = $left->istype("Array") ? $left : new Language::Farnsworth::Value::Array([$left]);
Branch: 1 branch
BranchTrueFalseTotal
$left->istype('Array') ? : 47 0 47
528  
529 47 $return = $self->{funcs}->calllambda($right, $args); #needs to be updated
530 }
531 ▶ elsif (($type eq "Array") || ($type eq "SubArray"))
Condition100%3/3
$type eq "Array" or $type eq "SubArray"
l!l&&r!l&&!r
21819991
MC/DC100%2/2
$type eq "Array" or $type eq "SubArray"
$type eq "Array" $type eq "SubArray"
Truth table100%3/3
A or B
A= $type eq "Array"B= $type eq "SubArray"
ABresult
1X1
011
000
532 {
533 219 my $array = []; #fixes bug with empty arrays
534 219 for my $bs (@$branch) #iterate over all the elements
535 {
536 311 my $type = ref($bs); #find out what kind of thing we are
537 311 my $value = $self->makevalue($bs);
538  
539 #print "ARRAY FILL -- $type\n";
540  
541 # if ($value->istype("Array"))
542 # {
543 #since we have an array, but its not in a SUBarray, we dereference it before the push
544 #push @$array, $value->getarray() unless ($type eq "SubArray");
545 #push @$array, $value;# if ($type eq "SubArray");
546 #}
547 #else
548 {
549 #print "ARRAY VALUE --- ".Dumper($value);
550 #its not an array or anything so we push it on
551 311 push @$array, $value; #we return an array ref! i need more error checking around for this later
552 }
553 }
554 219 $return = new Language::Farnsworth::Value::Array($array);
555 }
556 elsif ($type eq "ArgArray")
557 {
558 0 my $array = []; #autovivification wasn't working?
559 0 for my $bs (@$branch) #iterate over all the elements
560 {
561 0 my $type = ref($bs); #find out what kind of thing we are
562 0 my $value = $self->makevalue($bs);
563  
564 #even if it is an array we don't want to deref it here, because thats the wrong behavior, this will make things like push[a, 1,2,3] work properly
565 0 push @$array, $value; #we return an array ref! i need more error checking around for this later
566 }
567 0 $return = new Language::Farnsworth::Value::Array($array);
568 }
569 elsif ($type eq "ArrayFetch")
570 {
571 #print "\n\nAFETCH\n";
572 32 my $var = $self->makevalue($branch->[0]); #need to check if this is an array, and die if not
573 32 my $listval = $self->makevalue($branch->[1]);
574 32 my @rval;
575  
576 #print Data::Dumper->Dump([$branch, $var, $listval], ["branch","var","listval"]);
577  
578 32 for ($listval->getarray())
579 {
580 32 my $index = $_->getpari()*1.0;
581 #print STDERR "ARFET: ".$_->toperl()."\n";
582 #ok this line FOR WHATEVER REASON, makes Math::Pari.xs die in isnull(), WHY i don't know, there's something wrong here somewhere
583 #my $float = $_ * (Language::Farnsworth::Value::Pari->new(1.0)); #makes rationals work right
584
585 32 my $input = $var->getarrayref()->[$index];
586
587 #error "Array out of bounds\n" #old message, check is down below now;
588 32 ▶ $var->getarrayref()->[$index] = TYPE_UNDEF unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 32 32
589 32 ▶ $input = $var->getarrayref()->[$index] unless defined $input; #reset the value if needed, this code should be redone but i don't feel like it right now XXX
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 32 32
590
591 32 $input->setref(\$var->getarrayref()->[$index]);
592 32 push @rval, $input;
593 }
594  
595 #print Dumper(\@rval);
596  
597 32 ▶ if (@rval > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@rval > 1) { } 0 32 32
598 {
599 0 my $pr = new Language::Farnsworth::Value::Array([@rval]);
600 0 $return = $pr;
601 0 $return->setref(\$return); #i think this should work fine
602 }
603 else
604 {
605 32 $return = $rval[0];
606 }
607 }
608 elsif ($type eq "ArrayStore")
609 {
610 0 my $var = $self->makevalue(bless [$branch->[0]], 'Fetch'); #need to check if this is an array, and die if not
611 0 my $listval = $self->makevalue($branch->[1]);
612 0 my $rval = $self->makevalue($branch->[2]);
613  
614 #print Dumper($branch, $var, $listval);
615  
616 0 ▶ if ($listval->getarray() > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($listval->getarray > 1) 0 0 0
617 {
618 0 error "Assigning to slices not implemented yet\n";
619 }
620
621 0 ▶ error "Only numerics may be given as array indexes!" unless ($listval->getarrayref()->[0]->istype("Pari"));
Branch: 1 branch
BranchTrueFalseTotal
unless $listval->getarrayref->[0]->istype("Pari") 0 0 0
622  
623 0 my $num = $listval->getarrayref()->[0]->getpari() + 0; #the +0 makes sure its coerced into a number
624  
625 0 $var->getarrayref()->[$num] = $rval;
626  
627 0 for my $value ($var->getarray())
628 {
629 0 ▶ $value = $self->makevalue(bless [0], 'Num') if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
630 }
631  
632 0 $return = $rval;
633 }
634 elsif ($type eq "While")
635 {
636 5 my $cond = $branch->[0]; #what to check each time
637 5 my $stmts = $branch->[1]; #what to run each time
638  
639 5 my $condval = $self->makevalue($cond);
640 5 ▶ while ($condval)
Branch: 1 branch
BranchTrueFalseTotal
if ($condval) 23 5 28
641 {
642 23 my $v = $self->makevalue($stmts);
643 23 $condval = $self->makevalue($cond);
644 }
645  
646 5 $return = undef; #cause errors
647 }
648 elsif ($type eq "Stmt")
649 {
650 322 for my $bs (@$branch) #iterate over all the statements
651 {
652 9710 ▶ if (defined($bs))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bs) 9688 22 9710
653 {
654 9688 my $r = $self->makevalue($bs);
655 9678 ▶ $return = $r if defined $r; #this has interesting semantics!
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 9678 0 9678
656 }
657 }
658 }
659 elsif ($type eq "Paren")
660 {
661 347 $return = $self->makevalue($branch->[0]);
662 }
663 elsif ($type eq "SetDisplay")
664 {
665 #TODO make error checking
666 0 print Dumper($branch);
667 0 my $combo = $branch->[0][0]; #is a string?
668 0 my $right = $self->makevalue($branch->[1]);
669  
670 0 Language::Farnsworth::Output->setdisplay($combo, $right);
671 }
672 elsif ($type eq "UnitDef")
673 {
674 8680 my $unitsize = $self->makevalue($branch->[1]);
675 8680 my $name = $branch->[0];
676 8680 $self->{units}->addunit($name, $unitsize);
677 8680 $return = $unitsize;
678 }
679 elsif ($type eq "DefineDimen")
680 {
681 36 my $unit = $branch->[1];
682 36 my $dimen = $branch->[0];
683 36 $self->{units}->adddimen($dimen, $unit);
684 }
685 elsif ($type eq "DefineCombo")
686 {
687 224 my $combo = $branch->[1]; #should get me a string!
688 224 my $value = $self->makevalue($branch->[0]);
689 224 Language::Farnsworth::Output::addcombo($combo, $value);
690 }
691 ▶ elsif (($type eq "SetPrefix") || ($type eq "SetPrefixAbrv"))
Condition100%3/3
$type eq "SetPrefix" or $type eq "SetPrefixAbrv"
l!l&&r!l&&!r
1721685
MC/DC100%2/2
$type eq "SetPrefix" or $type eq "SetPrefixAbrv"
$type eq "SetPrefix" $type eq "SetPrefixAbrv"
Truth table100%3/3
A or B
A= $type eq "SetPrefix"B= $type eq "SetPrefixAbrv"
ABresult
1X1
011
000
692 {
693 340 my $name = $branch->[0];
694 340 my $value = $self->makevalue($branch->[1]);
695 #carp "SETTING PREFIX0: $name : $value : ".Dumper($branch->[1]) if ($name eq "m");
696 340 $self->{units}->setprefix($name, $value);
697 }
698 elsif ($type eq "Trans")
699 {
700 0 my $left = $self->makevalue($branch->[0]);
701 0 my $rights = eval {$self->makevalue($branch->[1])};
702 0 print "TRANS: right side eval\n";
703 #print Dumper($@);
704 0 my $right = $rights;
705  
706 0 ▶ if (!$@ && defined($rights) && $rights->istype("String")) #if its a string we do some fun stuff
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and defined $rights and $rights->istype("String")) 0 0 0
Condition0%0/6
not $@ and defined $rights and $rights->istype("String")
!ll&&!rl&&r
000
not $@ and defined $rights
!ll&&!rl&&r
000
MC/DC0%0/3
not $@ and defined $rights and $rights->istype("String")
not $@ defined $rights $rights->istype("String")
Truth table0%0/4
not(A) and B and C
A= not $@B= defined $rightsC= $rights->istype("String")
ABCresult
0XX0
10X0
1100
1111
707 {
708 0 print "STRINGED\n";
709 0 $right = $self->eval($rights->getstring()); #we need to set $right to the evaluation $rights
710 #print Dumper($rights, $right);
711 0 print "ERRORED: ".Dumper($@);
712 }
713  
714 0 ▶ if (!$@)
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 0 0
715 {
716 0 debug 1,"\n\nLEFT\n";
717 0 debug 1,ref($left);
718 0 debug 1,"RIGHT\n";
719 0 debug 1,ref($right);
720
721 0 ▶ if ($left->conforms($right)) #only do this if they are the same
Branch: 2 branches
BranchTrueFalseTotal
if ($left->conforms($right)) { } 0 0 0
elsif ($right->istype('Lambda')) { } 0 0 0
722 {
723 0 print "Got Conformity\n";
724 0 my $dispval = ($left / $right);
725  
726 #$return = $left;
727 0 %$return = %$left; #ok this makes NO SENSE as to WHY it would behave like it was...
728 0 bless $return, ref($left);
729
730 0 ▶ if ($rights->istype("String"))
Branch: 1 branch
BranchTrueFalseTotal
if ($rights->istype('String')) { } 0 0 0
731 {
732 #right side was a string, use it
733 0 $return->{outmagic} = [$dispval, $rights];
734 }
735 else
736 {
737 0 $return->{outmagic} = [$dispval];
738 }
739 }
740 elsif ($right->istype("Lambda"))
741 {
742 0 print "Got a lambda";
743 0 $return = $right * $left; #simple enough, just use the overloaded operator
744 }
745 # this code isn't being used is it? fuck i need better docs and tests
746 # elsif ($self->{funcs}->isfunc($branch->[1][0]))
747 # {
748 # $left = $left->istype("Array") ? $left : new Language::Farnsworth::Value::Array([$left]);
749 # $return = $self->{funcs}->callfunc($self, $branch->[1][0], $left);
750 #
751 # if ($rights->istype("String"))
752 # {
753 # #right side was a string, use it
754 # my $nm = {%$return}; #do a shallow copy!
755 # bless $nm, ref($return); #rebless it
756 # $return->{outmagic} = [$nm, $rights];
757 # }
758 # }
759 else
760 {
761 0 error "Conformance error, can't convert from ".($left->type($self))." to ".($right->type($self))."\n";
762 }
763 }
764 else
765 {
766 #$right doesn't evaluate... so we check for a function?
767 0 ▶ $left = $left->istype("Array") ? $left : new Language::Farnsworth::Value::Array([$left]);
Branch: 1 branch
BranchTrueFalseTotal
$left->istype('Array') ? : 0 0 0
768 0 $return = $self->{funcs}->callfunc($self, $branch->[1][0], $left);
769  
770 0 ▶ if (defined($rights) && $rights->istype("String"))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rights and $rights->istype("String")) 0 0 0
Condition0%0/3
defined $rights and $rights->istype("String")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rights and $rights->istype("String")
defined $rights $rights->istype("String")
Truth table0%0/3
A and B
A= defined $rightsB= $rights->istype("String")
ABresult
0X0
100
111
771 {
772 #right side was a string, use it
773 0 my $nm = {%$return}; #do a shallow copy!
774 0 bless $nm, ref($return); #rebless it
775 0 $return->{outmagic} = [$nm, $rights];
776 }
777 }
778 }
779  
780 21737 ▶ if (!defined($return))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $return) 726 21011 21737
781 {
782 #this creates a "true" undefined value for returning, this makes things funner! it also introduced a bug from naive coding above, which has been fixed
783 726 $return = new Language::Farnsworth::Value::Undef();
784 }
785
786 21737 return $return;
787 }
788  
789 sub makevalue
790 42475 ▶{
Subroutine makevalue: called CC=12 SCAR=28.5
Pod undocumented
791 42475 my $self = $_[0];
792 42475 my $input = $_[1]; #switching from shift here, so that i can keep @_ intact for recursing
793  
794 # print "MAKEVALUE---------\n";
795 # print Dumper($input);
796  
797 42475 ▶ if (ref($input) eq "Num")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq "Num") 8920 33555 42475
798 {
799 #need to make a value here with Language::Farnsworth::Value!
800 8920 my $val = new Language::Farnsworth::Value::Pari($input->[0]);
801 8920 return $val;
802 }
803 33555 ▶ if (ref($input) eq "HexNum")
Branch: 7 branches
BranchTrueFalseTotal
if (ref $input eq 'HexNum') { } 3 33552 33555
elsif (ref $input eq 'Fetch') { } 11950 21602 33552
elsif (ref $input eq 'GetFunc') { } 0 21602 21602
elsif (ref $input eq 'String') { } 22 21580 21602
elsif (ref $input eq 'Date') { } 3 21577 21580
elsif (ref $input eq 'VarArg') { } 22 21555 21577
elsif (ref($input) =~ /Language::Farnsworth::Value/) { } 141 21414 21555
804 {
805 #need to make a value here with Language::Farnsworth::Value!
806 #print "HEX VALUE: ".$input->[0]."\n";
807 #my $value = eval $input->[0]; #this SHOULD work, shouldn't be a security risk since its validated through the lexer and parser.
808 3 my $val = new Language::Farnsworth::Value::Pari($input->[0],undef,undef,1);
809 3 return $val;
810 }
811 elsif (ref($input) eq "Fetch")
812 {
813 #this needs to decide between variable and unit, but that'll come later
814 #esp since i also have to have this overridable for functions!
815  
816 11950 my $name = $input->[0];
817 11950 ▶ if ($self->{vars}->isvar($name))
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'vars'}->isvar($name)) { } 416 11534 11950
elsif ($self->{'units'}->isunit($name)) { } 11534 0 11534
818 {
819 416 return $self->{vars}->getvar($input->[0]);
820 }
821 elsif ($self->{units}->isunit($name))
822 {
823 #print "FETCH: $name\n" if ($name eq "milli");
824 11534 return $self->{units}->getunit($name);
825 }
826
827 0 error "Undefined symbol '$name'\n";
828 }
829 elsif (ref($input) eq "GetFunc")
830 {
831 0 my $name = $input->[0];
832 0 ▶ if ($self->{funcs}->isfunc($name))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'funcs'}->isfunc($name)) { } 0 0 0
833 {
834 0 return $self->{funcs}->getfunc($name)->{lambda};
835 }
836 else
837 {
838 0 error "Undefined function '$name'";
839 }
840 }
841 elsif (ref($input) eq "String") #we've got a string that should be a value!
842 {
843 22 my $value = $input->[0];
844 #here it comes in with quotes, so lets remove them
845 #$value =~ s/^"(.*)"$/$1/; #no longer needed
846 #$value =~ s/\\"/"/g; #i'm gonna move these into the constructor i think
847 #$value =~ s/\\\\/\\/g;
848 0 $value =~ s/\\(.)/qq("\\$1")/eeg;
849 my $ss = sub
850 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
851 0 my $var =shift;
852 0 $var =~ s/^[\$]//;
853 0 my $output = undef;
854 0 ▶ if ($var !~ /^{.*}$/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $var =~ /^{.*}$/) { } 0 0 0
855 {
856 0 $output = new Language::Farnsworth::Output($self->{units}, $self->{vars}->getvar($var), $self);
857 }
858 else
859 {
860 0 $var =~ s/[{}]//g;
861 0 $output = new Language::Farnsworth::Output($self->{units}, $self->eval($var), $self);
862 }
863  
864 0 "".$output;
865 22 };
866 0 $value =~ s/(?<!\\)(\$\w+|\${[^}]+})/$ss->($1)/eg;
867 22 my $val = new Language::Farnsworth::Value::String($value);
868 22 return $val;
869 }
870 elsif (ref($input) eq "Date")
871 {
872 #print "\n\n\nMaking DATE!\n\n\n";
873 3 my $val = new Language::Farnsworth::Value::Date($input->[0]);
874 # print Dumper($val);
875 3 return $val;
876 }
877 elsif (ref($input) eq "VarArg")
878 {
879 #warn "Got a VarArg, code untested, want to mark when i get them\n"; #just so i can track down the inevitable crash
880 22 return "VarArg";
881 }
882 elsif (ref($input) =~ /Language::Farnsworth::Value/)
883 {
884 141 debug 5, "Got a Language::Farnsworth::Value::*, i PROBABLY shouldn't be getting these, i'm just going to let it fall through";
885 141 return $input;
886 }
887  
888 #return $self->evalbranch($input);
889 21414 goto &evalbranch; #EVIL GOTO! but might save a stack frame! OMG!
890 }
891  
892 4 1;
893 __END__
« blib/lib/Language/Farnsworth/Value/Lambda.pm ↑ Summary blib/lib/Language/Farnsworth/Parser.pm »