lib/Language/Basic/Variable.pm

filter: Statementstmt 96.5% 84 / 87 Branchbran 81.8% 18 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 90.5% 115 / 127 CC 15 SCAR 12.6
CC
15
Cov
92%
get_cell
6.9
dimension
4.0
lookup
4.0
CRAP 3.5
« lib/Language/Basic/Function.pm ↑ Summary lib/Language/Basic/Token.pm »
1 package Language::Basic::Variable;
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::Variable - Module to handle parsing and implementing
10 BASIC variables.
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 There are two sorts of variables: Arrays and Scalars. Each of those
18 classes has a subclass for Numeric or String variables.
19  
20 =head1 DESCRIPTION
21  
22 An Array needs to have full LBV::Scalar objects in it, rather than just
23 having an array of values. The reason is that, for example, you might
24 use ARR(3) as the variable in a FOR loop. Also, the "set" and "value"
25 methods apply to a LBV::Scalar (since you can't set an array to a value
26 (in BASIC :)) so in order to be handle A(3)=3, A(3) needs to be an LBV::Scalar.
27  
28 The lookup method looks up a variable in the Array or Scalar lookup
29 table (depending on whether there were parentheses after the variable
30 name). BASIC allows undeclared variables, so if the variable name hasn't
31 been seen before, a new variable is created.
32  
33 =cut
34  
35 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # sub-packages
38 {
39 16 package Language::Basic::Variable;
40 package Language::Basic::Variable::Numeric;
41 package Language::Basic::Variable::String;
42 package Language::Basic::Variable::Scalar;
43 package Language::Basic::Variable::Array;
44 }
45 # Hash storing the program's variables
46 16 my %Scalars;
47 my %Arrays;
48  
49 # Look up a variable based on its name.
50 # Create a new Variable (Scalar or Array) if it doesn't yet exist.
51 # Arg0 is the name for the variable, Arg1 is defined if there were () after
52 # the name, i.e., if it's an array. For now, we don't care what's in the
53 # parens.
54 # Returns the Variable ref, whether or not it had to create a new one.
55 90 ▶sub lookup {
Subroutine lookup: called CC=4 SCAR=13.9
Pod undocumented
56 90 my $name = shift;
57 90 my $arglist = shift;
58 90 ▶ if (defined($arglist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arglist) { } 13 77 90
59 13 ▶ unless (exists $Arrays{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Arrays{$name}) 5 8 13
60 5 $Arrays{$name} = new Language::Basic::Variable::Array $name;
61 }
62 13 return $Arrays{$name};
63 } else {
64 77 ▶ unless (exists $Scalars{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Scalars{$name}) 23 54 77
65 23 $Scalars{$name} = new Language::Basic::Variable::Scalar $name;
66 }
67 77 return $Scalars{$name};
68 }
69 } # end sub Language::Basic::Variable::lookup
70  
71 ######################################################################
72 # package Language::Basic::Variable::Scalar
73 #
74 # Fields:
75 # value - the current value of the variable
76 #
77  
78 =head2 Language::Basic::Variable::Scalar class
79  
80 This class handles a variable or one cell in an array.
81  
82 Methods include "value", which gets the variable's value, and "set",
83 which sets it.
84  
85 =cut
86  
87 {
88 package Language::Basic::Variable::Scalar;
89 16 @Language::Basic::Variable::Scalar::ISA = qw(Language::Basic::Variable);
90  
91 23 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
92 23 my ($class, $name) = @_;
93 23 ▶ my $type = ($name =~ /\$$/) ? "String" : "Numeric";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\$$/ ? : 4 19 23
94 # Create a new subclass object, & return it
95 23 my $subclass = $class . "::$type";
96 23 return (new $subclass);
97 } # end sub Language::Basic::Variable::new
98  
99 # Set the variable to value arg1
100 160 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
101 160 my ($self, $value) = @_;
102 160 $self->{"value"} = $value;
103 }
104  
105 264 ▶sub value {return shift->{"value"} }
Subroutine value: called CC=1 SCAR=0.0
106  
107 package Language::Basic::Variable::Scalar::String;
108 16 @Language::Basic::Variable::Scalar::String::ISA =
109 qw (Language::Basic::Variable::Scalar Language::Basic::Variable::String);
110  
111 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
112 15 my $class = shift;
113 15 my $value = "";
114 15 my $self = {
115 "value" => $value,
116 };
117 15 bless $self, $class;
118 } # end sub Language::Basic::Variable::Scalar::String::new
119  
120 package Language::Basic::Variable::Scalar::Numeric;
121 16 @Language::Basic::Variable::Scalar::Numeric::ISA =
122 qw (Language::Basic::Variable::Scalar Language::Basic::Variable::Numeric);
123  
124 212 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
125 212 my $class = shift;
126 212 my $value = 0;
127 212 my $self = {
128 "value" => $value,
129 };
130 212 bless $self, $class;
131 } # end sub Language::Basic::Variable::Scalar::Numeric::new
132  
133 } # end package Language::Basic::Variable::Scalar
134  
135 ######################################################################
136 #
137 # Fields:
138 # cells - list (of lists, for 2- or more dimensional arrays) of
139 # Language::Basic::Variable::Scalar objects holding the actual values
140 # for each index
141  
142 =head2 Language::Basic::Variable::Array class
143  
144 This class handles a BASIC array. Each cell in the array is a LBV::Scalar
145 object.
146  
147 Methods include "dimension", which dimensions the array to a given size (or
148 a default size) and get_cell, which returns the LBV::Scalar object in a
149 given array location.
150  
151 Note that BASIC arrays start from 0!
152  
153 =cut
154  
155 {
156 16 package Language::Basic::Variable::Array;
157 16 @Language::Basic::Variable::Array::ISA = qw(Language::Basic::Variable);
158 16 ▶use Language::Basic::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 # Fields:
161 # cells - holds the LBV::Scalar::* objects in the array
162  
163 # Note that this returns subclasses of LBVA (String or Numeric)
164 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
165 5 my ($class, $name) = @_;
166 5 my $self = {
167 "cells" => [],
168 };
169  
170 5 ▶ my $type = ($name =~ /\$$/) ? "String" : "Numeric";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\$$/ ? : 1 4 5
171 5 my $subclass = $class . "::$type";
172 5 bless $self, $subclass;
173  
174 # Dimension the array to its default size
175 5 $self->dimension;
176 5 return $self;
177 } # end sub Language::Basic::Variable::Array::new
178  
179 # Make room in the array
180 # Input: Optionally, a list of sizes for each dimension. Otherwise, a
181 # one-dimensional array of default size is dimensioned.
182 # Error: Exit with error if the array will be too big.
183 8 ▶sub dimension {
Subroutine dimension: called CC=4 SCAR=13.9
184 8 my $MAXDIM = 100000;
185 8 my $self = shift;
186 8 my @Default = (10);
187  
188 # TODO multi-dim arrays
189 8 ▶ my @sizes = @_ ? @_ : @Default;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 5 8
190 8 my $size = 1;
191 8 for (@sizes) {$size *= ($_+1)}
192 8 ▶ if ($size > $MAXDIM)
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $MAXDIM) 0 8 8
193 0 {&Exit_Error("Array size may not be greater than $MAXDIM")}
194  
195 8 my $subclass = ref($self);
196 8 $subclass =~ s/Array/Scalar/;
197 8 $self->{"cells"} = &lol($subclass, @sizes);
198 8 $self->{"dimensions"} = \@sizes;
199 } # end sub Language::Basic::Variable::Array::dimension
200  
201 224 ▶sub lol {
Subroutine lol: called CC=2 SCAR=6.9
202 # create a list of lists of arg0 objects, dimensions arg1-n
203 224 my ($subclass, @sizes) = @_;
204 224 ▶ if (@sizes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sizes) { } 20 204 224
205 #recurse
206 20 my $size = shift(@sizes);
207 20 my @arr = map {&lol($subclass, @sizes)} (0 .. $size);
208 20 return \@arr;
209 } else {
210 # end recursion
211 # '$subclass->new' because 'new $subclass' calls LBVA::new!
212 204 my $ret = $subclass->new;
213 204 return $ret;
214 }
215 } # end sub Language::Basic::Variable::Array::lol
216  
217 # Get one cell of an array
218 # Input: a list of array indices
219 # Output: the Language::Basic::Variable::Scalar at that location in the array
220 54 ▶sub get_cell {
Subroutine get_cell: called CC=6 SCAR=19.3
221 54 my $self = shift;
222 54 my @indices = @_;
223 54 my @sizes = @{$self->{"dimensions"}};
224 0 ▶ unless (@sizes == @indices) {Exit_Error("Wrong number of indices!")}
Branch: 1 branch
BranchTrueFalseTotal
unless (@sizes == @indices) 0 54 54
225  
226 54 my $ptr = $self->{"cells"};
227 54 foreach my $index (@indices) {
228 58 my $size = shift(@sizes);
229 # index can't be negative or greater than the array size
230 58 ▶ if ($index !~ /^\d+$/ || $index > $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $index =~ /^\d+$/ or $index > $size) 0 58 58
Condition33%1/3
not $index =~ /^\d+$/ or $index > $size
l!l&&r!l&&!r
0058
MC/DC0%0/2
not $index =~ /^\d+$/ or $index > $size
not $index =~ /^\d+$/ $index > $size
Truth table33%1/3
not(A) or B
A= not $index =~ /^\d+$/B= $index > $size
ABresult
1X1
011
000
231 0 &Exit_Error ("Illegal array indexing '@indices'")
232 }
233 58 $ptr = $ptr->[$index];
234 }
235  
236 54 my $c = ref($ptr);
237 54 ▶ warn "Weird class $c" unless $ptr->isa("Language::Basic::Variable::Scalar");
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr->isa("Language::Basic::Variable::Scalar") 0 54 54
238 54 return $ptr;
239 } # end sub Language::Basic::Variable::Array::get_cell
240  
241 package Language::Basic::Variable::Array::Numeric;
242 16 @Language::Basic::Variable::Array::Numeric::ISA =
243 qw (Language::Basic::Variable::Array Language::Basic::Variable::Numeric);
244 package Language::Basic::Variable::Array::String;
245 16 @Language::Basic::Variable::Array::String::ISA =
246 qw (Language::Basic::Variable::Array Language::Basic::Variable::String);
247 } # end package Language::Basic::Variable::Array
248  
249 {
250 # set ISA for "return type" classes
251 16 package Language::Basic::Variable::Numeric;
252 16 @Language::Basic::Variable::Numeric::ISA = qw
253 (Language::Basic::Variable Language::Basic::Numeric);
254 package Language::Basic::Variable::String;
255 16 @Language::Basic::Variable::String::ISA = qw
256 (Language::Basic::Variable Language::Basic::String);
257 }
258 16 1; # end package Language::Basic::Variable
« lib/Language/Basic/Function.pm ↑ Summary lib/Language/Basic/Token.pm »