lib/Language/Basic/Expression.pm

filter: Statementstmt 75.3% 375 / 498 Branchbran 65.1% 116 / 178 Conditioncond 76.6% 23 / 30 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 74.0% 37 / 50 Podpod 0.0% 0 / 1 totaltotal 71.6% 557 / 777 CC 102 SCAR 19.9
CC
102
Cov
73%
output_perl
20.0
new
16.0
output_perl
12.0
CRAP 7.3
« lib/Language/Basic/Statement.pm ↑ Summary lib/Language/Basic/Function.pm »
1 package Language::Basic::Expression;
2 # Part of Language::Basic by Amir Karger (See Basic.pm for details)
3  
4 =pod
5  
6 =head1 NAME
7  
8 Language::Basic::Expression - Package to handle string, numeric, and
9 boolean expressions.
10  
11 =head1 SYNOPSIS
12  
13 See L<Language::Basic> for the overview of how the Language::Basic module
14 works. This pod page is more technical.
15  
16 # Given an LB::Token::Group, create an expression I<and> parse it
17 my $exp = new LB::Expression::Arithmetic $token_group;
18 # What's the value of the expression?
19 print $exp->evaluate;
20 # Perl equivalent of the BASIC expression
21 print $exp->output_perl;
22  
23 Expressions are basically the building blocks of Statements, in that every
24 BASIC statement is made up of keywords (like GOTO, TO, STEP) and expressions.
25 So expressions include not just the standard arithmetic and boolean expressions
26 (like 1 + 2), but also lvalues (scalar variables or arrays), functions, and
27 constants. See L<Language::Basic::Syntax> for details on the way expressions
28 are built.
29  
30 =head1 DESCRIPTION
31  
32 BASIC expressions are represented by various objects of subclasses of
33 Language::Basic::Expression. Most LB::Expressions are in turn made up of other
34 LB::Expressions. For example an LBE::Arithmetic may be made up of two
35 LBE::Multiplicative and a "plus". "Atoms" (indivisible LBE's) include
36 things like LBE::Constants and LBE::Lvalues (variables).
37  
38 =cut
39  
40 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # sub-packages
44 {
45 16 package Language::Basic::Expression::Logical_Or;
46 package Language::Basic::Expression::Logical_And;
47 package Language::Basic::Expression::Relational;
48  
49 package Language::Basic::Expression::Arithmetic;
50 package Language::Basic::Expression::Multiplicative;
51 package Language::Basic::Expression::Unary;
52  
53 package Language::Basic::Expression::Lvalue;
54 package Language::Basic::Expression::Arglist;
55 package Language::Basic::Expression::Function;
56 package Language::Basic::Expression::Constant;
57  
58 package Language::Basic::Expression::Numeric;
59 package Language::Basic::Expression::String;
60 package Language::Basic::Expression::Boolean;
61 }
62  
63 # No sub new. Each class must have its own
64  
65 # Most expressions have a "return type" that's String, Boolean, or Numeric.
66 # (Arglists don't, since they hold a list of expressions.)
67 #
68 # An arithmetic expression is a LBE::Arithmetic::Numeric if it's made up
69 # of LBE::Multiplicative::Numeric expressions, but LBE::Arithmetic::String
70 # if it's got a LBE::Unary::String in it. We never mix
71 # expression types (except within Arglists)
72 #
73 # This sub therefore blesses an object to its String/Numeric/Boolean subclass
74 # depending on the type of the sub-expression (and returns the newly blessed
75 # object.)
76 #
77 # Usually the sub-expression is itself an LB::Expression, but not always.
78 # We test for subexps of, e.g., LB::String rather than LBE::String,
79 # because we may be setting return type based on a LB::Variable::String or
80 # LB::Function::String, which aren't LB::Expressions.
81 #
82 # Arg0 is the thing to bless, arg1 is the subexp
83 132 ▶sub set_return_type {
Subroutine set_return_type: called CC=6 SCAR=18.1
Pod undocumented
84 132 my $self = shift;
85 132 my $class = ref($self);
86 # If we already are blessed, don't rebless!
87 132 foreach (qw(String Numeric Boolean)) {
88 396 ▶ return $self if $self->isa("Language::Basic::Expression::$_");
Branch: 1 branch
BranchTrueFalseTotal
if $self->isa("Language::Basic::Expression::$_") 0 396 396
89 }
90  
91 132 my $subexp = shift;
92 132 my $type; # Return type
93 132 foreach (qw(String Numeric Boolean)) {
94 # LB::Function::String
95 242 ▶ if ($subexp->isa("Language::Basic::$_")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subexp->isa("Language::Basic::$_")) 132 110 242
96 132 $type = $_;
97 132 last;
98 }
99 }
100 0 ▶ unless (defined $type) {die "Error refining $class to ",ref($subexp),"\n";}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 132 132
101  
102 #print "self, class, type is 1 $self 2 $class 3 $type\n";
103 # Note: "$class::$type" breaks!
104 132 my $subclass = $class . "::$type";
105 # TODO assert that class actually exists! E.g., call
106 # $self->isa(LBE)
107 132 bless $self, $subclass;
108 }
109  
110 ######################################################################
111  
112 =pod
113  
114 =head2 The LBE hierarchy
115  
116 A bunch of LBE subclasses represent various kinds of BASIC expressions.
117 These subclasses closely follow the BASIC syntax diagram.
118  
119 Expressions can be classified in two ways, which are sort of vertical and
120 horizontal. One classification method is what subexpressions (if any) an
121 expression is made of. For example, an Arith. Exp. is made up of one or more
122 Mult. Exps. connected by plus or minus signs, while a Mult. Exp. is made up of
123 one or more Unary Exps. This is a hierarchical (or vertical) distinction,
124 important for building up the tree of objects that represent a BASIC
125 expression.
126  
127 (Note that not all levels in the hierarchy have to be filled. We don't
128 bother making an Arith. Exp. which contains just one Mult. Exp. which contains
129 just one Unary Exp. Instead, we just use the Unary Exp. itself (when it's
130 safe to do so!)
131  
132 The second way of classifying expressions is by their return type. A String
133 Exp. is a string constant, a string variable, a string function, or some other
134 expression whose value when evaluated will be a string. A Numeric Exp.
135 evaluates to a number, and a Boolean to a True or False value. This
136 distinction is important for typechecking and finding syntax errors in BASIC
137 code. (Note that in BASIC -- unlike Perl or C -- you can't "cast" a boolean
138 value into an integer or string. This actually makes parsing more difficult.)
139  
140 Some expressions don't exactly fit any of these distinctions. For example, an
141 Arglist evaluates to a list of expressions, each of which may be Numeric or
142 Boolean.
143  
144  
145 =head2 subclass methods
146  
147 Each subclass has (at least) three methods:
148  
149 =over 4
150  
151 =item new
152  
153 The "new" method takes a class and a Token::Group (and possibly some other
154 args). It eats one or more Tokens from it, parsing them, creating a new object
155 of that class I, and setting various fields in that object, which it returns.
156 If the tokens don't match the class, "new" returns undef.
157  
158 If an expression contains just one subexpression often we'll just return the
159 subexpression. So if an Arith. Exp. contains just one Mult. Exp., we'll just
160 return the LBE::Multiplicative object and I<not> an LBE::Arithmetic object.
161  
162 =item evaluate
163  
164 Actually calculates the value of the expression. For a string
165 or numeric constant or variable, that just means taking the stored value
166 of that object. For other Expressions, you actually need to do math.
167  
168 =item output_perl
169  
170 Gives a string with the Perl equivalent to a BASIC expression. "1+2" is
171 converted to "1+2", but "A" becomes "$a", "A$" becomes "$a_str", and
172 function calls may be even more complicated.
173  
174 =back
175  
176 =head2 LBE subclasses
177  
178 The hierarchical list of subclasses follows:
179  
180 =over 4
181  
182 =cut
183  
184 =item Arithmetic
185  
186 An arithmetic expression is a set of multiplicative expressions connected by
187 plus or minus signs. (String expressions can only be connected by plus,
188 which is the BASIC concatenation operator.)
189  
190 =cut
191  
192 # In BASIC, Arithmetic expressions can't contain Boolean expressions.
193 # However, parentheses can confuse things.
194 # LBE::Relational is one of:
195 # (1) LBE::Arithmetic Rel. Op. LBE::Arithmetic
196 # (2) (Logical Or)
197 # It calls LBE::Arithmetic::new with "maybe_boolean" sometimes, to tell
198 # LBEA::new that if it finds a (parenthesized) Boolean expression, it's
199 # just case #2 above. (Otherwise, a Boolean subexpression is an error.)
200 {
201 package Language::Basic::Expression::Arithmetic;
202 16 @Language::Basic::Expression::Arithmetic::ISA = qw(Language::Basic::Expression);
203 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
204  
205 280 ▶sub new {
Subroutine new: called CC=8 SCAR=21.2
206 # The while loop is necessary in case we have an expression like 1+2+3
207 # It will effectively evaluate the +, - operators left to right
208 280 my $class = shift;
209 280 my $token_group = shift;
210 280 my $maybe_boolean = shift;
211 280 ▶ if (defined($maybe_boolean) && $maybe_boolean ne "maybe_boolean") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_boolean and $maybe_boolean ne "maybe_boolean") 0 280 280
Condition66%2/3
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
!ll&&!rl&&r
232480
MC/DC0%0/2
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
defined $maybe_boolean $maybe_boolean ne "maybe_boolean"
Truth table66%2/3
A and B
A= defined $maybe_booleanB= $maybe_boolean ne "maybe_boolean"
ABresult
0X0
100
111
212 0 Exit_Error("Internal Error: Weird arg '$maybe_boolean' to LBE::Arithmetic::new");
213 }
214  
215 280 my $exp = new Language::Basic::Expression::Multiplicative
216 ($token_group, $maybe_boolean);
217 280 ▶ if ($exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->isa("Language::Basic::Expression::Boolean")) 2 278 280
218 2 ▶ if ($maybe_boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_boolean) { } 2 0 2
219 2 return $exp;
220 } else {
221 0 Exit_Error("Syntax Error: Expected non-Boolean Expression!");
222 }
223 }
224  
225 278 my (@exps, @ops);
226 278 push @exps, $exp;
227 278 ▶ while (defined (my $tok =
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tok = $token_group->eat_if_class("Arithmetic_Operator"))) 11 278 289
228 $token_group->eat_if_class("Arithmetic_Operator"))) {
229 11 push @ops, $tok->text;
230 11 $exp = new Language::Basic::Expression::Multiplicative $token_group;
231 11 ▶ if ($exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->isa("Language::Basic::Expression::Boolean")) 0 11 11
232 0 Exit_Error("Syntax Error: Expected non-Boolean Expression!");
233 }
234 11 push @exps, $exp;
235 } # end while
236  
237 # Don't bother making an Arith. Exp. object if there's just one Mult. Exp.
238 # Return the Mult. Exp. instead.
239 278 ▶ return $exp unless @ops;
Branch: 1 branch
BranchTrueFalseTotal
unless @ops 268 10 278
240  
241 # Otherwise, we want to create the Arith. Exp.
242 10 my $self = {};
243 10 $self->{"expressions"} = \@exps;
244 10 $self->{"operations"} = \@ops;
245 10 bless $self, $class;
246  
247 # Bless to LBEA::String or Numeric
248 10 $self->set_return_type($exp);
249 10 return $self;
250 } # end sub Language::Basic::Expression::Arithmetic::new
251  
252 package Language::Basic::Expression::Arithmetic::String;
253 16 @Language::Basic::Expression::Arithmetic::String::ISA =
254 qw(Language::Basic::Expression::Arithmetic
255 Language::Basic::Expression::String);
256  
257 28 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
258 28 my $self = shift;
259 28 my @exps = @{$self->{"expressions"}};
260 # Ops ought to be all pluses, since that's all BASIC can do.
261 28 my @ops = @{$self->{"operations"}};
262  
263 28 my $exp = (shift @exps)->evaluate;
264 28 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 28 28 56
265 28 my $exp2 = (shift @exps)->evaluate;
266 28 ▶ if ($op eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '+') { } 28 0 28
267 28 $exp .= $exp2;
268 } else {
269 0 die "Unknown op in LBE::Arithmetic::String::evaluate!\n";
270 }
271 } # end while
272 28 return($exp);
273 } # end sub Language::Basic::Expression::Arithmetic::String::evaluate
274  
275 0 ▶sub output_perl {
Subroutine output_perl: not called CC=3 SCAR=24.8
276 0 my $self = shift;
277 0 my @exps = @{$self->{"expressions"}};
278 0 my @ops = @{$self->{"operations"}};
279  
280 0 my $ret = (shift @exps)->output_perl;
281 0 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 0 0 0
282 0 ▶ if ($op eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '+') { } 0 0 0
283 0 my $exp = (shift @exps)->output_perl;
284 0 $ret .= " . " . $exp;
285 } else {
286 0 die "Unknown op in LBE::Arithmetic::String::output_perl!\n";
287 }
288 } # end while
289 0 return($ret);
290 } # end sub Language::Basic::Expression::Arithmetic::String::output_perl
291  
292 package Language::Basic::Expression::Arithmetic::Numeric;
293 16 @Language::Basic::Expression::Arithmetic::Numeric::ISA =
294 qw(Language::Basic::Expression::Arithmetic
295 Language::Basic::Expression::Numeric);
296  
297 6 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
298 6 my $self = shift;
299 6 my @exps = @{$self->{"expressions"}};
300 6 my @ops = @{$self->{"operations"}};
301  
302 6 my $exp = (shift @exps)->evaluate;
303 6 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 7 6 13
304 7 my $exp2 = (shift @exps)->evaluate;
305 7 ▶ if ($op eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '+') { } 7 0 7
306 7 $exp = $exp + $exp2;
307 } else { # minus
308 0 $exp = $exp - $exp2;
309 }
310 } # end while
311 6 return($exp);
312 } # end sub Language::Basic::Expression::Arithmetic::Numeric::evaluate
313  
314 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
315 0 my $self = shift;
316 0 my @exps = @{$self->{"expressions"}};
317 0 my @ops = @{$self->{"operations"}};
318  
319 0 my $ret = (shift @exps)->output_perl;
320 0 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 0 0 0
321 0 my $exp = (shift @exps)->output_perl;
322 0 $ret .= $op . $exp;
323 } # end while
324 0 return($ret);
325 } # end sub Language::Basic::Expression::Arithmetic::Numeric::output_perl
326  
327 } # end package Language::Basic::Expression::Arithmetic
328  
329 =item Multiplicative
330  
331 a set of unary expressions connected by '*' or '/'.
332  
333 =cut
334  
335 {
336 16 package Language::Basic::Expression::Multiplicative;
337 16 @Language::Basic::Expression::Multiplicative::ISA = qw(Language::Basic::Expression);
338 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 291 ▶sub new {
Subroutine new: called CC=8 SCAR=21.2
341 291 my $class = shift;
342 291 my $token_group = shift;
343 291 my $maybe_boolean = shift;
344 291 ▶ if (defined($maybe_boolean) && $maybe_boolean ne "maybe_boolean") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_boolean and $maybe_boolean ne "maybe_boolean") 0 291 291
Condition66%2/3
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
!ll&&!rl&&r
243480
MC/DC0%0/2
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
defined $maybe_boolean $maybe_boolean ne "maybe_boolean"
Truth table66%2/3
A and B
A= defined $maybe_booleanB= $maybe_boolean ne "maybe_boolean"
ABresult
0X0
100
111
345 0 Exit_Error("Internal Error: Weird arg '$maybe_boolean' to LBE::Multiplicative::new");
346 }
347  
348 291 my $exp = new Language::Basic::Expression::Unary
349 ($token_group, $maybe_boolean);
350 291 ▶ if ($exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->isa("Language::Basic::Expression::Boolean")) 2 289 291
351 2 ▶ if ($maybe_boolean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_boolean) { } 2 0 2
352 2 return $exp;
353 } else {
354 0 Exit_Error("Syntax Error: Expected non-Boolean Expression!");
355 }
356 }
357  
358 289 my (@exps, @ops);
359 289 push @exps, $exp;
360 289 ▶ while (defined (my $tok =
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tok = $token_group->eat_if_class("Multiplicative_Operator"))) 7 289 296
361 $token_group->eat_if_class("Multiplicative_Operator"))) {
362 7 push @ops, $tok->text;
363 7 $exp = new Language::Basic::Expression::Unary $token_group;
364 7 ▶ if ($exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->isa("Language::Basic::Expression::Boolean")) 0 7 7
365 0 Exit_Error("Syntax Error: Expected non-Boolean Expression!");
366 }
367 7 push @exps, $exp;
368 } # end while
369  
370 # Don't bother making a Mult. Exp. object if there's just one Unary Exp.
371 # Return the Unary Exp. instead.
372 # Note that this will definitely happen if $exp is a String.
373 289 ▶ return $exp unless @ops;
Branch: 1 branch
BranchTrueFalseTotal
unless @ops 284 5 289
374  
375 # Otherwise, we want to create the Mult. Exp.
376 5 my $self = {};
377 5 $self->{"expressions"} = \@exps;
378 5 $self->{"operations"} = \@ops;
379 5 bless $self, $class;
380  
381 # Bless to LBEM::String or Numeric
382 5 $self->set_return_type($exp);
383 5 return $self;
384 } # end sub Language::Basic::Expression::Multiplicative::new
385  
386 8 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
387 8 my $self = shift;
388 8 my @exps = @{$self->{"expressions"}};
389 8 my @ops = @{$self->{"operations"}};
390  
391 8 my $exp = (shift @exps)->evaluate;
392 8 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 10 8 18
393 10 my $exp2 = (shift @exps)->evaluate;
394 10 ▶ if ($op eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '*') { } 8 2 10
395 8 $exp = $exp * $exp2;
396 } else {
397 2 $exp = $exp / $exp2;
398 }
399 } # end while
400 8 return($exp);
401 } # end sub Language::Basic::Expression::Multiplicative::evaluate
402  
403 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
404 0 my $self = shift;
405 0 my @exps = @{$self->{"expressions"}};
406 0 my @ops = @{$self->{"operations"}};
407  
408 0 my $ret = (shift @exps)->output_perl;
409 0 ▶ while (my $op = shift @ops) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = shift @ops) 0 0 0
410 0 my $exp = (shift @exps)->output_perl;
411 0 $ret .= $op . $exp;
412 } # end while
413 0 return($ret);
414 } # end sub Language::Basic::Expression::Multiplicative::output_perl
415  
416 # Sub packages
417 package Language::Basic::Expression::Multiplicative::Numeric;
418 16 @Language::Basic::Expression::Multiplicative::Numeric::ISA =
419 qw(Language::Basic::Expression::Multiplicative
420 Language::Basic::Expression::Numeric);
421 # Note that there can't possibly be an LBEM::String. LBEM::new will just
422 # return an LBE::Unary, since there are no string multiplying ops to find.
423 } # end package Language::Basic::Expression::Multiplicative
424  
425 =item Unary
426  
427 a variable, a function, a string or numeric constant, or an arithmetic
428 expression in parentheses, potentially with a unary minus sign.
429  
430 =cut
431  
432 {
433 16 package Language::Basic::Expression::Unary;
434 16 @Language::Basic::Expression::Unary::ISA = qw(Language::Basic::Expression);
435 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
436  
437 298 ▶sub new {
Subroutine new: called CC=14 SCAR=27.7
438 298 my $class = shift;
439 298 my $token_group = shift;
440 # Fields:
441 # nested This Expression contains a parenthesized exp.
442 # minus This Exp. has a unary minus in front of it
443 298 my $self = {
444 "nested" => "",
445 "minus" => "",
446 };
447  
448 # If we're inside a Relational Exp., then a parenthetical exp. may
449 # be either Boolean or non-Boolean. Otherwise, it has to be non-Boolean
450 298 my $maybe_boolean = shift;
451 298 ▶ if (defined($maybe_boolean) && $maybe_boolean ne "maybe_boolean") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_boolean and $maybe_boolean ne "maybe_boolean") 0 298 298
Condition66%2/3
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
!ll&&!rl&&r
250480
MC/DC0%0/2
defined $maybe_boolean and $maybe_boolean ne "maybe_boolean"
defined $maybe_boolean $maybe_boolean ne "maybe_boolean"
Truth table66%2/3
A and B
A= defined $maybe_booleanB= $maybe_boolean ne "maybe_boolean"
ABresult
0X0
100
111
452 0 Exit_Error("Internal Error: Weird arg '$maybe_boolean' to LBE::Unary::new");
453 }
454  
455 # unary minus in the expression?
456 298 ▶ $self->{"minus"} = 1 if defined($token_group->eat_if_string("-"));
Branch: 1 branch
BranchTrueFalseTotal
if defined $token_group->eat_if_string("-") 6 292 298
457  
458 298 my $unary;
459 my $try;
460 # if a parentheses, (recursively) parse what's inside
461 # If $maybe_boolean, then a paren'ed expression might be a Boolean exp.,
462 # so call LBE::Logical_Or (highest level Boolean exp.)
463 # However, in most cases, it'll be a non-Boolean, so call with
464 # "maybe_arithmetic" flag, which tells LBE::LO not to be surprised
465 # if it finds an arithmetic exp.
466 298 ▶ if (defined($token_group->eat_if_class("Left_Paren"))) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $token_group->eat_if_class('Left_Paren')) { } 8 290 298
elsif (defined($try = 'Language::Basic::Expression::Function'->new($token_group))) { } 12 278 290
elsif (defined($try = 'Language::Basic::Expression::Lvalue'->new($token_group))) { } 42 236 278
elsif (defined($try = 'Language::Basic::Expression::Constant'->new($token_group))) { } 236 0 236
467 8 $self->{"nested"} = 1;
468 8 $try = new Language::Basic::Expression::Logical_Or
469 ($token_group, "maybe_arithmetic");
470 # Skip End Paren
471 8 ▶ defined($token_group->eat_if_class("Right_Paren")) or
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_class("Right_Paren") or Exit_Error("Expected ')' to match '('!") 8 0 8
472 Exit_Error("Expected ')' to match '('!");
473 # if we found a Boolean, make sure we're allowed to have one.
474 8 ▶ if ($try->isa("Language::Basic::Expression::Boolean") &&
Branch: 1 branch
BranchTrueFalseTotal
if ($try->isa("Language::Basic::Expression::Boolean") and not $maybe_boolean) 0 8 8
Condition66%2/3
$try->isa("Language::Basic::Expression::Boolean") and not $maybe_boolean
!ll&&!rl&&r
620
MC/DC0%0/2
$try->isa("Language::Basic::Expression::Boolean") and not $maybe_boolean
$try->isa("Language::Basic::Expression::Boolean") not $maybe_boolean
Truth table66%2/3
A and not(B)
A= $try->isa("Language::Basic::Expression::Boolean")B= not $maybe_boolean
ABresult
0X0
100
111
475 !$maybe_boolean) {
476 0 Exit_Error("Syntax Error: Expected non-Boolean Expression!");
477 }
478 8 $unary = $try
479  
480 # OR it's a String or Numeric function
481 # NOTE that LBEF::new had better not eat the word if it returns undef!
482 } elsif (defined ($try =
483 new Language::Basic::Expression::Function $token_group)) {
484 12 $unary = $try;
485  
486 # OR it's a String or Numeric variable
487 } elsif (defined ($try =
488 new Language::Basic::Expression::Lvalue $token_group)) {
489 42 $unary = $try;
490  
491 # OR it's a String or Numeric constant
492 } elsif (defined ($try =
493 new Language::Basic::Expression::Constant $token_group)) {
494 236 $unary = $try;
495  
496 # Or die
497 } else {
498 0 ▶ my $tok = $token_group->lookahead or
Branch: 1 branch
BranchTrueFalseTotal
my $tok = $token_group->lookahead or Exit_Error("Found nothing when expected Unary Expression!") 0 0 0
499 Exit_Error("Found nothing when expected Unary Expression!");
500 0 Exit_Error("Unknown unary expression starts with '", $tok->text,"'");
501 }
502 #print "unary ref is ",ref($unary),"\n";
503  
504 # If it's just an Lvalue, say, then return the Lvalue object rather
505 # than making a Unary out of it. Can't do that if we're nested or minused.
506 298 ▶ if ($self->{"nested"} || $self->{"minus"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'nested'} or $self->{'minus'}) { } 12 286 298
Condition100%3/3
$self->{"nested"} or $self->{"minus"}
l!l&&r!l&&!r
84286
MC/DC100%2/2
$self->{"nested"} or $self->{"minus"}
$self->{"nested"} $self->{"minus"}
Truth table100%3/3
A or B
A= $self->{"nested"}B= $self->{"minus"}
ABresult
1X1
011
000
507 12 $self->{"expression"} = $unary;
508 12 bless $self, $class;
509 # Bless to LBEU::String or Numeric or Boolean
510 12 $self->set_return_type($unary);
511 12 return $self;
512 } else {
513 286 return $unary;
514 }
515 } # end Language::Basic::Expression::Unary::new
516  
517 16 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=6.9
518 16 my $self = shift;
519 16 my $exp = $self->{"expression"};
520  
521 16 my $value = $exp->evaluate;
522 16 ▶ $value = -$value if $self->{"minus"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"minus"} 10 6 16
523 16 return($value);
524 } # end sub Language::Basic::Expression::Unary::evaluate
525  
526 0 ▶sub output_perl {
Subroutine output_perl: not called CC=3 SCAR=24.8
527 0 my $self = shift;
528 0 ▶ my $ret = $self->{"minus"} ? "-" : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'minus'} ? : 0 0 0
529 0 my $exp = $self->{"expression"};
530 0 my $out = $exp->output_perl;
531 0 ▶ if ($self->{"nested"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"nested"}) 0 0 0
532 0 $out = "(" . $out . ")";
533 }
534 0 $ret .= $out;
535 0 return($ret);
536 } # end sub Language::Basic::Expression::Unary::output_perl
537  
538 # Sub packages
539 package Language::Basic::Expression::Unary::Numeric;
540 16 @Language::Basic::Expression::Unary::Numeric::ISA =
541 qw(Language::Basic::Expression::Unary
542 Language::Basic::Expression::Numeric);
543 package Language::Basic::Expression::Unary::String;
544 16 @Language::Basic::Expression::Unary::String::ISA =
545 qw(Language::Basic::Expression::Unary
546 Language::Basic::Expression::String);
547 package Language::Basic::Expression::Unary::Boolean;
548 16 @Language::Basic::Expression::Unary::Boolean::ISA =
549 qw(Language::Basic::Expression::Unary
550 Language::Basic::Expression::Boolean);
551 } # end package Language::Basic::Expression::Unary
552  
553 ######################################################################
554  
555 =item Constant
556  
557 a string or numeric constant, like "17" or 32.4
558  
559 =cut
560  
561 {
562 16 package Language::Basic::Expression::Constant;
563 16 @Language::Basic::Expression::Constant::ISA = qw(Language::Basic::Expression);
564  
565 # Returns a LBE::Constant::* subclass or undef
566 259 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
567 259 my $class = shift;
568 259 my $token_group = shift;
569 259 my ($const, $try);
570 259 ▶ if (defined ($try =
Branch: 2 branches
BranchTrueFalseTotal
if (defined($try = 'Language::Basic::Expression::Constant::Numeric'->new($token_group))) { } 221 38 259
elsif (defined($try = 'Language::Basic::Expression::Constant::String'->new($token_group))) { } 38 0 38
571 new Language::Basic::Expression::Constant::Numeric $token_group)) {
572 221 $const = $try;
573 } elsif (defined ($try =
574 new Language::Basic::Expression::Constant::String $token_group)) {
575 38 $const = $try;
576 } else {
577 0 return undef;
578 }
579  
580 259 return $const;
581 } # end Language::Basic::Expression::Constant::new
582  
583 0 ▶sub evaluate {return shift->{"expression"}->evaluate; }
Subroutine evaluate: not called CC=1 SCAR=6.9
584  
585 package Language::Basic::Expression::Constant::Numeric;
586 16 @Language::Basic::Expression::Constant::Numeric::ISA =
587 qw(Language::Basic::Expression::Constant
588 Language::Basic::Expression::Numeric);
589  
590 259 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
591 259 my $class = shift;
592 259 my $token_group = shift;
593 259 ▶ if (defined (my $tok =
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tok = $token_group->eat_if_class('Numeric_Constant'))) { } 221 38 259
594 $token_group->eat_if_class("Numeric_Constant"))) {
595 221 my $self = {"value" => $tok->text + 0};
596 221 bless $self, $class; # and return it
597 } else {
598 38 return undef;
599 }
600 } # end sub Language::Basic::Expression::Constant::Numeric::new
601  
602 316 ▶sub evaluate { return shift->{"value"} }
Subroutine evaluate: called CC=1 SCAR=0.0
603  
604 0 ▶sub output_perl {return shift->{"value"}}
Subroutine output_perl: not called CC=1 SCAR=6.9
605  
606 package Language::Basic::Expression::Constant::String;
607 16 @Language::Basic::Expression::Constant::String::ISA =
608 qw(Language::Basic::Expression::Constant
609 Language::Basic::Expression::String);
610  
611 38 ▶sub new {
Subroutine new: called CC=2 SCAR=7.5
612 38 my $class = shift;
613 38 my $token_group = shift;
614 38 ▶ if (defined (my $tok =
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tok = $token_group->eat_if_class('String_Constant'))) { } 38 0 38
615 $token_group->eat_if_class("String_Constant"))) {
616 38 (my $text = $tok->text) =~ s/^"(.*?)"/$1/;
617 38 my $self = {"value" => $text};
618 38 bless $self, $class; # and return it
619 } else {
620 # TODO handle unquoted string for Input, Data statements
621 0 warn "Currently only understand quoted strings for String Constant";
622 0 return undef;
623 }
624 } # end sub Language::Basic::Expression::Constant::String::new
625  
626 42 ▶sub evaluate { return shift->{"value"} }
Subroutine evaluate: called CC=1 SCAR=0.0
627  
628 # Don't return in single quotes, because single quotes may be in a BASIC
629 # string constant. Instead use quotemeta. But don't really use quotemeta,
630 # because it quotes too much.
631 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
632 0 my $self = shift;
633 0 my $str = $self->{"value"};
634 0 $str =~ s/([\$%@*&])/\\$1/g; # poor man's quotemeta
635 0 return '"' . $str . '"';
636 } # end sub Language::Basic::Expression::Constant::String::output_perl
637  
638 } # end package Language::Basic::Expression::Constant
639  
640 ######################################################################
641  
642 =item Lvalue
643  
644 a settable expression: a variable, X, or one cell in an array, A(17,Q). The
645 "variable" method returns the actual LB::Variable::Scalar object referenced by
646 this Lvalue.
647  
648 =cut
649  
650 {
651 16 package Language::Basic::Expression::Lvalue;
652 16 @Language::Basic::Expression::Lvalue::ISA = qw(Language::Basic::Expression);
653 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
654  
655 # Sub-packages
656 {
657 16 package Language::Basic::Expression::Lvalue::Numeric;
658 16 @Language::Basic::Expression::Lvalue::Numeric::ISA =
659 qw(Language::Basic::Expression::Lvalue
660 Language::Basic::Expression::Numeric);
661 package Language::Basic::Expression::Lvalue::String;
662 16 @Language::Basic::Expression::Lvalue::String::ISA =
663 qw(Language::Basic::Expression::Lvalue
664 Language::Basic::Expression::String);
665 }
666  
667 # Fields:
668 # varptr - ref to the LB::Variable (::Array or ::Scalar) object. Note
669 # that it does NOT ref a particular cell in an LBV::Array object!
670 # arglist - a set of Arithmetic Expressions describing which exact cell
671 # in an LBV::Array to get. undef for a LBV::Scalar
672 326 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
673 326 my $class = shift;
674 326 my $token_group = shift;
675 326 my $self = {};
676  
677 326 ▶ defined (my $tok =
Branch: 1 branch
BranchTrueFalseTotal
defined(my $tok = $token_group->eat_if_class("Identifier")) or return undef 90 236 326
678 $token_group->eat_if_class("Identifier")) or
679 return undef;
680 90 my $name = $tok->text;
681  
682 # read ( Arglist ) if it exists
683 # By default, though, it's a scalar, and has no ()
684 90 $self->{"arglist"} = undef;
685 90 ▶ if (defined (my $arglist =
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arglist = "Language::Basic::Expression::Arglist"->new($token_group))) 13 77 90
686 new Language::Basic::Expression::Arglist $token_group)) {
687 13 $self->{"arglist"} = $arglist;
688 }
689  
690 # Look up the variable by name in the (Array or Scalar) variable storage.
691 # (Also, create the Variable if it doesn't yet exist.)
692 90 my $var = &Language::Basic::Variable::lookup($name, $self->{"arglist"});
693 90 $self->{"varptr"} = $var;
694 90 $self->{"name"} = $name;
695  
696 90 bless $self, $class;
697 # Is it a string or numeric lvalue?
698 90 $self->set_return_type($var);
699 90 return $self;
700 } # end sub Language::Basic::Expression::Lvalue::new
701  
702 196 ▶sub evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
703 196 my $self = shift;
704 # This automatically gets the correct array cell if necessary
705 196 my $var = $self->variable;
706 196 my $value = $var->value;
707 196 return $value;
708 } # end sub Language::Basic::Expression::Lvalue::evaluate
709  
710 # returns a variable, e.g. for setting in a Let or changing in a Next
711 # Note that it always returns a LB::Variable::Scalar object. If the
712 # variable in this expression is an Array, it returns one cell from the array.
713 366 ▶sub variable {
Subroutine variable: called CC=2 SCAR=6.9
714 366 my $self = shift;
715 366 my $var = $self->{"varptr"};
716 # if Arglist exists, evaluate each arith. exp. in it and get that cell
717 # from the Array
718 366 ▶ if (defined (my $arglist = $self->{"arglist"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arglist = $self->{"arglist"})) 54 312 366
719 54 my @args = $arglist->evaluate;
720 54 $var = $var->get_cell(@args);
721 }
722  
723 366 return $var;
724 } # end sub Language::Basic::Expression::Lvalue::variable
725  
726 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
727 0 my $self = shift;
728 0 my $name = $self->{"name"};
729 0 $name =~ s/\$$/_str/; # make name perl-like
730 0 my $ret = '$' . lc($name);
731 0 ▶ if (defined $self->{"arglist"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"arglist"}) 0 0 0
732 0 my $args = join("][", ($self->{"arglist"}->output_perl));
733 0 $ret .= "[" . $args . "]";
734 }
735 0 return $ret;
736 } # end sub Language::Basic::Expression::Lvalue::output_perl
737  
738 } # end package Language::Basic::Expression::Lvalue
739  
740 ######################################################################
741  
742 =item Function
743  
744 Either an Intrinsic or a User-Defined function.
745  
746 =cut
747  
748 #
749 # Fields:
750 # function - ref to the LB::Function (::Intrinsic or ::Defined) used
751 # by this expression
752 # arglist - a set of Arithmetic Expressions describing the arguments
753 # to pass to the function
754 {
755 16 package Language::Basic::Expression::Function;
756 16 @Language::Basic::Expression::Function::ISA = qw(Language::Basic::Expression);
757 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
758  
759 # Sub-packages
760 {
761 16 package Language::Basic::Expression::Function::Numeric;
762 16 @Language::Basic::Expression::Function::Numeric::ISA =
763 qw(Language::Basic::Expression::Function
764 Language::Basic::Expression::Numeric);
765 package Language::Basic::Expression::Function::String;
766 16 @Language::Basic::Expression::Function::String::ISA =
767 qw(Language::Basic::Expression::Function
768 Language::Basic::Expression::String);
769 }
770  
771 # Arg0, Arg1 are the object and a ref to the string being parsed, as usual.
772 # Arg2, if it exists, says we're in a DEF statement, so that if the
773 # function doesn't exist, we should create it rather than returning undef.
774 293 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
775 293 my $class = shift;
776 293 my $token_group = shift;
777 293 my $self = {};
778  
779 # Don't eat it if it's not a true function name (could be an lvalue)
780 293 my $tok = $token_group->lookahead;
781 293 ▶ return undef unless $tok->isa("Language::Basic::Token::Identifier");
Branch: 1 branch
BranchTrueFalseTotal
unless $tok->isa("Language::Basic::Token::Identifier") 236 57 293
782 57 my $name = $tok->text;
783 57 my $defining = (defined (my $exp = shift));
784  
785 # Look up the function name
786 # If the function doesn't exist, the word is a variable or something...
787 # Alternatively, if there was a second argument to parse, then we're
788 # in a DEF statement & should create the function.
789 57 my $func;
790 57 ▶ if ($defining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defining) { } 3 54 57
791 # TODO should this check be somewhere else, so that we can
792 # give a more descriptive error message in Statement::Def::new?
793 3 ▶ return undef unless $name =~ /^FN/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^FN/ 0 3 3
794 3 $func = new Language::Basic::Function::Defined $name;
795 } else {
796 54 ▶ $func = &Language::Basic::Function::lookup($name) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$func = &Language::Basic::Function::lookup($name) or return undef 12 42 54
797 }
798 15 $self->{"function"} = $func;
799  
800 #Now that we know it's a function, eat the token
801 15 $token_group->eat;
802  
803 # read ( Arglist )
804 # TODO Actually, whether or not we're defining, we should just read
805 # an LBE::Arglist here. If $defining, define() can make sure all args
806 # are actually Lvalues containing Scalar Variables. However, this
807 # requires that Arglist has Lvalues, rather than Arith. Exp.'s
808 # containing (ME's containing...) Lvalues.
809 15 ▶ if ($defining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defining) { } 3 12 15
810 # Empty parens aren't allowed! (and \s* has been removed by lexing)
811 3 ▶ defined($token_group->eat_if_class("Left_Paren")) or
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_class("Left_Paren") or Exit_Error("Function must take at least one argument.") 3 0 3
812 Exit_Error("Function must take at least one argument.");
813 3 my @args;
814 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_string(",") and do { do { my $arg = "Language::Basic::Expression::Lvalue"->new($token_group); push @args, $arg } } 1 3 4
815 4 my $arg = new Language::Basic::Expression::Lvalue $token_group;
816 4 push @args, $arg;
817 } while (defined $token_group->eat_if_string(","));
818 3 ▶ defined($token_group->eat_if_class("Right_Paren")) or
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_class("Right_Paren") or Exit_Error("Expected ')' to match '('!") 3 0 3
819 Exit_Error("Expected ')' to match '('!");
820  
821 # Declare the number & type of args in the subroutine
822 3 $func->declare (\@args);
823  
824 } else {
825 12 ▶ my $arglist = new Language::Basic::Expression::Arglist $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $arglist = "Language::Basic::Expression::Arglist"->new($token_group) or Exit_Error("Function without arglist!") 12 0 12
826 or Exit_Error("Function without arglist!");
827 # check if the number or type of args is wrong.
828 12 $func->check_args($arglist);
829 12 $self->{"arglist"} = $arglist;
830 }
831  
832 15 bless $self, $class;
833 # Is it a string or numeric Function?
834 15 $self->set_return_type($func);
835 15 return $self;
836 } # end sub Language::Basic::Expression::Function::new
837  
838 34 ▶sub evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
839 34 my $self = shift;
840 34 my $func = $self->{"function"};
841 34 my $arglist = $self->{"arglist"};
842 # Note we tested number & type of args in new
843 34 my @args = $arglist->evaluate;
844 34 my $value = $func->evaluate(@args);
845 34 return $value;
846 } # end sub Language::Basic::Expression::Function::evaluate
847  
848 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
849 0 my $self = shift;
850 # Function name
851 0 my $func = $self->{"function"};
852 0 my $ret = $func->output_perl;
853 # If it's either a user-defined function or a BASIC intrinsic (that
854 # doesn't have a Perl equivalent), add a &
855 0 ▶ if ($ret =~ /(fun|bas)$/) {$ret = '&' . $ret}
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /(fun|bas)$/) 0 0 0
856  
857 # Function args
858 0 $ret .= "(";
859
860 0 my @args = $self->{"arglist"}->output_perl;
861 0 $ret .= join(", ", @args);
862 0 $ret .= ")";
863 0 return $ret;
864 }
865  
866 } # end package Language::Basic::Expression::Function
867  
868 =item Arglist
869  
870 a list of arguments to an array or function
871  
872 =cut
873  
874 {
875 16 package Language::Basic::Expression::Arglist;
876 16 @Language::Basic::Expression::Arglist::ISA = qw(Language::Basic::Expression);
877 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
878  
879 102 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
880 102 my $class = shift;
881 102 my $token_group = shift;
882 102 my $self = {};
883  
884 # Has to start with paren
885 102 ▶ defined($token_group->eat_if_class("Left_Paren")) or
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_class("Left_Paren") or return undef 25 77 102
886 return undef;
887 # Eat args
888 25 my @args = ();
889 25 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_string(",") and do { do { my $arg = "Language::Basic::Expression::Arithmetic"->new($token_group); push @args, $arg } } 13 25 38
890 38 my $arg = new Language::Basic::Expression::Arithmetic $token_group;
891 # TODO test that arg is a Scalar!
892 38 push @args, $arg;
893 } while (defined($token_group->eat_if_string(",")));
894  
895 # Has to end with paren
896 25 ▶ defined($token_group->eat_if_class("Right_Paren")) or
Branch: 1 branch
BranchTrueFalseTotal
defined $token_group->eat_if_class("Right_Paren") or Exit_Error("Arglist without ')' at end!") 25 0 25
897 Exit_Error("Arglist without ')' at end!");
898 0 ▶ unless (@args) {Exit_Error("Empty argument list ().")}
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 25 25
899  
900 25 $self->{"arguments"} = \@args;
901 25 bless $self, $class;
902 } # end sub Language::Basic::Expression::Arglist::new
903  
904 # Returns a LIST of values
905 91 ▶sub evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
906 91 my $self = shift;
907 91 my @values = map {$_->evaluate} @{$self->{"arguments"}};
908 91 return @values;
909 } # end sub Language::Basic::Expression::Arglist::evaluate
910  
911 # Note this returns an ARRAY of args. Messes up the output_perl paradigm,but
912 # functions & arrays need to do different things to the args.
913 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
914 0 my $self = shift;
915 0 return map {$_->output_perl} @{$self->{"arguments"}};
916 } # end sub Language::Basic::Expression::Arglist::output_perl
917 } # end package Language::Basic::Expression::Arglist
918  
919 ######################################################################
920 # Boolean stuff
921 # Booleans don't care whether the stuff in them is String or Numeric,
922 # so no sub-packages are needed.
923  
924 =item Logical_Or
925  
926 a set of Logical_And expressions connected by "OR"
927  
928 =cut
929  
930 # In BASIC, Boolean expressions can't contain non-Boolean expressions
931 # except for Relational Exps. (which have two Arithmetic Exps. separated by
932 # a Rel. Op.)
933 # However, parentheses can confuse things.
934 # LBE::Unary is one of:
935 # (1) A constant, variable, function, etc.
936 # (2) (Arithmetic Exp.)
937 # (3) (Logical Or)
938 # Unary::new calls LBE::Logical_Or::new with "maybe_arithmetic" sometimes, to
939 # tell LBELO::new that if it finds a (parenthesized) non-Boolean expression,
940 # it's just case #2 above. (Otherwise, a non-Boolean subexpression is an error.)
941 {
942 16 package Language::Basic::Expression::Logical_Or;
943 16 @Language::Basic::Expression::Logical_Or::ISA =
944 qw(Language::Basic::Expression::Boolean);
945 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
946  
947 36 ▶sub new {
Subroutine new: called CC=8 SCAR=21.4
948 # No "operators" field is necessary since operators must all be "OR"
949 36 my $class = shift;
950 36 my $token_group = shift;
951 36 my $maybe_arithmetic = shift;
952 36 ▶ if (defined($maybe_arithmetic) && $maybe_arithmetic ne "maybe_arithmetic") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic") 0 36 36
Condition66%2/3
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
!ll&&!rl&&r
2880
MC/DC0%0/2
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
defined $maybe_arithmetic $maybe_arithmetic ne "maybe_arithmetic"
Truth table66%2/3
A and B
A= defined $maybe_arithmeticB= $maybe_arithmetic ne "maybe_arithmetic"
ABresult
0X0
100
111
953 0 Exit_Error("Internal Error: Weird arg '$maybe_arithmetic' to LBE::Logical_Or::new");
954 }
955  
956 36 my $exp = new Language::Basic::Expression::Logical_And
957 ($token_group, $maybe_arithmetic); # TODO ... or Error...
958 36 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exp->isa("Language::Basic::Expression::Boolean")) 6 30 36
959 6 ▶ if ($maybe_arithmetic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_arithmetic) { } 6 0 6
960 6 return $exp;
961 } else {
962 0 Exit_Error("Syntax Error: Expected Boolean Expression");
963 }
964 }
965  
966 30 my @exps;
967 30 push @exps, $exp;
968 30 ▶ while (defined ($token_group->eat_if_string("OR"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_group->eat_if_string("OR")) 6 30 36
969 6 $exp = new Language::Basic::Expression::Logical_And $token_group;
970 6 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exp->isa("Language::Basic::Expression::Boolean")) 0 6 6
971 0 Exit_Error("Syntax Error: Expected Boolean Expression!");
972 }
973 6 push @exps, $exp;
974 } # end while
975  
976 # Don't bother making a Logical_Or object if there's just one Logical_And
977 30 ▶ return $exp if @exps == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @exps == 1 24 6 30
978  
979 # Otherwise, we want to create the Logical_Or
980 6 my $self = {"expressions" => \@exps};
981 6 bless $self, $class;
982 } # end sub Language::Basic::Expression::Logical_Or::new
983  
984 6 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
985 6 my $self = shift;
986 6 my @exps = @{$self->{"expressions"}};
987  
988 6 my $exp = (shift @exps)->evaluate;
989 # TODO stop calculating when we find a true one?
990 6 ▶ while (defined(my $exp2 = shift @exps)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $exp2 = shift @exps)) 6 6 12
991 6 ▶ $exp = $exp || $exp2->evaluate;
Condition100%3/3
$exp || $exp2->evaluate
l!l&&r!l&&!r
321
MC/DC100%2/2
$exp || $exp2->evaluate
$exp $exp2->evaluate
Truth table100%3/3
A || B
A= $expB= $exp2->evaluate
ABresult
1X1
011
000
992 } # end while
993 6 return($exp);
994 } # end sub Language::Basic::Expression::Logical_Or::evaluate
995  
996 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
997 0 my $self = shift;
998 0 my @exps = @{$self->{"expressions"}};
999  
1000 0 my $ret = (shift @exps)->output_perl;
1001 0 ▶ while (defined(my $exp = shift @exps)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $exp = shift @exps)) 0 0 0
1002 0 $ret .= " || " . $exp->output_perl;
1003 } # end while
1004 0 return($ret);
1005 } # end sub Language::Basic::Expression::Logical_Or::output_perl
1006  
1007 } # end package Language::Basic::Expression::Logical_Or
1008  
1009 =item Logical_And
1010  
1011 a set of Relational expressions connected by "AND"
1012  
1013 =cut
1014  
1015 {
1016 16 package Language::Basic::Expression::Logical_And;
1017 16 @Language::Basic::Expression::Logical_And::ISA =
1018 qw(Language::Basic::Expression::Boolean);
1019 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
1020  
1021 42 ▶sub new {
Subroutine new: called CC=8 SCAR=21.4
1022 # No "operators" field is necessary since operators must all be "AND"
1023 42 my $class = shift;
1024 42 my $token_group = shift;
1025 42 my $maybe_arithmetic = shift;
1026 42 ▶ if (defined($maybe_arithmetic) && $maybe_arithmetic ne "maybe_arithmetic") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic") 0 42 42
Condition66%2/3
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
!ll&&!rl&&r
3480
MC/DC0%0/2
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
defined $maybe_arithmetic $maybe_arithmetic ne "maybe_arithmetic"
Truth table66%2/3
A and B
A= defined $maybe_arithmeticB= $maybe_arithmetic ne "maybe_arithmetic"
ABresult
0X0
100
111
1027 0 Exit_Error("Internal Error: Weird arg '$maybe_arithmetic' to LBE::Logical_And::new");
1028 }
1029  
1030 42 my $exp = new Language::Basic::Expression::Relational
1031 ($token_group, $maybe_arithmetic);
1032 42 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exp->isa("Language::Basic::Expression::Boolean")) 6 36 42
1033 6 ▶ if ($maybe_arithmetic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_arithmetic) { } 6 0 6
1034 6 return $exp;
1035 } else {
1036 0 Exit_Error("Syntax Error: Expected Boolean Expression!");
1037 }
1038 }
1039  
1040 36 my @exps;
1041 36 push @exps, $exp;
1042 36 ▶ while (defined ($token_group->eat_if_string("AND"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_group->eat_if_string("AND")) 6 36 42
1043 6 $exp = new Language::Basic::Expression::Relational $token_group;
1044 6 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exp->isa("Language::Basic::Expression::Boolean")) 0 6 6
1045 0 Exit_Error("Syntax Error: Expected Boolean Expression!");
1046 }
1047 6 push @exps, $exp;
1048 } # end while
1049  
1050 # Don't bother making a Logical_And object if there's just one Relational
1051 36 ▶ return $exp if @exps == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @exps == 1 30 6 36
1052  
1053 # Otherwise, we want to create the Logical_And
1054 6 my $self = {"expressions" => \@exps};
1055 6 bless $self, $class;
1056 } # end sub Language::Basic::Expression::Logical_And::new
1057  
1058 6 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
1059 6 my $self = shift;
1060 6 my @exps = @{$self->{"expressions"}};
1061  
1062 6 my $exp = (shift @exps)->evaluate;
1063 # TODO stop calculating when we find a true one?
1064 6 ▶ while (defined(my $exp2 = shift @exps)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $exp2 = shift @exps)) 6 6 12
1065 6 ▶ $exp = $exp && $exp2->evaluate;
Condition100%3/3
$exp && $exp2->evaluate
!ll&&!rl&&r
312
MC/DC100%2/2
$exp && $exp2->evaluate
$exp $exp2->evaluate
Truth table100%3/3
A && B
A= $expB= $exp2->evaluate
ABresult
0X0
100
111
1066 } # end while
1067 6 return($exp);
1068 } # end sub Language::Basic::Expression::Logical_And::evaluate
1069  
1070 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
1071 0 my $self = shift;
1072 0 my @exps = @{$self->{"expressions"}};
1073  
1074 0 my $ret = (shift @exps)->output_perl;
1075 0 ▶ while (defined(my $exp = shift @exps)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $exp = shift @exps)) 0 0 0
1076 0 $ret .= " && " . $exp->output_perl;
1077 } # end while
1078 0 return($ret);
1079 } # end sub Language::Basic::Expression::Logical_And::output_perl
1080  
1081 } # end package Language::Basic::Expression::Logical_And
1082  
1083 =item Relational
1084  
1085 A relational expression, like "A>B+C", optionally with a NOT in front of it.
1086  
1087 =cut
1088  
1089 {
1090 16 package Language::Basic::Expression::Relational;
1091 16 @Language::Basic::Expression::Relational::ISA =
1092 qw(Language::Basic::Expression::Boolean);
1093 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
1094  
1095 # Usually, an LBE::Relational is just LBE::Arithmetic Rel. Op. LBE::Arithmetic
1096 # However, if the first sub-expression in the LBE::Relational is parenthesized,
1097 # it could be either
1098 # (1) (Logical Or Exp.) --- E.g. IF (A>B OR C>D) THEN...
1099 # (2) (Arith. Exp.) --- E.g. IF (A+1)>B THEN...
1100 # So we call the first LBE::Arithmetic::new with "maybe_boolean", so that
1101 # it knows it may find a Boolean sub-expression
1102 # Note that in case (1), we don't need to look for a Rel. Op., because
1103 # IF (A>B OR C>D) > 2 is illegal.
1104 #
1105 # Rel. Exp. usually has two expressions in the "expressions" field, and
1106 # an operator in the "operator" field. However, in case (1) above, there will
1107 # only be one (Boolean) expression, and no op.
1108  
1109 48 ▶sub new {
Subroutine new: called CC=10 SCAR=23.7
1110 48 my ($class, $token_group) = (shift, shift);
1111 48 my $self = {};
1112 48 my $maybe_arithmetic = shift;
1113 48 ▶ if (defined($maybe_arithmetic) && $maybe_arithmetic ne "maybe_arithmetic") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic") 0 48 48
Condition66%2/3
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
!ll&&!rl&&r
4080
MC/DC0%0/2
defined $maybe_arithmetic and $maybe_arithmetic ne "maybe_arithmetic"
defined $maybe_arithmetic $maybe_arithmetic ne "maybe_arithmetic"
Truth table66%2/3
A and B
A= defined $maybe_arithmeticB= $maybe_arithmetic ne "maybe_arithmetic"
ABresult
0X0
100
111
1114 0 Exit_Error("Internal Error: Weird arg '$maybe_arithmetic' to LBE::Relational::new");
1115 }
1116  
1117 # "NOT" in the expression?
1118 48 $self->{"not"} = defined($token_group->eat_if_string("NOT"));
1119  
1120 48 ▶ my $e = new Language::Basic::Expression::Arithmetic
Branch: 1 branch
BranchTrueFalseTotal
my $e = "Language::Basic::Expression::Arithmetic"->new($token_group, "maybe_boolean") or Exit_Error("Unexpected text at beginning of Rel. Exp.") 48 0 48
1121 ($token_group, "maybe_boolean")
1122 or Exit_Error("Unexpected text at beginning of Rel. Exp.");
1123 48 push @{$self->{"expressions"}}, $e;
1124  
1125 # Did we find a parenthesized Boolean exp? Then just return it.
1126 # Don't even look for a rel. op. since it would be illegal!
1127 48 ▶ if ($e->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->isa("Language::Basic::Expression::Boolean")) 2 46 48
1128 # TODO return $e instead of blessing unless self->not?
1129 2 bless $self, $class;
1130 2 return $self;
1131 }
1132  
1133 # Read the Rel. Op.
1134 46 my $tok;
1135 46 ▶ if (!defined ($tok = $token_group->eat_if_class("Relational_Operator"))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($tok = $token_group->eat_if_class("Relational_Operator"))) 6 40 46
1136 # Found a parenthesized Arithmetic Exp.?
1137 6 ▶ if ($maybe_arithmetic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_arithmetic) { } 6 0 6
1138 6 return $e; # Don't bother blessing & returning $self
1139 } else {
1140 0 Exit_Error("Syntax Error: No Relational Operator in Rel. Exp.");
1141 }
1142 }
1143  
1144 40 my $op = $tok->text;
1145  
1146 # Note: $e2 isn't allowed to be arithmetic, so no maybe_arithmetic arg
1147 40 ▶ my $e2 = new Language::Basic::Expression::Arithmetic $token_group or
Branch: 1 branch
BranchTrueFalseTotal
my $e2 = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Unexpected text in Rel. Exp. after '${op}'") 40 0 40
1148 Exit_Error("Unexpected text in Rel. Exp. after '$op'");
1149 40 push @{$self->{"expressions"}}, $e2;
1150  
1151 # Convert BASIC ops to perlops
1152 40 my $num_op = {
1153 "=" => "==",
1154 ">" => ">",
1155 "<" => "<",
1156 ">=" => ">=",
1157 "<=" => "<=",
1158 "<>" => "!=",
1159 };
1160 40 my $string_op = {
1161 "=" => "eq",
1162 ">" => "gt",
1163 "<" => "lt",
1164 ">=" => "ge",
1165 "<=" => "le",
1166 "<>" => "ne",
1167 };
1168 40 ▶ my $trans = ($e->isa("Language::Basic::Expression::String")
Branch: 1 branch
BranchTrueFalseTotal
$e->isa('Language::Basic::Expression::String') ? : 0 40 40
1169 ? $string_op : $num_op);
1170 40 ▶ my $perlop = $trans->{$op} or Exit_Error("Unrecognized Rel. op. '$op'");
Branch: 1 branch
BranchTrueFalseTotal
my $perlop = $trans->{$op} or Exit_Error("Unrecognized Rel. op. '${op}'") 40 0 40
1171 40 $self->{"operator"} = $perlop;
1172 40 bless $self, $class;
1173 } # end sub Language::Basic::Expression::Relational::new
1174  
1175 38 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.0
1176 # If this Rel. Exp. has a nested Boolean Exp. inside it, then just
1177 # evaluate that (and NOT it if nec.)
1178 # Otherwise, evaluate the two sides of the Rel. Exp. (each is non-Boolean
1179 # exp -- either they're both arithmetic or they're both string) and
1180 # compare them.
1181 38 my $self = shift;
1182  
1183 38 my @exps = @{$self->{"expressions"}};
1184 38 my $exp = shift @exps;
1185 38 my $e = $exp->evaluate;
1186 38 my $value;
1187 38 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $exp->isa('Language::Basic::Expression::Boolean')) { } 36 2 38
1188 36 my $exp2 = shift @exps;
1189 36 my $e2 = $exp2->evaluate;
1190  
1191 36 my $perlop = $self->{"operator"};
1192 # I'm vainly hoping that Perl eval will get the same result BASIC would
1193 # Need to use \Q in case we say IF A$ = "\", which should really compare
1194 # with \\.
1195 36 my $perlexp = "\"\Q$e\E\" " . $perlop . " \"\Q$e2\E\"";
1196 36 $value = eval $perlexp;
1197 #print "exp is '$perlexp', value is '$value'\n";
1198  
1199 } else { # exp has a nested Boolean Exp. in it. There is no exp2
1200 2 $value = $e;
1201 }
1202
1203 38 ▶ $value = !$value if $self->{"not"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"not"} 4 34 38
1204 38 return $value;
1205 } # end sub Language::Basic::Expression::Relational::evaluate
1206  
1207 0 ▶sub output_perl {
Subroutine output_perl: not called CC=4 SCAR=30.0
1208 0 my $self = shift;
1209 0 my @exps = @{$self->{"expressions"}};
1210 0 my $exp = shift @exps;
1211 0 my $e = $exp->output_perl;
1212  
1213 0 my $ret;
1214 # "Normal" Rel. Exp., or nested Boolean exp.?
1215 0 ▶ if (! $exp->isa("Language::Basic::Expression::Boolean")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $exp->isa('Language::Basic::Expression::Boolean')) { } 0 0 0
1216 0 my $exp2 = shift @exps;
1217 0 my $e2 = $exp2->output_perl;
1218  
1219 0 my $perlop = $self->{"operator"};
1220 0 $ret = join(" ",$e, $perlop, $e2);
1221 } else {
1222 0 $ret = $e;
1223 }
1224  
1225 0 ▶ if ($self->{"not"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"not"}) 0 0 0
1226 # Don't add parens if it's already paren'd
1227 0 ▶ $ret = "(" . $ret . ")" unless $ret =~ /^\(.*\)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret =~ /^\(.*\)$/ 0 0 0
1228 0 $ret = "!" . $ret;
1229 }
1230  
1231 0 return($ret);
1232 } # end sub Language::Basic::Expression::Relational::output_perl
1233  
1234 } # end package Language::Basic::Expression::Relational
1235  
1236 {
1237 # set ISA for "return type" classes
1238 16 package Language::Basic::Expression::Numeric;
1239 16 @Language::Basic::Expression::Numeric::ISA = qw
1240 (Language::Basic::Expression Language::Basic::Numeric);
1241 package Language::Basic::Expression::String;
1242 16 @Language::Basic::Expression::String::ISA = qw
1243 (Language::Basic::Expression Language::Basic::String);
1244 package Language::Basic::Expression::Boolean;
1245 16 @Language::Basic::Expression::Boolean::ISA = qw
1246 (Language::Basic::Expression Language::Basic::Boolean);
1247 }
1248  
1249 =pod
1250  
1251 =back
1252  
1253 =cut
1254  
1255 16 1; # end package Language::Basic::Expression
« lib/Language/Basic/Statement.pm ↑ Summary lib/Language/Basic/Function.pm »