| 1 | package Language::Basic; | ||||||||||||||||||||||||||||
| 2 | # by Amir Karger (See below for copyright/license/etc.) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =pod | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Language::Basic - Perl Module to interpret BASIC | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use Language::Basic; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $Program = new Language::Basic::Program; | ||||||||||||||||||||||||||||
| 15 | $Program->input("program.bas"); # Read lines from a file | ||||||||||||||||||||||||||||
| 16 | $Program->parse; # Parse the Program | ||||||||||||||||||||||||||||
| 17 | $Program->implement; # Run the Program | ||||||||||||||||||||||||||||
| 18 | $Program->output_perl; # output Program as a Perl program | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $Program->line("20 PRINT X"); # add one line to existing Program | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Featured scripts: | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =over 4 | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =item basic.pl | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Runs BASIC programs from the command line. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item termbasic.pl | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Term::Readline program. Input one line of BASIC at a time, then run the | ||||||||||||||||||||||||||||
| 33 | program. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =item basic2pl.pl | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Outputs a Perl program that does the same thing as the input BASIC program. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =back | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This module lets you run any BASIC programs you may have lying around, or | ||||||||||||||||||||||||||||
| 44 | may inspire you to write new ones! | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The aspects of the language that are supported are described below. Note | ||||||||||||||||||||||||||||
| 47 | that I was pretty much aiming for Applesoft BASIC (tm) ca. 1985, not some | ||||||||||||||||||||||||||||
| 48 | modern BASIC with real subroutines. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 16 | require 5.004; # I use 'foreach my' | |||||||||||||||||||||||||||
| 54 | 16 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 16 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 16 | require Exporter; | |||||||||||||||||||||||||||
| 58 | 16 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 59 | 16 | @EXPORT = qw( | |||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Stolen from `man perlmod` | ||||||||||||||||||||||||||||
| 63 | 16 | $VERSION = do { my @r = (q$Revision: 1.44 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Sub-packages | ||||||||||||||||||||||||||||
| 66 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 16 | ▶ | use Language::Basic::Expression; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 16 | ▶ | use Language::Basic::Function; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 16 | ▶ | use Language::Basic::Statement; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 16 | ▶ | use Language::Basic::Token; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 16 | ▶ | use Language::Basic::Variable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # sub-packages | ||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | package Language::Basic::Program; | ||||||||||||||||||||||||||||
| 76 | package Language::Basic::Line; | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ###################################################################### | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 Class Language::Basic::Program | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | This class handles a whole program. A Program is just a bunch of Lines, | ||||||||||||||||||||||||||||
| 84 | each of which has one or more Statements on it. Running the program | ||||||||||||||||||||||||||||
| 85 | involves moving through the lines, usually in numerical order, and | ||||||||||||||||||||||||||||
| 86 | implementing each line. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Methods: | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =over 4 | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 16 | package Language::Basic::Program; | |||||||||||||||||||||||||||
| 96 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Fields: | ||||||||||||||||||||||||||||
| 99 | # lines Keys are line numbers, values are LB::Line objects | ||||||||||||||||||||||||||||
| 100 | # curr_line LB::Line currently being implemented/parsed/whatever | ||||||||||||||||||||||||||||
| 101 | # end_program Done implementing the program? | ||||||||||||||||||||||||||||
| 102 | # stack The subroutine stack. In BASIC, it's just a list of | ||||||||||||||||||||||||||||
| 103 | # statements we GOSUB'ed from. | ||||||||||||||||||||||||||||
| 104 | # data The data holder (stuff from DATA statements, read by READ) | ||||||||||||||||||||||||||||
| 105 | # parsed Has this Program been parsed since the last time | ||||||||||||||||||||||||||||
| 106 | # new lines were added? | ||||||||||||||||||||||||||||
| 107 | # needed_subs Functions whose perl-equivalent subs we need to print out | ||||||||||||||||||||||||||||
| 108 | # at the end of the program. (Keys are names of subs, values | ||||||||||||||||||||||||||||
| 109 | # are sub descriptions.) | ||||||||||||||||||||||||||||
| 110 | # column Current column of the screen the program is printing to | ||||||||||||||||||||||||||||
| 111 | 21 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 21 | my ($class, $infile) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #Initialize the intrinsic functions | ||||||||||||||||||||||||||||
| 115 | 21 | &Language::Basic::Function::Intrinsic::initialize(); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 21 | my $in = { | |||||||||||||||||||||||||||
| 118 | "lines" => {}, | ||||||||||||||||||||||||||||
| 119 | "curr_line" => undef, | ||||||||||||||||||||||||||||
| 120 | "end_program" => 0, | ||||||||||||||||||||||||||||
| 121 | 'stack' => [], | ||||||||||||||||||||||||||||
| 122 | "for_statements" => {}, | ||||||||||||||||||||||||||||
| 123 | 'data' => [], | ||||||||||||||||||||||||||||
| 124 | 'parsed' => 0, | ||||||||||||||||||||||||||||
| 125 | "needed_subs" => {}, | ||||||||||||||||||||||||||||
| 126 | "column" => 0, | ||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||
| 128 | 21 | bless $in, $class; | |||||||||||||||||||||||||||
| 129 | } # end sub Language::Basic::Program::new | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item current_program | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Returns the program currently being parsed/implemented/whatever | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item set_current_program | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Sets arg0 to be the Current Program | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 16 | my $_Current_Program; # Gasp! It's an Evil Global Variable! | |||||||||||||||||||||||||||
| 142 | 245 | ▶ | sub current_program { | ||||||||||||||||||||||||||
|
Subroutine
current_program: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 245 | return $_Current_Program; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 190 | ▶ | sub set_current_program { | ||||||||||||||||||||||||||
|
Subroutine
set_current_program: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 146 | 190 | ▶ | my $self = shift or die "LBP::set_current_program must have argument!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 190 | $_Current_Program = $self; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item current_line | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Returns the LB::line currently being parsed/implemented/whatever | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item set_current_line | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Sets the current line in Program arg0 to be line I<number> arg1 | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item first_line_number | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Returns (not surprisingly) the first line number in Program arg0 | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =cut | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 679 | ▶ | sub current_line { return shift->{"curr_line"}; } | ||||||||||||||||||||||||||
|
Subroutine
current_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 165 | 507 | ▶ | sub set_current_line { | ||||||||||||||||||||||||||
|
Subroutine
set_current_line: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 166 | 507 | my $prog = shift; | |||||||||||||||||||||||||||
| 167 | 507 | my $num = shift; | |||||||||||||||||||||||||||
| 168 | 507 | ▶ | if (defined $num && exists $prog->{"lines"}->{$num}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $num and exists $prog->{"lines"}{$num}
MC/DC50%1/2
defined $num and exists $prog->{"lines"}{$num}
defined $num
exists $prog->{"lines"}{$num}
Truth table66%2/3
A and B
A= defined $numB= exists $prog->{"lines"}{$num}
| |||||||||||||||||||||||||||||
| 169 | 486 | $prog->{"curr_line"} = $prog->{"lines"}->{$num}; | |||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||
| 171 | 21 | $prog->{"curr_line"} = undef; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | 42 | ▶ | sub first_line_number {return (sort {$a <=> $b} keys %{shift->{"lines"}})[0]; } | ||||||||||||||||||||||||||
|
Subroutine
first_line_number: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item current_line_number | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | What line number in Program arg0 are we currently on? | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 21 | ▶ | sub current_line_number { | ||||||||||||||||||||||||||
|
Subroutine
current_line_number: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 183 | 21 | my $prog = shift; | |||||||||||||||||||||||||||
| 184 | 21 | my $line = $prog->current_line; | |||||||||||||||||||||||||||
| 185 | 21 | ▶ | return (defined $line ? $line->line_number : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item input | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | This method reads in a program from a file, whose name is the string arg0. It | ||||||||||||||||||||||||||||
| 191 | doesn't do any parsing, except for taking the line number out of the line. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | ▶ | sub input { | ||||||||||||||||||||||||||
|
Subroutine
input: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||
| 196 | 1 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 197 | 1 | $self->set_current_program; | |||||||||||||||||||||||||||
| 198 | 1 | my $fh = new IO::File $filename; | |||||||||||||||||||||||||||
| 199 | 1 | ▶ | die "Error opening $filename: $!\n" unless defined $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 1 | my $old_num = -1; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 2 | ▶ | next if /^\s*$/; # empty lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 2 | chomp; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # Line Number | ||||||||||||||||||||||||||||
| 207 | 2 | my $line_num = $self->_add_line($_); | |||||||||||||||||||||||||||
| 208 | 2 | ▶ | defined $line_num | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 209 | or die "Missing line number " . | ||||||||||||||||||||||||||||
| 210 | ($old_num > 0 ? "after line $old_num\n" : "on first line\n"); | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # In input files, we make sure lines are in numerical order. | ||||||||||||||||||||||||||||
| 213 | # If they're not, it's most likely a bug. | ||||||||||||||||||||||||||||
| 214 | # Same is not true for a Term::Readline interpreter | ||||||||||||||||||||||||||||
| 215 | 2 | ▶ | if ($line_num <= $old_num) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | die "Line $line_num: lines in file must be in increasing order.\n"; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 2 | $old_num = $line_num; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | 1 | close ($fh); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # order the lines | ||||||||||||||||||||||||||||
| 224 | 1 | $self->_fix_lines; | |||||||||||||||||||||||||||
| 225 | 1 | $self->{'parsed'} = 0; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | } # end sub Language::Basic::Program::input | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =item line | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | This method takes a line of BASIC (arg1, already chomped), forms a new LB::Line | ||||||||||||||||||||||||||||
| 232 | with it, and adds it to the Program (arg0). It doesn't do any parsing, | ||||||||||||||||||||||||||||
| 233 | except for taking the line number out of the line. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =cut | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 147 | ▶ | sub line { | ||||||||||||||||||||||||||
|
Subroutine
line: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 238 | 147 | my $self = shift; | |||||||||||||||||||||||||||
| 239 | 147 | $self->set_current_program; | |||||||||||||||||||||||||||
| 240 | 147 | my $line = shift; # sans \n | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 147 | ▶ | defined $self->_add_line($line) or die "Missing line number in line()!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | 147 | $self->_fix_lines; | |||||||||||||||||||||||||||
| 244 | 147 | $self->{'parsed'} = 0; | |||||||||||||||||||||||||||
| 245 | } # end sub Language::Basic::Program::line | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 149 | ▶ | sub _add_line { | ||||||||||||||||||||||||||
|
Subroutine
_add_line: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 248 | # takes the line (sans \n), returns the line number read or undef if there | ||||||||||||||||||||||||||||
| 249 | # is none. | ||||||||||||||||||||||||||||
| 250 | # You must call _fix_lines between _add_line and returning to the | ||||||||||||||||||||||||||||
| 251 | # user's program! | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 149 | my $self = shift; | |||||||||||||||||||||||||||
| 254 | 149 | my $line = shift; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # Line Number | ||||||||||||||||||||||||||||
| 257 | 149 | ▶ | $line =~ s/^\s*(\d+)\s+// or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 149 | my $line_num = $1; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Create an LB::Line with what's left of the line | ||||||||||||||||||||||||||||
| 261 | 149 | $self->{'lines'}{$line_num} = new Language::Basic::Line($line, $line_num); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 149 | return $line_num; | |||||||||||||||||||||||||||
| 264 | } # end sub Language::Basic::Program::_add_line | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # fix the ordering of the lines in the program | ||||||||||||||||||||||||||||
| 267 | 148 | ▶ | sub _fix_lines { | ||||||||||||||||||||||||||
|
Subroutine
_fix_lines: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 268 | 148 | my $self = shift; | |||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 148 | my @line_numbers = sort {$a <=> $b} keys %{$self->{"lines"}}; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 148 | ▶ | for (my $i = 0; $i < @line_numbers - 1; $i++) { # process all but last | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 864 | my $line = $self->{'lines'}{ $line_numbers[$i] }; | |||||||||||||||||||||||||||
| 274 | 864 | $line->set_next( $line_numbers[ $i+1 ] ); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 148 | $self->{'lines'}{ $line_numbers[-1] }->set_next( undef ); | |||||||||||||||||||||||||||
| 278 | } # end sub Language::Basic::Program::_fix_lines | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =item parse | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | This method parses the program, which just involves looping over the lines | ||||||||||||||||||||||||||||
| 283 | in the program and parsing each line. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 21 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 288 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 289 | 21 | $self->set_current_program; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 21 | ▶ | return if $self->{'parsed'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 21 | $self->set_current_line($self->first_line_number); | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # Loop through the lines in the program, parse each | ||||||||||||||||||||||||||||
| 296 | 21 | ▶ | while (defined (my $line = $self->current_line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | #print $line->line_number," ",$line->{"text"},"\n"; | ||||||||||||||||||||||||||||
| 298 | 149 | $line->parse; | |||||||||||||||||||||||||||
| 299 | 149 | $self->set_current_line($line->get_next); | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 21 | $self->{'parsed'} = 1; | |||||||||||||||||||||||||||
| 303 | } # end sub Language::Basic::Program::parse | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =item implement | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | This method actually runs the program. That is, it starts on the first line, | ||||||||||||||||||||||||||||
| 308 | and implements statements one at a time. It performs the statements on a | ||||||||||||||||||||||||||||
| 309 | line in order, and goes from line to line in numerical order, unless a GOTO, | ||||||||||||||||||||||||||||
| 310 | NEXT, etc. sends it somewhere else. It stops when it hits an END statement or | ||||||||||||||||||||||||||||
| 311 | "falls off" the end of the program. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 21 | ▶ | sub implement { | ||||||||||||||||||||||||||
|
Subroutine
implement: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 316 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 317 | 21 | $self->set_current_program; | |||||||||||||||||||||||||||
| 318 | # In case you're lazy & call implement w/out parsing first | ||||||||||||||||||||||||||||
| 319 | 21 | ▶ | $self->parse unless $self->{'parsed'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # Zero stack, etc., start at beginning of program | ||||||||||||||||||||||||||||
| 322 | 21 | $self->start; | |||||||||||||||||||||||||||
| 323 | # Mini-kludge to get the program running | ||||||||||||||||||||||||||||
| 324 | 21 | $self->goto_line($self->current_line_number); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | # Loop over statements while there are statements | ||||||||||||||||||||||||||||
| 327 | 21 | ▶ | while (defined(my $curr_statement = $self->increment)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # TODO create a "trace" command that prints out line numbers | ||||||||||||||||||||||||||||
| 330 | # for debugging | ||||||||||||||||||||||||||||
| 331 | #my $line = $self->current_line; | ||||||||||||||||||||||||||||
| 332 | #print $line->line_number," ",$line->{"text"},"\n"; | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # Do the statement! | ||||||||||||||||||||||||||||
| 335 | # Hooray for OO; just call "implement" on everything! | ||||||||||||||||||||||||||||
| 336 | #print "Statement class ",ref($curr_statement),"\n"; | ||||||||||||||||||||||||||||
| 337 | # Note that this may well change where the next &increment will go | ||||||||||||||||||||||||||||
| 338 | 305 | $curr_statement->implement; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | #Done! | ||||||||||||||||||||||||||||
| 342 | # TODO Exit more gracefully? | ||||||||||||||||||||||||||||
| 343 | } # end sub Language::Basic::Program::implement | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | # Return the next Statement we're supposed to execute, based on the Program's | ||||||||||||||||||||||||||||
| 346 | # next_statement field. And set the default action for the subsequent call | ||||||||||||||||||||||||||||
| 347 | # to increment, which is to do the next Statement in order. (Or return | ||||||||||||||||||||||||||||
| 348 | # undef if the program is done.) | ||||||||||||||||||||||||||||
| 349 | # | ||||||||||||||||||||||||||||
| 350 | # In the simplest case, next_statement will just be the Statement after the | ||||||||||||||||||||||||||||
| 351 | # current one on the current Line, although it my well be in a totally | ||||||||||||||||||||||||||||
| 352 | # different place due to GOTOs, RETURNs, ELSEs or other interesting programming | ||||||||||||||||||||||||||||
| 353 | # tools. | ||||||||||||||||||||||||||||
| 354 | # | ||||||||||||||||||||||||||||
| 355 | # If next_statement is undefined, we're done with this line (and haven't been | ||||||||||||||||||||||||||||
| 356 | # directed to go somewhere more interesting), so go to the next line in order. | ||||||||||||||||||||||||||||
| 357 | # | ||||||||||||||||||||||||||||
| 358 | # TODO should this method be podded? | ||||||||||||||||||||||||||||
| 359 | 387 | ▶ | sub increment { | ||||||||||||||||||||||||||
|
Subroutine
increment: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 360 | 387 | my $self = shift; | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 387 | my $next; | |||||||||||||||||||||||||||
| 363 | 387 | ▶ | unless (defined($next = $self->{"next_statement"})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | # Program is at the end of a line | ||||||||||||||||||||||||||||
| 365 | 233 | my $line = $self->current_line; | |||||||||||||||||||||||||||
| 366 | 233 | my $number = $line->get_next; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # goto_line will set Program's next_statement | ||||||||||||||||||||||||||||
| 369 | # ($number = undef will set "end_program") | ||||||||||||||||||||||||||||
| 370 | 233 | $self->goto_line($number); | |||||||||||||||||||||||||||
| 371 | 233 | $next = $self->{"next_statement"}; | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | # Did we hit an END or "fall off" the last line of the program? | ||||||||||||||||||||||||||||
| 374 | 387 | ▶ | return undef if $self->{"end_program"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | # By default, we're going to go on to the next statement after this one | ||||||||||||||||||||||||||||
| 377 | 366 | $self->{"next_statement"} = $next->{"next_statement"}; | |||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # Whether or not we were at end of line, we now know what next | ||||||||||||||||||||||||||||
| 380 | # Statement is, so return it. | ||||||||||||||||||||||||||||
| 381 | 366 | return $next; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =item start | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | This method erases program stack and moves line pointer to beginning of program | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | It should be called any time we start going through the program. | ||||||||||||||||||||||||||||
| 390 | (Either implement or output_perl.) | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =cut | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | # Don't erase "data". It's set during parsing. | ||||||||||||||||||||||||||||
| 395 | 21 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 396 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 397 | 21 | $self->{"stack"} = []; | |||||||||||||||||||||||||||
| 398 | 21 | $self->{"for_statements"} = {}; | |||||||||||||||||||||||||||
| 399 | 21 | $self->{"column"} = 0; | |||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # Start on the first line of the program | ||||||||||||||||||||||||||||
| 402 | 21 | $self->set_current_line($self->first_line_number); | |||||||||||||||||||||||||||
| 403 | } # end sub Language::Basic::Program::start | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =item goto_line | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | Continue Program execution at the first Statement on line number arg1. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =cut | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 277 | ▶ | sub goto_line { | ||||||||||||||||||||||||||
|
Subroutine
goto_line: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 412 | 277 | my $self = shift; | |||||||||||||||||||||||||||
| 413 | 277 | my $next_line = shift; | |||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 277 | ▶ | if (defined $next_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | 255 | $self->set_current_line($next_line); | |||||||||||||||||||||||||||
| 417 | 255 | ▶ | my $line = $self->current_line or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | Exit_Error("Can't find line $next_line!"); | ||||||||||||||||||||||||||||
| 419 | 255 | $self->{"next_statement"} = $line->{"first_statement"}; | |||||||||||||||||||||||||||
| 420 | } else { | ||||||||||||||||||||||||||||
| 421 | 22 | $self->{"end_program"} = 1; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | } # end sub Language::Basic::Program::set_next_line | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =item goto_after_statement | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | Kind of like goto_line, except go to the Statement I<after> Statement arg1. | ||||||||||||||||||||||||||||
| 429 | (Or the first statement on the line just after Statement arg1, if it's the last | ||||||||||||||||||||||||||||
| 430 | Statement on its line.) E.g., when you RETURN from a GOSUB, you want to return | ||||||||||||||||||||||||||||
| 431 | to the GOSUB line but start execution after the GOSUB. Same with FOR. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =cut | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 61 | ▶ | sub goto_after_statement { | ||||||||||||||||||||||||||
|
Subroutine
goto_after_statement: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 436 | 61 | my $self = shift; | |||||||||||||||||||||||||||
| 437 | 61 | my $st = shift; | |||||||||||||||||||||||||||
| 438 | 61 | $self->{"next_statement"} = $st; | |||||||||||||||||||||||||||
| 439 | # May have jumped to (the beginning or middle of) a new line, | ||||||||||||||||||||||||||||
| 440 | # so we have to reset this. (It stays the same if we're jumping w/in | ||||||||||||||||||||||||||||
| 441 | # one line, but that's OK.) | ||||||||||||||||||||||||||||
| 442 | 61 | $self->set_current_line($st->{"line_number"}); | |||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | # Goto the statement, and set Program's next_statement field, so | ||||||||||||||||||||||||||||
| 445 | # that when Program::implement calls increment, it goes to the | ||||||||||||||||||||||||||||
| 446 | # statement *after* this one. | ||||||||||||||||||||||||||||
| 447 | 61 | $self->increment; | |||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | } # end sub Language::Basic::Program::goto_after_statement | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =pod | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =back | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | The following methods are called from LB::Statement parse or implement | ||||||||||||||||||||||||||||
| 456 | methods to implement various BASIC commands. | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =over 4 | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =item push_stack | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | (GOSUB) Call a subroutine, i.e. push the current Statement::Gosub onto the | ||||||||||||||||||||||||||||
| 463 | Program's calling stack | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =item pop_stack | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | (RETURN) Return from a subroutine, i.e., pop the top Statement::Gosub off of | ||||||||||||||||||||||||||||
| 468 | the Program's calling stack | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =cut | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 13 | ▶ | sub push_stack { | ||||||||||||||||||||||||||
|
Subroutine
push_stack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 473 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 474 | 13 | my $st = shift; | |||||||||||||||||||||||||||
| 475 | 13 | push @{ $self->{'stack'} }, $st; | |||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 13 | ▶ | sub pop_stack { | ||||||||||||||||||||||||||
|
Subroutine
pop_stack: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 479 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 480 | 13 | return pop @{ $self->{'stack'} }; | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =item store_for | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | (FOR) Store a Statement::For arg1, so that when we get to the corresponding | ||||||||||||||||||||||||||||
| 486 | Statement::Next, we know where to go back to | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =item pop_stack | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | (NEXT) Get the Statement::For corresponding to Statement::Next arg1 | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =cut | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 12 | ▶ | sub store_for { | ||||||||||||||||||||||||||
|
Subroutine
store_for: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 495 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 496 | 12 | my $for_statement = shift; | |||||||||||||||||||||||||||
| 497 | 12 | my $lvalue = $for_statement->{"lvalue"}; | |||||||||||||||||||||||||||
| 498 | 12 | my $name = $lvalue->{"name"}; | |||||||||||||||||||||||||||
| 499 | 12 | $self->{"for_statements"}->{$name} = $for_statement; | |||||||||||||||||||||||||||
| 500 | } # end sub Language::Basic::Program::store_for | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 60 | ▶ | sub get_for { | ||||||||||||||||||||||||||
|
Subroutine
get_for: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 503 | 60 | my $self = shift; | |||||||||||||||||||||||||||
| 504 | 60 | my $next_statement = shift; | |||||||||||||||||||||||||||
| 505 | 60 | my $lvalue = $next_statement->{"lvalue"}; | |||||||||||||||||||||||||||
| 506 | 60 | my $name = $lvalue->{"name"}; | |||||||||||||||||||||||||||
| 507 | 60 | ▶ | if (exists $self->{"for_statements"}->{$name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | 60 | return $self->{"for_statements"}->{$name}; | |||||||||||||||||||||||||||
| 509 | } else { | ||||||||||||||||||||||||||||
| 510 | 0 | Exit_Error("NEXT $name without FOR!"); | |||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | } # end sub Language::Basic::Program::get_for | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =item add_data | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | (DATA) Add a piece of data to the Program's data storage, to be accessed | ||||||||||||||||||||||||||||
| 517 | later. | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =cut | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | 23 | ▶ | sub add_data { | ||||||||||||||||||||||||||
|
Subroutine
add_data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 522 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 523 | 23 | my $thing = shift; | |||||||||||||||||||||||||||
| 524 | 23 | push @{ $self->{'data'} }, $thing; | |||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | =item get_data | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | (READ) Get a piece of data that was stored earlier. | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | =cut | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 23 | ▶ | sub get_data { | ||||||||||||||||||||||||||
|
Subroutine
get_data: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 534 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 535 | 23 | ▶ | @{ $self->{'data'} } or Exit_Error("More items READ than input in DATA!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | 23 | my $thing = shift @{ $self->{'data'} }; | |||||||||||||||||||||||||||
| 537 | 23 | return $thing; | |||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =pod | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | =back | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | Finally, there are methods for translating a Program to Perl. | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | =over 4 | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =item output_perl | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | This method translates a program to Perl and outputs it. It does so by | ||||||||||||||||||||||||||||
| 551 | looping through the Lines of the program in order, and calling output_perl on | ||||||||||||||||||||||||||||
| 552 | each one. It also prints some pre- and post- data, such as any subroutines it | ||||||||||||||||||||||||||||
| 553 | needs to declare (e.g., subs that imitate BASIC functionality, as well as subs | ||||||||||||||||||||||||||||
| 554 | that correspond to BASIC DEF statements). | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | It attempts to print everything out nicely, with added whitespace et al. to | ||||||||||||||||||||||||||||
| 557 | make the code somewhat readable. (Note that all of the subpackages' | ||||||||||||||||||||||||||||
| 558 | output_perl methods I<return> strings rather than printing them, so we can | ||||||||||||||||||||||||||||
| 559 | handle all of the printing, indenting, etc. here.) | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | =cut | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | 0 | ▶ | sub output_perl { | ||||||||||||||||||||||||||
|
Subroutine
output_perl: not called CC=16 SCAR=56.1
| |||||||||||||||||||||||||||||
| 564 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 565 | 0 | $self->set_current_program; | |||||||||||||||||||||||||||
| 566 | # In case you're lazy & call implement w/out parsing first | ||||||||||||||||||||||||||||
| 567 | 0 | ▶ | $self->parse unless $self->{'parsed'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | 0 | my $sep = '#' x 78; | |||||||||||||||||||||||||||
| 570 | # TODO these variables should be changeable by switches to basic2pl! | ||||||||||||||||||||||||||||
| 571 | 0 | my $spaces_per_indent = 4; | |||||||||||||||||||||||||||
| 572 | # Indenting for outputted Perl | ||||||||||||||||||||||||||||
| 573 | 0 | my $Output_Indent = 2; # eight spaces by default | |||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | # Beginning of the program | ||||||||||||||||||||||||||||
| 576 | # TODO should basic2pl do these two lines? | ||||||||||||||||||||||||||||
| 577 | 0 | print '#!/usr/bin/perl -w'; | |||||||||||||||||||||||||||
| 578 | 0 | print "\n#Translated from BASIC by basic2pl\n\n"; | |||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | 0 | ▶ | if (@{$self->{"data"}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 581 | 0 | print "$sep\n# Setup\n#\n"; | |||||||||||||||||||||||||||
| 582 | 0 | print "# Read data\n"; | |||||||||||||||||||||||||||
| 583 | 0 | print "while (<DATA>) {chomp; push \@Data, \$_}\n\n"; | |||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | # Zero program stack, etc., start at beginning of program | ||||||||||||||||||||||||||||
| 587 | 0 | $self->start; | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | # Loop through the lines in the program | ||||||||||||||||||||||||||||
| 590 | 0 | print "$sep\n# Main program\n#\n"; | |||||||||||||||||||||||||||
| 591 | 0 | ▶ | while (defined (my $line = $self->current_line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 592 | 0 | my $line_num = $line->line_number; | |||||||||||||||||||||||||||
| 593 | #warn "Line $line_num\n"; | ||||||||||||||||||||||||||||
| 594 | 0 | my $label = "L$line_num:"; | |||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | # What's the line? | ||||||||||||||||||||||||||||
| 597 | 0 | my $out = $label . $line->output_perl; | |||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | # Print labels all the way against the left edge of the line, | ||||||||||||||||||||||||||||
| 600 | # then indent the rest of the line. | ||||||||||||||||||||||||||||
| 601 | # Split with -1 so final \n's don't get ignored | ||||||||||||||||||||||||||||
| 602 | 0 | foreach (split (/\n/, $out, -1)) { | |||||||||||||||||||||||||||
| 603 | # Change indenting for next time? | ||||||||||||||||||||||||||||
| 604 | 0 | ▶ | $Output_Indent += 1, next if $_ eq "INDENT"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 605 | 0 | ▶ | $Output_Indent -= 1, next if $_ eq "UNINDENT"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 606 | 0 | ▶ | warn "weird indenting $Output_Indent\n" if $Output_Indent < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | # If we didn't hit an indent-changing command, print the | ||||||||||||||||||||||||||||
| 609 | # label (if any) and the actual string | ||||||||||||||||||||||||||||
| 610 | # TODO only print out the labels we have to! | ||||||||||||||||||||||||||||
| 611 | 0 | ▶ | $label = (s/^A?L\d+:// ? $& : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 612 | # minus for left justify | ||||||||||||||||||||||||||||
| 613 | 0 | my $indent = -$Output_Indent * $spaces_per_indent; | |||||||||||||||||||||||||||
| 614 | 0 | printf("%*s", $indent, $label); | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # print the actual string | ||||||||||||||||||||||||||||
| 617 | 0 | print $_; | |||||||||||||||||||||||||||
| 618 | 0 | print "\n"; # the \n we lost from split, or the last \n | |||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | # Go through lines in order | ||||||||||||||||||||||||||||
| 622 | 0 | $self->set_current_line($line->get_next); | |||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | # TODO why not indent these nicely? | ||||||||||||||||||||||||||||
| 626 | 0 | my $n = $self->{"needed_subs"}; | |||||||||||||||||||||||||||
| 627 | 0 | ▶ | print "\n$sep\n# Subroutine Definitions\n#\n" if %$n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 628 | # Print out required subroutines | ||||||||||||||||||||||||||||
| 629 | 0 | foreach (sort keys %$n) { | |||||||||||||||||||||||||||
| 630 | 0 | my $out = join(" ", "sub", $_, $n->{$_}, "# end sub $_\n\n"); | |||||||||||||||||||||||||||
| 631 | 0 | $Output_Indent = 0; | |||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | 0 | foreach (split (/\n/, $out, -1)) { | |||||||||||||||||||||||||||
| 634 | # Change indenting for next time? | ||||||||||||||||||||||||||||
| 635 | 0 | ▶ | $Output_Indent += 1, next if $_ eq "INDENT"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 636 | 0 | ▶ | $Output_Indent -= 1, next if $_ eq "UNINDENT"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 637 | 0 | ▶ | warn "weird indenting $Output_Indent\n" if $Output_Indent < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | # If we didn't hit an indent-changing command, print the string | ||||||||||||||||||||||||||||
| 640 | 0 | my $indent = $Output_Indent * $spaces_per_indent; | |||||||||||||||||||||||||||
| 641 | 0 | print " " x $indent; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | # print the actual string | ||||||||||||||||||||||||||||
| 644 | 0 | print $_; | |||||||||||||||||||||||||||
| 645 | 0 | print "\n"; # the \n we lost from split, or the last \n | |||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | # If there were any DATA statements... | ||||||||||||||||||||||||||||
| 650 | 0 | ▶ | if (@{$self->{"data"}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 651 | 0 | print "\n\n$sep\n# Data\n#\n__DATA__\n"; | |||||||||||||||||||||||||||
| 652 | 0 | print join("\n", map {$_->output_perl} @{$self->{"data"}}); | |||||||||||||||||||||||||||
| 653 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||||||||
| 655 | } # end sub Language::Basic::Program::output_perl | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | =item need_sub | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | Tells the Program that it needs to use the sub named arg0 (whose definition | ||||||||||||||||||||||||||||
| 660 | is in arg1). This is used for outputting a Perl translation of a BASIC | ||||||||||||||||||||||||||||
| 661 | program, so that you only write "sub mid_str {...}" if MID$ is used in | ||||||||||||||||||||||||||||
| 662 | the BASIC program. | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | =back | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =cut | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | 0 | ▶ | sub need_sub { | ||||||||||||||||||||||||||
|
Subroutine
need_sub: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 669 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 670 | 0 | my $n = $self->{"needed_subs"}; | |||||||||||||||||||||||||||
| 671 | 0 | my ($func_name, $func_desc) = @_; | |||||||||||||||||||||||||||
| 672 | 0 | ▶ | return if exists $n->{$func_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 673 | 0 | $n->{$func_name} = $func_desc; | |||||||||||||||||||||||||||
| 674 | } # end sub Language::Basic::Program::need_sub | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | } # end package Language::Basic::Program | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | ###################################################################### | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | =head2 Class Language::Basic::Line | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | This class handles one line of a BASIC program, which has one or more | ||||||||||||||||||||||||||||
| 683 | Statements on it. | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | This class has no implement method. The reason is that sometimes, you'll | ||||||||||||||||||||||||||||
| 686 | jump to the middle of a line. (E.g., returning from the GOSUBs in | ||||||||||||||||||||||||||||
| 687 | 10 FOR A=1 TO 10: GOSUB 1000: NEXT A) | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | Methods: | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | =over 4 | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | =cut | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | { | ||||||||||||||||||||||||||||
| 696 | 16 | package Language::Basic::Line; | |||||||||||||||||||||||||||
| 697 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | # Make a new LB::Line with the text given (don't parse it yet) | ||||||||||||||||||||||||||||
| 700 | 149 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 701 | 149 | my $class = shift; | |||||||||||||||||||||||||||
| 702 | 149 | my $text = shift; | |||||||||||||||||||||||||||
| 703 | 149 | my $line_number = shift; | |||||||||||||||||||||||||||
| 704 | 149 | my $in = { | |||||||||||||||||||||||||||
| 705 | # literal text of the line (not including line number) | ||||||||||||||||||||||||||||
| 706 | "text" => $text, | ||||||||||||||||||||||||||||
| 707 | # Pointer to first LB::Statement on the line | ||||||||||||||||||||||||||||
| 708 | "first_statement" => 0, | ||||||||||||||||||||||||||||
| 709 | # number of next line (accessed with set/get_next) | ||||||||||||||||||||||||||||
| 710 | 'next_line' => undef, | ||||||||||||||||||||||||||||
| 711 | # BASIC line number of this Line | ||||||||||||||||||||||||||||
| 712 | "line_number" => $line_number, | ||||||||||||||||||||||||||||
| 713 | }; | ||||||||||||||||||||||||||||
| 714 | 149 | bless $in, $class; | |||||||||||||||||||||||||||
| 715 | } # end sub Language::Basic::Line::new | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | =item get_next | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | Returns the Line's line number | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =cut | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | 21 | ▶ | sub line_number { shift->{"line_number"} } | ||||||||||||||||||||||||||
|
Subroutine
line_number: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | =item get_next | ||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||
| 727 | Returns the next line number in the Program | ||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||
| 729 | =item set_next | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | Sets the next line number in the Program to be arg1. | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | =cut | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | 382 | ▶ | sub get_next { return shift->{'next_line'}; } | ||||||||||||||||||||||||||
|
Subroutine
get_next: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | # TODO Should this be _set_next and undocumented? Only gets called by _fix_lines | ||||||||||||||||||||||||||||
| 738 | 1012 | ▶ | sub set_next { | ||||||||||||||||||||||||||
|
Subroutine
set_next: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 739 | 1012 | my $self = shift; | |||||||||||||||||||||||||||
| 740 | 1012 | my $next = shift; | |||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | 1012 | $self->{'next_line'} = $next; | |||||||||||||||||||||||||||
| 743 | } # end sub Language::Basic::Line::set_next | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | =item parse | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | This method breaks the line up into Statements (and removes whitespace, except | ||||||||||||||||||||||||||||
| 748 | in strings), then parses the Statements in order. | ||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||
| 750 | =cut | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | 149 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 753 | 149 | my $self = shift; | |||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | # Break the line up into Tokens for later eating/parsing | ||||||||||||||||||||||||||||
| 756 | 149 | my $token_group = new Language::Basic::Token::Group; | |||||||||||||||||||||||||||
| 757 | 149 | $token_group->lex($self->{"text"}); | |||||||||||||||||||||||||||
| 758 | 149 | my $oldst; | |||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||
| 760 | # Parse Statement(s) in the Line | ||||||||||||||||||||||||||||
| 761 | 149 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 762 | # Create the new Statement and figure out what kind of statement it | ||||||||||||||||||||||||||||
| 763 | # is. $statement will be an object of a subclass LB::Statement::*) | ||||||||||||||||||||||||||||
| 764 | 157 | my $statement = new Language::Basic::Statement $token_group; | |||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | # Actually parse the Statement | ||||||||||||||||||||||||||||
| 767 | 157 | $statement->parse($token_group); | |||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | # Each statement needs to know which line it's on, in case we | ||||||||||||||||||||||||||||
| 770 | # RETURN or NEXT into the middle of a line. | ||||||||||||||||||||||||||||
| 771 | 157 | $statement->set_line_number($self->{"line_number"}); | |||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | # Create a linked list of the Statements in the line | ||||||||||||||||||||||||||||
| 774 | 157 | ▶ | if (defined $oldst) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 775 | 8 | $oldst->{"next_statement"} = $statement | |||||||||||||||||||||||||||
| 776 | } else { | ||||||||||||||||||||||||||||
| 777 | 149 | $self->{"first_statement"} = $statement; | |||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||
| 779 | 157 | $oldst = $statement; | |||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||
| 781 | # If there's a colon, eat it and parse the next Statement on the Line | ||||||||||||||||||||||||||||
| 782 | } while ($token_group->eat_if_class("Statement_End")); | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | # TODO make this error prettier | ||||||||||||||||||||||||||||
| 785 | 149 | ▶ | if ($token_group->stuff_left) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 786 | 0 | my $p = "Extra tokens left after parsing!\n" . $token_group->print; | |||||||||||||||||||||||||||
| 787 | 0 | chomp($p); | |||||||||||||||||||||||||||
| 788 | 0 | Exit_Error($p); | |||||||||||||||||||||||||||
| 789 | } | ||||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | =item output_perl | ||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | This method simply calls output_perl on each of the Line's Statements in | ||||||||||||||||||||||||||||
| 795 | order. | ||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | =back | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | =cut | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | 0 | ▶ | sub output_perl { | ||||||||||||||||||||||||||
|
Subroutine
output_perl: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 802 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 803 | 0 | my $statement = $self->{"first_statement"}; | |||||||||||||||||||||||||||
| 804 | 0 | my $out = $statement->output_perl; | |||||||||||||||||||||||||||
| 805 | # Do each statement in the line in order | ||||||||||||||||||||||||||||
| 806 | # Put each statement on a separate line. | ||||||||||||||||||||||||||||
| 807 | 0 | ▶ | while (defined ($statement = $statement->{"next_statement"})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 808 | 0 | $out .= "\n"; | |||||||||||||||||||||||||||
| 809 | 0 | $out .= $statement->output_perl; | |||||||||||||||||||||||||||
| 810 | } | ||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||
| 812 | # Output the statement | ||||||||||||||||||||||||||||
| 813 | 0 | return $out; | |||||||||||||||||||||||||||
| 814 | } # end sub Language::Basic::Line::output_perl | ||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||
| 816 | } # end package Language::Basic::Line | ||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | # end package Language::Basic | ||||||||||||||||||||||||||||
| 820 | 16 | 1; | |||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | __END__ | ||||||||||||||||||||||||||||