| 1 | package Language::Basic::Function; | ||||||||||||||||||||||||||||
| 2 | # Part of Language::Basic by Amir Karger (See Basic.pm for details) | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =pod | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Language::Basic::Function - Package to handle user-defined and intrinsic | ||||||||||||||||||||||||||||
| 9 | Functions in BASIC. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | See L<Language::Basic> for the overview of how the Language::Basic module | ||||||||||||||||||||||||||||
| 14 | works. This pod page is more technical. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | A Function can be either an intrinsic BASIC function, like INT or CHR$, | ||||||||||||||||||||||||||||
| 17 | or a user-defined function, like FNX (defined with the DEF command). | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | The check_args method checks that the right number and type of function | ||||||||||||||||||||||||||||
| 22 | arguments were input. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | The evaluate method actually calculates the value of the function, given | ||||||||||||||||||||||||||||
| 25 | certain arguments. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | The lookup method looks up the function in the function lookup table. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | The output_perl method returns a string that's the Perl equivalent to | ||||||||||||||||||||||||||||
| 30 | the BASIC function. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Fields: | ||||||||||||||||||||||||||||
| 35 | # arg_types - a string. If a function takes a String and two Numeric | ||||||||||||||||||||||||||||
| 36 | # arguments, the string will be "SNN". Like in Perl, a semicolon | ||||||||||||||||||||||||||||
| 37 | # separates required from optional arguments | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # sub-packages | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 16 | package Language::Basic::Function::Intrinsic; | |||||||||||||||||||||||||||
| 45 | package Language::Basic::Function::Defined; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Lookup table for functions | ||||||||||||||||||||||||||||
| 49 | 16 | my %Table; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # This sub puts the function in the lookup table | ||||||||||||||||||||||||||||
| 52 | 171 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 171 | my ($class, $name) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 171 | my $self = { | |||||||||||||||||||||||||||
| 56 | "name" => $name, | ||||||||||||||||||||||||||||
| 57 | } ; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Put this sub in lookup table | ||||||||||||||||||||||||||||
| 60 | 171 | $Table{$name} = $self; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 171 | ▶ | my $type = ($name =~ /\$$/) ? "String" : "Numeric"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | # Create a new subclass object, & return it | ||||||||||||||||||||||||||||
| 64 | 171 | my $subclass = $class . "::$type"; | |||||||||||||||||||||||||||
| 65 | 171 | bless $self, $subclass; | |||||||||||||||||||||||||||
| 66 | } # end sub Language::Basic::Function::new | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Lookup a function by name in the function table. | ||||||||||||||||||||||||||||
| 69 | # This will (in theory) never be called before new has been called | ||||||||||||||||||||||||||||
| 70 | # for function $name | ||||||||||||||||||||||||||||
| 71 | 54 | ▶ | sub lookup { | ||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 54 | my $name = shift; | |||||||||||||||||||||||||||
| 73 | 54 | return $Table{$name}; | |||||||||||||||||||||||||||
| 74 | } # end sub Language::Basic::Variable::lookup | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Check argument number and type. Exit_Error if there's a problem. | ||||||||||||||||||||||||||||
| 77 | 12 | ▶ | sub check_args { | ||||||||||||||||||||||||||
|
Subroutine
check_args: called CC=10 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 12 | my ($self, $arglist) = @_; | |||||||||||||||||||||||||||
| 79 | 12 | my @args = @{$arglist->{"arguments"}}; | |||||||||||||||||||||||||||
| 80 | # Test for several errors at once | ||||||||||||||||||||||||||||
| 81 | 12 | my $error = ""; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Handle optional args | ||||||||||||||||||||||||||||
| 84 | 12 | my ($min_types, $max_types); | |||||||||||||||||||||||||||
| 85 | 12 | my $types = $self->{"arg_types"}; | |||||||||||||||||||||||||||
| 86 | 12 | ▶ | if ($types =~ s/(.*);/$1/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 3 | $min_types = length($1); | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 9 | $min_types = length($types); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 12 | $max_types = length($types); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 12 | ▶ | $error .= ("Wrong number of arguments to function\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args <= $max_types and @args >= $min_types
MC/DC0%0/2
@args <= $max_types and @args >= $min_types
@args <= $max_types
@args >= $min_types
Truth table33%1/3
A and B
A= @args <= $max_typesB= @args >= $min_types
| |||||||||||||||||||||||||||||
| 94 | unless @args <= $max_types && @args >= $min_types; | ||||||||||||||||||||||||||||
| 95 | # Now check each argument type | ||||||||||||||||||||||||||||
| 96 | 12 | foreach my $type (split (//, $types)) { | |||||||||||||||||||||||||||
| 97 | 19 | ▶ | my $arg = shift @args or last; # may be optional args | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | # This should never happen, hence die, not Exit_Error | ||||||||||||||||||||||||||||
| 99 | 19 | ▶ | ref($arg) =~ /(String|Numeric)$/ or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | die "Error in LBF::Defined::check_args"; | ||||||||||||||||||||||||||||
| 101 | 19 | my $atype = substr($1,0,1); | |||||||||||||||||||||||||||
| 102 | 19 | ▶ | if ($atype ne $type) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $error .= $type eq "N" ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | "String argument given, Numeric required.\n" : | ||||||||||||||||||||||||||||
| 105 | "Numeric argument given, String required.\n"; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 12 | chomp($error); # Exit_Error will add last \n back in. | |||||||||||||||||||||||||||
| 109 | 12 | ▶ | Exit_Error($error) if $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } # end sub Language::Basic::Variable::check_args | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Class Language::Basic::Function::Intrinsic | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | This class handles intrinsic BASIC functions. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||
| 121 | # Fields: | ||||||||||||||||||||||||||||
| 122 | # subroutine - a ref to a sub that implements the BASIC routine in Perl | ||||||||||||||||||||||||||||
| 123 | # (assuming the args are in @_) | ||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | package Language::Basic::Function::Intrinsic; | ||||||||||||||||||||||||||||
| 126 | 16 | @Language::Basic::Function::Intrinsic::ISA = qw(Language::Basic::Function); | |||||||||||||||||||||||||||
| 127 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =pod | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | The initialize method sets up BASIC's supported functions at the beginning | ||||||||||||||||||||||||||||
| 132 | of the program. The all-important @Init holds a ref for each function | ||||||||||||||||||||||||||||
| 133 | to an array holding: | ||||||||||||||||||||||||||||
| 134 | - the function name, | ||||||||||||||||||||||||||||
| 135 | - the number and type of arguments (in a Perl function prototype-like style), | ||||||||||||||||||||||||||||
| 136 | - a subref that performs the equivalent of the BASIC function, and | ||||||||||||||||||||||||||||
| 137 | - a string for the output_perl method. That string is either the name of an | ||||||||||||||||||||||||||||
| 138 | equivalent Perl function, like "ord" for BASIC's "ASC", or (if there is no | ||||||||||||||||||||||||||||
| 139 | exact equivalent) a BLOCK that performs the same action. | ||||||||||||||||||||||||||||
| 140 | Adding intrinsic BASIC functions therefore involves adding to this array. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 21 | ▶ | sub initialize { | ||||||||||||||||||||||||||
|
Subroutine
initialize: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 145 | # The type is an N or S for each Numeric or String argument the | ||||||||||||||||||||||||||||
| 146 | # function takes. | ||||||||||||||||||||||||||||
| 147 | # funcstring is a string that gives the perl equivalent to the | ||||||||||||||||||||||||||||
| 148 | # BASIC function. (Used for output_perl) If it's just a word, then perl | ||||||||||||||||||||||||||||
| 149 | # and BASIC have exactly equivalent functions, which makes the function | ||||||||||||||||||||||||||||
| 150 | # call much easier. Otherwise, it's something in {} that will become | ||||||||||||||||||||||||||||
| 151 | # a sub. | ||||||||||||||||||||||||||||
| 152 | # TODO it would be pretty sexy to have the subref and the funcstring | ||||||||||||||||||||||||||||
| 153 | # do the same thing (i.e., create the sub with an eval of funcstring). | ||||||||||||||||||||||||||||
| 154 | # Only reason so far I can think of not to is Exit_Error call in CHR$. | ||||||||||||||||||||||||||||
| 155 | # But I could create an Exit_Error routine in output perl script! | ||||||||||||||||||||||||||||
| 156 | my @Init = ( | ||||||||||||||||||||||||||||
| 157 | # Numeric functions... | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | ["ASC", "S", sub {ord(shift)}, "ord" ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | ["INT", "N", sub {int(shift)}, "int" ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | ["LEN", "S", sub {length(shift)}, "length" ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 161 | # Don't use the arg. BASIC passes in | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | ["RND", "N", sub {rand()}, "{rand()}" ], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | ["VAL", "S", sub {0+shift;}, "{0+shift;}"], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # and String functions... | ||||||||||||||||||||||||||||
| 166 | ['CHR$', "N", | ||||||||||||||||||||||||||||
| 167 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||
| 168 | 1 | my $a=shift; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ($a>127 || $a<0) {Exit_Error("Arg. to CHR\$ must be < 127")} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$a > 127 or $a < 0
MC/DC0%0/2
$a > 127 or $a < 0
$a > 127
$a < 0
Truth table33%1/3
A or B
A= $a > 127B= $a < 0
| |||||||||||||||||||||||||||||
| 170 | 1 | chr($a); | |||||||||||||||||||||||||||
| 171 | }, "chr" | ||||||||||||||||||||||||||||
| 172 | ], | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | ['MID$', "SN;N", | ||||||||||||||||||||||||||||
| 175 | 25 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 176 | 25 | my ($str, $index, $length) = @_; | |||||||||||||||||||||||||||
| 177 | 25 | $index--; # BASIC strings index from 1! | |||||||||||||||||||||||||||
| 178 | 25 | ▶ | return (defined $length ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | substr($str, $index, $length) : | ||||||||||||||||||||||||||||
| 180 | substr($str, $index) ); | ||||||||||||||||||||||||||||
| 181 | }, | ||||||||||||||||||||||||||||
| 182 | join("\n\t", | ||||||||||||||||||||||||||||
| 183 | "{", | ||||||||||||||||||||||||||||
| 184 | 'my ($str, $index, $length) = @_;', | ||||||||||||||||||||||||||||
| 185 | '$index--;', | ||||||||||||||||||||||||||||
| 186 | 'return (defined $length ? ', | ||||||||||||||||||||||||||||
| 187 | ' substr($str, $index, $length)', | ||||||||||||||||||||||||||||
| 188 | ' : substr($str, $index) );') | ||||||||||||||||||||||||||||
| 189 | . "\n}" | ||||||||||||||||||||||||||||
| 190 | ], | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | ['STR$', "N", sub {'' . shift;}, "{'' . shift;}"], | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # Initialize intrinsic functions | ||||||||||||||||||||||||||||
| 195 | 21 | foreach (@Init) { | |||||||||||||||||||||||||||
| 196 | 168 | my ($name, $arg_types, $subref, $perl_sub) = @$_; | |||||||||||||||||||||||||||
| 197 | 168 | my $func = new Language::Basic::Function::Intrinsic ($name); | |||||||||||||||||||||||||||
| 198 | # Now set up the Function object with the function definition etc. | ||||||||||||||||||||||||||||
| 199 | 168 | $func->define($arg_types, $subref, $perl_sub); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } # end sub Language::Basic::Function::Intrinsic::initialize | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # This sub defines a function, i.e. says what it does with its arguments | ||||||||||||||||||||||||||||
| 204 | 168 | ▶ | sub define { | ||||||||||||||||||||||||||
|
Subroutine
define: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | # $subref is a sub ref which "translates" the BASIC function into Perl | ||||||||||||||||||||||||||||
| 206 | # arg_types is a string containing an N or S for each Numeric or String | ||||||||||||||||||||||||||||
| 207 | # argument the function takes | ||||||||||||||||||||||||||||
| 208 | # perlsub is a string which is the perl equivalent of the basic function | ||||||||||||||||||||||||||||
| 209 | 168 | my ($self, $arg_types, $subref, $perl_sub) = @_; | |||||||||||||||||||||||||||
| 210 | 168 | $self->{"subroutine"} = $subref; | |||||||||||||||||||||||||||
| 211 | 168 | $self->{"arg_types"} = $arg_types; | |||||||||||||||||||||||||||
| 212 | 168 | $self->{"perl_sub"} = $perl_sub; | |||||||||||||||||||||||||||
| 213 | } # end sub Language::Basic::Function::Intrinsic::define | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 27 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 216 | # Note that number & type of args has already been checked | ||||||||||||||||||||||||||||
| 217 | 27 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 218 | # Put this in an eval to find errors? | ||||||||||||||||||||||||||||
| 219 | 27 | return &{$self->{"subroutine"}} (@args); | |||||||||||||||||||||||||||
| 220 | } # end sub Language::Basic::Function::Intrinsic::evaluate | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | # output the function name | ||||||||||||||||||||||||||||
| 223 | 0 | ▶ | sub output_perl { | ||||||||||||||||||||||||||
|
Subroutine
output_perl: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 224 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 225 | 0 | my $prog = &Language::Basic::Program::current_program; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # If it's a basic function that translates to an intrinsic function, | ||||||||||||||||||||||||||||
| 228 | # just return the function | ||||||||||||||||||||||||||||
| 229 | 0 | my $perl_sub = $self->{"perl_sub"}; | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | return $perl_sub unless $perl_sub =~ /^\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Otherwise, it's more complicated | ||||||||||||||||||||||||||||
| 233 | 0 | my $name = $self->{"name"}; | |||||||||||||||||||||||||||
| 234 | # Use ucfirst(lc) for intrinsic functions so we don't get | ||||||||||||||||||||||||||||
| 235 | # messed up with real intrinsic functions | ||||||||||||||||||||||||||||
| 236 | 0 | $name = ucfirst(lc($name)); | |||||||||||||||||||||||||||
| 237 | 0 | $name =~ s/\$$/_str/; | |||||||||||||||||||||||||||
| 238 | # It's a BASIC intrinsic function w/ a perl equivalent | ||||||||||||||||||||||||||||
| 239 | 0 | $name .= "_bas"; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Note that we're going to have to add sub description at the | ||||||||||||||||||||||||||||
| 242 | # end of the perl script | ||||||||||||||||||||||||||||
| 243 | 0 | $prog->need_sub($name, $perl_sub); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | return $name; | |||||||||||||||||||||||||||
| 246 | } # end sub Language::Basic::Function::Intrinsic::output_perl | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | package Language::Basic::Function::Intrinsic::String; | ||||||||||||||||||||||||||||
| 249 | 16 | @Language::Basic::Function::Intrinsic::String::ISA = | |||||||||||||||||||||||||||
| 250 | qw(Language::Basic::Function::Intrinsic Language::Basic::Function::String); | ||||||||||||||||||||||||||||
| 251 | package Language::Basic::Function::Intrinsic::Numeric; | ||||||||||||||||||||||||||||
| 252 | 16 | @Language::Basic::Function::Intrinsic::Numeric::ISA = | |||||||||||||||||||||||||||
| 253 | qw(Language::Basic::Function::Intrinsic Language::Basic::Function::Numeric); | ||||||||||||||||||||||||||||
| 254 | } # end package Language::Basic::Function::Intrinsic | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | ###################################################################### | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Class Language::Basic::Function::Defined | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | This class handles functions defined by the user in DEF statements. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =cut | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # | ||||||||||||||||||||||||||||
| 267 | # Fields: | ||||||||||||||||||||||||||||
| 268 | # variables - the function parameters. (LB::Variable::Scalar objects) | ||||||||||||||||||||||||||||
| 269 | # expression - an arithmetic expression. When the function parameters | ||||||||||||||||||||||||||||
| 270 | # are correctly set, evaluating this expression will yield the | ||||||||||||||||||||||||||||
| 271 | # value of the function | ||||||||||||||||||||||||||||
| 272 | { | ||||||||||||||||||||||||||||
| 273 | 16 | package Language::Basic::Function::Defined; | |||||||||||||||||||||||||||
| 274 | 16 | @Language::Basic::Function::Defined::ISA = qw(Language::Basic::Function); | |||||||||||||||||||||||||||
| 275 | 16 | ▶ | use Language::Basic::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # This sub declares a function, i.e. says how many arguments it has | ||||||||||||||||||||||||||||
| 278 | 3 | ▶ | sub declare { | ||||||||||||||||||||||||||
|
Subroutine
declare: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 279 | # $arglist is a ref to a list of LB::Variable::Lvalues, which are the | ||||||||||||||||||||||||||||
| 280 | # arguments to the Function. (E.g., X in DEF FN(X)) | ||||||||||||||||||||||||||||
| 281 | # $exp is an LB::Expression which, when evaluated on the arguments, | ||||||||||||||||||||||||||||
| 282 | # will implement the function | ||||||||||||||||||||||||||||
| 283 | 3 | my ($self, $arglistref) = @_; | |||||||||||||||||||||||||||
| 284 | 3 | my $types; # Each arg is S (String) or N (Numeric) | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 3 | foreach my $arg (@$arglistref) { | |||||||||||||||||||||||||||
| 287 | 4 | ▶ | ref($arg) =~ /(String|Numeric)$/ or die "Error in LBF::Defined::define"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 4 | $types .= substr($1,0,1); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | 3 | $self->{"arg_types"} = $types; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 3 | $self->{"arguments"} = $arglistref; | |||||||||||||||||||||||||||
| 293 | } # end sub Language::Basic::Function::Defined::define | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # This sub defines a function, i.e. says what it does with its arguments | ||||||||||||||||||||||||||||
| 296 | # Just involves setting the function's "expression" field. | ||||||||||||||||||||||||||||
| 297 | 3 | ▶ | sub define { | ||||||||||||||||||||||||||
|
Subroutine
define: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 298 | 3 | my ($self, $exp) = @_; | |||||||||||||||||||||||||||
| 299 | 3 | $self->{"expression"} = $exp; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # Actually evaluate the function on its arguments | ||||||||||||||||||||||||||||
| 303 | # Set each parameter (in "variables" field) to the value given in the | ||||||||||||||||||||||||||||
| 304 | # arguments, then evaluate the expression. | ||||||||||||||||||||||||||||
| 305 | # Just in case user has a function FN(X) and uses X elsewhere in the | ||||||||||||||||||||||||||||
| 306 | # program, save the value of X just before we set X based on the argument. | ||||||||||||||||||||||||||||
| 307 | # This is a poor man's version of variable scoping. | ||||||||||||||||||||||||||||
| 308 | 7 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 309 | # Note that number & type of args has already been checked | ||||||||||||||||||||||||||||
| 310 | 7 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 311 | 7 | ▶ | Exit_Error("Function is not defined!") unless defined $self->{"expression"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 7 | my @save_vars; | |||||||||||||||||||||||||||
| 314 | 7 | foreach (@{$self->{"arguments"}}) { | |||||||||||||||||||||||||||
| 315 | 8 | my $var = $_->variable; | |||||||||||||||||||||||||||
| 316 | 8 | my $arg = shift @args; | |||||||||||||||||||||||||||
| 317 | 8 | push @save_vars, $var->value; | |||||||||||||||||||||||||||
| 318 | 8 | $var->set($arg); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 7 | my $value = $self->{"expression"}->evaluate; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # Now restore the values of the function parameters that we may have | ||||||||||||||||||||||||||||
| 324 | # changed. | ||||||||||||||||||||||||||||
| 325 | 7 | foreach (@{$self->{"arguments"}}) { | |||||||||||||||||||||||||||
| 326 | 8 | my $var = $_->variable; | |||||||||||||||||||||||||||
| 327 | 8 | my $save = shift @save_vars; | |||||||||||||||||||||||||||
| 328 | 8 | $var->set($save); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 7 | return $value; | |||||||||||||||||||||||||||
| 332 | } # end sub Language::Basic::Function::Defined::evaluate | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | # output the function name | ||||||||||||||||||||||||||||
| 335 | 0 | ▶ | sub output_perl { | ||||||||||||||||||||||||||
|
Subroutine
output_perl: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 336 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 337 | 0 | my $name = $self->{"name"}; | |||||||||||||||||||||||||||
| 338 | 0 | $name = lc($name); | |||||||||||||||||||||||||||
| 339 | # First "string", then "function" | ||||||||||||||||||||||||||||
| 340 | 0 | $name =~ s/\$$/_str/; | |||||||||||||||||||||||||||
| 341 | 0 | $name =~ s/^fn(.*)/$1_fun/; | |||||||||||||||||||||||||||
| 342 | 0 | return $name; | |||||||||||||||||||||||||||
| 343 | } # end sub Language::Basic::Function::Defined::output_perl | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | package Language::Basic::Function::Defined::String; | ||||||||||||||||||||||||||||
| 346 | 16 | @Language::Basic::Function::Defined::String::ISA = | |||||||||||||||||||||||||||
| 347 | qw(Language::Basic::Function::Defined Language::Basic::Function::String); | ||||||||||||||||||||||||||||
| 348 | package Language::Basic::Function::Defined::Numeric; | ||||||||||||||||||||||||||||
| 349 | 16 | @Language::Basic::Function::Defined::Numeric::ISA = | |||||||||||||||||||||||||||
| 350 | qw(Language::Basic::Function::Defined Language::Basic::Function::Numeric); | ||||||||||||||||||||||||||||
| 351 | } # end package Language::Basic::Function::Defined | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | { | ||||||||||||||||||||||||||||
| 354 | # set ISA for "return type" classes | ||||||||||||||||||||||||||||
| 355 | 16 | package Language::Basic::Function::Numeric; | |||||||||||||||||||||||||||
| 356 | 16 | @Language::Basic::Function::Numeric::ISA = qw | |||||||||||||||||||||||||||
| 357 | (Language::Basic::Function Language::Basic::Numeric); | ||||||||||||||||||||||||||||
| 358 | package Language::Basic::Function::String; | ||||||||||||||||||||||||||||
| 359 | 16 | @Language::Basic::Function::String::ISA = qw | |||||||||||||||||||||||||||
| 360 | (Language::Basic::Function Language::Basic::String); | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | 16 | 1; # end package Language::Basic::Function | |||||||||||||||||||||||||||