blib/lib/Language/Farnsworth/Value/Lambda.pm

filter: Statementstmt 29.4% 96 / 326 Branchbran 34.9% 67 / 192 Conditioncond 29.1% 7 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 72.4% 21 / 29 Podpod 0.0% 0 / 19 totaltotal 31.6% 191 / 603 CC 110 SCAR 72.7
CC
110
Cov
31%
deparsetree
2519.2
div
56.0
add
30.0
CRAP 1432.2
↑ Summary blib/lib/Language/Farnsworth/Evaluate.pm »
1 package Language::Farnsworth::Value::Lambda;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Language::Farnsworth::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Language::Farnsworth::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use base 'Language::Farnsworth::Value';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Language::Farnsworth::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 our $VERSION = 0.6;
16  
17 use overload
18 4 ▶ '+' => \&add,
Subroutine BEGIN: called CC=1 SCAR=0.0
19 '-' => \&subtract,
20 '*' => \&mult,
21 '/' => \&div,
22 '%' => \&mod,
23 '**' => \&pow,
24 '<=>' => \&compare,
25 4 'bool' => \&bool;
26  
27 4 ▶use base qw(Language::Farnsworth::Value);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 #this is the REQUIRED fields for Language::Farnsworth::Value subclasses
30 #
31 #dimen => a Language::Farnsworth::Dimension object
32 #
33 #this is so i can make a -> conforms in Language::Farnsworth::Value, to replace the existing code, i'm also planning on adding some definitions such as, TYPE_PARI, TYPE_STRING, TYPE_LAMBDA, TYPE_DATE, etc. to make certain things easier
34  
35 sub new
36 380 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
37 380 my $class = shift;
38 380 my $scope = shift;
39 380 my $args = shift;
40 380 my $code = shift;
41 380 my $branches = shift;
42 380 my $name = shift;
43
44 380 my $outmagic = shift; #i'm still not sure on this one
45  
46 380 debug 5, "Need error checking in lambda creation!";
47  
48 380 my $self = {};
49  
50 380 bless $self, $class;
51  
52 380 $self->{outmagic} = $outmagic;
53  
54 380 $self->{scope} = $scope;
55 380 $self->{code} = $code;
56 380 $self->{args} = $args;
57 380 $self->{branches} = $branches;
58 380 $self->{name} = $name;
59
60 380 return $self;
61 }
62  
63 sub getcode
64 166 ▶{
Subroutine getcode: called CC=1 SCAR=0.0
Pod undocumented
65 166 return $_[0]->{code};
66 }
67  
68 sub getargs
69 166 ▶{
Subroutine getargs: called CC=1 SCAR=0.0
Pod undocumented
70 166 return $_[0]->{args};
71 }
72  
73 sub getscope
74 164 ▶{
Subroutine getscope: called CC=1 SCAR=0.0
Pod undocumented
75 164 return $_[0]->{scope};
76 }
77  
78 sub getbranches
79 1 ▶{
Subroutine getbranches: called CC=1 SCAR=0.0
Pod undocumented
80 1 return $_[0]->{branches};
81 }
82  
83 sub getname
84 166 ▶{
Subroutine getname: called CC=2 SCAR=6.9
Pod undocumented
85 166 ▶ return defined($_[0]->{name}) ? $_[0]->{name} : "lambda";
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'name'} ? : 96 70 166
86 }
87  
88 sub setname
89 2 ▶{
Subroutine setname: called CC=1 SCAR=0.0
Pod undocumented
90 2 return ($_[0]->{name} = $_[1]);
91 }
92  
93 sub type
94 0 ▶{
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
95 0 return "Lambda";
96 }
97  
98 #######
99 #The rest of this code can be GREATLY cleaned up by assuming that $one is of type, Language::Farnsworth::Value::Pari, this means that i can slowly redo a lot of this code
100  
101 sub add
102 0 ▶{
Subroutine add: not called CC=5 SCAR=34.0
Pod undocumented
103 0 my ($one, $two, $rev) = @_;
104  
105 0 ▶ error "Non reference given to addition of lambdas" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
106  
107 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
108 0 ▶ error "Scalar value given to addition to Lambda" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
109 0 ▶ return $two->add($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
110 0 ▶ if (!$two->istype("Lambda"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Lambda")) 0 0 0
111 {
112 0 error "Given non lambda to lambda operation";
113 }
114  
115 #NOTE TO SELF this needs to be more helpful, i'll probably do this by creating an "error" class that'll be captured in ->evalbranch's recursion and use that to add information from the parse tree about WHERE the error occured
116 0 error "Adding lambda is not a good idea\n";
117 }
118  
119 sub subtract
120 0 ▶{
Subroutine subtract: not called CC=5 SCAR=34.0
Pod undocumented
121 0 my ($one, $two, $rev) = @_;
122  
123 0 ▶ error "Non reference given to subtraction of lambda" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
124  
125 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
126 0 ▶ error "Scalar value given to subtraction of Lambda" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
127 0 ▶ return $two->subtract($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
128 0 ▶ if (!$two->istype("Lambda"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Lambda")) 0 0 0
129 {
130 0 error "Given non lambda to lambda operation";
131 }
132  
133 0 error "Subtracting lambdas? what did you think this would do, create a black hole?";
134 }
135  
136 sub modulus
137 0 ▶{
Subroutine modulus: not called CC=5 SCAR=34.0
Pod undocumented
138 0 my ($one, $two, $rev) = @_;
139  
140 0 ▶ error "Non reference given to modulus of lambda" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
141  
142 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
143 0 ▶ error "Scalar value given to modulus to lambda" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
144 0 ▶ return $two->mod($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
145 0 ▶ if (!$two->istype("Lambda"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Lambda")) 0 0 0
146 {
147 0 error "Given non lambda to lambda operation";
148 }
149  
150 0 error "Modulusing lambda? what did you think this would do, create a black hole?";
151 }
152  
153 sub mult
154 117 ▶{
Subroutine mult: called CC=6 SCAR=19.3
Pod undocumented
155 117 my ($one, $two, $rev) = @_;
156  
157 117 ▶ error "Non reference given to multiplication of lambdas" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 117 117
158 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
159 #confess "Scalar value given to multiplcation to lambda. ED: This will make white holes later" if ($two->isa("Language::Farnsworth::Value::Pari"));
160 117 ▶ return $two->mult($one, !$rev) unless ($two->ismediumtype() || $two->istype("Pari") || $two->istype("Lambda"));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda") 0 117 117
Condition50%3/6
$two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda")
l!l&&r!l&&!r
11700
$two->ismediumtype or $two->istype("Pari")
l!l&&r!l&&!r
11340
MC/DC0%0/3
$two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda")
$two->ismediumtype $two->istype("Pari") $two->istype("Lambda")
Truth table50%2/4
A or B or C
A= $two->ismediumtypeB= $two->istype("Pari")C= $two->istype("Lambda")
ABCresult
1XX1
01X1
0011
0000
161
162 # if (!$two->istype("Lambda"))
163 # {
164 # confess "Given non lambda to lambda operation";
165 # }
166  
167 117 ▶ my $args = $two->istype("Array") ? $two : new Language::Farnsworth::Value::Array([$two]);
Branch: 1 branch
BranchTrueFalseTotal
$two->istype('Array') ? : 113 4 117
168  
169 #this code is debug code, but i'm afraid to take it out, when i put it here it started working properly
170 #print "LAMBDAMULT\n";
171 #eval{print Dumper($one->{scope}->{vars}->getvar('x'), $one->{scope}->{vars}->getvar('y'))}; #this bug was fixed
172  
173 117 return $one->{scope}->{funcs}->calllambda($one, $args); #needs to be updated
174  
175 # die "Multiplying lambdas? what did you think this would do, create a black hole? ED: this will make black holes later";
176 }
177  
178 sub div
179 0 ▶{
Subroutine div: not called CC=7 SCAR=40.3
Pod undocumented
180 0 my ($one, $two, $rev) = @_;
181  
182 #############################################
183 #############################################
184 ## WARNING! WARNING! WARNING! ##
185 #############################################
186 #############################################
187 #
188 # This entire section is experimental, and not checked for errors in testing, and is liable to cause the sun to explode
189
190 0 print "INSIDE LAMBDA DIVISION\n";
191  
192 0 ▶ error "Non reference given to division of lambdas" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
193  
194 #if there's a higher type, use it
195
196 0 ▶ return $two->div($one, !$rev) unless ($two->ismediumtype()|| $two->istype("Pari") || $two->istype("Lambda"));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda") 0 0 0
Condition0%0/6
$two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda")
l!l&&r!l&&!r
000
$two->ismediumtype or $two->istype("Pari")
l!l&&r!l&&!r
000
MC/DC0%0/3
$two->ismediumtype or $two->istype("Pari") or $two->istype("Lambda")
$two->ismediumtype $two->istype("Pari") $two->istype("Lambda")
Truth table0%0/4
A or B or C
A= $two->ismediumtypeB= $two->istype("Pari")C= $two->istype("Lambda")
ABCresult
1XX1
01X1
0011
0000
197  
198 0 ▶ if ($two->isa("Language::Farnsworth::Value::Pari"))
Branch: 1 branch
BranchTrueFalseTotal
if ($two->isa("Language::Farnsworth::Value::Pari")) 0 0 0
199 {
200 #ok i've got a simple thing here i think!
201 #its not simple, will not be simple and will not end up working right, this is a hack to make 10 kg per cubic meter, and the like to work, until i add objects
202 0 my $onevalue = Language::Farnsworth::Value::Pari->new(1); #don't use 1.0 it'll coerce things into floats unneccesarily
203 0 my $newv = $onevalue * $one; #this'll remultiply things!
204 0 my $ret = $two / $newv; #this COULD be dangerous since i can see how to make an inf loop in a division this way
205 0 return $ret;
206 }
207  
208 0 ▶ if (!$two->istype("Lambda"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Lambda")) 0 0 0
209 {
210 0 error "Given non lambda to lambda operation";
211 }
212  
213 0 error "Dividing lambdas? what did you think this would do, create a black hole?";
214 }
215  
216 sub bool
217 0 ▶{
Subroutine bool: not called CC=1 SCAR=6.9
Pod undocumented
218 # my $self = shift;
219  
220 #seems good enough of an idea to me
221 #i have a bug HERE
222 # print "BOOLCONV\n";
223 # print Dumper($self);
224 # print "ENDBOOLCONV\n";
225 0 return 1; #for now lambdas are ALWAYS true!
226
227 # print Dumper [caller()];
228 # die "FUCK\n";
229 }
230  
231 sub pow
232 0 ▶{
Subroutine pow: not called CC=5 SCAR=34.0
Pod undocumented
233 0 my ($one, $two, $rev) = @_;
234  
235 0 ▶ error "Non reference given to exponentiation of lambda" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
236  
237 #if there's a higher type, use it, subtraction otherwise doesn't make sense on arrays
238 0 ▶ error "Scalar given to exponentiation of lambda" if ($two->isa("Language::Farnsworth::Value::Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->isa("Language::Farnsworth::Value::Pari") 0 0 0
239 0 ▶ return $two->pow($one, !$rev) unless ($two->ismediumtype());
Branch: 1 branch
BranchTrueFalseTotal
unless $two->ismediumtype 0 0 0
240 0 ▶ if (!$two->istype("Lambda"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($two->istype("Lambda")) 0 0 0
241 {
242 0 error "Given non boolean to lambdas operation";
243 }
244  
245 0 error "Exponentiating lambdas? what did you think this would do, create a black hole?";
246 }
247  
248 sub compare
249 0 ▶{
Subroutine compare: not called CC=4 SCAR=30.0
Pod undocumented
250 0 my ($one, $two, $rev) = @_;
251  
252 0 ▶ error "Non reference given to compare of lambda" unless ref($two);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $two 0 0 0
253  
254 #if we're not being added to a Language::Farnsworth::Value::Pari, the higher class object needs to handle it.
255 0 ▶ error "Scalar value given to division to lambdas" if ($two->istype("Pari"));
Branch: 1 branch
BranchTrueFalseTotal
if $two->istype("Pari") 0 0 0
256 0 ▶ return $two->compare($one, !$rev) unless ($two->istype("Lambda"));
Branch: 1 branch
BranchTrueFalseTotal
unless $two->istype("Lambda") 0 0 0
257  
258 0 return 0; #i don't have any metric for comparing lambdas, so... they'll always be equal
259 }
260  
261 sub eval
262 2 ▶{
Subroutine eval: called CC=2 SCAR=7.1
Pod undocumented
263 2 my ($self, $two, $eval) = @_;
264  
265 2 ▶ my $args = $two->istype("Array") ? $two : new Language::Farnsworth::Value::Array([$two]);
Branch: 1 branch
BranchTrueFalseTotal
$two->istype('Array') ? : 2 0 2
266  
267 #this code is debug code, but i'm afraid to take it out, when i put it here it started working properly
268 #print "LAMBDAMULT\n";
269 #eval{print Dumper($one->{scope}->{vars}->getvar('x'), $one->{scope}->{vars}->getvar('y'))}; #this bug was fixed
270  
271 2 return $eval->{funcs}->calllambda($self, $args, $eval); #needs to be updated
272 }
273  
274 sub tostring
275 1 ▶{
Subroutine tostring: called CC=1 SCAR=0.0
Pod undocumented
276 1 my $self = shift;
277 1 return $self->deparsetree($self->getbranches())
278 }
279  
280 sub deparsetree
281 5 ▶{
Subroutine deparsetree: called CC=78 SCAR=78.3
Pod undocumented
282 5 my $self = shift;
283 5 my $branch = shift;
284  
285 5 my $type = ref($branch);
286 5 my $return;
287
288 5 ▶ if ($type eq "CODE")
Branch: 59 branches
BranchTrueFalseTotal
if ($type eq 'CODE') { } 0 5 5
elsif ($type eq 'PreDec') { } 0 5 5
elsif ($type eq 'PreInc') { } 0 5 5
elsif ($type eq 'PostDec') { } 0 5 5
elsif ($type eq 'PostInc') { } 0 5 5
elsif ($type eq 'Add') { } 1 4 5
elsif ($type eq 'Sub') { } 0 4 4
elsif ($type eq 'Mul') { } 0 4 4
elsif ($type eq 'Div') { } 0 4 4
elsif ($type eq 'Conforms') { } 0 4 4
elsif ($type eq 'Mod') { } 0 4 4
elsif ($type eq 'Pow') { } 0 4 4
elsif ($type eq 'And') { } 0 4 4
elsif ($type eq 'Or') { } 0 4 4
elsif ($type eq 'Xor') { } 0 4 4
elsif ($type eq 'Not') { } 0 4 4
elsif ($type eq 'Gt') { } 0 4 4
elsif ($type eq 'Lt') { } 0 4 4
elsif ($type eq 'Ge') { } 0 4 4
elsif ($type eq 'Le') { } 0 4 4
elsif ($type eq 'Compare') { } 0 4 4
elsif ($type eq 'Eq') { } 0 4 4
elsif ($type eq 'Ne') { } 0 4 4
elsif ($type eq 'Ternary') { } 0 4 4
elsif ($type eq 'If') { } 0 4 4
elsif ($type eq 'Store') { } 0 4 4
elsif ($type eq 'StoreAdd') { } 0 4 4
elsif ($type eq 'StoreSub') { } 0 4 4
elsif ($type eq 'StoreMul') { } 0 4 4
elsif ($type eq 'StoreDiv') { } 0 4 4
elsif ($type eq 'StoreMod') { } 0 4 4
elsif ($type eq 'StorePow') { } 0 4 4
elsif ($type eq 'DeclareVar') { } 0 4 4
elsif ($type eq 'FuncDef') { } 0 4 4
elsif ($type eq 'FuncCall') { } 0 4 4
elsif ($type eq 'DeclareFunc') { } 0 4 4
elsif ($type eq 'GetFunc') { } 0 4 4
elsif ($type eq 'Lambda') { } 1 3 4
elsif ($type eq 'LambdaCall') { } 0 3 3
elsif ($type eq 'Array' or $type eq 'SubArray') { } 0 3 3
elsif ($type eq 'ArgArray') { } 0 3 3
elsif ($type eq 'ArrayFetch') { } 0 3 3
elsif ($type eq 'ArrayStore') { } 0 3 3
elsif ($type eq 'While') { } 0 3 3
elsif ($type eq 'Stmt') { } 1 2 3
elsif ($type eq 'Paren') { } 0 2 2
elsif ($type eq 'SetDisplay') { } 0 2 2
elsif ($type eq 'UnitDef') { } 0 2 2
elsif ($type eq 'DefineDimen') { } 0 2 2
elsif ($type eq 'DefineCombo') { } 0 2 2
elsif ($type eq 'SetPrefix' or $type eq 'SetPrefixAbrv') { } 0 2 2
elsif ($type eq 'Trans') { } 0 2 2
elsif ($type eq 'Num' or $type eq 'Fetch' or $type eq 'HexNum') { } 2 0 2
elsif ($type eq 'String') { } 0 0 0
elsif ($type eq 'Date') { } 0 0 0
elsif ($type eq 'VarArg') { } 0 0 0
elsif (not defined $branch) { } 0 0 0
elsif ($type =~ /^Language::Farnsworth::Value/) { } 0 0 0
elsif ($branch eq 'VarArg') { } 0 0 0
289 {
290 #got perl code here!
291 0 return "/* PERL CODE */";
292 }
293 elsif ($type eq "PreDec")
294 {
295 0 my $a = $self->deparsetree($branch->[0]);
296 0 return "--$a"
297 }
298 elsif ($type eq "PreInc")
299 {
300 0 my $a = $self->deparsetree($branch->[0]);
301 0 return "++$a"
302 }
303 elsif ($type eq "PostDec")
304 {
305 0 my $a = $self->deparsetree($branch->[0]);
306 0 return "$a--"
307 }
308 elsif ($type eq "PostInc")
309 {
310 0 my $a = $self->deparsetree($branch->[0]);
311 0 return "$a++"
312 }
313 elsif ($type eq "Add")
314 {
315 1 my $a = $self->deparsetree($branch->[0]);
316 1 my $b = $self->deparsetree($branch->[1]);
317 1 return $a . " + " . $b;
318 }
319 elsif ($type eq "Sub")
320 {
321 0 my $a = $self->deparsetree($branch->[0]);
322 0 my $b = $self->deparsetree($branch->[1]);
323 0 return $a . " - " . $b;
324 }
325 elsif ($type eq "Mul")
326 {
327 0 my $a = $self->deparsetree($branch->[0]);
328 0 my $b = $self->deparsetree($branch->[1]);
329 0 my $t = $branch->[2];
330  
331 0 ▶ return $a . ($t eq 'imp' ? '' : ' * ') . $b; #NOTE: this should listen to the 'imp' or '*' in the tree!
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'imp' ? : 0 0 0
332 }
333 elsif ($type eq "Div")
334 {
335 0 my $a = $self->deparsetree($branch->[0]);
336 0 my $b = $self->deparsetree($branch->[1]);
337 0 my $t = $branch->[2];
338  
339 0 $return = $a . " $t " . $b;
340 }
341 elsif ($type eq "Conforms")
342 {
343 0 my $a = $self->deparsetree($branch->[0]);
344 0 my $b = $self->deparsetree($branch->[1]);
345 0 return "$a conforms $b";
346 }
347 elsif ($type eq "Mod")
348 {
349 0 my $a = $self->deparsetree($branch->[0]);
350 0 my $b = $self->deparsetree($branch->[1]);
351 0 return "$a % $b";
352 }
353 elsif ($type eq "Pow")
354 {
355 0 my $a = $self->deparsetree($branch->[0]);
356 0 my $b = $self->deparsetree($branch->[1]);
357 0 return "$a ^ $b";
358 }
359 elsif ($type eq "And")
360 {
361 0 my $a = $self->deparsetree($branch->[0]);
362 0 my $b = $self->deparsetree($branch->[1]);
363  
364 0 return "$a && $b";
365 }
366 elsif ($type eq "Or")
367 {
368 0 my $a = $self->deparsetree($branch->[0]);
369 0 my $b = $self->deparsetree($branch->[1]);
370
371 0 return "$a || $b";
372 }
373 elsif ($type eq "Xor")
374 {
375 0 my $a = $self->deparsetree($branch->[0]);
376 0 my $b = $self->deparsetree($branch->[1]);
377 0 return "$a ^^ $b";
378 }
379 elsif ($type eq "Not")
380 {
381 0 my $a = $self->deparsetree($branch->[0]);
382 0 return "!$a";
383 }
384 elsif ($type eq "Gt")
385 {
386 0 my $a = $self->deparsetree($branch->[0]);
387 0 my $b = $self->deparsetree($branch->[1]);
388 0 return "$a > $b";
389 }
390 elsif ($type eq "Lt")
391 {
392 0 my $a = $self->deparsetree($branch->[0]);
393 0 my $b = $self->deparsetree($branch->[1]);
394 0 return "$a < $b";
395 }
396 elsif ($type eq "Ge")
397 {
398 0 my $a = $self->deparsetree($branch->[0]);
399 0 my $b = $self->deparsetree($branch->[1]);
400 0 return "$a >= $b";
401 }
402 elsif ($type eq "Le")
403 {
404 0 my $a = $self->deparsetree($branch->[0]);
405 0 my $b = $self->deparsetree($branch->[1]);
406 0 return "$a <= $b";
407 }
408 elsif ($type eq "Compare")
409 {
410 0 my $a = $self->deparsetree($branch->[0]);
411 0 my $b = $self->deparsetree($branch->[1]);
412 0 return "$a <=> $b";
413 }
414 elsif ($type eq "Eq")
415 {
416 0 my $a = $self->deparsetree($branch->[0]);
417 0 my $b = $self->deparsetree($branch->[1]);
418 0 return "$a == $b";
419 }
420 elsif ($type eq "Ne")
421 {
422 0 my $a = $self->deparsetree($branch->[0]);
423 0 my $b = $self->deparsetree($branch->[1]);
424 0 return "$a != $b";
425 }
426 elsif ($type eq "Ternary")
427 {
428 0 my $left = $self->deparsetree($branch->[0]);
429 0 my $one = $self->deparsetree($branch->[1]);
430 0 my $two = $self->deparsetree($branch->[2]);
431  
432 0 return "$left ? $one : $two";
433 }
434 elsif ($type eq "If")
435 {
436 0 my $return = "";
437 0 my $left = $self->deparsetree($branch->[0]);
438 0 my $std = $self->deparsetree($branch->[1]);
439
440 0 $return = "if ($left) { $std }";
441  
442 0 ▶ if ($branch->[2])
Branch: 1 branch
BranchTrueFalseTotal
if ($branch->[2]) 0 0 0
443 {
444 0 my $else = $self->deparsetree($branch->[2]);
445 0 $return .= " else { $else }";
446 }
447
448 #$return .= ";"; #NOTE: DO I NEED THIS? probably not!
449  
450 0 return $return;
451 }
452 elsif ($type eq "Store")
453 {
454 0 my $name = $self->deparsetree($branch->[0]);
455 0 my $value = $self->deparsetree($branch->[1]);
456  
457 0 return "$name = $value";
458 }
459 elsif ($type eq "StoreAdd")
460 {
461 0 my $name = $self->deparsetree($branch->[0]);
462 0 my $value = $self->deparsetree($branch->[1]);
463  
464 0 return "$name += $value";
465 }
466 elsif ($type eq "StoreSub")
467 {
468 0 my $name = $self->deparsetree($branch->[0]);
469 0 my $value = $self->deparsetree($branch->[1]);
470  
471 0 return "$name -= $value";
472 }
473 elsif ($type eq "StoreMul")
474 {
475 0 my $name = $self->deparsetree($branch->[0]);
476 0 my $value = $self->deparsetree($branch->[1]);
477  
478 0 return "$name *= $value";
479 }
480 elsif ($type eq "StoreDiv")
481 {
482 0 my $name = $self->deparsetree($branch->[0]);
483 0 my $value = $self->deparsetree($branch->[1]);
484  
485 0 return "$name /= $value";
486 }
487 elsif ($type eq "StoreMod")
488 {
489 0 my $name = $self->deparsetree($branch->[0]);
490 0 my $value = $self->deparsetree($branch->[1]);
491  
492 0 return "$name %= $value";
493 }
494 elsif ($type eq "StorePow")
495 {
496 0 my $name = $self->deparsetree($branch->[0]);
497 0 my $value = $self->deparsetree($branch->[1]);
498  
499 0 return "$name ^= $value";
500 }
501 elsif ($type eq "DeclareVar")
502 {
503 0 my $name = $branch->[0];
504  
505 0 my $return = "var $name";
506  
507 0 ▶ if (defined($branch->[1]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $branch->[1]) 0 0 0
508 {
509 0 my $val = $self->deparsetree($branch->[1]);
510 0 $return .= " = $val";
511 }
512  
513 0 return $return;
514 }
515 elsif ($type eq "FuncDef")
516 {
517 #print Dumper($branch);
518 0 my $name = $branch->[0];
519 0 my $args = $branch->[1];
520 0 my $value = $self->deparsetree($branch->[2]); #not really a value, but in fact the tree to run for the function
521  
522 0 my $return = "${name}{";
523  
524 0 my @vargs;
525 0 my $vargs = "";
526  
527 0 for my $arg (@$args)
528 {
529 0 my $foobs="";
530 0 my $constraint = $arg->[2];
531 0 my $default = $arg->[1];
532 0 my $name = $arg->[0]; #name
533  
534 0 $foobs = $name;
535 0 ▶ if (defined($default))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 0 0 0
536 {
537 0 $foobs .= " = ".$self->deparsetree($default); #should be right
538 }
539  
540 0 ▶ if (defined($constraint))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constraint) 0 0 0
541 {
542 #print Dumper($constraint);
543 0 $foobs .= " isa ".$self->deparsetree($constraint); #should be right
544 #print Dumper($constraint);
545 }
546  
547 0 push @vargs, $foobs;
548 }
549  
550 0 $vargs = join " , ", @vargs;
551  
552 0 $return .= "$vargs} := { $value }";
553 }
554 elsif ($type eq "FuncCall")
555 {
556 0 my $name = $branch->[0];
557 0 my $args = $self->deparsetree($branch->[1]); #this is an array, need to evaluate it
558  
559 0 return "$name\[$args\]";
560 }
561 # | 'defun' NAME '=' expr { bless [ @_[2,4] ], 'DeclareFunc' }
562 elsif ($type eq "DeclareFunc")
563 {
564 0 my $name = $branch->[0];
565 0 my $value = $self->deparsetree($branch->[1]);
566
567 0 return "defun $name = $value";
568 }
569 elsif ($type eq "GetFunc")
570 {
571 0 my $name = $branch->[0];
572 0 return "&$name";
573 }
574 elsif ($type eq "Lambda")
575 {
576 1 my $args = $branch->[0];
577 1 my $code = $self->deparsetree($branch->[1]);
578  
579 1 my @vargs;
580 1 my $vargs = "";
581  
582 1 for my $arg (@$args)
583 {
584 1 my $foobs="";
585 1 my $reference = $arg->[3];
586 1 my $constraint = $arg->[2];
587 1 my $default = $arg->[1];
588 1 my $name = $arg->[0]; #name
589  
590 1 $foobs = $name;
591 1 ▶ if ($reference)
Branch: 1 branch
BranchTrueFalseTotal
if ($reference) 0 1 1
592 {
593 0 $foobs .= " byref "; #should be right
594 }
595  
596 1 ▶ if (defined($default))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 0 1 1
597 {
598 0 $foobs .= " = ".$self->deparsetree($default); #should be right
599 }
600  
601 1 ▶ if (defined($constraint))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $constraint) 0 1 1
602 {
603 #print Dumper($constraint);
604 0 $foobs .= " isa ".$self->deparsetree($constraint); #should be right
605 #print Dumper($constraint);
606 }
607  
608 1 push @vargs, $foobs;
609 }
610 1 $vargs = join " , ", @vargs;
611  
612 1 return "{`$vargs` $code}";
613 }
614 elsif ($type eq "LambdaCall")
615 {
616 0 my $left = $self->deparsetree($branch->[0]);
617 0 my $right = $self->deparsetree($branch->[1]);
618  
619 0 return "$left => $right";
620 }
621 ▶ elsif (($type eq "Array") || ($type eq "SubArray"))
Condition33%1/3
$type eq "Array" or $type eq "SubArray"
l!l&&r!l&&!r
003
MC/DC0%0/2
$type eq "Array" or $type eq "SubArray"
$type eq "Array" $type eq "SubArray"
Truth table33%1/3
A or B
A= $type eq "Array"B= $type eq "SubArray"
ABresult
1X1
011
000
622 {
623 0 my $array = []; #fixes bug with empty arrays
624 0 for my $bs (@$branch) #iterate over all the elements
625 {
626 0 my $type = ref($bs); #find out what kind of thing we are
627 0 my $value = $self->deparsetree($bs);
628  
629 #since we have an array, but its not in a SUBarray, we dereference it before the push
630 0 push @$array, $value;
631 #push @$array, '['.$value.']' if ($type eq "SubArray");
632 }
633 0 return '[ '.(join ', ',@$array).' ]';
634
635 }
636 elsif ($type eq "ArgArray")
637 {
638 0 my $array = [];
639 0 for my $bs (@$branch) #iterate over all the elements
640 {
641 0 my $value = $self->deparsetree($bs);
642  
643 0 push @$array, $value; #we return an array ref! i need more error checking around for this later
644 }
645 0 return join ', ', @$array;
646 }
647 elsif ($type eq "ArrayFetch")
648 {
649 0 my $var = $self->deparsetree($branch->[0]); #need to check if this is an array, and die if not
650 0 my $listval = $self->deparsetree($branch->[1]);
651
652 0 $listval = substr $listval, 1,length($listval)-2; #strip the []
653  
654 0 return "$var\@$listval\$";
655 }
656 elsif ($type eq "ArrayStore")
657 {
658 0 my $var = $self->deparsetree(bless [$branch->[0]], 'Fetch'); #need to check if this is an array, and die if not
659 0 my $listval = $self->deparsetree($branch->[1]);
660 0 my $rval = $self->deparsetree($branch->[2]);
661  
662 0 $listval = substr $listval, 1,length($listval)-2; #strip the []
663  
664 0 return "$var\@$listval\$ = $rval";
665 }
666 elsif ($type eq "While")
667 {
668 0 my $cond = $self->deparsetree($branch->[0]); #what to check each time
669 0 my $stmts = $self->deparsetree($branch->[1]); #what to run each time
670  
671 0 return "while ($cond) { $stmts }"
672 }
673 elsif ($type eq "Stmt")
674 {
675 1 my $return = "";
676 1 for my $bs (@$branch) #iterate over all the statements
677 1 { my $r = $self->deparsetree($bs);
678 1 ▶ $return .= "$r; " if defined $r; #this has interesting semantics!
Branch: 1 branch
BranchTrueFalseTotal
if defined $r 1 0 1
679 }
680 1 return $return;
681 }
682 elsif ($type eq "Paren")
683 {
684 0 return '(' . $self->deparsetree($branch->[0]) . ')';
685 }
686 elsif ($type eq "SetDisplay")
687 {
688 0 my $combo = $branch->[0][0]; #is a string?
689 0 my $right = $self->deparsetree($branch->[1]);
690  
691 0 return "$combo :-> $right";
692 }
693 elsif ($type eq "UnitDef")
694 {
695 0 my $unitsize = $self->deparsetree($branch->[1]);
696 0 my $name = $branch->[0];
697
698 0 return "$name := $unitsize";
699 }
700 elsif ($type eq "DefineDimen")
701 {
702 0 my $unit = $branch->[1];
703 0 my $dimen = $branch->[0];
704
705 0 return "$dimen =!= $unit";
706 }
707 elsif ($type eq "DefineCombo")
708 {
709 0 my $combo = $branch->[1]; #should get me a string!
710 0 my $value = $self->deparsetree($branch->[0]);
711
712 0 return "$value ||| $combo";
713 }
714 ▶ elsif (($type eq "SetPrefix") || ($type eq "SetPrefixAbrv"))
Condition33%1/3
$type eq "SetPrefix" or $type eq "SetPrefixAbrv"
l!l&&r!l&&!r
002
MC/DC0%0/2
$type eq "SetPrefix" or $type eq "SetPrefixAbrv"
$type eq "SetPrefix" $type eq "SetPrefixAbrv"
Truth table33%1/3
A or B
A= $type eq "SetPrefix"B= $type eq "SetPrefixAbrv"
ABresult
1X1
011
000
715 {
716 0 my $name = $branch->[0];
717 0 my $value = $self->deparsetree($branch->[1]);
718  
719 0 return "$name ::- $value";
720 }
721 elsif ($type eq "Trans")
722 {
723 0 my $left = $self->deparsetree($branch->[0]);
724 0 my $right = $self->deparsetree($branch->[1]);
725  
726 0 return "$left -> $right";
727 }
728 ▶ elsif (($type eq "Num") || ($type eq "Fetch") || ($type eq "HexNum"))
Condition33%2/6
$type eq "Num" or $type eq "Fetch" or $type eq "HexNum"
l!l&&r!l&&!r
200
$type eq "Num" or $type eq "Fetch"
l!l&&r!l&&!r
020
MC/DC0%0/3
$type eq "Num" or $type eq "Fetch" or $type eq "HexNum"
$type eq "Num" $type eq "Fetch" $type eq "HexNum"
Truth table25%1/4
A or B or C
A= $type eq "Num"B= $type eq "Fetch"C= $type eq "HexNum"
ABCresult
1XX1
01X1
0011
0000
729 {
730 2 return $branch->[0]; #its already a string!
731 }
732 elsif ($type eq "String")
733 {
734 0 return '"'.$branch->[0].'"';
735 }
736 elsif ($type eq "Date")
737 {
738 0 return "#".$branch->[0]."#";
739 }
740 elsif ($type eq "VarArg")
741 {
742 0 return "...";
743 }
744 elsif (!defined($branch))
745 {
746 0 return ""; #got an undefined value, just make it blank
747 }
748 elsif ($type =~ /^Language::Farnsworth::Value/) # use the output class for all real values
749 {
750 0 my $output = Language::Farnsworth::Output->new($self->getscope()->{units}, $branch, $self->getscope());
751 0 return $output."";
752 }
753 elsif ($branch eq "VarArg")
754 {
755 0 return "...";
756 }
757 else
758 {
759 # cluck "Unhandled input!";
760 0 return '/*'.Dumper($branch).'*/';
761 }
762 }
763  
764 4 1;
↑ Summary blib/lib/Language/Farnsworth/Evaluate.pm »