| 1 | # Copyright (C) 2011-2024 A S Lewis | ||
| 2 | # | ||
| 3 | # This program is free software: you can redistribute it and/or modify it under the terms of the GNU | ||
| 4 | # Lesser Public License as published by the Free Software Foundation, either version 3 of the | ||
| 5 | # License, or (at your option) any later version. | ||
| 6 | # | ||
| 7 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without | ||
| 8 | # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 9 | # Lesser Public License for more details. | ||
| 10 | # | ||
| 11 | # You should have received a copy of the GNU Lesser Public License along with this program. If not, | ||
| 12 | # see <http://www.gnu.org/licenses/>. | ||
| 13 | # | ||
| 14 | # | ||
| 15 | # Language::Axbasic::Variable, based on Language::Basic by Amir Karger | ||
| 16 | |||
| 17 | { package Language::Axbasic::Variable; | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | # use diagnostics; | ||
| 22 | |||
| 23 | use Glib qw(TRUE FALSE); | ||
| 24 | |||
| 25 | @Language::Axbasic::Variable::ISA = qw( | ||
| 26 | Language::Axbasic | ||
| 27 | ); | ||
| 28 | |||
| 29 | # Paraphrased from Language::Basic | ||
| 30 | # There are two sorts of variables: Arrays and Scalars. Each of those classes has a subclass for | ||
| 31 | # Numeric or String variables. | ||
| 32 | # An Array needs to have full LA::Variable::Scalar objects in it, rather than just having an | ||
| 33 | # array of values. The reason is that, for example, you might use ARR(3) as the variable in a | ||
| 34 | # FOR loop. Also, the 'set' and 'value' methods apply to a LA::Variable::Scalar (since you | ||
| 35 | # can't set an array to a value (in Axbasic) so in order to be handle A(3)=3, A(3) needs to be | ||
| 36 | # an LA::Variable::Scalar. | ||
| 37 | # The lookup method looks up a variable in the Array or Scalar lookup table (depending on | ||
| 38 | # whether there were parentheses after the variable name). Axbasic allows undeclared | ||
| 39 | # variables, so if the variable name hasn't been seen before, a new variable is created. | ||
| 40 | |||
| 41 | # Sub-classes | ||
| 42 | { | ||
| 43 | package Language::Axbasic::Variable::Numeric; | ||
| 44 | package Language::Axbasic::Variable::String; | ||
| 45 | package Language::Axbasic::Variable::Scalar; | ||
| 46 | package Language::Axbasic::Variable::Array; | ||
| 47 | } | ||
| 48 | |||
| 49 | ################## | ||
| 50 | # Constructors | ||
| 51 | |||
| 52 | # sub new {} # Each subclass must have its own ->new | ||
| 53 | |||
| 54 | sub lookup { | ||
| 55 | |||
| 56 | # Called by LA::Expression::Lvalue->new | ||
| 57 | # | ||
| 58 | # Look up a variable based on its name, and create a new variable (Scalar or Array) if it | ||
| 59 | # doesn't exist yet | ||
| 60 | # | ||
| 61 | # Expected arguments | ||
| 62 | # $scriptObj - Blessed reference to the parent LA::Script | ||
| 63 | # $varName - The variable's identifier (e.g. A, a$, array) | ||
| 64 | # | ||
| 65 | # Optional arguments | ||
| 66 | # $argListObj - For an identifier that refers to a cell in an array, blessed reference to | ||
| 67 | # the LA::Expression::Arglist or LA::Expression::SpecialArgList that | ||
| 68 | # represents the list | ||
| 69 | # - NB Some functions that want to create an array might prefer to pass a | ||
| 70 | # string like 'fake_arg_list', so that the 'if (! $argListObj)' condition | ||
| 71 | # that appears in this function is TRUE | ||
| 72 | # | ||
| 73 | # Return values | ||
| 74 | # 'undef' on improper arguments, or if there is an error (for example in creating a new | ||
| 75 | # variable) | ||
| 76 | # Otherwise returns the variable's blessed reference, regardless of whether or not this | ||
| 77 | # function had to create it | ||
| 78 | |||
| 79 | my ($class, $scriptObj, $varName, $argListObj, $check) = @_; | ||
| 80 | |||
| 81 | # Local variables | ||
| 82 | my ( | ||
| 83 | $currentSub, $declareMode, $varObj, | ||
| 84 | @boundList, | ||
| 85 | ); | ||
| 86 | |||
| 87 | # Check for improper arguments | ||
| 88 | if (! defined $scriptObj || ! defined $varName || defined $check) { | ||
| 89 | |||
| 90 | return $axmud::CLIENT->writeImproper($class . '->lookup', @_); | ||
| 91 | } | ||
| 92 | |||
| 93 | # Get the current subroutine/function, which stores local variables | ||
| 94 | $currentSub = $scriptObj->returnCurrentSub(); | ||
| 95 | if (! defined $currentSub) { | ||
| 96 | |||
| 97 | return undef; | ||
| 98 | } | ||
| 99 | |||
| 100 | # If it's a scalar variable... | ||
| 101 | $declareMode = $scriptObj->declareMode; | ||
| 102 | if (! $argListObj) { | ||
| 103 | |||
| 104 | if ($declareMode eq 'global_scalar') { | ||
| 105 | |||
| 106 | # We're in a GLOBAL statement. Check that this variable hasn't already been declared | ||
| 107 | # as a global variable | ||
| 108 | if ($scriptObj->ivExists('globalScalarHash', $varName)) { | ||
| 109 | |||
| 110 | return $scriptObj->setError( | ||
| 111 | 'variable_VAR_already_declared_global', | ||
| 112 | $class . '->lookup', | ||
| 113 | 'VAR', $varName, | ||
| 114 | ); | ||
| 115 | } | ||
| 116 | |||
| 117 | # Otherwise, declare the global variable | ||
| 118 | $varObj = Language::Axbasic::Variable::Scalar->new($scriptObj, $varName); | ||
| 119 | if (! $varObj) { | ||
| 120 | |||
| 121 | return undef; | ||
| 122 | |||
| 123 | } else { | ||
| 124 | |||
| 125 | $scriptObj->add_globalScalar($varName, $varObj); | ||
| 126 | |||
| 127 | return $varObj; | ||
| 128 | } | ||
| 129 | |||
| 130 | } elsif ($declareMode eq 'local_scalar') { | ||
| 131 | |||
| 132 | # We're in a LOCAL or SUB statement. Check that this variable hasn't already been | ||
| 133 | # declared as a local variable | ||
| 134 | if ($currentSub->ivExists('localScalarHash', $varName)) { | ||
| 135 | |||
| 136 | return $scriptObj->setError( | ||
| 137 | 'variable_VAR_already_declared_local', | ||
| 138 | $class . '->lookup', | ||
| 139 | 'VAR', $varName, | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | # Otherwise, declare the local variable | ||
| 144 | $varObj = Language::Axbasic::Variable::Scalar->new($scriptObj, $varName); | ||
| 145 | if (! $varObj) { | ||
| 146 | |||
| 147 | return undef; | ||
| 148 | |||
| 149 | } else { | ||
| 150 | |||
| 151 | $currentSub->add_localScalar($varName, $varObj); | ||
| 152 | |||
| 153 | return $varObj; | ||
| 154 | } | ||
| 155 | |||
| 156 | } elsif ($declareMode eq 'simple') { | ||
| 157 | |||
| 158 | # We're in a SORT statement (or similar), or a FOR EACH, POP, PUSH, SHIFT, UNSHIFT | ||
| 159 | # (or similar) statement in which the variable is not a scalar, but an array | ||
| 160 | # created by (for example) DIM var (10) | ||
| 161 | |||
| 162 | # We don't create a new scalar variable, but instead return the array variable with | ||
| 163 | # the same name | ||
| 164 | if ($currentSub->ivExists('localArrayHash', $varName)) { | ||
| 165 | |||
| 166 | # It's a local array variable | ||
| 167 | return $currentSub->ivShow('localArrayHash', $varName); | ||
| 168 | |||
| 169 | } elsif ($scriptObj->ivExists('globalArrayHash', $varName)) { | ||
| 170 | |||
| 171 | # It's a scalar array variable | ||
| 172 | return $scriptObj->ivShow('globalArrayHash', $varName); | ||
| 173 | |||
| 174 | } else { | ||
| 175 | |||
| 176 | # We have to issue an error, even when OPTION TYPO hasn't been used | ||
| 177 | return $scriptObj->setError( | ||
| 178 | 'variable_VAR_not_an_array', | ||
| 179 | $class . '->lookup', | ||
| 180 | 'VAR', $varName, | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | } elsif ($currentSub->ivExists('localScalarHash', $varName)) { | ||
| 185 | |||
| 186 | # This variable has already been declared as a local variable in the scope of the | ||
| 187 | # current subroutine | ||
| 188 | return $currentSub->ivShow('localScalarHash', $varName); | ||
| 189 | |||
| 190 | } elsif ($scriptObj->ivExists('globalScalarHash', $varName)) { | ||
| 191 | |||
| 192 | # This variable has already been declared as a global variable | ||
| 193 | return $scriptObj->ivShow('globalScalarHash', $varName); | ||
| 194 | |||
| 195 | } else { | ||
| 196 | |||
| 197 | # The scalar hasn't been declared yet, and we're not in a GLOBAL, LOCAL or SUB | ||
| 198 | # statement | ||
| 199 | if ( | ||
| 200 | # Primitive line-numbered scripts, all variables global | ||
| 201 | $scriptObj->executionMode eq 'line_num' | ||
| 202 | # PEEK or PEEK... statement | ||
| 203 | || $declareMode eq 'peek_scalar' | ||
| 204 | # Script doesn't specify OPTION TYPO, forcing us to use GLOBAL or LOCAL | ||
| 205 | || ! $scriptObj->ivShow('optionStatementHash', 'typo') | ||
| 206 | ) { | ||
| 207 | # Create the new global variable | ||
| 208 | $varObj = Language::Axbasic::Variable::Scalar->new($scriptObj, $varName); | ||
| 209 | if (! $varObj) { | ||
| 210 | |||
| 211 | return undef; | ||
| 212 | |||
| 213 | } else { | ||
| 214 | |||
| 215 | $scriptObj->add_globalScalar($varName, $varObj); | ||
| 216 | |||
| 217 | return $varObj; | ||
| 218 | } | ||
| 219 | |||
| 220 | } else { | ||
| 221 | |||
| 222 | # The variable hasn't been declared yet | ||
| 223 | return $scriptObj->setError( | ||
| 224 | 'undeclared_variable_VAR', | ||
| 225 | $class . '->lookup', | ||
| 226 | 'VAR', $varName, | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | # If it's an array variable... | ||
| 232 | } else { | ||
| 233 | |||
| 234 | if ($declareMode eq 'global_array') { | ||
| 235 | |||
| 236 | # We're in a DIM or DIM GLOBAL statement. Check that this variable hasn't already | ||
| 237 | # been declared as a global array | ||
| 238 | if ($scriptObj->ivExists('globalArrayHash', $varName)) { | ||
| 239 | |||
| 240 | return $scriptObj->setError( | ||
| 241 | 'array_variable_VAR_already_declared_global', | ||
| 242 | $class . '->lookup', | ||
| 243 | 'VAR', $varName, | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | |||
| 247 | # Otherwise, declare the global array | ||
| 248 | $varObj = Language::Axbasic::Variable::Array->new($scriptObj, $varName); | ||
| 249 | if (! $varObj) { | ||
| 250 | |||
| 251 | return undef; | ||
| 252 | |||
| 253 | } else { | ||
| 254 | |||
| 255 | $scriptObj->add_globalArray($varName, $varObj); | ||
| 256 | |||
| 257 | return $varObj; | ||
| 258 | } | ||
| 259 | |||
| 260 | } elsif ($declareMode eq 'local_array') { | ||
| 261 | |||
| 262 | # We're in a DIM LOCAL statement. Check that this variable hasn't already been | ||
| 263 | # declared as a local array | ||
| 264 | if ($currentSub->ivExists('localScalarHash', $varName)) { | ||
| 265 | |||
| 266 | return $scriptObj->setError( | ||
| 267 | 'array_variable_VAR_already_declared_local', | ||
| 268 | $class . '->lookup', | ||
| 269 | 'VAR', $varName, | ||
| 270 | ); | ||
| 271 | } | ||
| 272 | |||
| 273 | # Otherwise, declare the local array | ||
| 274 | $varObj = Language::Axbasic::Variable::Array->new($scriptObj, $varName); | ||
| 275 | if (! $varObj) { | ||
| 276 | |||
| 277 | return undef; | ||
| 278 | |||
| 279 | } else { | ||
| 280 | |||
| 281 | $currentSub->add_localArray($varName, $varObj); | ||
| 282 | |||
| 283 | return $varObj; | ||
| 284 | } | ||
| 285 | |||
| 286 | } elsif ($currentSub->ivExists('localArrayHash', $varName)) { | ||
| 287 | |||
| 288 | # This variable has already been declared as a local array in the scope of the | ||
| 289 | # current subroutine | ||
| 290 | return $currentSub->ivShow('localArrayHash', $varName); | ||
| 291 | |||
| 292 | } elsif ($scriptObj->ivExists('globalArrayHash', $varName)) { | ||
| 293 | |||
| 294 | # This variable has already been declared as a global array | ||
| 295 | return $scriptObj->ivShow('globalArrayHash', $varName); | ||
| 296 | |||
| 297 | } elsif ($declareMode eq 'peek_array') { | ||
| 298 | |||
| 299 | # We're in a PEEK or PEEK... statement, and there isn't an existing global or | ||
| 300 | # local array called $varName, so declare a global array | ||
| 301 | $varObj = Language::Axbasic::Variable::Array->new($scriptObj, $varName); | ||
| 302 | if (! $varObj) { | ||
| 303 | |||
| 304 | return undef; | ||
| 305 | |||
| 306 | } else { | ||
| 307 | |||
| 308 | $scriptObj->add_globalArray($varName, $varObj); | ||
| 309 | |||
| 310 | return $varObj; | ||
| 311 | } | ||
| 312 | |||
| 313 | } elsif ($scriptObj->executionMode eq 'line_num') { | ||
| 314 | |||
| 315 | # In programmes with line numbers, undeclared array variables cause the creation of | ||
| 316 | # the array, as if DIM had been used | ||
| 317 | # Declare a global array | ||
| 318 | $varObj = Language::Axbasic::Variable::Array->new($scriptObj, $varName); | ||
| 319 | if (! $varObj) { | ||
| 320 | |||
| 321 | return undef; | ||
| 322 | |||
| 323 | } else { | ||
| 324 | |||
| 325 | $scriptObj->add_globalArray($varName, $varObj); | ||
| 326 | |||
| 327 | # In this situation, every array dimension is given the bounds 0..10 | ||
| 328 | foreach my $listRef ($argListObj->argList) { | ||
| 329 | |||
| 330 | push (@boundList, 0, 10); | ||
| 331 | } | ||
| 332 | |||
| 333 | if (! $varObj->dimension(@boundList)) { | ||
| 334 | return undef; | ||
| 335 | } else { | ||
| 336 | return $varObj; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | } else { | ||
| 341 | |||
| 342 | # The array variable hasn't been declared yet, and we're not in a DIM, DIM GLOBAL or | ||
| 343 | # DIM LOCAL statement | ||
| 344 | return $scriptObj->setError( | ||
| 345 | 'undeclared_array_variable_VAR', | ||
| 346 | $class . '->lookup', | ||
| 347 | 'VAR', $varName, | ||
| 348 | ); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | ################## | ||
| 354 | # Methods | ||
| 355 | |||
| 356 | ################## | ||
| 357 | # Accessors - set | ||
| 358 | |||
| 359 | ################## | ||
| 360 | # Accessors - get | ||
| 361 | } | ||
| 362 | |||
| 363 | { package Language::Axbasic::Variable::Scalar; | ||
| 364 | |||
| 365 | use strict; | ||
| 366 | use warnings; | ||
| 367 | # use diagnostics; | ||
| 368 | |||
| 369 | use Glib qw(TRUE FALSE); | ||
| 370 | |||
| 371 | @Language::Axbasic::Variable::Scalar::ISA = qw( | ||
| 372 | Language::Axbasic::Variable | ||
| 373 | ); | ||
| 374 | |||
| 375 | ################## | ||
| 376 | # Constructors | ||
| 377 | |||
| 378 | sub new { | ||
| 379 | |||
| 380 | # Called by LA::Line->parse | ||
| 381 | # The class that handles a variable or one cell in an array | ||
| 382 | # Methods include ->value, which gets the variable's value, and ->set, which sets it | ||
| 383 | # | ||
| 384 | # Expected arguments | ||
| 385 | # $scriptObj - Blessed reference of the parent LA::Script | ||
| 386 | # $varName - The variable's name | ||
| 387 | # | ||
| 388 | # Return values | ||
| 389 | # 'undef' on improper arguments | ||
| 390 | # Blessed reference to the newly-created object on success | ||
| 391 | |||
| 392 | my ($class, $scriptObj, $varName, $check) = @_; | ||
| 393 | |||
| 394 | # Local variables | ||
| 395 | my ($subClass, $type); | ||
| 396 | |||
| 397 | # Check for improper arguments | ||
| 398 | if (! defined $class || ! defined $scriptObj || ! defined $varName || defined $check) { | ||
| 399 | |||
| 400 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 401 | } | ||
| 402 | |||
| 403 | # If the variable name ends with a $, it's a string variable, otherwise it's a numeric | ||
| 404 | # variable | ||
| 405 | if ($varName =~ /\$$/) { | ||
| 406 | $type = 'String'; | ||
| 407 | } else { | ||
| 408 | $type = 'Numeric'; | ||
| 409 | } | ||
| 410 | |||
| 411 | # Create a new subclass object, and return it | ||
| 412 | $subClass = $class . '::' . $type; | ||
| 413 | return $subClass->new($scriptObj); | ||
| 414 | } | ||
| 415 | |||
| 416 | ################## | ||
| 417 | # Methods | ||
| 418 | |||
| 419 | sub value { | ||
| 420 | |||
| 421 | # Retrieves the scalar's value | ||
| 422 | # | ||
| 423 | # Expected arguments | ||
| 424 | # (none besides $self) | ||
| 425 | # | ||
| 426 | # Return values | ||
| 427 | # 'undef' on improper arguments | ||
| 428 | # The scalar's value otherwise | ||
| 429 | |||
| 430 | my ($self, $check) = @_; | ||
| 431 | |||
| 432 | # Check for improper arguments | ||
| 433 | if (defined $check) { | ||
| 434 | |||
| 435 | return $axmud::CLIENT->writeImproper($self->_objClass . '->value', @_); | ||
| 436 | } | ||
| 437 | |||
| 438 | return $self->value; | ||
| 439 | } | ||
| 440 | |||
| 441 | ################## | ||
| 442 | # Accessors - set | ||
| 443 | |||
| 444 | sub set { | ||
| 445 | |||
| 446 | # Sets the scalar's value | ||
| 447 | # Generates an error if we try to assign a string value like "hello" to a numeric variable | ||
| 448 | # | ||
| 449 | # Expected arguments | ||
| 450 | # $value - The value to set | ||
| 451 | # | ||
| 452 | # Return values | ||
| 453 | # 'undef' on improper arguments | ||
| 454 | # $value otherwise | ||
| 455 | |||
| 456 | my ($self, $value, $check) = @_; | ||
| 457 | |||
| 458 | # Check for improper arguments | ||
| 459 | if (defined $check) { | ||
| 460 | |||
| 461 | return $axmud::CLIENT->writeImproper($self->_objClass . '->set', @_); | ||
| 462 | } | ||
| 463 | |||
| 464 | # Can't assign a string value to a numeric variable | ||
| 465 | if (ref($self) =~ m/Numeric/ && ! Scalar::Util::looks_like_number($value)) { | ||
| 466 | |||
| 467 | return $self->scriptObj->setError( | ||
| 468 | 'type_mismatch_error', | ||
| 469 | $self->_objClass . '->set', | ||
| 470 | ); | ||
| 471 | |||
| 472 | } else { | ||
| 473 | |||
| 474 | $self->ivPoke('value', $value); | ||
| 475 | |||
| 476 | return $value; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | |||
| 480 | ################## | ||
| 481 | # Accessors - get | ||
| 482 | } | ||
| 483 | |||
| 484 | { package Language::Axbasic::Variable::Scalar::String; | ||
| 485 | |||
| 486 | use strict; | ||
| 487 | use warnings; | ||
| 488 | # use diagnostics; | ||
| 489 | |||
| 490 | use Glib qw(TRUE FALSE); | ||
| 491 | |||
| 492 | @Language::Axbasic::Variable::Scalar::String::ISA = qw( | ||
| 493 | Language::Axbasic::Variable::Scalar | ||
| 494 | Language::Axbasic::Variable::String | ||
| 495 | ); | ||
| 496 | |||
| 497 | ################## | ||
| 498 | # Constructors | ||
| 499 | |||
| 500 | sub new { | ||
| 501 | |||
| 502 | # Creates the new LA::Variable::Scalar::String | ||
| 503 | # | ||
| 504 | # Expected arguments | ||
| 505 | # $scriptObj - Blessed reference of the parent LA::Script | ||
| 506 | # | ||
| 507 | # Return values | ||
| 508 | # 'undef' on improper arguments | ||
| 509 | # Blessed reference to the newly-created object on success | ||
| 510 | |||
| 511 | my ($class, $scriptObj, $check) = @_; | ||
| 512 | |||
| 513 | # Check for improper arguments | ||
| 514 | if (! defined $class || ! defined $scriptObj || defined $check) { | ||
| 515 | |||
| 516 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 517 | } | ||
| 518 | |||
| 519 | # Setup | ||
| 520 | my $self = { | ||
| 521 | _objName => $class, # Name Axbasic objects after their class | ||
| 522 | _objClass => $class, | ||
| 523 | _parentFile => undef, # No parent file object | ||
| 524 | _parentWorld => undef, # No parent file object | ||
| 525 | _privFlag => TRUE, # All IVs are private | ||
| 526 | |||
| 527 | # Perl object components | ||
| 528 | # ---------------------- | ||
| 529 | |||
| 530 | # The parent LA::Script | ||
| 531 | scriptObj => $scriptObj, | ||
| 532 | |||
| 533 | # IVs | ||
| 534 | # ---- | ||
| 535 | |||
| 536 | # The initial value is an empty string for string scalar | ||
| 537 | value => '', | ||
| 538 | }; | ||
| 539 | |||
| 540 | bless $self, $class; | ||
| 541 | return $self; | ||
| 542 | } | ||
| 543 | |||
| 544 | ################## | ||
| 545 | # Methods | ||
| 546 | |||
| 547 | ################## | ||
| 548 | # Accessors - set | ||
| 549 | |||
| 550 | ################## | ||
| 551 | # Accessors - get | ||
| 552 | |||
| 553 | sub scriptObj | ||
| 554 | { $_[0]->{scriptObj} } | ||
| 555 | |||
| 556 | sub value | ||
| 557 | { $_[0]->{value} } | ||
| 558 | } | ||
| 559 | |||
| 560 | { package Language::Axbasic::Variable::Scalar::Numeric; | ||
| 561 | |||
| 562 | use strict; | ||
| 563 | use warnings; | ||
| 564 | # use diagnostics; | ||
| 565 | |||
| 566 | use Glib qw(TRUE FALSE); | ||
| 567 | |||
| 568 | @Language::Axbasic::Variable::Scalar::Numeric::ISA = qw( | ||
| 569 | Language::Axbasic::Variable::Scalar | ||
| 570 | Language::Axbasic::Variable::Numeric | ||
| 571 | ); | ||
| 572 | |||
| 573 | ################## | ||
| 574 | # Constructors | ||
| 575 | |||
| 576 | sub new { | ||
| 577 | |||
| 578 | # Creates the new LA::Variable::Scalar::Numeric | ||
| 579 | # | ||
| 580 | # Expected arguments | ||
| 581 | # $scriptObj - Blessed reference of the parent LA::Script | ||
| 582 | # | ||
| 583 | # Return values | ||
| 584 | # 'undef' on improper arguments | ||
| 585 | # Blessed reference to the newly-created object on success | ||
| 586 | |||
| 587 | my ($class, $scriptObj, $check) = @_; | ||
| 588 | |||
| 589 | # Check for improper arguments | ||
| 590 | if (! defined $class || ! defined $scriptObj || defined $check) { | ||
| 591 | |||
| 592 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 593 | } | ||
| 594 | |||
| 595 | # Setup | ||
| 596 | my $self = { | ||
| 597 | _objName => $class, # Name Axbasic objects after their class | ||
| 598 | _objClass => $class, | ||
| 599 | _parentFile => undef, # No parent file object | ||
| 600 | _parentWorld => undef, # No parent file object | ||
| 601 | _privFlag => TRUE, # All IVs are private | ||
| 602 | |||
| 603 | # Perl object components | ||
| 604 | # ---------------------- | ||
| 605 | |||
| 606 | # The parent LA::Script | ||
| 607 | scriptObj => $scriptObj, | ||
| 608 | |||
| 609 | # IVs | ||
| 610 | # --- | ||
| 611 | |||
| 612 | # The initial value is zero for a numeric scalar | ||
| 613 | value => 0, | ||
| 614 | }; | ||
| 615 | |||
| 616 | bless $self, $class; | ||
| 617 | return $self; | ||
| 618 | } | ||
| 619 | |||
| 620 | ################## | ||
| 621 | # Methods | ||
| 622 | |||
| 623 | ################## | ||
| 624 | # Accessors - set | ||
| 625 | |||
| 626 | ################## | ||
| 627 | # Accessors - get | ||
| 628 | |||
| 629 | sub scriptObj | ||
| 630 | { $_[0]->{scriptObj} } | ||
| 631 | |||
| 632 | sub value | ||
| 633 | { $_[0]->{value} } | ||
| 634 | } | ||
| 635 | |||
| 636 | { package Language::Axbasic::Variable::Array; | ||
| 637 | |||
| 638 | use strict; | ||
| 639 | use warnings; | ||
| 640 | # use diagnostics; | ||
| 641 | |||
| 642 | use Glib qw(TRUE FALSE); | ||
| 643 | |||
| 644 | @Language::Axbasic::Variable::Array::ISA = qw( | ||
| 645 | Language::Axbasic::Variable | ||
| 646 | ); | ||
| 647 | |||
| 648 | ################## | ||
| 649 | # Constructors | ||
| 650 | |||
| 651 | sub new { | ||
| 652 | |||
| 653 | # Paraphrased from Language::Basic | ||
| 654 | # The class that handles an Axbasic array. Each cell in the array is a LA::Variable::Scalar | ||
| 655 | # object | ||
| 656 | # Methods include ->dimension, which dimensions the array to a given size (or a default | ||
| 657 | # size) and ->getCell, which returns the LA::Variable::Scalar object in a given array | ||
| 658 | # location | ||
| 659 | # | ||
| 660 | # Before Axbasic v1.4, array dimensions started at index #1, and empty arrays could not be | ||
| 661 | # created with a DIM statement (but could be created with statements like PEEK) | ||
| 662 | # Since Axbasic v1.4, arrays are handled in two different ways: | ||
| 663 | # | ||
| 664 | # Programmes with line numbers: | ||
| 665 | # Following Dartmouth BASIC, a DIM .. (20) statement creates an array with lower/upper | ||
| 666 | # bounds of 0..20. DIM statements are not required. If a variable with a | ||
| 667 | # subscript is referenced, and the array has not been DIMmed, then an array is | ||
| 668 | # created with each dimension having the bounds 0..10. Unlike Dartmouth BASIC, | ||
| 669 | # arrays are not limited to two dimensions (but the maximum cell limit still | ||
| 670 | # applies). Unlike Dartmouth BASIC, DIM .. () creates an empty one-dimensional | ||
| 671 | # array | ||
| 672 | # | ||
| 673 | # Programmes without line numbers: | ||
| 674 | # Following True BASIC, a DIM statement is always required before a variable with a | ||
| 675 | # subscript can be referenced, or before statements like POP, PUSH (etc) can be | ||
| 676 | # used. However, statements like PEEK still create an array, if one has not yet | ||
| 677 | # been DIMmed. DIM .. () creates an empty one-dimensional array (stack). In a | ||
| 678 | # multi-dimensional array, each dimension must contain at least one cell. A | ||
| 679 | # DIM .. (20) statement creates an array with lower/upper bounds of 1..20. The | ||
| 680 | # programme can specify its own lower/upper bounds for each dimension, or not, as | ||
| 681 | # in the following examples | ||
| 682 | # | ||
| 683 | # DIM data (2, 5, 10) | ||
| 684 | # DIM data (0 TO 2, 0 TO 5, 10 TO 20) | ||
| 685 | # DIM data (2, 5, 10 TO 20) | ||
| 686 | # | ||
| 687 | # Expected arguments | ||
| 688 | # $scriptObj - Blessed reference of the parent LA::Script | ||
| 689 | # $arrayName - The array's name | ||
| 690 | # | ||
| 691 | # Return values | ||
| 692 | # 'undef' on improper arguments | ||
| 693 | # Blessed reference to the newly-created object on success | ||
| 694 | |||
| 695 | my ($class, $scriptObj, $arrayName, $check) = @_; | ||
| 696 | |||
| 697 | # Local variables | ||
| 698 | my ($type, $subClass); | ||
| 699 | |||
| 700 | # Check for improper arguments | ||
| 701 | if (! defined $class || ! defined $scriptObj || ! defined $arrayName || defined $check) { | ||
| 702 | |||
| 703 | return $axmud::CLIENT->writeImproper($class . '->new', @_); | ||
| 704 | } | ||
| 705 | |||
| 706 | # Setup | ||
| 707 | my $self = { | ||
| 708 | _objName => $class, # Name Axbasic objects after their class | ||
| 709 | _objClass => $class, | ||
| 710 | _parentFile => undef, # No parent file object | ||
| 711 | _parentWorld => undef, # No parent file object | ||
| 712 | _privFlag => TRUE, # All IVs are private | ||
| 713 | |||
| 714 | # Perl object components | ||
| 715 | # ---------------------- | ||
| 716 | |||
| 717 | # The parent LA::Script | ||
| 718 | scriptObj => $scriptObj, | ||
| 719 | |||
| 720 | # IVs | ||
| 721 | # ---- | ||
| 722 | |||
| 723 | arrayName => $arrayName, | ||
| 724 | # A 1D, 2D or moreD array of cells. A 1D array is represented as a simple set of values. | ||
| 725 | # For a 2D array, ->cellList contains a set of list references, with each list | ||
| 726 | # reference containing a simple set of values - and so on | ||
| 727 | # Each set of values starts with the lower bound for the dimension, followed by the | ||
| 728 | # array variables themselves (if any) | ||
| 729 | cellList => [], | ||
| 730 | # The current number of dimensions | ||
| 731 | dimCount => 0, | ||
| 732 | # Maximum size of the array - this number refers to the maximum number of cells | ||
| 733 | arrayMaxSize => 1000000, | ||
| 734 | }; | ||
| 735 | |||
| 736 | # If the array name ends with a $, it's a string array, otherwise it's a numeric array | ||
| 737 | if ($arrayName =~ /\$$/) { | ||
| 738 | $type = 'String'; | ||
| 739 | } else { | ||
| 740 | $type = 'Numeric'; | ||
| 741 | } | ||
| 742 | |||
| 743 | # Create a new subclass object | ||
| 744 | $subClass = $class . '::' . $type; | ||
| 745 | bless $self, $subClass; | ||
| 746 | return $self; | ||
| 747 | } | ||
| 748 | |||
| 749 | ################## | ||
| 750 | # Methods | ||
| 751 | |||
| 752 | sub dimension { | ||
| 753 | |||
| 754 | # Called by DIM and REDIM statements. For programmes with line numbers, also called by | ||
| 755 | # LA::Variable->lookup | ||
| 756 | # Re-dimension the array | ||
| 757 | # | ||
| 758 | # Expected arguments | ||
| 759 | # (none besides $self) | ||
| 760 | # | ||
| 761 | # Optional arguments | ||
| 762 | # @boundList - A flat list in groups of two, the lower/upper bounds for each dimension. If | ||
| 763 | # an empty list, a one-dimensional empty array is created | ||
| 764 | # | ||
| 765 | # Return values | ||
| 766 | # 'undef' on improper arguments or if the array is too big | ||
| 767 | # 1 if the redimensioning is successful | ||
| 768 | |||
| 769 | my ($self, @boundList) = @_; | ||
| 770 | |||
| 771 | # Local variables | ||
| 772 | my ( | ||
| 773 | $size, $subClass, $listRef, $dimCount, | ||
| 774 | @sizeList, | ||
| 775 | ); | ||
| 776 | |||
| 777 | # (No improper arguments to check) | ||
| 778 | |||
| 779 | # Check that each bound is a valid integer, that the lower bound is not less than the higher | ||
| 780 | # bound | ||
| 781 | $dimCount = 0; | ||
| 782 | if (! @boundList) { | ||
| 783 | |||
| 784 | # Empty one-dimensional array | ||
| 785 | $dimCount = 1; | ||
| 786 | |||
| 787 | } else { | ||
| 788 | |||
| 789 | for (my $i = 0; $i < (scalar @boundList); $i = $i + 2) { | ||
| 790 | |||
| 791 | my ($lower, $upper); | ||
| 792 | |||
| 793 | $lower = $boundList[$i]; | ||
| 794 | $upper = $boundList[$i + 1]; | ||
| 795 | $dimCount++; | ||
| 796 | |||
| 797 | if (! ($lower =~ m/^[-]?\d+$/)) { | ||
| 798 | |||
| 799 | return $self->scriptObj->setError( | ||
| 800 | 'invalid_array_dimension_size_NUM', | ||
| 801 | $self->_objClass . '->dimension', | ||
| 802 | 'NUM', $lower, | ||
| 803 | ); | ||
| 804 | |||
| 805 | } elsif (! ($upper =~ m/^[-]?\d+$/) || $upper < $lower) { | ||
| 806 | |||
| 807 | return $self->scriptObj->setError( | ||
| 808 | 'invalid_array_dimension_size_NUM', | ||
| 809 | $self->_objClass . '->dimension', | ||
| 810 | 'NUM', $upper, | ||
| 811 | ); | ||
| 812 | } | ||
| 813 | |||
| 814 | push (@sizeList, ($upper - $lower + 1)); | ||
| 815 | } | ||
| 816 | } | ||
| 817 | |||
| 818 | # Check that the total size of the array doesn't exceed the maximum size | ||
| 819 | if (@boundList) { | ||
| 820 | |||
| 821 | $size = 1; | ||
| 822 | foreach my $dim (@sizeList) { | ||
| 823 | |||
| 824 | $size *= ($dim + 1); | ||
| 825 | } | ||
| 826 | |||
| 827 | if ($size > $self->arrayMaxSize) { | ||
| 828 | |||
| 829 | return $self->scriptObj->setError( | ||
| 830 | 'array_exceeds_maximum_size', | ||
| 831 | $self->_objClass . '->dimension', | ||
| 832 | ); | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | # Create a new array, of the right size, full of LA::Scalar::Variable objects | ||
| 837 | $subClass = $self->_objClass; | ||
| 838 | $subClass =~ s/Array/Scalar/; | ||
| 839 | |||
| 840 | $self->ivPoke('dimCount', $dimCount); | ||
| 841 | |||
| 842 | if (! @boundList) { | ||
| 843 | |||
| 844 | # Empty one-dimensional array | ||
| 845 | if ($self->scriptObj->executionMode eq 'line_num') { | ||
| 846 | $self->ivPoke('cellList', 0); | ||
| 847 | } else { | ||
| 848 | $self->ivPoke('cellList', 1); | ||
| 849 | } | ||
| 850 | |||
| 851 | } else { | ||
| 852 | |||
| 853 | $listRef = $self->createListOfLists($subClass, @boundList); | ||
| 854 | $self->ivPoke('cellList', @$listRef); | ||
| 855 | } | ||
| 856 | |||
| 857 | return 1; | ||
| 858 | } | ||
| 859 | |||
| 860 | sub createListOfLists { | ||
| 861 | |||
| 862 | # Called by $self->dimension, or by this function recursively | ||
| 863 | # Create a list of lists in order to form the new array | ||
| 864 | # | ||
| 865 | # Expected arguments | ||
| 866 | # $subClass - The class of objects being created | ||
| 867 | # @boundList - A list in groups of two, the lower/upper bounds for each dimension. If an | ||
| 868 | # empty list, a one-dimensional empty array is created. ($self->dimension has already | ||
| 869 | # checked that the contents of @boundList are valid) | ||
| 870 | # | ||
| 871 | # Return values | ||
| 872 | # Blessed reference to the new subclass (we don't check improper arguments in this | ||
| 873 | # recursive function) | ||
| 874 | |||
| 875 | my ($self, $subClass, @boundList) = @_; | ||
| 876 | |||
| 877 | # Local variables | ||
| 878 | my ( | ||
| 879 | $lower, $upper, | ||
| 880 | @array, | ||
| 881 | ); | ||
| 882 | |||
| 883 | # (Don't check improper arguments) | ||
| 884 | |||
| 885 | if (@boundList) { | ||
| 886 | |||
| 887 | # Recursion | ||
| 888 | $lower = shift(@boundList); | ||
| 889 | $upper = shift(@boundList); | ||
| 890 | |||
| 891 | @array = map {$self->createListOfLists($subClass, @boundList)} ($lower .. $upper); | ||
| 892 | # The first value for each dimension is not an array value, but the lower bound | ||
| 893 | # (Thus the upper bound is the lower bound, plus the number of array values, minus 1) | ||
| 894 | unshift (@array, $lower); | ||
| 895 | |||
| 896 | return \@array; | ||
| 897 | |||
| 898 | } else { | ||
| 899 | |||
| 900 | # End recursion | ||
| 901 | return $subClass->new($self->scriptObj, $self->arrayName); | ||
| 902 | } | ||
| 903 | } | ||
| 904 | |||
| 905 | sub getCell { | ||
| 906 | |||
| 907 | # Get the value stored in a single cell of the array | ||
| 908 | # | ||
| 909 | # Expected arguments | ||
| 910 | # @indices - A list of array indices, e.g. (5, 6, 1) to get the value stored in the | ||
| 911 | # cell at x=5, y=6, z=1 | ||
| 912 | # | ||
| 913 | # Return values | ||
| 914 | # 'undef' on improper arguments or if the cell doesn't exist | ||
| 915 | # Otherwise returns the contents of the cell | ||
| 916 | |||
| 917 | my ($self, @indices) = @_; | ||
| 918 | |||
| 919 | # Local variables | ||
| 920 | my $pointer; | ||
| 921 | |||
| 922 | # Check for improper arguments | ||
| 923 | if (! @indices) { | ||
| 924 | |||
| 925 | return $axmud::CLIENT->writeImproper($self->_objClass . '->getCell', @_); | ||
| 926 | } | ||
| 927 | |||
| 928 | if (scalar @indices != $self->dimCount) { | ||
| 929 | |||
| 930 | return $self->scriptObj->setError( | ||
| 931 | 'subscript_out_of_bounds', | ||
| 932 | $self->_objClass . '->getCell', | ||
| 933 | ); | ||
| 934 | } | ||
| 935 | |||
| 936 | # Set $pointer to the correct cell by stripping away the dimensions, one by one, until only | ||
| 937 | # one cell remains | ||
| 938 | $pointer = $self->{'cellList'}; | ||
| 939 | foreach my $index (@indices) { | ||
| 940 | |||
| 941 | my ($lower, $size); | ||
| 942 | |||
| 943 | # The first value in each dimension is the lower bound, so take that into account | ||
| 944 | $size = scalar @$pointer - 1; | ||
| 945 | $lower = $$pointer[0]; | ||
| 946 | |||
| 947 | # The index can't be outside the lower/upper bounds for this dimension | ||
| 948 | if ( | ||
| 949 | ! ($index =~ m/^[-]?\d+$/) | ||
| 950 | || ($index - $lower + 1) < 1 | ||
| 951 | || $index > ($lower + $size - 1) | ||
| 952 | ) { | ||
| 953 | return $self->scriptObj->setError( | ||
| 954 | 'subscript_out_of_bounds', | ||
| 955 | $self->_objClass . '->getCell', | ||
| 956 | ); | ||
| 957 | } | ||
| 958 | |||
| 959 | $pointer = $pointer->[$index - $lower + 1]; | ||
| 960 | } | ||
| 961 | |||
| 962 | if (! $pointer->isa('Language::Axbasic::Variable::Scalar')) { | ||
| 963 | |||
| 964 | return $self->scriptObj->setDebug( | ||
| 965 | 'Strange array cell class', | ||
| 966 | $self->_objClass . '->getCell', | ||
| 967 | ); | ||
| 968 | |||
| 969 | } else { | ||
| 970 | |||
| 971 | return $pointer; | ||
| 972 | } | ||
| 973 | } | ||
| 974 | |||
| 975 | sub testCell { | ||
| 976 | |||
| 977 | # Tests whether a single cell of the array exists, or not | ||
| 978 | # A modified version of $self->getCell, used by statements like FOR EACH ... NEXT to handle | ||
| 979 | # multi-dimension arrays. That function will throw up an Axbasic error if the cell | ||
| 980 | # doesn't exist; this one merely returns a true or false value | ||
| 981 | # | ||
| 982 | # Expected arguments | ||
| 983 | # @indices - A list of array indices, e.g. (5, 6, 1) to get the value stored in the | ||
| 984 | # cell at x=5, y=6, z=1 | ||
| 985 | # | ||
| 986 | # Return values | ||
| 987 | # 'undef' on improper arguments or if the cell doesn't exist | ||
| 988 | # 1 if the cell exists | ||
| 989 | |||
| 990 | my ($self, @indices) = @_; | ||
| 991 | |||
| 992 | # Local variables | ||
| 993 | my $pointer; | ||
| 994 | |||
| 995 | # Check for improper arguments | ||
| 996 | if (! @indices) { | ||
| 997 | |||
| 998 | return $axmud::CLIENT->writeImproper($self->_objClass . '->testCell', @_); | ||
| 999 | } | ||
| 1000 | |||
| 1001 | if (scalar @indices != $self->dimCount) { | ||
| 1002 | |||
| 1003 | return undef; | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | # Set $pointer to the contents of the correct cell by stripping away the dimensions, one by | ||
| 1007 | # one, until only one cell remains | ||
| 1008 | $pointer = $self->{'cellList'}; | ||
| 1009 | foreach my $index (@indices) { | ||
| 1010 | |||
| 1011 | my ($lower, $size); | ||
| 1012 | |||
| 1013 | # The first value in each dimension is the lower bound, so take that into account | ||
| 1014 | $size = scalar @$pointer - 1; | ||
| 1015 | $lower = $$pointer[0]; | ||
| 1016 | |||
| 1017 | # The index can't be outside the lower/upper bounds for this dimension | ||
| 1018 | if ( | ||
| 1019 | ! ($index =~ m/^[-]?\d+$/) | ||
| 1020 | || ($index - $lower + 1) < 1 | ||
| 1021 | || $index > ($lower + $size - 1) | ||
| 1022 | ) { | ||
| 1023 | return undef; | ||
| 1024 | } else { | ||
| 1025 | $pointer = $pointer->[$index - $lower + 1]; | ||
| 1026 | } | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | if (! $pointer->isa('Language::Axbasic::Variable::Scalar')) { | ||
| 1030 | return undef; | ||
| 1031 | } else { | ||
| 1032 | return 1; | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | # (Called by POP, PUSH, SHIFT and UNSHIFT statements only) | ||
| 1037 | |||
| 1038 | sub doPop { | ||
| 1039 | |||
| 1040 | # Pops an item from the stack (i.e. removes an item from the end of the array) | ||
| 1041 | # If this is a multi-dimensional array, or if the array is empty, returns an empty string or | ||
| 1042 | # zero, as appropriate | ||
| 1043 | # | ||
| 1044 | # Expected arguments | ||
| 1045 | # (none besides $self) | ||
| 1046 | # | ||
| 1047 | # Return values | ||
| 1048 | # 'undef' on improper arguments | ||
| 1049 | # The last item in the array, or an empty string/zero | ||
| 1050 | |||
| 1051 | my ($self, $check) = @_; | ||
| 1052 | |||
| 1053 | # Check for improper arguments | ||
| 1054 | if (defined $check) { | ||
| 1055 | |||
| 1056 | return $axmud::CLIENT->writeImproper($self->_objClass . '->doPop', @_); | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | # (Take account the first item in each dimension, which gives the lower bound) | ||
| 1060 | if ($self->dimCount > 1 || (scalar $self->cellList) <= 1) { | ||
| 1061 | |||
| 1062 | if ($self->isa('Language::Axbasic::Variable::Array::Numeric')) { | ||
| 1063 | return 0; | ||
| 1064 | } else { | ||
| 1065 | return ''; | ||
| 1066 | } | ||
| 1067 | |||
| 1068 | } else { | ||
| 1069 | |||
| 1070 | return $self->ivPop('cellList'); | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | |||
| 1074 | sub doPush { | ||
| 1075 | |||
| 1076 | # Pushes an item to the stack (i.e. adds an item to the end of the array). Does nothing if | ||
| 1077 | # the array is multi-dimensional | ||
| 1078 | # | ||
| 1079 | # Expected arguments | ||
| 1080 | # $value - The value to push to the stack | ||
| 1081 | # | ||
| 1082 | # Return values | ||
| 1083 | # 'undef' on improper arguments, or if the array is multi-dimensional, or if the maximum | ||
| 1084 | # array size has been exceeded | ||
| 1085 | # 1 otherwise | ||
| 1086 | |||
| 1087 | my ($self, $value, $check) = @_; | ||
| 1088 | |||
| 1089 | # Local variables | ||
| 1090 | my ($subClass, $varObj); | ||
| 1091 | |||
| 1092 | # Check for improper arguments | ||
| 1093 | if (! defined $value || defined $check) { | ||
| 1094 | |||
| 1095 | return $axmud::CLIENT->writeImproper($self->_objClass . '->doPush', @_); | ||
| 1096 | } | ||
| 1097 | |||
| 1098 | if ($self->dimCount > 1 || $self->ivNumber('cellList') > $self->arrayMaxSize) { | ||
| 1099 | |||
| 1100 | # Stacks cannot be grids | ||
| 1101 | return undef; | ||
| 1102 | } | ||
| 1103 | |||
| 1104 | # Create a new variable of the right type (numeric or scalar) | ||
| 1105 | $subClass = $self->_objClass; | ||
| 1106 | $subClass =~ s/Array/Scalar/; | ||
| 1107 | $varObj = $subClass->new($self->scriptObj, $self->arrayName); | ||
| 1108 | $varObj->set($value); | ||
| 1109 | # ...and push it to the stack | ||
| 1110 | $self->ivPush('cellList', $varObj); | ||
| 1111 | |||
| 1112 | return 1; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | sub doShift { | ||
| 1116 | |||
| 1117 | # Shifts an item from the stack (i.e. removes an item from the beginning of the array) | ||
| 1118 | # If this is a multi-dimensional array, or if the array is empty, returns an empty string or | ||
| 1119 | # zero, as appropriate | ||
| 1120 | # | ||
| 1121 | # Expected arguments | ||
| 1122 | # (none besides $self) | ||
| 1123 | # | ||
| 1124 | # Return values | ||
| 1125 | # 'undef' on improper arguments | ||
| 1126 | # The first item in the array, or an empty string/zero | ||
| 1127 | |||
| 1128 | my ($self, $check) = @_; | ||
| 1129 | |||
| 1130 | # Local variables | ||
| 1131 | my @list; | ||
| 1132 | |||
| 1133 | # Check for improper arguments | ||
| 1134 | if (defined $check) { | ||
| 1135 | |||
| 1136 | return $axmud::CLIENT->writeImproper($self->_objClass . '->doShift', @_); | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | # (Take account the first item in each dimension, which gives the lower bound) | ||
| 1140 | if ($self->dimCount > 1 || (scalar $self->cellList) <= 1) { | ||
| 1141 | |||
| 1142 | if ($self->isa('Language::Axbasic::Variable::Array::Numeric')) { | ||
| 1143 | return 0; | ||
| 1144 | } else { | ||
| 1145 | return ''; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | } else { | ||
| 1149 | |||
| 1150 | @list = $self->ivSplice('cellList', 1, 1); | ||
| 1151 | # (Return a scalar value, not a list of one) | ||
| 1152 | return $list[0]; | ||
| 1153 | } | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | sub doUnshift { | ||
| 1157 | |||
| 1158 | # Unshifts an item to the stack (i.e. adds an item to the end of the array). Does nothing if | ||
| 1159 | # the array is multi-dimensional | ||
| 1160 | # | ||
| 1161 | # Expected arguments | ||
| 1162 | # $value - The value to unshift to the stack | ||
| 1163 | # | ||
| 1164 | # Return values | ||
| 1165 | # 'undef' on improper arguments, or if the array is multi-dimensional | ||
| 1166 | # 1 otherwise | ||
| 1167 | |||
| 1168 | my ($self, $value, $check) = @_; | ||
| 1169 | |||
| 1170 | # Local variables | ||
| 1171 | my ($subClass, $varObj); | ||
| 1172 | |||
| 1173 | # Check for improper arguments | ||
| 1174 | if (! defined $value || defined $check) { | ||
| 1175 | |||
| 1176 | return $axmud::CLIENT->writeImproper($self->_objClass . '->doUnshift', @_); | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | if ($self->dimCount > 1 || $self->ivNumber('cellList') > $self->arrayMaxSize) { | ||
| 1180 | |||
| 1181 | # Stacks cannot be grids | ||
| 1182 | return undef; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | # Create a new variable of the right type (numeric or scalar) | ||
| 1186 | $subClass = $self->_objClass; | ||
| 1187 | $subClass =~ s/Array/Scalar/; | ||
| 1188 | $varObj = $subClass->new($self->scriptObj, $self->arrayName); | ||
| 1189 | $varObj->set($value); | ||
| 1190 | # ...and unshift it to the stack, taking into account the first item in each dimension, | ||
| 1191 | # which gives the lower bound) | ||
| 1192 | $self->ivSplice('cellList', 1, 0, $varObj); | ||
| 1193 | |||
| 1194 | return 1; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | ################## | ||
| 1198 | # Accessors - set | ||
| 1199 | |||
| 1200 | ################## | ||
| 1201 | # Accessors - get | ||
| 1202 | |||
| 1203 | sub scriptObj | ||
| 1204 | { $_[0]->{scriptObj} } | ||
| 1205 | |||
| 1206 | sub arrayName | ||
| 1207 | { $_[0]->{arrayName} } | ||
| 1208 | sub cellList | ||
| 1209 | { my $self = shift; return @{$self->{cellList}}; } | ||
| 1210 | sub dimCount | ||
| 1211 | { $_[0]->{dimCount} } | ||
| 1212 | |||
| 1213 | sub arrayMaxSize | ||
| 1214 | { $_[0]->{arrayMaxSize} } | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | { package Language::Axbasic::Variable::Array::Numeric; | ||
| 1218 | |||
| 1219 | use strict; | ||
| 1220 | use warnings; | ||
| 1221 | # use diagnostics; | ||
| 1222 | |||
| 1223 | use Glib qw(TRUE FALSE); | ||
| 1224 | |||
| 1225 | @Language::Axbasic::Variable::Array::Numeric::ISA = qw( | ||
| 1226 | Language::Axbasic::Variable::Array | ||
| 1227 | Language::Axbasic::Variable::Numeric | ||
| 1228 | ); | ||
| 1229 | } | ||
| 1230 | |||
| 1231 | { package Language::Axbasic::Variable::Array::String; | ||
| 1232 | |||
| 1233 | use strict; | ||
| 1234 | use warnings; | ||
| 1235 | # use diagnostics; | ||
| 1236 | |||
| 1237 | use Glib qw(TRUE FALSE); | ||
| 1238 | |||
| 1239 | @Language::Axbasic::Variable::Array::String::ISA = qw( | ||
| 1240 | Language::Axbasic::Variable::Array | ||
| 1241 | Language::Axbasic::Variable::String | ||
| 1242 | ); | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | { package Language::Axbasic::Variable::Numeric; | ||
| 1246 | |||
| 1247 | use strict; | ||
| 1248 | use warnings; | ||
| 1249 | # use diagnostics; | ||
| 1250 | |||
| 1251 | use Glib qw(TRUE FALSE); | ||
| 1252 | |||
| 1253 | # Set ISA for 'return type' classes | ||
| 1254 | @Language::Axbasic::Variable::Numeric::ISA = qw( | ||
| 1255 | Language::Axbasic::Variable | ||
| 1256 | Language::Axbasic::Numeric | ||
| 1257 | ); | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | { package Language::Axbasic::Variable::String; | ||
| 1261 | |||
| 1262 | use strict; | ||
| 1263 | use warnings; | ||
| 1264 | # use diagnostics; | ||
| 1265 | |||
| 1266 | use Glib qw(TRUE FALSE); | ||
| 1267 | |||
| 1268 | # Set ISA for 'return type' classes | ||
| 1269 | @Language::Axbasic::Variable::String::ISA = qw( | ||
| 1270 | Language::Axbasic::Variable | ||
| 1271 | Language::Axbasic::String | ||
| 1272 | ); | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | # Package must return a true value | ||
| 1276 | 1 |