lib/Language/Basic/Statement.pm

filter: Statementstmt 59.2% 317 / 535 Branchbran 47.0% 80 / 170 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 73.0% 46 / 63 Podpod 0.0% 0 / 6 totaltotal 56.0% 450 / 803 CC 97 SCAR 23.0
CC
97
Cov
56%
output_perl
56.0
parse
30.0
output_perl
20.0
CRAP 9.9
« lib/Language/Basic.pm ↑ Summary lib/Language/Basic/Expression.pm »
1 package Language::Basic::Statement;
2  
3 # Part of Language::Basic by Amir Karger (See Basic.pm for details)
4  
5 =pod
6  
7 =head1 NAME
8  
9 Language::Basic::Statement - Package to handle parsing and implementing single
10 BASIC statements.
11  
12 =head1 SYNOPSIS
13  
14 See L<Language::Basic> for the overview of how the Language::Basic module
15 works. This pod page is more technical.
16  
17 A Statement is something like 'GOTO 20' or 'PRINT "HELLO"'. A line of
18 BASIC code is made up of one or more Statements.
19  
20 # Create the statement from an LB::Token::Group and
21 # bless it to an LBS::* subclass
22 my $statement = new Language::Basic::Statement $token_group;
23 $statement->parse; # Parse the statement
24 $statement->implement; # Implement the statement
25  
26 # Return a string containing the Perl equivalent of the statement
27 $str = $statement->output_perl;
28  
29 =head1 DESCRIPTION
30  
31 Take a program like:
32  
33 5 LET A = 2
34  
35 10 IF A >= 3 THEN GOTO 20 ELSE PRINT "IT'S SMALLER"
36  
37 Line 5 has just one statement. Line 10 actually contains three. The first
38 is an IF statement, but the results of the THEN and the ELSE are entire
39 statements in themselves.
40  
41 Each type of statement in BASIC has an associated LB::Statement class.
42 For example, there's LB::Statement::Let and LB::Statement::If. (But no
43 LB::Statement::Then! Instead the "then" field of the LB::Statement::If
44 object will point to another statement. In the above program, it would
45 point to a LB::Statement::Goto.)
46  
47 Parsing a line of BASIC starts with removing the line number and lexing
48 the line, breaking it into Tokens which are held in an LB::Token::Group.
49 LB::Statement::new, refine, and parse, are all called with a Token::Group
50 argument. These methods gradually "eat" their way through the Tokens.
51  
52 LBS::new simply creates an LBS object. However, it then calls LBS::refine,
53 which looks at the first Token of the command and blesses the object to
54 the correct LBS::* subclass.
55  
56 Each LBS subclass then has (at least) the methods parse, implement,
57 and output_perl.
58  
59 The parse method goes through the text and digests it and sets various
60 fields in the object, which are used by implement and output_perl. The
61 implement method actually implements the BASIC command. The
62 output_perl method returns a string (with ; but not \n at the end) of the Perl
63 equivalent of the BASIC statement.
64  
65 =cut
66  
67 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 # sub-packages
71 {
72 16 package Language::Basic::Statement::Data;
73 package Language::Basic::Statement::Def;
74 package Language::Basic::Statement::Dim;
75 package Language::Basic::Statement::End;
76 package Language::Basic::Statement::For;
77 package Language::Basic::Statement::Gosub;
78 package Language::Basic::Statement::Goto;
79 package Language::Basic::Statement::If;
80 package Language::Basic::Statement::Input;
81 package Language::Basic::Statement::Let;
82 package Language::Basic::Statement::Next;
83 package Language::Basic::Statement::On;
84 package Language::Basic::Statement::Print;
85 package Language::Basic::Statement::Read;
86 package Language::Basic::Statement::Rem;
87 package Language::Basic::Statement::Return;
88 }
89  
90 # Note: This sub first blesses itself to be class LB::Statement, but then
91 # class LB::Statement::refine, which blesses the object to a subclass
92 # depending on what sort of statement it is. The refined object is returned.
93 #
94 # Fields:
95 # next_statement - reference to next Statment on this Line. (or undef)
96 # Note that next doesn't point to an If's Then/Else sub-statements
97 #
98 # lvalue - an LB::Expression::Lvalue object, which represents an
99 # expression like X or AR(3+Q), which can be on the left hand
100 # side of an assignment statement
101 # expression - an LB::Expression:: subclass (e.g., Arithmetic or
102 # Relational.) Sometimes there are multiple expressions.
103 193 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
104 193 my $class = shift;
105 193 my $token_group = shift;
106 193 my $line_num_ok = shift;
107 193 my $self = {
108 "next_statement" => undef,
109 "line_number" => undef,
110 };
111  
112 193 bless $self, $class;
113 193 $self->refine( $token_group, $line_num_ok );
114 } # end sub Language::Basic::Statement::new
115  
116 # Refine LB::Statement to the correct subclass
117 # I.e., Read the command this statement starts with, and bless the
118 # Statement to be a new subclass
119 193 ▶sub refine {
Subroutine refine: called CC=11 SCAR=25.2
Pod undocumented
120 193 my $self = shift;
121 193 my $token_group = shift;
122 193 my $line_num_ok = shift;
123 193 ▶ die "LBS::refine called with weird arg $line_num_ok" if
Branch: 1 branch
BranchTrueFalseTotal
if defined $line_num_ok and $line_num_ok ne "line_num_ok" 0 193 193
Condition66%2/3
defined $line_num_ok and $line_num_ok ne "line_num_ok"
!ll&&!rl&&r
157360
MC/DC0%0/2
defined $line_num_ok and $line_num_ok ne "line_num_ok"
defined $line_num_ok $line_num_ok ne "line_num_ok"
Truth table66%2/3
A and B
A= defined $line_num_okB= $line_num_ok ne "line_num_ok"
ABresult
0X0
100
111
124 defined $line_num_ok && $line_num_ok ne "line_num_ok";
125  
126 # Valid BASIC statements
127 16 ▶ use constant KEYWORDS =>
Subroutine BEGIN: called CC=1 SCAR=0.0
128 qw(DATA DEF DIM END FOR GOSUB GOTO IF INPUT
129 16 LET NEXT ON PRINT READ REM RETURN);
130 # TODO In theory, this would let us make STOP exactly synonymous
131 # with END, or CLEAR synonymous with CLS, etc.
132 193 my %keywords = map {$_, ucfirst(lc($_))} (KEYWORDS);
133  
134 # First word is a command, or a variable (implied LET statment)
135 193 my $tok = $token_group->lookahead;
136 193 ▶ Exit_Error("Empty statement?!") unless defined $tok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tok 0 193 193
137 193 my $command;
138 193 (my $class = ref($tok)) =~ s/^Language::Basic::Token:://;
139 193 ▶ if ($class eq "Keyword") {
Branch: 4 branches
BranchTrueFalseTotal
if ($class eq 'Keyword') { } 171 22 193
elsif ($class eq 'Comment') { } 6 16 22
elsif ($class eq 'Identifier') { } 14 2 16
elsif ($line_num_ok and $class eq 'Numeric_Constant' and $tok->text =~ /^\d+$/) { } 2 0 2
140 171 my $text = $tok->text;
141 171 ▶ if (exists $keywords{$text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $keywords{$text}) { } 171 0 171
142 171 $token_group->eat;
143 171 $command = $keywords{$text};
144 } else {
145 # Statement started with, e.g., "TO" or "ELSE"
146 0 Exit_Error("Illegal reserved word '$text' at start of statement");
147 }
148  
149 } elsif ($class eq "Comment") {
150 6 $command = "Rem";
151 } elsif ($class eq "Identifier") {
152 14 $command = "Let";
153 # If we're in a THEN or ELSE, a line number means GOTO that line
154 ▶ } elsif ($line_num_ok &&
Condition33%2/6
$line_num_ok and $class eq "Numeric_Constant" and $tok->text =~ /^\d+$/
!ll&&!rl&&r
002
$line_num_ok and $class eq "Numeric_Constant"
!ll&&!rl&&r
002
MC/DC0%0/3
$line_num_ok and $class eq "Numeric_Constant" and $tok->text =~ /^\d+$/
$line_num_ok $class eq "Numeric_Constant" $tok->text =~ /^\d+$/
Truth table25%1/4
A and B and C
A= $line_num_okB= $class eq "Numeric_Constant"C= $tok->text =~ /^\d+$/
ABCresult
0XX0
10X0
1100
1111
155 $class eq "Numeric_Constant" &&
156 $tok->text =~ /^\d+$/) {
157 2 $command = "Goto";
158 } else {
159 0 Exit_Error("Syntax Error: No Keyword or Identifier at start of statement!");
160 }
161 193 my $subclass = "Language::Basic::Statement::" . $command;
162 #print "New $subclass Statement\n";
163  
164 193 bless $self, $subclass;
165 } # end sub Language::Basic::Statement::refine
166  
167 # By default, parsing does nothing. Useful, e.g., for REM
168 19 ▶sub parse { }
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
169  
170 # By default, implementing does nothing. Useful, e.g., for REM
171 14 ▶sub implement { }
Subroutine implement: called CC=1 SCAR=0.0
Pod undocumented
172  
173 # By default, output an empty statement. Note that you need the semicolon,
174 # because we write a line label for each line.
175 0 ▶sub output_perl {return ";";}
Subroutine output_perl: not called CC=1 SCAR=6.9
Pod undocumented
176  
177 165 ▶sub set_line_number {
Subroutine set_line_number: called CC=1 SCAR=0.0
Pod undocumented
178 165 my $self = shift;
179 165 my $num = shift;
180 165 $self->{"line_number"} = $num;
181 }
182  
183 ######################################################################
184 # package Language::Basic::Statement::Data
185 # A DATA statement in a BASIC program.
186 {
187 package Language::Basic::Statement::Data;
188 16 @Language::Basic::Statement::Data::ISA = qw(Language::Basic::Statement);
189  
190 5 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
191 5 my $self = shift;
192 5 my $token_group = shift;
193 5 my $prog = &Language::Basic::Program::current_program;
194  
195 # The rest of the statement is things to dim and how big to dim them
196 5 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(",") and do { do { my $exp = "Language::Basic::Expression::Constant"->new($token_group); $prog->add_data($exp) } } 18 5 23
197 23 my $exp = new Language::Basic::Expression::Constant $token_group;
198 23 $prog->add_data($exp);
199 } while ($token_group->eat_if_string(","));
200 } # end sub Language::Basic::Statement::Data::parse
201  
202 # no sub implement nec.
203 # no sub output_perl nec.
204  
205 } # end package Language::Basic::Statement::Data
206  
207 ######################################################################
208 # package Language::Basic::Statement::Def
209 # A DEF statement in a BASIC program.
210 {
211 16 package Language::Basic::Statement::Def;
212 16 @Language::Basic::Statement::Def::ISA = qw(Language::Basic::Statement);
213 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 3 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.2
216 3 my $self = shift;
217 3 my $token_group = shift;
218  
219 # Function name (and args) is stuff up to equals
220 # Call LBE::Function::new with extra argument so it knows not to
221 # complain about an unknown function.
222 3 ▶ my $funcexp = new Language::Basic::Expression::Function
Branch: 1 branch
BranchTrueFalseTotal
my $funcexp = "Language::Basic::Expression::Function"->new($token_group, "defining") or Exit_Error("Missing/Bad Function Name or Args in DEF!") 3 0 3
223 ($token_group, "defining")
224 or Exit_Error("Missing/Bad Function Name or Args in DEF!");
225 3 ▶ $token_group->eat_if_string("=") or Exit_Error("DEF missing '='!");
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string("=") or Exit_Error("DEF missing '='!") 3 0 3
226  
227 # We don't actually want the LB::Expression, just the function
228 # we've declared.
229 3 my $func = $funcexp->{"function"};
230  
231 # Read function definition
232 3 ▶ my $exp = new Language::Basic::Expression::Arithmetic $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Missing/Bad function definition in DEF!") 3 0 3
233 or Exit_Error("Missing/Bad function definition in DEF!");
234  
235 # Now actually define the function
236 3 $func->define($exp);
237  
238 3 $self->{"function"} = $func;
239 # TODO note that output_perl may not work now
240 } # end sub Language::Basic::Statement::Def::parse
241  
242 # No sub implement: definition happens at compile time
243  
244 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
245 0 my $self = shift;
246 0 my $prog = &Language::Basic::Program::current_program;
247 # LB::Function::Defined object
248 0 my $func = $self->{"function"};
249  
250 # Function name
251 0 my $name = $func->output_perl;
252 0 my $desc = "{\n";
253 0 $desc .= "INDENT\n";
254  
255 # Function args
256 0 $desc .= "my (";
257 0 my @args = map {$_->output_perl} @{$func->{"arguments"}};
258 0 $desc .= join (", ", @args);
259 0 $desc .= ") = \@_;\n";
260  
261 # Function def
262 0 my $exp = $func->{"expression"}->output_perl;
263 0 $desc .= "return " . $exp . ";\n";
264 0 $desc .= "UNINDENT\n}";
265 # Tell program to print it out at the end of the perl script
266 0 $prog->need_sub($name, $desc);
267  
268 0 return (";"); # put empty statement in program here
269 } # end sub Language::Basic::Statement::Def::output_perl
270  
271 } # end package Language::Basic::Statement::Def
272  
273 ######################################################################
274 # package Language::Basic::Statement::Dim
275 # A DIM statement in a BASIC program.
276 {
277 16 package Language::Basic::Statement::Dim;
278 16 @Language::Basic::Statement::Dim::ISA = qw(Language::Basic::Statement);
279  
280 2 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
281 2 my $self = shift;
282 2 my $token_group = shift;
283  
284 # The rest of the statement is things to dim and how big to dim them
285 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(",") and do { do { my $exp = "Language::Basic::Expression::Lvalue"->new($token_group); push @{$$self{"arrays"};}, $exp } } 1 2 3
286 3 my $exp = new Language::Basic::Expression::Lvalue $token_group;
287 3 push @{$self->{"arrays"}}, $exp;
288 # TODO test that dims are constants!
289 } while ($token_group->eat_if_string(","));
290 } # end sub Language::Basic::Statement::Dim::parse
291  
292 2 ▶sub implement {
Subroutine implement: called CC=2 SCAR=6.9
293 2 my $self = shift;
294 2 foreach (@{$self->{"arrays"}}) {
295 # The Lvalue's Array
296 3 my $array = $_->{"varptr"};
297 3 my @indices = $_->{"arglist"}->evaluate;
298 3 $array->dimension(@indices);
299 }
300 } # end sub Language::Basic::Statement::Dim::implement
301  
302 # no sub output_perl necessary
303  
304 } # end package Language::Basic::Statement::Dim
305  
306 ######################################################################
307 # package Language::Basic::Statement::End
308 # An END statement in a BASIC program.
309 {
310 16 package Language::Basic::Statement::End;
311 16 @Language::Basic::Statement::End::ISA = qw(Language::Basic::Statement);
312 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
313  
314 7 ▶sub implement {
Subroutine implement: called CC=1 SCAR=0.0
315 7 my $prog = &Language::Basic::Program::current_program;
316 7 $prog->goto_line(undef);
317 } # end sub Language::Basic::Statement::End::implement
318  
319 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
320 0 return ("exit;");
321 } # end sub Language::Basic::Statement::End::output_perl
322  
323 } # end package Language::Basic::Statement::End
324  
325 ######################################################################
326 # package Language::Basic::Statement::For
327 # A FOR statement in a BASIC program.
328 {
329 16 package Language::Basic::Statement::For;
330 16 @Language::Basic::Statement::For::ISA = qw(Language::Basic::Statement);
331 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
332  
333 10 ▶sub parse {
Subroutine parse: called CC=10 SCAR=24.5
334 10 my $self = shift;
335 10 my $token_group = shift;
336  
337 # Read variable name and "="
338 10 ▶ my $lvalue = new Language::Basic::Expression::Lvalue $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $lvalue = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Missing variable in FOR!") 10 0 10
339 or Exit_Error("Missing variable in FOR!");
340 # No strings allowed, at least for now
341 10 ▶ if ($lvalue->isa("Language::Basic::Expression::String")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lvalue->isa("Language::Basic::Expression::String")) 0 10 10
342 0 Exit_Error("FOR statements can't use strings!");
343 }
344 10 $self->{"lvalue"} = $lvalue;
345  
346 # Read initialization value
347 10 ▶ $token_group->eat_if_string("=") or Exit_Error("FOR missing '='!");
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string("=") or Exit_Error("FOR missing '='!") 10 0 10
348 10 ▶ $self->{"start"} =
Branch: 1 branch
BranchTrueFalseTotal
$self->{"start"} = "Language::Basic::Expression::Arithmetic::Numeric"->new($token_group) or Exit_Error("Missing/Bad initialization expression in FOR!") 10 0 10
349 new Language::Basic::Expression::Arithmetic::Numeric $token_group
350 or Exit_Error("Missing/Bad initialization expression in FOR!");
351 10 ▶ $token_group->eat_if_string("TO") or Exit_Error("FOR missing 'TO'!");
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string("TO") or Exit_Error("FOR missing 'TO'!") 10 0 10
352  
353 # Until the token "step" OR the end of the statement, we're copying an
354 # expression, namely the variable's increment
355 10 ▶ $self->{"limit"} =
Branch: 1 branch
BranchTrueFalseTotal
$self->{"limit"} = "Language::Basic::Expression::Arithmetic::Numeric"->new($token_group) or Exit_Error("Missing/Bad limit expression in FOR!") 10 0 10
356 new Language::Basic::Expression::Arithmetic::Numeric $token_group
357 or Exit_Error("Missing/Bad limit expression in FOR!");
358  
359 # If there's anything left, it had better be a step...
360 # Otherwise, step = 1
361 10 my $step;
362 10 ▶ if ($token_group->eat_if_string("STEP")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_group->eat_if_string('STEP')) { } 2 8 10
363 2 ▶ $step = new Language::Basic::Expression::Arithmetic::Numeric
Branch: 1 branch
BranchTrueFalseTotal
$step = "Language::Basic::Expression::Arithmetic::Numeric"->new($token_group) or Exit_Error("Missing/Bad step expression in FOR!") 2 0 2
364 $token_group
365 or Exit_Error("Missing/Bad step expression in FOR!");
366 } else {
367 8 ▶ Exit_Error("Unknown stuff after limit expression in FOR!")
Branch: 1 branch
BranchTrueFalseTotal
if $token_group->stuff_left 0 8 8
368 if $token_group->stuff_left;
369 8 my $foo = new Language::Basic::Token::Group;
370 8 $foo->lex("1");
371 8 $step = new Language::Basic::Expression::Arithmetic::Numeric $foo;
372 }
373 10 $self->{"step"} = $step;
374 } # end sub Language::Basic::Statement::For::parse
375  
376 12 ▶sub implement {
Subroutine implement: called CC=1 SCAR=0.0
377 # TODO BASIC doesn't check for start being greater than limit
378 # before doing a loop once. Might want to make a flag to do it.
379 12 my $self = shift;
380 12 my $prog = &Language::Basic::Program::current_program;
381 12 my $lvalue = $self->{"lvalue"};
382 12 my $var = $lvalue->variable;
383 12 $var->set($self->{"start"}->evaluate);
384 # Store this FOR statement, so that we can access it when we
385 # get to "NEXT var"
386 12 $prog->store_for($self);
387 } # end sub Language::Basic::Statement::For::implement
388  
389 # Outputs $var = start; and the beginning of a do {}
390 # We also have to set the step here, because we need to test in the loop
391 # whether it's positive or negative so we can know whether to test for
392 # being greater than or less than the limit!
393 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
394 0 my $self = shift;
395 # print var = start
396 0 my $lvalue = $self->{"lvalue"}->output_perl;
397 0 my $exp = $self->{"start"}->output_perl;
398 0 my $ret = join(" ", $lvalue, "=", $exp);
399 0 $ret .= ";\n";
400  
401 # set the step
402 0 my $step = $self->{"step"}->output_perl;
403 0 $lvalue =~ /\w+/;
404 0 my $vname = $&;
405 0 $ret .= join(" ", "\$step_for_$vname =", $step);
406 0 $ret .= ";\n";
407  
408 # set the limit
409 0 my $limit = $self->{"limit"}->output_perl;
410 0 $ret .= join(" ", "\$limit_for_$vname =", $limit);
411 0 $ret .= ";\n";
412  
413 # Now start the do loop
414 0 $ret .= "do {";
415 0 $ret .= "\nINDENT";
416 0 return $ret;
417 } # end sub Language::Basic::Statement::For::output_perl
418  
419 } # end package Language::Basic::Statement::For
420  
421 ######################################################################
422 # package Language::Basic::Statement::Gosub
423 # A GOSUB statement in a BASIC program.
424 {
425 16 package Language::Basic::Statement::Gosub;
426 16 @Language::Basic::Statement::Gosub::ISA = qw(Language::Basic::Statement);
427 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
428  
429 5 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.1
430 5 my $self = shift;
431 5 my $token_group = shift;
432  
433 # The rest of the statement is an expression for the line to go to
434 5 ▶ $self->{"expression"} = new Language::Basic::Expression::Arithmetic $token_group
Branch: 1 branch
BranchTrueFalseTotal
$self->{"expression"} = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Bad expression in GOSUB!") 5 0 5
435 or Exit_Error("Bad expression in GOSUB!");
436 } # end sub Language::Basic::Statement::Gosub::parse
437  
438 7 ▶sub implement {
Subroutine implement: called CC=2 SCAR=7.1
439 7 my $self = shift;
440 7 my $prog = &Language::Basic::Program::current_program;
441 7 my $goto = $self->{"expression"}->evaluate;
442 0 ▶ if ($goto !~ /^\d+$/) {Exit_Error("Bad GOSUB: $goto")}
Branch: 1 branch
BranchTrueFalseTotal
unless ($goto =~ /^\d+$/) 0 7 7
443 # Push the current statement onto the subroutine stack;
444 7 $prog->push_stack($self);
445 # Then GOTO the new line
446 7 $prog->goto_line($goto);
447 } # end sub Language::Basic::Statement::Gosub::implement
448  
449 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
450 # Perl script should print a label after the gosub. But before that,
451 # it pushes the label name onto the global gosub stack. THen when
452 # we hit the RETURN, we can pop the stack & goto back to this lable.
453 0 my $self = shift;
454 0 my $prog = &Language::Basic::Program::current_program;
455 0 my $exp = $self->{"expression"};
456 0 my $goto = $exp->output_perl;
457 0 my $ret = "";
458  
459 # Form the label name to return to
460 0 my $label = "AL" . $prog->current_line_number;
461 0 $ret .= "push \@Gosub_Stack, \"$label\";\n";
462  
463 # Form the label name to goto
464 # if it's just a number , don't use $tmp
465 0 ▶ if ($goto =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($goto =~ /^\d+$/) { } 0 0 0
466 0 $ret .= "goto L$goto;";
467 } else {
468 # Form the label name
469 0 $ret .= "\$Gosub_tmp = 'L' . " . $goto . ";\n";
470 # Go to it
471 0 $ret .= "goto \$Gosub_tmp;";
472 }
473  
474 # Write the return-to label after the goto
475 0 $ret .= "\n$label:;";
476  
477 0 return ($ret);
478 } # end sub Language::Basic::Statement::Gosub::output_perl
479 } # end package Language::Basic::Statement::Gosub
480  
481 ######################################################################
482 # package Language::Basic::Statement::Goto
483 # A GOTO statement in a BASIC program.
484 {
485 16 package Language::Basic::Statement::Goto;
486 16 @Language::Basic::Statement::Goto::ISA = qw(Language::Basic::Statement);
487 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
488  
489 5 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.1
490 5 my $self = shift;
491 5 my $token_group = shift;
492  
493 # The rest of the statement is an expression for the line to go to
494 5 ▶ $self->{"expression"} = new Language::Basic::Expression::Arithmetic $token_group
Branch: 1 branch
BranchTrueFalseTotal
$self->{"expression"} = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Bad expression in GOTO!") 5 0 5
495 or Exit_Error("Bad expression in GOTO!");
496 } # end sub Language::Basic::Statement::Goto::parse
497  
498 # Note that this sub allows "GOTO X+17/3", not just "GOTO 20"
499 3 ▶sub implement {
Subroutine implement: called CC=2 SCAR=7.2
500 3 my $self = shift;
501 3 my $prog = &Language::Basic::Program::current_program;
502 3 my $goto = $self->{"expression"}->evaluate;
503 0 ▶ if ($goto !~ /^\d+$/) {Exit_Error("Bad GOTO: $goto")}
Branch: 1 branch
BranchTrueFalseTotal
unless ($goto =~ /^\d+$/) 0 3 3
504 3 $prog->goto_line($goto);
505 } # end sub Language::Basic::Statement::Goto::implement
506  
507 0 ▶sub output_perl {
Subroutine output_perl: not called CC=2 SCAR=17.9
508 0 my $self = shift;
509 # if it's just a number , don't use $tmp
510 0 my $exp = $self->{"expression"};
511 0 my $goto = $exp->output_perl;
512 0 my $ret;
513 0 ▶ if ($goto =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($goto =~ /^\d+$/) { } 0 0 0
514 0 $ret = "goto L$goto;";
515 } else {
516 # Form the label name
517 0 $ret = "\$Goto_tmp = 'L' . " . $goto . ";\n";
518 # Go to it
519 0 $ret .= "goto \$Goto_tmp;";
520 }
521  
522 0 return ($ret);
523 } # end sub Language::Basic::Statement::Goto::output_perl
524 } # end package Language::Basic::Statement::Goto
525  
526 ######################################################################
527 # package Language::Basic::Statement::If
528 # An IF statement in a BASIC program.
529 {
530 16 package Language::Basic::Statement::If;
531 16 @Language::Basic::Statement::If::ISA = qw(Language::Basic::Statement);
532 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
533  
534 28 ▶sub parse {
Subroutine parse: called CC=10 SCAR=26.0
535 28 my $self = shift;
536 28 my $token_group = shift;
537  
538 # Until the token "then", we're copying a conditional expression
539 28 ▶ my $exp = new Language::Basic::Expression::Logical_Or $token_group or
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Logical_Or"->new($token_group) or Exit_Error("Bad Condition in IF!") 28 0 28
540 Exit_Error("Bad Condition in IF!");
541 28 $self->{"condition"} = $exp;
542 28 ▶ $token_group->eat_if_string("THEN") or Exit_Error("IF missing 'THEN'!");
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string("THEN") or Exit_Error("IF missing 'THEN'!") 28 0 28
543  
544 # Until the token "ELSE" or the end of the line, is one or more
545 # statements to do if the IF is true
546 # TODO we need to handle ELSE either within the same statement
547 # as the last THEN statement *OR* at the beginning of a statement.
548 # Also nested IFs?
549  
550 # Take everything up to ELSE into a separate Token::Group &
551 # call parsing with that so that other parse routines can complain if
552 # there's something left in their token_group. Right now, they'll have
553 # problem with ELSE token
554 # TODO need a Token::Group::split method or some such
555 28 my $t1 = new Language::Basic::Token::Group;
556 28 $t1->slurp($token_group, "ELSE");
557  
558 # Call new with an extra arg so it knows it's parsing a THEN/ELSE.
559 # That way, "THEN 20" gets parsed like "THEN GOTO 20"
560 28 ▶ my $then = new Language::Basic::Statement $t1, "line_num_ok" or
Branch: 1 branch
BranchTrueFalseTotal
my $then = "Language::Basic::Statement"->new($t1, "line_num_ok") or Exit_Error("No statement found after THEN") 28 0 28
561 Exit_Error("No statement found after THEN");
562 28 $then->parse($t1);
563 28 my $oldst = $then;
564 # Eat [: Statement]*
565 28 ▶ while (defined($t1->eat_if_class("Statement_End"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t1->eat_if_class("Statement_End")) 0 28 28
566 # Plain line number is only allowed in the *first* THEN/ELSE statement
567 0 my $st = new Language::Basic::Statement $t1;
568 0 $st->parse($t1);
569 0 $oldst->{"next_statement"} = $st;
570 0 $oldst = $st;
571 }
572 # Make sure we don't do the ELSE after the THEN!
573 28 $oldst->{"next_statement"} = undef;
574  
575 # If there's anything left in $token_group, it's the ELSE.
576 28 my $else;
577 28 ▶ if (defined($token_group->eat_if_string("ELSE"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_group->eat_if_string('ELSE')) { } 8 20 28
578 # Use up all the leftover tokens
579 8 ▶ $else = new Language::Basic::Statement $token_group, "line_num_ok" or
Branch: 1 branch
BranchTrueFalseTotal
$else = "Language::Basic::Statement"->new($token_group, "line_num_ok") or Exit_Error("No statement found after THEN") 8 0 8
580 Exit_Error("No statement found after THEN");
581 8 $else->parse ($token_group);
582 8 $oldst = $else;
583 8 ▶ while (defined($token_group->eat_if_class("Statement_End"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_group->eat_if_class("Statement_End")) 0 8 8
584 0 my $st = new Language::Basic::Statement $token_group;
585 0 $st->parse($token_group);
586 0 $oldst->{"next_statement"} = $st;
587 0 $oldst = $st;
588 }
589 8 $oldst->{"next_statement"} = undef;
590 8 ▶ Exit_Error("Unknown stuff after ELSE statement(s)") if
Branch: 1 branch
BranchTrueFalseTotal
if $token_group->stuff_left 0 8 8
591 $token_group->stuff_left;
592 } else {
593 20 ▶ Exit_Error("Unknown stuff after THEN statement(s)") if
Branch: 1 branch
BranchTrueFalseTotal
if $token_group->stuff_left 0 20 20
594 $token_group->stuff_left;
595 }
596  
597 28 $self->{"then_s"} = $then;
598 28 $self->{"else_s"} = $else; # may be undef
599 } # end sub Language::Basic::Statement::If::parse
600  
601 # Need to set line numbers for THEN and ELSE statements, so we can't
602 # use the default LBS::set_line_number
603 28 ▶sub set_line_number {
Subroutine set_line_number: called CC=3 SCAR=11.0
604 28 my $self = shift;
605 28 my $num = shift;
606 28 $self->{"line_number"} = $num;
607 28 foreach ("then_s", "else_s") {
608 56 my $st = $self->{"$_"};
609 56 ▶ while (defined $st) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $st) 36 56 92
610 36 $st->set_line_number($num);
611 36 $st = $st->{"next_statement"};
612 }
613 }
614 }
615  
616 30 ▶sub implement {
Subroutine implement: called CC=2 SCAR=6.9
617 30 my $self = shift;
618 30 my $prog = &Language::Basic::Program::current_program;
619  
620 30 ▶ if ($self->{"condition"}->evaluate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'condition'}->evaluate) { } 21 9 30
621 21 $prog->{"next_statement"} = $self->{"then_s"};
622 } else {
623 # This may be undef, in which case, code will just continue to next line
624 9 $prog->{"next_statement"} = $self->{"else_s"};
625 }
626 } # end sub Language::Basic::Statement::If::implement
627  
628 0 ▶sub output_perl {
Subroutine output_perl: not called CC=4 SCAR=30.0
629 0 my $self = shift;
630 0 my $ret = "if (";
631 0 $ret .= $self->{"condition"}->output_perl;
632 0 $ret .= ") {\n";
633 0 $ret .= "INDENT";
634 0 my $st = $self->{"then_s"};
635 do {
636 0 $ret .= "\n" . $st->output_perl;
637 0 ▶ } while (defined ($st = $st->{"next_statement"}));
Branch: 1 branch
BranchTrueFalseTotal
defined($st = $st->{"next_statement"}) and do { do { $ret .= "\n" . $st->output_perl } } 0 0 0
638  
639 0 ▶ if (defined $self->{"else_s"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"else_s"}) 0 0 0
640 # TODO only double-\n if there's a long THEN
641 0 $ret .= "\n\nUNINDENT";
642 0 $ret .= "\n} else {\n";
643 0 $ret .= "INDENT";
644 0 $st = $self->{"else_s"};
645 do {
646 0 $ret .= "\n" . $st->output_perl;
647 0 ▶ } while (defined ($st = $st->{"next_statement"}));
Branch: 1 branch
BranchTrueFalseTotal
defined($st = $st->{"next_statement"}) and do { do { $ret .= "\n" . $st->output_perl } } 0 0 0
648 }
649 0 $ret .= "\nUNINDENT";
650 0 $ret .= "\n}";
651  
652 0 return ($ret);
653 } # end sub Language::Basic::Statement::If::output_perl
654  
655 } # end package Language::Basic::Statement::If
656  
657 ######################################################################
658 # package Language::Basic::Statement::Input
659 # An INPUT statement in a BASIC program.
660 {
661 16 package Language::Basic::Statement::Input;
662 16 @Language::Basic::Statement::Input::ISA = qw(Language::Basic::Statement);
663 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
664  
665 0 ▶sub parse {
Subroutine parse: not called CC=5 SCAR=34.0
666 0 my $self = shift;
667 0 my $token_group = shift;
668  
669 # Handle INPUT "FOO"; BAR, BLAH
670 # TODO I should really just try to call LBE::Constant::String and not
671 # do anything if it returns undef. But currently that warns that what
672 # we're trying to input isn't a quoted string if there's not quotation
673 # mark.
674 0 ▶ if ($token_group->lookahead->
Branch: 1 branch
BranchTrueFalseTotal
if ($token_group->lookahead->isa("Language::Basic::Token::String_Constant")) 0 0 0
675 isa("Language::Basic::Token::String_Constant")) {
676 0 my $prompt = new
677 Language::Basic::Expression::Constant::String $token_group;
678 0 $self->{"to_print"} = $prompt;
679 0 ▶ $token_group->eat_if_string(";") or
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(";") or Exit_Error("Expected ';' after INPUT prompt!") 0 0 0
680 Exit_Error("Expected ';' after INPUT prompt!");
681 }
682  
683 # The rest of the inputs will be separated by commas
684 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(",") and do { do { my $exp = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Incorrect INPUT!"); push @{$$self{"lvalues"};}, $exp } } 0 0 0
685 0 ▶ my $exp = new Language::Basic::Expression::Lvalue $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Incorrect INPUT!") 0 0 0
686 or Exit_Error("Incorrect INPUT!");
687 0 push @{$self->{"lvalues"}}, $exp;
688 } while $token_group->eat_if_string(",");
689 } # end sub Language::Basic::Statement::Input::parse
690  
691 0 ▶sub implement {
Subroutine implement: not called CC=4 SCAR=30.0
692 0 my $self = shift;
693 TRY_AGAIN:
694 # Print a prompt, if it exists
695 my $to_print = (exists $self->{"to_print"} ?
696 0 ▶ $self->{"to_print"}->evaluate :
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'to_print'} ? : 0 0 0
697 "");
698 0 print "$to_print? ";
699  
700 # TODO set Program's "column" field to zero!
701 # Read the variables
702 0 my $in = <>;
703 0 chomp($in);
704 # TODO read Constants (String or Numeric) followed by commas if nec.
705 # TODO type checking: make sure a string is a string
706 # (this might be done by a different part of the program)
707 # TODO Use "EXTRA IGNORED?" to let user know they need to quote commas?
708 0 my @ins = split(/\s*,\s*/, $in);
709 0 ▶ if (@ins != @{$self->{"lvalues"}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ins != @{$self->{"lvalues"};}) 0 0 0
710 0 print "Not enough inputs! Try whole statement again...\n";
711 # Can't have a BASIC interpreter without a GOTO!
712 0 goto TRY_AGAIN;
713 }
714  
715 # set the variables to the inputted value
716 0 foreach (@{$self->{"lvalues"}}) {
717 0 my $var = $_->variable; # LB::Variable object
718 # TODO Print "??" if they don't input enough.
719 0 my $value = shift @ins;
720 0 $var->set($value);
721 }
722  
723 0 return $self->{"next_statement"};
724 } # end sub Language::Basic::Statement::Input::implement
725  
726 0 ▶sub output_perl {
Subroutine output_perl: not called CC=3 SCAR=24.8
727 0 my $self = shift;
728 # Print the prompt
729 0 my $ret = "print ";
730 0 ▶ if (exists $self->{"to_print"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"to_print"}) 0 0 0
731 0 $ret .= $self->{"to_print"}->output_perl;
732 0 $ret .= " . "; # concat with the ? below
733 }
734 0 $ret .= "\"? \";\n";
735  
736 # Input the line
737 0 $ret .= "\$input_tmp = <>;\n";
738 0 $ret .= "chomp(\$input_tmp);\n";
739  
740 # Set the values
741 0 my @lvalues = map {$_->output_perl} @{$self->{"lvalues"}};
742 0 my $tmp = join(", ", @lvalues);
743 # Make the code a bit simpler for just one input
744 0 my $multi = @lvalues > 1;
745 0 ▶ if ($multi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multi) { } 0 0 0
746 0 $ret .="($tmp) = split(/\\s*,\\s*/, \$input_tmp);";
747 } else {
748 0 $ret .="$tmp = \$input_tmp;";
749 }
750  
751 0 return $ret;
752 } # end sub Language::Basic::Statement::Input::output_perl
753  
754 } # end package Language::Basic::Statement::Input
755  
756 ######################################################################
757 # package Language::Basic::Statement::Let
758 # A LET statement in a BASIC program.
759 {
760 16 package Language::Basic::Statement::Let;
761 16 @Language::Basic::Statement::Let::ISA = qw(Language::Basic::Statement);
762 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
763  
764 15 ▶sub parse {
Subroutine parse: called CC=4 SCAR=14.5
765 15 my $self = shift;
766 15 my $token_group = shift;
767  
768 # Read variable name and "="
769 15 ▶ my $lvalue = new Language::Basic::Expression::Lvalue $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $lvalue = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Missing variable in LET!") 15 0 15
770 or Exit_Error("Missing variable in LET!");
771 15 $self->{"lvalue"} = $lvalue;
772  
773 # The rest of the statement is an expression to set the variable equal to
774 15 ▶ $token_group->eat_if_string("=") or Exit_Error("LET missing '='!");
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string("=") or Exit_Error("LET missing '='!") 15 0 15
775 15 ▶ $self->{"expression"} =
Branch: 1 branch
BranchTrueFalseTotal
$self->{"expression"} = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Missing right side expression in LET!") 15 0 15
776 new Language::Basic::Expression::Arithmetic $token_group
777 or Exit_Error("Missing right side expression in LET!");
778 } # end sub Language::Basic::Statement::Let::parse
779  
780 49 ▶sub implement {
Subroutine implement: called CC=1 SCAR=0.0
781 49 my $self = shift;
782 49 my $lvalue = $self->{"lvalue"};
783 49 my $var = $lvalue->variable;
784 49 my $value = $self->{"expression"}->evaluate;
785 49 $var->set($value);
786  
787 49 return $self->{"next_statement"};
788 } # end sub Language::Basic::Statement::Let::implement
789  
790 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
791 0 my $self = shift;
792 0 my $lvalue = $self->{"lvalue"}->output_perl;
793 0 my $exp = $self->{"expression"}->output_perl;
794 0 my $ret = join(" ", $lvalue, "=", $exp);
795 0 $ret .= ";";
796  
797 0 return ($ret);
798 } # end sub Language::Basic::Statement::Let::output_perl
799  
800 } # end package Language::Basic::Statement::Let
801  
802 ######################################################################
803 # package Language::Basic::Statement::Next
804 # A NEXT statement in a BASIC program.
805 {
806 16 package Language::Basic::Statement::Next;
807 16 @Language::Basic::Statement::Next::ISA = qw(Language::Basic::Statement);
808 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
809  
810 10 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.8
811 10 my $self = shift;
812 10 my $token_group = shift;
813  
814 10 ▶ my $lvalue = new Language::Basic::Expression::Lvalue $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $lvalue = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Incorrect NEXT!") 10 0 10
815 or Exit_Error("Incorrect NEXT!");
816 # No strings allowed, at least for now
817 10 ▶ if ($lvalue->variable->isa("Language::Basic::Variable::String")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lvalue->variable->isa("Language::Basic::Variable::String")) 0 10 10
818 0 Exit_Error("NEXT statements can't use strings!");
819 }
820 10 $self->{"lvalue"} = $lvalue;
821 } # end sub Language::Basic::Statement::Next::parse
822  
823 60 ▶sub implement {
Subroutine implement: called CC=3 SCAR=11.0
824 60 my $self = shift;
825 60 my $prog = &Language::Basic::Program::current_program;
826  
827 # Get the "FOR var" statement that this NEXT refers to.
828 60 my $for_statement = $prog->get_for($self);
829 my ($limit,$step) =
830 60 map {$for_statement->{$_}->evaluate} qw (limit step);
831  
832 # Increment the variable
833 60 my $lvalue = $self->{"lvalue"};
834 60 my $var = $lvalue->variable;
835 60 my $value = $var->value;
836 60 $value += $step;
837 60 $var->set($value);
838 #print "next: '$value' '$limit' '$step' '$goto'\n";
839  
840 #test
841 60 ▶ my $done = ($step > 0 ? $value > $limit : $value < $limit);
Branch: 1 branch
BranchTrueFalseTotal
$step > 0 ? : 54 6 60
842 60 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 48 12 60
843 # Go to the statement *after* the statement the FOR started on
844 48 $prog->goto_after_statement($for_statement);
845 }
846 } # end sub Language::Basic::Statement::Next::implement
847  
848 # Outputs $var increment and end of do{}until block
849 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
850 0 my $self = shift;
851 # Increment variable
852 0 my $lvalue = $self->{"lvalue"};
853 0 my $lv = $lvalue->output_perl;
854 0 $lv =~ /\w+/;
855 0 my $vname = $&;
856 # Note that we add step_for even if it's negative.
857 0 my $ret = join(" ", $lv, "+=", "\$step_for_$vname");
858 0 $ret .= ";\n";
859 0 $ret .= "UNINDENT\n";
860  
861 # End the do {} block
862 0 $ret .= "} ";
863  
864 # test the until
865 0 $ret .= "until (\$step_for_$vname > 0 ? ";
866 0 $ret .= $lv . " > \$limit_for_$vname : " .$lv. " < \$limit_for_$vname);";
867 0 return $ret;
868 } # end sub Language::Basic::Statement::Next::output_perl
869  
870 } # end package Language::Basic::Statement::Next
871  
872 ######################################################################
873 # package Language::Basic::Statement::On
874 # An ON statement in a BASIC program.
875 {
876 16 package Language::Basic::Statement::On;
877 16 @Language::Basic::Statement::On::ISA = qw(Language::Basic::Statement);
878 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
879  
880 2 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.6
881 2 my $self = shift;
882 2 my $token_group = shift;
883  
884 2 ▶ $self->{"expression"} =
Branch: 1 branch
BranchTrueFalseTotal
$self->{"expression"} = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Missing Arith. Exp. in ON!") 2 0 2
885 new Language::Basic::Expression::Arithmetic $token_group
886 or Exit_Error("Missing Arith. Exp. in ON!");
887 # Until the token "GOSUB/GOTO", we're copying an arithmetic expression
888 2 my $tok = $token_group->eat_if_class("Keyword");
889 2 ▶ defined $tok and $tok->text =~ /^GO(SUB|TO)$/
Branch: 1 branch
BranchTrueFalseTotal
defined $tok and $tok->text =~ /^GO(SUB|TO)$/ or Exit_Error("ON missing GOSUB/GOTO!") 2 0 2
Condition33%1/3
defined $tok and $tok->text =~ /^GO(SUB|TO)$/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $tok and $tok->text =~ /^GO(SUB|TO)$/
defined $tok $tok->text =~ /^GO(SUB|TO)$/
Truth table33%1/3
A and B
A= defined $tokB= $tok->text =~ /^GO(SUB|TO)$/
ABresult
0X0
100
111
890 or Exit_Error("ON missing GOSUB/GOTO!");
891 2 my $type = $tok->text;
892 2 $self->{"type"} = $type;
893  
894 # The rest of the inputs will be separated by commas
895 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(",") and do { do { my $exp = "Language::Basic::Expression::Arithmetic::Numeric"->new($token_group) or Exit_Error("Incorrect Expression in ON ... $type!"); push @{$$self{"gotos"};}, $exp } } 4 2 6
896 6 ▶ my $exp =
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Arithmetic::Numeric"->new($token_group) or Exit_Error("Incorrect Expression in ON ... $type!") 6 0 6
897 new Language::Basic::Expression::Arithmetic::Numeric $token_group
898 or Exit_Error("Incorrect Expression in ON ... $type!");
899 6 push @{$self->{"gotos"}}, $exp;
900 } while $token_group->eat_if_string(",");
901 } # end sub Language::Basic::Statement::On::parse
902  
903 6 ▶sub implement {
Subroutine implement: called CC=5 SCAR=17.6
904 6 my $self = shift;
905 6 my $prog = &Language::Basic::Program::current_program;
906 6 my $type = $self->{"type"};
907 6 my $value = $self->{"expression"}->evaluate;
908 6 ▶ if ($value !~ /^\d+$/ || $value > @{$self->{"gotos"}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /^\d+$/ or $value > @{$self->{"gotos"};}) 0 6 6
Condition33%1/3
not $value =~ /^\d+$/ or $value > @{$self->{"gotos"};}
l!l&&r!l&&!r
006
MC/DC0%0/2
not $value =~ /^\d+$/ or $value > @{$self->{"gotos"};}
not $value =~ /^\d+$/ $value > @{$self->{"gotos"};}
Truth table33%1/3
not(A) or B
A= not $value =~ /^\d+$/B= $value > @{$self->{"gotos"};}
ABresult
1X1
011
000
909 0 Exit_Error("Bad value in ON: $value")
910 }
911  
912 6 my $goto = ${$self->{"gotos"}}[$value-1]->evaluate;
913 0 ▶ if ($goto !~ /^\d+$/) {Exit_Error("Bad GOTO in ON: $goto")}
Branch: 1 branch
BranchTrueFalseTotal
unless ($goto =~ /^\d+$/) 0 6 6
914 6 $prog->goto_line($goto);
915  
916 # And if it's a GOSUB, push the program stack so we can get back
917 6 ▶ $prog->push_stack($self) if $type eq "GOSUB";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "GOSUB" 6 0 6
918 } # end sub Language::Basic::Statement::On::implement
919  
920 0 ▶sub output_perl {
Subroutine output_perl: not called CC=4 SCAR=30.0
921 0 my $self = shift;
922 0 my $prog = &Language::Basic::Program::current_program;
923 0 my $type = $self->{"type"};
924  
925 # List of lines to go to
926 0 my @gotos = map {$_->output_perl} @{$self->{"gotos"}};
927 0 my $ret = "\@Gotos_tmp = map {'L' . ";
928 # If there's any expressions, be more fancy
929 0 ▶ if (grep {$_ !~ /^\d+$/} @gotos) {$ret .= "eval "}
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not $_ =~ /^\d+$/;} @gotos) 0 0 0
930 0 $ret .= "\$_} (";
931 0 $ret .= join(", ", @gotos);
932 0 $ret .= ");\n";
933  
934 # Index in the list
935 0 my $branch = $self->{"expression"}->output_perl;
936 0 $ret .= "\$index_tmp = ";
937 0 $ret .= $branch . ";\n";
938  
939 # Form the label name to return to
940 0 my $label;
941 0 ▶ if ($type eq "GOSUB") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "GOSUB") 0 0 0
942 0 $label = "AL" . $prog->current_line_number;
943 0 $ret .= "push \@Gosub_Stack, \"$label\";\n";
944 }
945  
946 # Go to it
947 0 $ret .= "goto \$Gotos_tmp[\$index_tmp-1];";
948  
949 # Write the return-to label after the goto
950 0 ▶ if ($type eq "GOSUB") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "GOSUB") 0 0 0
951 0 $ret .= "\n$label:;";
952 }
953  
954 0 return ($ret);
955 } # end sub Language::Basic::Statement::On::output_perl
956  
957 } # end package Language::Basic::Statement::On
958  
959 ######################################################################
960 # package Language::Basic::Statement::Print
961 # A PRINT statement in a BASIC program.
962 {
963 16 package Language::Basic::Statement::Print;
964 16 @Language::Basic::Statement::Print::ISA = qw(Language::Basic::Statement);
965 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
966  
967 80 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.0
968 80 my $self = shift;
969 80 my $token_group = shift;
970 # empty print statement?
971 80 ▶ unless ($token_group->stuff_left) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($token_group->stuff_left) 2 78 80
972 2 $token_group = new Language::Basic::Token::Group;
973 2 $token_group->lex('""');
974 }
975  
976 80 my $endchar;
977 80 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->stuff_left and do { do { my $exp = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Weird thing to print in PRINT statement!"); my $tok; if ($tok = $token_group->eat_if_class("Separator")) { $endchar = $tok->text; } elsif (not $token_group->stuff_left) { $endchar = ""; } else { Exit_Error("Unexpected extra thing in PRINT statement!"); } ; push @{$$self{"to_print"};}, [$exp, $endchar] } } 8 80 88
978 88 ▶ my $exp = new Language::Basic::Expression::Arithmetic $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Arithmetic"->new($token_group) or Exit_Error("Weird thing to print in PRINT statement!") 88 0 88
979 or Exit_Error("Weird thing to print in PRINT statement!");
980 88 my $tok;
981 88 ▶ if ($tok = $token_group->eat_if_class("Separator")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tok = $token_group->eat_if_class('Separator')) { } 73 15 88
elsif (not $token_group->stuff_left) { } 15 0 15
982 # It's a comma or semicolon
983 73 $endchar = $tok->text;
984 } elsif (! $token_group->stuff_left) {
985 15 $endchar = "";
986 } else {
987 0 Exit_Error("Unexpected extra thing in PRINT statement!");
988 }
989 88 push @{$self->{"to_print"}}, [$exp , $endchar];
990  
991 } while ($token_group->stuff_left);
992 } # end sub Language::Basic::Statement::Print::parse
993  
994 82 ▶sub implement {
Subroutine implement: called CC=8 SCAR=24.7
995 # TODO More than one expression to print! Use an array of LB::Expressions
996 82 my $self = shift;
997 82 my $prog = &Language::Basic::Program::current_program;
998 82 foreach my $thing (@{$self->{"to_print"}}) {
999 90 my ($exp, $endchar) = @$thing;
1000 90 my $string = $exp->evaluate;
1001  
1002 # Never print after column 70
1003 # But "print ''" shouldn't print two \n's!
1004 90 ▶ if ($prog->{"column"} >= 70 && length($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prog->{"column"} >= 70 and length $string) 0 90 90
Condition33%1/3
$prog->{"column"} >= 70 and length $string
!ll&&!rl&&r
9000
MC/DC0%0/2
$prog->{"column"} >= 70 and length $string
$prog->{"column"} >= 70 length $string
Truth table33%1/3
A and B
A= $prog->{"column"} >= 70B= length $string
ABresult
0X0
100
111
1005 0 print "\n";
1006 0 $prog->{"column"} = 0;
1007 }
1008  
1009 # Print the string
1010 90 print $string;
1011 90 $prog->{"column"} += length($string);
1012  
1013 # Handle the thing after the string
1014 90 ▶ if ($endchar eq ",") {
Branch: 2 branches
BranchTrueFalseTotal
if ($endchar eq ',') { } 0 90 90
elsif ($endchar eq ';') { } 70 20 90
1015 # Paraphrased from a BASIC manual:
1016 # If the printhead (!) is at char 56 or more after the expression,
1017 # print \n, else print spaces until the printhead is at the
1018 # beginning of the next 14-character field
1019 0 ▶ if ($prog->{"column"} >= 56) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prog->{'column'} >= 56) { } 0 0 0
1020 0 print "\n";
1021 0 $prog->{"column"} = 0;
1022 } else {
1023 0 my $c = 14 - $prog->{"column"} % 14;
1024 0 print (" " x $c);
1025 0 $prog->{"column"} += $c;
1026 }
1027 } elsif ($endchar eq ";") {
1028 # In BASIC, you always print a space after numbers, but not
1029 # after strings. That seems a bit dumb, but that's how it is.
1030 70 ▶ if (ref($exp) =~ /::Numeric$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($exp) =~ /::Numeric$/) 50 20 70
1031 50 print " ";
1032 50 $prog->{"column"}++;
1033 }
1034 } else {
1035 20 print "\n";
1036 20 $prog->{"column"} = 0;
1037 }
1038 } # end foreach loop over expressions to print
1039 } # end sub Language::Basic::Statement::Print::implement
1040  
1041 0 ▶sub output_perl {
Subroutine output_perl: not called CC=7 SCAR=40.3
1042 0 my $self = shift;
1043 0 my $ret = "print(";
1044 0 my @to_print = @{$self->{"to_print"}};
1045 # TODO create a Print subroutine that takes exp/endchar array & prints
1046 # in the exact way BASIC does. (How do we make that subroutine print
1047 # a space after numerical expressions?!)
1048 0 ▶ while (my $thing = shift @to_print) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $thing = shift @to_print) 0 0 0
1049 0 my ($exp, $endchar) = @$thing;
1050 0 my $string = $exp->output_perl;
1051 0 $ret .= $string;
1052 0 ▶ $ret .= ",' '" if ref($exp) =~ /Numeric$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($exp) =~ /Numeric$/ 0 0 0
1053 0 ▶ if ($endchar eq ",") {
Branch: 2 branches
BranchTrueFalseTotal
if ($endchar eq ',') { } 0 0 0
elsif ($endchar eq '') { } 0 0 0
1054 0 $ret .= ", \"\\t\"";
1055 } elsif ($endchar eq "") {
1056 0 $ret .= ", \"\\n\"";
1057 # This had better be the last exp!
1058 0 ▶ warn "Internal error: obj. w/out endchar isn't last!" if @to_print;
Branch: 1 branch
BranchTrueFalseTotal
if @to_print 0 0 0
1059 } # otherwise it's ';', we hope
1060  
1061 0 ▶ if (@to_print) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_print) { } 0 0 0
1062 0 $ret .= ", ";
1063 } else {
1064 0 $ret .= ");";
1065 }
1066 }
1067  
1068 0 return ($ret);
1069 } # end sub Language::Basic::Statement::Print::output_perl
1070  
1071 } # end package Language::Basic::Statement::Print
1072  
1073 ######################################################################
1074 # package Language::Basic::Statement::Read
1075 # A READ statement in a BASIC program.
1076 {
1077 16 package Language::Basic::Statement::Read;
1078 16 @Language::Basic::Statement::Read::ISA = qw(Language::Basic::Statement);
1079  
1080 3 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
1081 3 my $self = shift;
1082 3 my $token_group = shift;
1083  
1084 # The rest of the statement is lvalues to read in
1085 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$token_group->eat_if_string(",") and do { do { my $exp = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Incorrect READ statement!"); push @{$$self{"lvalues"};}, $exp } } 3 3 6
1086 6 ▶ my $exp = new Language::Basic::Expression::Lvalue $token_group
Branch: 1 branch
BranchTrueFalseTotal
my $exp = "Language::Basic::Expression::Lvalue"->new($token_group) or Exit_Error("Incorrect READ statement!") 6 0 6
1087 or Exit_Error("Incorrect READ statement!");
1088 6 push @{$self->{"lvalues"}}, $exp;
1089 } while $token_group->eat_if_string(",");
1090 } # end sub Language::Basic::Statement::Read::parse
1091  
1092 20 ▶sub implement {
Subroutine implement: called CC=2 SCAR=6.9
1093 20 my $self = shift;
1094 20 my $prog = &Language::Basic::Program::current_program;
1095 20 foreach (@{$self->{"lvalues"}}) {
1096 23 my $var = $_->variable;
1097 23 my $data = $prog->get_data();
1098 # Data will just be a LBE::Constant, but we still have to &evaluate it
1099 23 my $value = $data->evaluate;
1100 23 $var->set($value);
1101 }
1102 } # end sub Language::Basic::Statement::Read::implement
1103  
1104 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
1105 0 my $self = shift;
1106 # Set a list...
1107 0 my $ret = "(";
1108 0 my @lvalues = map {$_->output_perl} @{$self->{"lvalues"}};
1109 0 $ret .= join(", ", @lvalues);
1110 0 $ret .= ") = ";
1111  
1112 # equal to a splice from @Data
1113 0 my $num = @lvalues;
1114 0 $ret .= "splice(\@Data, 0, $num);";
1115  
1116 0 return ($ret);
1117 } # end sub Language::Basic::Statement::Read::output_perl
1118  
1119 } # end package Language::Basic::Statement::Read
1120  
1121 ######################################################################
1122 # package Language::Basic::Statement::Rem
1123 # A REM statement in a BASIC program.
1124 {
1125 16 package Language::Basic::Statement::Rem;
1126 16 @Language::Basic::Statement::Rem::ISA = qw(Language::Basic::Statement);
1127 6 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
1128 # Eat the whole line (including colons if any)
1129 6 my $self = shift;
1130 6 my $token_group = shift;
1131 6 my $tok = $token_group->eat_if_class("Comment");
1132 # Use original text to retain spaces and case.
1133 6 my $text = $tok->{"original_text"};
1134 6 $text =~ s/REM//;
1135 6 $self->{"comment"} = $text;
1136 } # end sub Language::Basic::Statement::Rem::parse
1137  
1138 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
1139 0 my $self = shift;
1140 # Need to have a semicolon because the line label requires a
1141 # statement after it. (And we need a line label in case we GOTO this line
1142 0 my $ret = "; # " . $self->{"comment"};
1143 0 return $ret;
1144 } # end sub Language::Basic::Statement::Rem::output_perl
1145  
1146 } # end package Language::Basic::Statement::Rem
1147  
1148 ######################################################################
1149 # package Language::Basic::Statement::Return
1150 # A RETURN statement in a BASIC program.
1151 {
1152 16 package Language::Basic::Statement::Return;
1153 16 @Language::Basic::Statement::Return::ISA = qw(Language::Basic::Statement);
1154 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
1155  
1156 # No need to have a sub parse
1157  
1158 13 ▶sub implement {
Subroutine implement: called CC=2 SCAR=7.0
1159 13 my $self = shift;
1160 13 my $prog = &Language::Basic::Program::current_program;
1161 13 ▶ my $gosub = $prog->pop_stack or
Branch: 1 branch
BranchTrueFalseTotal
my $gosub = $prog->pop_stack or Exit_Error("RETURN without GOSUB") 13 0 13
1162 Exit_Error("RETURN without GOSUB");
1163 # Start at the statement *after* the GOSUB statement
1164 13 $prog->goto_after_statement($gosub);
1165 } # end sub Language::Basic::Statement::Return::implement
1166  
1167 0 ▶sub output_perl {
Subroutine output_perl: not called CC=1 SCAR=6.9
1168 0 my $ret = "\$Return_tmp = pop \@Gosub_Stack;\n";
1169 0 $ret .= "goto \$Return_tmp;";
1170  
1171 0 return ($ret);
1172 } # end sub Language::Basic::Statement::Return::output_perl
1173  
1174 } # end package Language::Basic::Statement::Return
1175  
1176 16 1; # end of package Language::Basic::Statement
« lib/Language/Basic.pm ↑ Summary lib/Language/Basic/Expression.pm »