blib/lib/Language/Axbasic.pm untested

filter: Statementstmt 0.0% 0 / 1110 Branchbran 0.0% 0 / 382 Conditioncond 0.0% 0 / 339 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 224 Podpod 0.0% 0 / 181 totaltotal 0.0% 0 / 2236 CC 192 SCAR 41.9
CC
192
Cov
0%
terminateExecution
506.0
implement
380.0
upload
210.0
CRAP 66.1
1 # Copyright (C) 2011-2021 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, based on Language::Basic by Amir Karger
16  
17 { package Language::Axbasic;
18  
19 use strict;
20 use warnings;
21 use diagnostics;
22  
23 use Fcntl qw(SEEK_SET SEEK_END);
24 use File::Basename;
25 use Glib qw(TRUE FALSE);
26 use Math::Trig;
27 use POSIX qw(ceil);
28 use Scalar::Util qw(looks_like_number);
29  
30 require Exporter;
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
32 @ISA = qw(Exporter Games::Axmud);
33 @EXPORT = qw();
34  
35 # Sub-packages
36 {
37 package Language::Axbasic::Script;
38 package Language::Axbasic::RawScript;
39 package Language::Axbasic::Subroutine;
40 package Language::Axbasic::Line;
41  
42 package Language::Axbasic::TokenGroup;
43 package Language::Axbasic::Token;
44  
45 package Language::Axbasic::Statement;
46 package Language::Axbasic::Expression;
47 package Language::Axbasic::Variable;
48 package Language::Axbasic::Function;
49  
50 package Language::Axbasic::String;
51 package Language::Axbasic::Boolean;
52 package Language::Axbasic::Numeric;
53 }
54  
55 ##################
56 # Accessors - get
57  
58 # There are five standard IVs for every Axmud (and Axbasic) Perl object
59 sub _objName
60 { $_[0]->{_objName} }
61 sub _objClass
62 { $_[0]->{_objClass} }
63 sub _parentFile
64 { $_[0]->{_parentFile} }
65 sub _parentWorld
66 { $_[0]->{_parentWorld} }
67 sub _privFlag
68 { $_[0]->{_privFlag} }
69 }
70  
71 { package Language::Axbasic::Error;
72  
73 use strict;
74 use warnings;
75 use diagnostics;
76  
77 use Glib qw(TRUE FALSE);
78  
79 @Language::Axbasic::Error::ISA = qw(
80 Language::Axbasic
81 );
82  
83 ##################
84 # Constructors
85  
86 sub new {
87  
88 # Creates a new instance of the Axbasic error object which stores data produced by an
89 # Axbasic error. Called whenever any part of the Axbasic code calls ->setError or
90 # ->setDebug
91 # The details of the error are stored in this object, and this object's blessed reference
92 # can then be added to LA::Script->errorList
93 #
94 # Expected arguments
95 # $scriptObj - Blessed reference to the parent LA::Script
96 # $category - 'error' (calls from ->setError) or 'debug' (calls from ->setDebug)
97 # $errorCode - The error message to display, e.g. 'syntax_error'
98 # $func - A string describing the function that generated the error (e.g.
99 # 'Language::Axbasic::Statement->parse')
100 #
101 # Optional arguments
102 # $line - Which line number in the script produced the error, if any (matches
103 # LA::Script->currentLine, 'undef' if no specific line)
104 # %errorHash - A hash of arguments used to substitute words in $errorCode (see comments
105 # below; can be an empty hash)
106 #
107 # Return values
108 # 'undef' on improper arguments
109 # Blessed reference to the newly-created object on success
110  
111 my ($class, $scriptObj, $category, $errorCode, $func, $line, %errorHash) = @_;
112  
113 # Check for improper arguments
114 if (
115 ! defined $class || ! defined $scriptObj || ! defined $category || ! defined $errorCode
116 || ! defined $func
117 ) {
118 return $axmud::CLIENT->writeImproper($class . '->new', @_);
119 }
120  
121 # Setup
122 my $self = {
123 _objName => $class, # Name Axbasic objects after their class
124 _objClass => $class,
125 _parentFile => undef, # No parent file object
126 _parentWorld => undef, # No parent file object
127 _privFlag => TRUE, # All IVs are private
128  
129 # Perl object components
130 # ----------------------
131  
132 # The parent LA::Script
133 scriptObj => $scriptObj,
134  
135 # IVs
136 # ---
137  
138 # What kind of error this is ('error' or 'debug')
139 category => $category,
140  
141 # The error message itself
142 # $errorCode is a string. For standard error messages, words are separated by underline
143 # characters, e.g. 'syntax_error' or 'illegal_word_WORD'. For non-standard error
144 # messages and debug messages, just use spaces
145 # For standard error messages, any word in $errorCode in CAPITALS, like the
146 # aforementioned WORD, is usually intended to be substituted using %errorHash. (If
147 # the word is not in capitals, substitution will still work)
148 errorCode => $errorCode,
149 # A string describing the function that generated the error
150 func => $func,
151 # Which line number in the script produced the error ('undef' if no specific line)
152 line => $line,
153  
154 # A hash of arguments used to substitute words in $errorCode (empty if there are no
155 # words to substitute)
156 # For example, in the hypothetical error 'WORD_is_an_illegal_word', the key-value pair
157 # added to this hash would be
158 # key: WORD (all capitals signifies that this word should be substituted)
159 # value: the offending word itself, 'prnit' perhaps
160 errorHash => \%errorHash,
161 };
162  
163 bless $self, $class;
164 return $self;
165 }
166  
167 ##################
168 # Methods
169  
170 ##################
171 # Accessors - set
172  
173 ##################
174 # Accessors - get
175  
176 sub scriptObj
177 { $_[0]->{scriptObj} }
178  
179 sub category
180 { $_[0]->{category} }
181  
182 sub errorCode
183 { $_[0]->{errorCode} }
184 sub func
185 { $_[0]->{func} }
186 sub line
187 { $_[0]->{line} }
188  
189 sub errorHash
190 { my $self = shift; return %{$self->{errorHash}}; }
191 }
192  
193 { package Language::Axbasic::RawScript;
194  
195 use strict;
196 use warnings;
197 use diagnostics;
198  
199 use Glib qw(TRUE FALSE);
200  
201 @Language::Axbasic::RawScript::ISA = qw(
202 Language::Axbasic
203 );
204  
205 ##################
206 # Constructors
207  
208 sub new {
209  
210 # The class that handles a whole Axbasic script in its original, human-edited form
211 # (including empty lines, comments, leading whitespace, and primitive line numbers)
212 # The contents of the script can be edited freely. When LA::Script->new is called, it calls
213 # LA::RawScript->upload to make a copy of the script without empty lines, comments, or
214 # primitive line numbers
215 # After the call to LA::RawScript->upload, the copy of the script stored in LA::Script is
216 # fixed (and cannot be edited); meanwhile the copy stored in LA::RawScript can continue to
217 # be edited freely
218 #
219 # Call ->new to create the Perl object, ->loadFile to read a script into memory from a
220 # file, ->saveFile to save a script in memory to file
221 # Don't call ->upload directly; let LA::Script do it
222 #
223 # Expected arguments
224 # $session - The parent GA::Session
225 # $name - A named for the script
226 #
227 # Return values
228 # 'undef' on improper arguments
229 # Blessed reference to the newly-created object on success
230  
231 my ($class, $session, $name, $check) = @_;
232  
233 # Check for improper arguments
234 if (! defined $class || ! defined $session || ! defined $name || defined $check) {
235  
236 return $axmud::CLIENT->writeImproper($class . '->new', @_);
237 }
238  
239 # Setup
240 my $self = {
241 _objName => $name,
242 _objClass => $class,
243 _parentFile => undef, # No parent file object
244 _parentWorld => undef, # No parent file object
245 _privFlag => TRUE, # All IVs are private
246  
247 # Perl object components
248 # ----------------------
249  
250 # The parent GA::Session
251 session => $session,
252  
253 # Raw script IVs
254 # --------------
255  
256 # The script name
257 name => $name,
258 # The full file path from which the script is loaded, every time $self->loadFile is
259 # called
260 filePath => undef,
261  
262 # The lines of the script. A hash in the form
263 # $lineHash{line_number} = string_of_text
264 # ...where 'line_number' is in the range 1 to $self->lineCount
265 #
266 # (NB 'line_number' refers to the physical line number in the script - not the primitive
267 # line number, if it is being used)
268 lineHash => {},
269 # How many lines in total (0 when the script is empty)
270 lineCount => 0,
271  
272 # Flag set to TRUE whenever the script is modified. Set back to FALSE after any load or
273 # save operation
274 modifyFlag => FALSE,
275 };
276  
277 bless $self, $class;
278 return $self;
279 }
280  
281 ##################
282 # Methods
283  
284 sub setError {
285  
286 # Can be called by any of the methods in this object when they notice an error caused by the
287 # Perl Code
288 # (Replaces LA::Script->setError, which can't be called because the corresponding
289 # LA::Script object probably doesn't exist yet)
290 #
291 # Expected arguments
292 # $errorMsg - The error message to display
293 # $func - A string describing the function that generated the error (e.g.
294 # 'Language::Axbasic::Statement->parse')
295 #
296 # Return values
297 # 'undef'
298  
299 my ($self, $errorMsg, $func, $check) = @_;
300  
301 # Check for improper arguments
302 if (! defined $errorMsg || ! defined $func || defined $check) {
303  
304 return $axmud::CLIENT->writeImproper($self->_objClass . '->setError', @_);
305 }
306  
307 $self->session->writeText('AXBASIC: ERROR: ' . $errorMsg . ' (' . $func . ')');
308  
309 # This return value should trickle all the way down to LA::Script->parse or
310 # LA::Script->implement
311 return undef;
312 }
313  
314 sub loadFile {
315  
316 # Loads a file into memory. The file should contain an Axbasic script
317 # If the load succeeds, the contents of $self->lineHash and $self->lineCount are set from
318 # the contents of the file, and ->modifyFlag is set to FALSE.
319 # If the load fails, these IVs are not changed
320 #
321 # Expected arguments
322 # $filePath - Full path to the file to be loaded (must end with .bas)
323 #
324 # Return values
325 # 'undef' on improper arguments, if file loading/saving has been disabled, if the file
326 # doesn't exist, or if it can't be loaded, or if an operation to sort the lines into
327 # the right order fails
328 # 1 otherwise
329  
330 my ($self, $filePath, $check) = @_;
331  
332 # Local variables
333 my (
334 $semaphoreHandle, $fileHandle, $lineCount, $result,
335 %lineHash,
336 );
337  
338 # Check for improper arguments
339 if (! defined $filePath || defined $check) {
340  
341 return $axmud::CLIENT->writeImproper($self->_objClass . '->loadFile', @_);
342 }
343  
344 # Check that file loading is enabled
345 if (! $axmud::CLIENT->loadDataFlag) {
346  
347 return $self->setError(
348 'Can\'t load Axbasic script because file access is disabled',
349 $self->_objClass . '->loadFile',
350 );
351 }
352  
353 # Check that the specified file exists
354 if (! (-e $filePath)) {
355  
356 return $self->setError(
357 'Can\'t load Axbasic script because the file \'' . $filePath . '\' doesn\'t exist',
358 $self->_objClass . '->loadFile',
359 );
360 }
361  
362 # Check that the specified file appears to be a .bas file
363 if (! $filePath =~ m/\.bas$/) {
364  
365 return $self->setError(
366 'Can\'t load Axbasic script because file doesn\'t appear to be a .bas file',
367 $self->_objClass . '->loadFile',
368 );
369 }
370  
371 # Get a shared lock on the twinned semaphore file using GA::Obj::File's file lock functions
372 $semaphoreHandle = $axmud::CLIENT->configFileObj->getFileLock(
373 $axmud::DATA_DIR . '/data/temp/axbasic.sem', # Semaphore file
374 FALSE, # Shared lock for reading
375 $filePath,
376 );
377  
378 # Don't try to load the file if no lock could be taken out on the semaphore file
379 if (! $semaphoreHandle) {
380  
381 return $self->setError(
382 'General error loading the Axbasic script',
383 $self->_objClass . '->loadFile',
384 );
385 }
386  
387 # Open the file for reading
388 if (! open ($fileHandle, "<$filePath")) {
389  
390 return $self->setError(
391 'General error loading the Axbasic script',
392 $self->_objClass . '->loadFile',
393 );
394 }
395  
396 # Read the file
397 $lineCount = 0;
398 while (defined (my $line = <$fileHandle>)) {
399  
400 chomp $line;
401 $lineCount++;
402 $lineHash{$lineCount} = $line;
403 }
404  
405 # Close the file
406 $result = close $fileHandle;
407 # Release the lock on the semaphore file
408 $axmud::CLIENT->configFileObj->releaseFileLock($semaphoreHandle);
409  
410 if ($result) {
411  
412 # Loading succeeded. Transfer the file's contents to this object's IVs
413 $self->ivPoke('filePath', $filePath);
414 $self->ivPoke('lineHash', %lineHash);
415 $self->ivPoke('lineCount', $lineCount);
416 $self->ivPoke('modifyFlag', FALSE);
417  
418 return 1;
419  
420 } else {
421  
422 # Loading failed
423 return $self->setError(
424 'General error loading the Axbasic script',
425 $self->_objClass . '->loadFile',
426 );
427 }
428 }
429  
430 sub saveFile {
431  
432 # Saves a file from memory. The file should contain an Axbasic script.
433 # If the save succeeds, ->modifyFlag is set to FALSE. If the save fails, the flag is not
434 # changed
435 #
436 # Expected arguments
437 # $filePath - Full path to the file to be saved (must end with .bas)
438 #
439 # Return values
440 # 'undef' on improper arguments, if file loading/saving has been disabled, if the script
441 # in memory is empty or if the file can't be saved
442 # 1 otherwise
443  
444 my ($self, $filePath, $check) = @_;
445  
446 # Local variables
447 my (
448 $semaphoreHandle, $fileHandle, $result,
449 @list,
450 );
451  
452 # Check for improper arguments
453 if (! defined $filePath || defined $check) {
454  
455 return $axmud::CLIENT->writeImproper($self->_objClass . '->saveFile', @_);
456 }
457  
458 # Check that file saving is enabled
459 if (! $axmud::CLIENT->saveDataFlag) {
460  
461 return $self->setError(
462 'Can\'t save Axbasic script because file access is disabled',
463 $self->_objClass . '->saveFile',
464 );
465 }
466  
467 # Check that the specified file appears to be a .bas file
468 if (! $filePath =~ m/\.bas$/) {
469  
470 return $self->setError(
471 'Can\'t save Axbasic script because file doesn\'t appear to be a .bas file',
472 $self->_objClass . '->saveFile',
473 );
474 }
475  
476 # Don't save anything if the script is empty
477 if (! $self->lineCount) {
478  
479 return $self->setError(
480 'Can\'t save Axbasic script because the script in memory is empty',
481 $self->_objClass . '->saveFile',
482 );
483 }
484  
485 # Get an exclusive lock on the twinned semaphore file using GA::Obj::FileObj's file lock
486 $semaphoreHandle = $axmud::CLIENT->configFileObj->getFileLock(
487 $axmud::DATA_DIR . '/data/temp/axbasic.sem', # Semaphore file
488 TRUE, # Exclusive lock for writing
489 $filePath,
490 );
491  
492 # Don't try to save the file if no lock could be taken out on the semaphore file
493 if (! defined $semaphoreHandle) {
494  
495 return $self->setError(
496 'General error saving the Axbasic script',
497 $self->_objClass . '->saveFile',
498 );
499 }
500  
501 # Open the file for writing, overwriting previous contents
502 if (! open ($fileHandle, ">$filePath")) {
503  
504 return $self->setError(
505 'General error saving the Axbasic script',
506 $self->_objClass . '->saveFile',
507 );
508 }
509  
510 # Retrieve the contents of the script as a list of strings, in order, from 1 to
511 # $self->lineCount
512 if ($self->lineCount) {
513  
514 for (my $count = 1; $count <= $self->lineCount; $count++) {
515  
516 if ($self->ivExists('lineHash', $count)) {
517  
518 push (@list, $self->ivShow('lineHash', $count));
519 }
520 }
521 }
522  
523 # Add newline characters to every entry in @list before writing the whole list to the file
524 foreach my $item (@list) {
525  
526 $item .= "\n";
527 }
528  
529 print $fileHandle @list;
530  
531 # Writing complete
532 $result = close $fileHandle;
533 # Release the lock on the semaphore file
534 $axmud::CLIENT->configFileObj->releaseFileLock($semaphoreHandle);
535  
536 if ($result) {
537  
538 # Saving succeeded
539 $self->ivPoke('modifyFlag', FALSE);
540  
541 return 1;
542  
543 } else {
544  
545 # Saving failed
546 return $self->setError(
547 'General error saving the Axbasic script',
548 $self->_objClass . '->saveFile',
549 );
550 }
551 }
552  
553 sub upload {
554  
555 # Called by LA::Script->new
556 # Processes the script in memory to remove all the extraneous stuff, including:
557 # - empty lines
558 # - leading whitespace at the beginning of lines
559 # - lines starting with comments (REM xxx)
560 # Also checks whether the Axbasic script seems to be using primitive line numbers. If so,
561 # the line numbers are removed from the beginning of each line and stored instead in a
562 # LA::Line object
563 # (However, statements like 'goto 550' don't have the line number removed - it's up to
564 # LA::Statement::Goto to deal with it)
565 #
566 # From the resulting stripped-down script, for every line of Axbasic, a new LA::Line
567 # object is created, and its blessed reference added to LA::Script->lineHash
568 # In this manner, the raw script stored in a LA::RawScript object is uploaded as a
569 # stripped-down copy of the script stored in a LA::Script object
570 #
571 # Expected arguments
572 # $scriptObj - Blessed reference to the LA::Script object, to which the script must
573 # be uploaded
574 #
575 # Return values
576 # 'undef' on improper arguments, or if there is no script in memory, if there is an error
577 # creating a LA::Line object, or if the script is empty (once all the extraneous stuff
578 # has been removed)
579 # 1 on success
580  
581 my ($self, $scriptObj, $check) = @_;
582  
583 # Local variables
584 my (
585 $count, $procLineCount, $firstLine, $executionMode,
586 @initialList,
587 %uploadHash, %primLineHash,
588 );
589  
590 # Check for improper arguments
591 if (! defined $scriptObj || defined $check) {
592  
593 return $axmud::CLIENT->writeImproper($self->_objClass . '->upload', @_);
594 }
595  
596 # Check that there's a script in memory
597 if (! $self->lineCount) {
598  
599 return $self->setError(
600 'Can\'t process Axbasic script (the file is probably empty)',
601 $self->_objClass . '->upload',
602 );
603 }
604  
605 # Find the first line which contains a character. Test the first character on that line;
606 # if it's a digit, assume that the script uses primitive line numbering
607 $count = 0;
608 do {
609  
610 $count++;
611 $firstLine = $self->ivShow('lineHash', $count);
612  
613 if ($firstLine =~ m/\w/) {
614  
615 # This contains at least one character, so use it as the first line
616 $count = $self->lineCount; # Escape the loop
617 }
618  
619 } until ($count >= $self->lineCount);
620  
621 if ($firstLine =~ m/^\s*(\d+)\s+/) {
622  
623 # Primitive line numbering is now in effect
624 $executionMode = 'line_num';
625  
626 # Make sure all the lines in the script are in the right order
627 $self->sortLines();
628  
629 } else {
630  
631 # No primitive line numbering
632 $executionMode = 'no_line_num';
633 }
634  
635 # For each line in the script...
636 $procLineCount = 0;
637 OUTER: for (my $origLineCount = 1; $origLineCount <= $self->lineCount; $origLineCount++) {
638  
639 my ($lineText, $lineObj);
640  
641 # Check that the line exists
642 if (! $self->ivExists('lineHash', $origLineCount)) {
643  
644 # There is no reason why it should not exist - it's an error
645 return $self->setError(
646 'Can\'t process Axbasic script, missing line #' . $origLineCount,
647 $self->_objClass . '->upload',
648 );
649 }
650  
651 # Check that the line actually contains some text
652 $lineText = $self->ivShow('lineHash', $origLineCount);
653 if (! $lineText || $lineText =~ m/^\s+$/) {
654  
655 # This is an empty line. Move on to the next one
656 next OUTER;
657 }
658  
659 # No primitive line numbering...
660 if ($executionMode eq 'no_line_num') {
661  
662 # Remove any whitespace from the beginning and end of the line
663 $lineText = $axmud::CLIENT->trimWhitespace($lineText);
664  
665 # If the whole line is a comment, ignore it
666 if ($lineText =~ /^rem /i) {
667  
668 next OUTER;
669 }
670  
671 # Create a LA::Line with what's left of the line (after whitespace was removed)
672 $procLineCount++;
673 $lineObj = Language::Axbasic::Line->new(
674 $scriptObj,
675 $lineText,
676 $origLineCount,
677 $procLineCount,
678 );
679  
680 if (! defined $lineObj) {
681  
682 return $self->setError(
683 'Can\'t upload Axbasic script, error creating line #' . $origLineCount,
684 $self->_objClass . 'upload',
685 );
686  
687 } else {
688  
689 $uploadHash{$procLineCount} = $lineObj;
690 }
691  
692 # Primitive line numbering...
693 } elsif ($executionMode eq 'line_num') {
694  
695 my ($result, $primLineNum, $lineBody);
696  
697 # Check that the line starts with a number, and has a body (text after the line
698 # number)
699 $result = $lineText =~ m/^\s*(\d+)\s+(.*)/;
700 $primLineNum = $1;
701 $lineBody = $2;
702  
703 if (! $result) {
704  
705 return $self->setError(
706 'Can\'t process Axbasic script, invalid primitive line number format at'
707 . ' line #' . $origLineCount,
708 $self->_objClass . '->upload',
709 );
710 }
711  
712 # Remove any whitespace from the end of the line body
713 $lineBody =~ s/\s+$//;
714  
715 # Create a LA::Line with the line body
716 $procLineCount++;
717 $lineObj = Language::Axbasic::Line->new(
718 $scriptObj,
719 $lineBody,
720 $origLineCount,
721 $procLineCount,
722 $primLineNum,
723 );
724  
725 if (! defined $lineObj) {
726  
727 return $self->setError(
728 'Can\'t process Axbasic script, error creating line #'
729 . $origLineCount,
730 $self->_objClass . '->upload',
731 );
732  
733 } else {
734  
735 $uploadHash{$procLineCount} = $lineObj;
736 # Update the hash which converts the primitive line number into the processed
737 # line number
738 $primLineHash{$primLineNum} = $procLineCount;
739 }
740 }
741 }
742  
743 if (! $procLineCount) {
744  
745 # Nothing to upload. Use the same error message for an empty file, as for a file which
746 # just contains a load of whitespace
747 return $self->setError(
748 'Can\'t process Axbasic script (the file is probably empty)',
749 $self->_objClass . '->upload',
750 );
751  
752 } else {
753  
754 # Upload the processed script to the LA::Script object
755 $scriptObj->download(
756 ($procLineCount + 1), # Stored as LA::Script->lineCount
757 $executionMode, # ->executionMode
758 \%uploadHash, # ->lineHash
759 \%primLineHash, # ->primLineHash
760 );
761  
762 return 1;
763 }
764 }
765  
766 sub sortLines {
767  
768 # Called by $self->upload
769 # Sorts the script's lines into the right order
770 # e.g. '20 goto 10 : 10 print "hello"' becomes '10 print "hello" : 20 goto 10'
771 #
772 # Expected arguments
773 # (none besides $self)
774 #
775 # Return values
776 # 'undef' on improper arguments
777 # 1 on success
778  
779 my ($self, $check) = @_;
780  
781 # Local variables
782 my (
783 $total,
784 @list, @sortedList,
785 %lineHash, %tempHash,
786 );
787  
788 # Check for improper arguments
789 if (defined $check) {
790  
791 return $axmud::CLIENT->writeImproper($self->_objClass . '->sortLines', @_);
792 }
793  
794 # First import the values of the $self->lineHash into a list, so we can re-fill it. The
795 # current hash keys are now irrelevant
796 @list = $self->ivValues('lineHash');
797  
798 # Put the lines into a new hash, one by one
799 # The keys of the hash are the primitive line number. The values are the whole line,
800 # including the primitive line number
801 foreach my $line (@list) {
802  
803 my $primLineNum;
804  
805 if ($line =~ m/^\s*(\d+)(\s+)/) {
806  
807 $primLineNum = $1;
808  
809 # Check that a line with the same number doesn't already exist (duplicates are not
810 # allowed)
811 if (exists $tempHash{$primLineNum}) {
812  
813 return $self->setError(
814 'Duplicate line #' . $primLineNum,
815 $self->_objClass . '->sortLines',
816 );
817  
818 # Otherwise use this line
819 } else {
820  
821 $tempHash{$primLineNum} = $line;
822 }
823  
824 } else {
825  
826 # Now that we're using primitive line numbering, lines without numbers are not
827 # allowed (but empty lines are allowed, and just ignored)
828 if ($line =~ m/\w/) {
829  
830 return $self->setError(
831 'Un-numbered line in raw script (expecting all lines to be numbered)',
832 $self->_objClass . '->sortLines',
833 );
834 }
835 }
836 }
837  
838 # Compile a sorted list of the primitive line numbers
839 @sortedList = sort {$a <=> $b} (keys %tempHash);
840  
841 # Re-stock the original hash
842 $total = scalar @sortedList;
843 for (my $count = 1; $count <= $total; $count++) {
844  
845 $lineHash{$count} = $tempHash{shift @sortedList};
846 }
847  
848 return 1;
849 }
850  
851 ##################
852 # Accessors - set
853  
854 ##################
855 # Accessors - get
856  
857 sub session
858 { $_[0]->{session} }
859  
860 sub name
861 { $_[0]->{name} }
862 sub filePath
863 { $_[0]->{filePath} }
864  
865 sub lineHash
866 { my $self = shift; return %{$self->{lineHash}}; }
867 sub lineCount
868 { $_[0]->{lineCount} }
869  
870 sub modifyFlag
871 { $_[0]->{modifyFlag} }
872 }
873  
874 { package Language::Axbasic::Script;
875  
876 use strict;
877 use warnings;
878 use diagnostics;
879  
880 use Glib qw(TRUE FALSE);
881  
882 @Language::Axbasic::Script::ISA = qw(
883 Language::Axbasic
884 );
885  
886 ##################
887 # Constructors
888  
889 sub new {
890  
891 # The class that handles a whole script in a processed form (with all the empty lines,
892 # comments, etc removed).
893 # Call ->new to create a LA::Script object, which automatically downloads the code from
894 # the specified LA::RawScript object
895 # (LA::RawScript contains a script includes whitespace, empty lines, comments and possibly
896 # line numbers; LA::Script contains a script with all that stuff removed)
897 #
898 # Once created, the script stored in this object can be accessed in two modes:
899 # - Call ->parse to parse the script, looking for errors, but don't actually execute it
900 # - Call ->implement to parse the script, line-by-line, and then execute it
901 # If the Axbasic script pauses (usually when the script is being executed as an Axmud task),
902 # call ->parse or ->implement again to resume execution
903 #
904 # Expected arguments
905 # $session - The parent GA::Session
906 #
907 # Optional arguments
908 # $rawScriptObj - Blessed reference of the LA::RawScript from which to download the
909 # Axbasic script. If not specified, no script is downloaded (this
910 # is useful for accessing default values stored in this object's IVs)
911 #
912 # Return values
913 # 'undef' on improper arguments, or if the specified raw script can't be uploaded or if
914 # there's an error setting up intrinsic function objects
915 # Blessed reference to the newly-created object on success
916  
917 my ($class, $session, $rawScriptObj, $check) = @_;
918  
919 # Local variables
920 my (
921 @keywordList, @logicalOpList,
922 %keywordHash,
923 );
924  
925 # Check for improper arguments
926 if (! defined $class || ! defined $session || defined $check) {
927  
928 return $axmud::CLIENT->writeImproper($class . '->new', @_);
929 }
930  
931 # Define the list of Axbasic keywords - used to set more than one IV
932 @keywordList = (
933 'access', 'addalias', 'addcongauge', 'addconstatus', 'addgauge', 'addhook', 'addmacro',
934 'addstatus', 'addtimer', 'addtrig', 'angle', 'array', 'beep', 'begin', 'break',
935 'bypass', 'call', 'case', 'client', 'cls', 'close', 'closewin', 'create', 'data',
936 'debug', 'def', 'degrees', 'delalias', 'delgauge', 'delhook', 'deliface', 'delmacro',
937 'delstatus', 'deltimer', 'deltrig', 'dim', 'dimensions', 'do', 'each', 'else',
938 'elseif', 'emptywin', 'end', 'erase', 'error', 'exit', 'flashwin', 'for', 'global',
939 'gosub', 'goto', 'help', 'if', 'in', 'input', 'let', 'local', 'login', 'loop', 'lower',
940 'multi', 'move', 'name', 'needtask', 'new', 'newold', 'next', 'nextiface', 'nolet',
941 'numeric', 'old', 'on', 'open', 'openentry', 'openwin', 'option', 'org', 'organization',
942 'outin', 'output', 'paintwin', 'pause', 'peek', 'peekequals', 'peekexists', 'peekfind',
943 'peekfirst', 'peekget', 'peekindex', 'peekkeys', 'peeklast', 'peekmatch', 'peeknumber',
944 'peekpairs', 'peekshow', 'peekvalues', 'perl', 'persist', 'play', 'poke', 'pokeadd',
945 'pokedec', 'pokedechash', 'pokedelete', 'pokedivide', 'pokeempty', 'pokefalse',
946 'pokeinc', 'pokeinchash', 'pokeint', 'pokeminus', 'pokemultiply', 'pokeplus', 'pokepop',
947 'pokepush', 'pokereplace', 'pokeset', 'pokeshift', 'poketrue', 'pokeundef',
948 'pokeunshift', 'pop', 'print', 'profile', 'pseudo', 'push', 'radians', 'randomize',
949 'read', 'redim', 'redirect', 'relay', 'rem', 'require', 'reset', 'restore', 'return',
950 'revpath', 'select', 'send', 'setalias', 'setgauge', 'sethook', 'setmacro', 'setstatus',
951 'settimer', 'settrig', 'shift', 'silent', 'size', 'skipiface', 'sleep', 'sort',
952 'sortcase', 'sortcaser', 'sortr', 'speak', 'speed', 'step', 'stop', 'string', 'sub',
953 'tab', 'text', 'titlewin', 'then', 'to', 'typo', 'unflashwin', 'unshift', 'until',
954 'upper', 'waitactive', 'waitalias', 'waitalive', 'waitarrive', 'waitdead', 'waitep',
955 'waitgp', 'waithook', 'waithp', 'waitmacro', 'waitmp', 'waitnextxp', 'waitnotactive',
956 'waitpassout', 'waitscript', 'waitsleep', 'waitsp', 'waittask', 'waittimer',
957 'waittotalxp', 'waittrig', 'waitxp', 'warning', 'while', 'write', 'writewin',
958 );
959  
960 foreach my $keyword (@keywordList) {
961  
962 $keywordHash{$keyword} = undef;
963 }
964  
965 # Define the list of logical operators - used to set more than one IV
966 @logicalOpList = ('and', 'or', 'not');
967  
968 # Setup
969 my $self = {
970 _objName => $class, # Name Axbasic objects after their class
971 _objClass => $class,
972 _parentFile => undef, # No parent file object
973 _parentWorld => undef, # No parent file object
974 _privFlag => TRUE, # All IVs are private
975  
976 # Perl object components
977 # ----------------------
978  
979 # The parent GA::Session
980 session => $session,
981  
982 # Script IVs
983 # ----------
984  
985 # The script name (matches the ->name of the LA::RawScript object from which the Axbasic
986 # script has been downloaded; set to 'undef' until then)
987 name => undef,
988 # The full file path from which the script is loaded, every time LA::RawScript->loadFile
989 # is called (set by this function, if a script is downloaded, but remains set to
990 # 'undef' otherwise)
991 filePath => undef,
992  
993 # All the lines in the script, a hash in the form
994 # $lineHash{line_number} = blessed_reference_to_LA::Line object
995 # The first line number is $lineHash{1}
996 lineHash => {},
997 # How many lines in total (0 when the script is empty)
998 lineCount => 0,
999 # The current line number (matches a key in $self->lineHash)
1000 currentLine => undef,
1001 # The next line to execute (the line's ->procLineNum, not its blessed reference)
1002 nextLine => undef,
1003 # Blessed reference of the next statement to execute
1004 nextStatement => undef,
1005  
1006 # A hash to convert primitive line numbers into ->procLineNum values
1007 # e.g. 10 PRINT "hello"
1008 # 20 GOTO 10
1009 # key = 10, value = 0 / key = 20, value = 1
1010 primLineHash => {},
1011  
1012 # Execution mode:
1013 # 'not_set' - not set yet
1014 # 'no_line_num' - modern-style BASIC scripts without line numbers, GOTO, etc
1015 # 'line_num' primitive BASIC scripts with line numbers, GOTO, etc
1016 # When LA::RawScript->upload is called, it sets this IV. If the first character in
1017 # the raw script is a number, it assumes that it's a primitive script with line
1018 # numbers, and sets this IV to 'line_num'. Otherwise, sets it to 'no_line_num'
1019 executionMode => 'not_set',
1020 # Normally, only the first error message generated is displayed in the 'main' window by
1021 # $self->terminateExecution. In that case, this flag is set to FALSE.
1022 # If the flag is set to TRUE, all error messages are displayed (this can make for a
1023 # confusing experience for the user, which is why it's turned off by default)
1024 allErrorsFlag => FALSE,
1025 # Flag set to TRUE at the start of a call to $self->implement, and set back to FALSE
1026 # when that function ends.
1027 # When the flag is true, any recursive calls to $self->implement are ignored; this
1028 # prevents any part of the Axmud code calling ->implement, which in turn calls some
1029 # part of the Axmud code, which in turns calls ->implement again
1030 implementFlag => FALSE,
1031  
1032 # A list of all Axbasic keywords
1033 keywordList => \@keywordList,
1034 # A hash of keywords (for lookup by the Axbasic help functions), in the form
1035 # $keywordHash{keyword} = undef
1036 keywordHash => \%keywordHash,
1037 # A list of Axbasic keywords only available in $self->executionMode 'no_line_num'
1038 # (modern-style BASIC scripts)
1039 modernKeywordList => [
1040 'call', 'case', 'do', 'exit', 'global', 'local', 'loop', 'numeric', 'select',
1041 'string', 'sub', 'until', 'while',
1042 ],
1043 # A list of Axbasic keywords only available in $self->executionMode 'line_num'
1044 # (primitive BASIC scripts)
1045 primKeywordList => [
1046 'goto', 'gosub', 'on',
1047 ],
1048 # A list of keywords used to set/fetch Axmud's internal variables
1049 peekPokeList => [
1050 'peek', 'peekequals', 'peekexists', 'peekfind', 'peekfirst', 'peekget', 'peekindex',
1051 'peekkeys', 'peeklast', 'peekmatch', 'peeknumber', 'peekpairs', 'peekshow',
1052 'peekvalues', 'poke', 'pokeadd', 'pokedec', 'pokedechash', 'pokedelete',
1053 'pokedivide', 'pokeempty', 'pokefalse', 'pokeinc', 'pokeinchash', 'pokeint',
1054 'pokeminus', 'pokemultiply', 'pokeplus', 'pokepop', 'pokepush', 'pokereplace',
1055 'pokeset', 'pokeshift', 'poketrue', 'pokeundef', 'pokeunshift',
1056 ],
1057 # A hash of keywords that can't begin a statement. The keys in the hash are the keywords
1058 # themselves; the corresponding values are keywords which can begin a statement. When
1059 # the user types a command like ';axbasic help angle', they are presented help for the
1060 # value ('option') corresponding to the key ('angle')
1061 weakKeywordHash => {
1062 'access' => 'open',
1063 'array' => 'peek', # Also used in POKE, PEEK..., POKE...
1064 'angle' => 'option',
1065 'begin' => 'reset',
1066 'create' => 'open',
1067 'degrees' => 'option',
1068 'dimensions' => 'let',
1069 'each' => 'for',
1070 'in' => 'for',
1071 'lower' => 'let',
1072 'name' => 'open',
1073 'needtask' => 'option',
1074 'new' => 'open',
1075 'newold' => 'open',
1076 'nolet' => 'option',
1077 'numeric' => 'sub',
1078 'old' => 'open',
1079 'org' => 'open',
1080 'organization' => 'open',
1081 'outin' => 'open',
1082 'output' => 'open',
1083 'persist' => 'option',
1084 'pseudo' => 'option',
1085 'radians' => 'option',
1086 'redirect' => 'option',
1087 'require' => 'option',
1088 'silent' => 'option',
1089 'size' => 'let',
1090 'step' => 'for',
1091 'string' => 'sub',
1092 'tab' => 'print',
1093 'text' => 'open',
1094 'then' => 'for',
1095 'to' => 'for',
1096 'typo' => 'option',
1097 'upper' => 'let',
1098 },
1099 # A hash of keywords that are either synonyms of other keywords (such as PAUSE and
1100 # SLEEP), or whose code is so similar that their LA::Statement objects should be
1101 # combined (such as ADDGAUGE and ADDCONGAUGE)
1102 equivKeywordHash => {
1103 'addcongauge' => 'addgauge',
1104 'addconstatus' => 'addstatus',
1105 'elseif' => 'else',
1106 'sleep' => 'pause',
1107 },
1108 # A list of statements that interact directly with Axmud
1109 clientKeywordList => [
1110 'addalias', 'addcongauge', 'addconstatus', 'addgauge', 'addstatus', 'addhook',
1111 'addmacro', 'addtimer', 'addtrig', 'array', 'beep', 'break', 'bypass', 'client',
1112 'closewin', 'debug', 'delalias', 'delgauge', 'deliface', 'delhook', 'delmacro',
1113 'delstatus', 'deltimer', 'deltrig', 'emptywin', 'error', 'flashwin', 'login',
1114 'move', 'multi', 'nextiface', 'openentry', 'openwin', 'paintwin', 'pause', 'peek',
1115 'peekequals', 'peekexists', 'peekfind', 'peekfirst', 'peekget', 'peekindex',
1116 'peekkeys', 'peeklast', 'peekmatch', 'peeknumber', 'peekpairs', 'peekshow',
1117 'peekvalues', 'perl', 'play', 'poke', 'pokeadd', 'pokedec', 'pokedechash',
1118 'pokedelete', 'pokedivide', 'pokeempty', 'pokefalse', 'pokeinc', 'pokeinchash',
1119 'pokeint', 'pokeminus', 'pokemultiply', 'pokeplus', 'pokepop', 'pokepush',
1120 'pokereplace', 'pokeset', 'pokeshift', 'poketrue', 'pokeundef', 'pokeunshift',
1121 'profile', 'relay', 'revpath', 'send', 'setalias', 'setgauge', 'sethook',
1122 'setmacro', 'setstatus', 'settimer', 'settrig', 'skipiface', 'sleep', 'speak',
1123 'speed', 'titlewin', 'unflashwin', 'waitactive', 'waitalias', 'waitalive',
1124 'waitarrive', 'waitdead', 'waitep', 'waitgp', 'waithook', 'waithp', 'waitmacro',
1125 'waitmp', 'waitnextxp', 'waitpassout', 'waitscript', 'waitsleep', 'waitsp',
1126 'waittask', 'waittimer', 'waittotalxp', 'waittrig', 'waitxp', 'warning', 'write',
1127 'writewin',
1128 ],
1129 # A list of keywords that are ignored when the Axbasic script isn't being run from
1130 # within a task
1131 taskKeywordList => [
1132 'addcongauge', 'addgauge', 'addconstatus', 'addstatus', 'break', 'closewin',
1133 'delgauge', 'emptywin', 'flashwin', 'openentry', 'openwin', 'paintwin', 'pause',
1134 'setalias', 'setgauge', 'sethook', 'setmacro', 'settimer', 'settrig', 'sleep',
1135 'titlewin', 'unflashwin', 'waitactive', 'waitalias', 'waitalive', 'waitarrive',
1136 'waitdead', 'waitep', 'waitgp', 'waithook', 'waithp', 'waitmacro', 'waitmp',
1137 'waitnextxp', 'waitnotactive', 'waitpassout', 'waitscript', 'waitsleep', 'waitsp',
1138 'waittask', 'waittimer', 'waittotalxp', 'waittrig', 'waitxp', 'writewin',
1139 ],
1140  
1141 # A list of logical operators ('and', 'or', 'not')
1142 logicalOpList => \@logicalOpList,
1143 # A list of regular expression with which to split a line of Axbasic into a series of
1144 # tokens, in the format
1145 # $regexHash{token_category} = regex_to_identify_it
1146 regexHash => {
1147 'short_comment' => '(?i)!($|.*)',
1148 'comment' => '(?i)REM($|\s.*)',
1149 'keyword' => "(?i)(" . join("|", @keywordList) . ")\\b",
1150 'identifier' => '(?i)[A-Z][A-Z0-9_]*\\$?',
1151 'logical_operator' => "(?i)(" . join("|", @logicalOpList) . ")\\b",
1152 'string_constant' => '".*?"',
1153 'numeric_constant' => '(\\d*\\.)?\\d+',
1154 'left_paren' => '\\(',
1155 'right_paren' => '\\)',
1156 'file_channel' => '\#[0-9]+\s*\:?',
1157 'separator' => '[,;]' ,
1158 'arithmetic_operator' => '[-+&]',
1159 'multiplicative_operator'
1160 => '[*/\^]',
1161 'relational_operator' => '<[=>]?|>=?|=',
1162 'assignment_operator' => '=',
1163 'statement_end' => ':',
1164 },
1165  
1166 # The list of token categories, in a standard order
1167 categoryList => [
1168 'short_comment',
1169 'comment',
1170 'keyword',
1171 'identifier',
1172 'logical_operator',
1173 'string_constant',
1174 'numeric_constant',
1175 'left_paren',
1176 'right_paren',
1177 'file_channel',
1178 'separator',
1179 'arithmetic_operator',
1180 'multiplicative_operator',
1181 'relational_operator',
1182 'assignment_operator',
1183 'statement_end',
1184 ],
1185  
1186 # The script's status:
1187 # 'waiting' - parsing (or implementation) not yet started
1188 # 'parsing' - being parsed (or implemented)
1189 # 'paused' - being parsed (or implemented), but paused (normally when the Axbasic
1190 # script is being run as an Axmud task)
1191 # 'wait_input' - being implemented (when the Axbasic script is being run as an Axmud
1192 # task), but waiting for INPUT
1193 # 'finished' - parsed (or implemented) and finished
1194 # 'basic_error' - parsed (or implemented) but found an error in the Axbasic script
1195 # 'perl_error' - parsed (or implemented) but found an error in the Perl Code
1196 # 'wait_status' - being implemented, but execution has halted after a statement like
1197 # WAITHP. Control is passed back to the parent task, but the parent task isn't
1198 # paused
1199 # 'wait_active' - being implemented, but execution has halted after a WAITACTIVE
1200 # statement. Control is passed back to the parent task, but the parent task isn't
1201 # paused
1202 # 'wait_not_active' - being implemented, but execution has halted after a
1203 # WAITNOTACTIVE statement. Control is passed back to the parent task, but the
1204 # parent task isn't paused
1205 # NB Only the first error message sets ->scriptStatus
1206 scriptStatus => 'waiting',
1207 # The execution mode
1208 # 'waiting' - execution not yet started
1209 # 'parse' - parse, but don't execute (used for checking for syntax errors), set when
1210 # $self->parse is called
1211 # 'implement - execute (implement), set when $self->implement is called
1212 # 'finished' - parsing/implementation finished
1213 executionStatus => 'waiting',
1214  
1215 # Blessed reference of the Script task running the script ('undef' if the script isn't
1216 # being run from within a task)
1217 parentTask => undef,
1218 # When the script is being run by an Axmud task, it's useful to limit the number of
1219 # steps allowed on each spin of the task loop; so that if the script gets into a very
1220 # long calculation (or even an infinite loop), the Axmud client doesn't freeze
1221 # If the value is set to 0, there is no maximum: the script will keep running until a
1222 # PAUSE, BREAK or END (etc) statement
1223 stepMax => 100,
1224 # How many steps have been used during this task loop (0 if script not run by a task)
1225 stepCount => 0,
1226  
1227 # List of names of the independent interfaces created by ADDTRIG, ADDALIAS, ADDMACRO,
1228 # ADDTIMER and ADDHOOK statements, which are stored in the current world's cages
1229 # (unless a different cage is specified)
1230 # If OPTION PERSIST has not been used, all these interfaces must are deleted when the
1231 # Axbasic script terminates
1232 # The list is in the form:
1233 # (
1234 # independent_interface_name, profile_name, category,
1235 # independent_interface_name, profile_name, category
1236 # ...
1237 # )
1238 # 'category' will be one of 'trigger', 'alias', 'macro', 'timer' or 'hook'
1239 indepInterfaceList => [],
1240 # IV which stores whether the most recent attempt to create an independent trigger
1241 # (etc) was successful
1242 # Stores the name of the trigger (etc) for success, or 'undef' for failure (or when none
1243 # yet created by this Axbasic script)
1244 indepInterfaceName => undef,
1245 # List of names of the dependent interfaces created by SETTRIG (etc) and WAITTRIG (etc)
1246 # statements.
1247 # These interfaces are deleted when the script terminates, regardless of whether
1248 # OPTION PERSIST has been used
1249 # NB ->indepInterfaceList and ->indepInterfaceName are not currently updated to remove
1250 # an interface, after a DELTRIG (etc) statement
1251 depInterfaceList => [],
1252 # Flag which stores whether the most recent attempt to create a dependent trigger (etc)
1253 # resulted in the creation of an interface
1254 # Stores the name of the interface for success, or 'undef' for failure (or when none
1255 # created by this Axbasic script)
1256 depInterfaceName => undef,
1257 # Every time a dependent interface created by a SETTRIG (etc) statement fires,
1258 # information about the interface is stored in a LA::Notification object, which is
1259 # then added to the end of this list. (The script can retrieve notifications from the
1260 # list at its own convenience)
1261 # NB ->depInterfaceList and ->depInterfaceName ARE currently updated to remove an
1262 # interface, after a DELIFACE statement
1263 notificationList => [],
1264 # If there is more than one LA::Notification in ->notificationList, one of them (usually
1265 # the first in the list) is the 'current' one. This IV stores the index containing the
1266 # current LA::Notification. If there is one notification in the list, this IV is set
1267 # to 0. When the list is empty, it is set to -1
1268 currentNotification => -1,
1269  
1270 # Whenever an error is found, a LA::Error object is created to store details of the
1271 # error, until the error message (or messages) can be displayed, at the moment when
1272 # execution halts
1273 # The error objects are stored in this list. The first error generated corresponds to
1274 # the first error object in the list
1275 errorList => [],
1276  
1277 # The following IVs are set with calls to $self->popSubStack and $self->pushSubStack
1278 # Subroutine/function stack. (Single-line functions, declared with the 'def' statement,
1279 # aren't inserted into the stack)
1280 # In execution mode 1 (no line numbers), contains a list of LA::Subroutines; the first
1281 # one in the list is the '*main" 'subroutine' which represents the main Axbasic
1282 # script
1283 # In execution mode 2 (primitive line numbers), contains only '*main'
1284 subStackList => [],
1285 # How many subroutines/functions are in the stack
1286 subCount => 0,
1287 # In execution mode 1 (no line numbers), not used
1288 # In execution mode 2 (primitive line numbers), contains a list of GOSUB or ON-GOSUB
1289 # statements so that, upon RETURN, execution resumes with the line immediately after
1290 # the GOSUB statement
1291 gosubStackList => [],
1292  
1293 # Subroutines and functions have separate namespaces, so it's possible to have a
1294 # function, and a subroutine, both called 'test'
1295 # Hash of subroutine names, so that we don't create two subroutines with the same name
1296 # (this should cause an error). Hash in the form
1297 # $subNameHash{name} = blessed_reference_to_LA::Subroutine
1298 # (This is only used in execution mode 1)
1299 subNameHash => {},
1300 # Hash of function names (including single-line functions). If a function is declared a
1301 # second time, it overwrites the original profile (even if it's an intrinsic
1302 # function). Hash in the form
1303 # $funcNameHash{name} = blessed_reference_to_LA::Function
1304 # (This is only used in execution mode 1)
1305 funcNameHash => {},
1306 # Hash of intrinsic (pre-defined) functions, in the form
1307 # $funcArgHash{function_name} = argument_string
1308 # Each argument in the argument string is represented by a letter; N for a numeric
1309 # argument, S for a string argument. Arguments after a ';' are optional. An empty
1310 # argument string is used with intrinsic functions that take no arguments
1311 # Intrinsic function return either a string value (in which case, they end with $) or
1312 # a numeric value
1313 funcArgHash => {
1314 # Pure BASIC functions (returning a numeric value)
1315 'abs' => 'N',
1316 'acos' => 'N',
1317 'angle' => 'NN',
1318 'asc' => 'S',
1319 'asin' => 'N',
1320 'atn' => 'N',
1321 'ceil' => 'N',
1322 'cos' => 'N',
1323 'cosh' => 'N',
1324 'cot' => 'N',
1325 'cpos' => 'SS;N',
1326 'cposr' => 'SS;N',
1327 'csc' => 'N',
1328 'date' => '',
1329 'deg' => 'N',
1330 'eof' => 'N',
1331 'epoch' => '',
1332 'exp' => 'N',
1333 'floor' => 'N',
1334 'fp' => 'N',
1335 'int' => 'N',
1336 'ip' => 'N',
1337 'len' => 'S',
1338 'log' => 'N',
1339 'log10' => 'N',
1340 'log2' => 'N',
1341 'match' => 'SS',
1342 'matchi' => 'SS',
1343 'max' => 'NN',
1344 'min' => 'NN',
1345 'mod' => 'NN',
1346 'ncpos' => 'SS;N',
1347 'ncposr' => 'SS;N',
1348 'pi' => '',
1349 'pos' => 'SS;N',
1350 'posr' => 'SS;N',
1351 'rad' => 'N',
1352 'remainder' => 'NN',
1353 'rnd' => 'N',
1354 'round' => 'N;N',
1355 'sec' => 'N',
1356 'sgn' => 'N',
1357 'sin' => 'N',
1358 'sinh' => 'N',
1359 'sqr' => 'N',
1360 'tan' => 'N',
1361 'tanh' => 'N',
1362 'testpat' => 'S',
1363 'time' => '',
1364 'trunc' => 'N;N',
1365 'val' => 'S',
1366 'version' => '',
1367 # Pure BASIC functions (returning a string value)
1368 'chr$' => 'N',
1369 'date$' => '',
1370 'ip$' => '',
1371 'lcase$' => 'S',
1372 'left$' => 'SN',
1373 'ltrim$' => 'S',
1374 'mid$' => 'SN;N',
1375 'repeat$' => 'SN',
1376 'right$' => 'SN',
1377 'rtrim$' => 'S',
1378 'str$' => 'N',
1379 'testpat$' => 'S',
1380 'time$', => '',
1381 'trim$' => 'S',
1382 'ucase$' => 'S',
1383 # Axmud-dependent functions (returning a numeric value)
1384 'addfirstroom' => '',
1385 'addlabel' => 'SNNN',
1386 'addregion' => 'S',
1387 'addroom' => 'NNN',
1388 'addtempregion' => ';S',
1389 'closemap' => '',
1390 'counttask' => 'S',
1391 'delregion' => 'S',
1392 'deltempregions' => '',
1393 'getexitdest' => 'N',
1394 'getexitnum' => 'N',
1395 'getexittwin' => 'N',
1396 'getlostroom' => '',
1397 'getobjectalive' => 'N',
1398 'getobjectcount' => 'N',
1399 'getregionnum' => '',
1400 'getroomexits' => '',
1401 'getroomnum' => '',
1402 'getroomobjects' => ';S',
1403 'ifacecount' => '',
1404 'ifacedefined' => 'N',
1405 'ifacepos' => '',
1406 'ifacenum' => '',
1407 'ifacestrings' => '',
1408 'ifacetime' => '',
1409 'isscript' => '',
1410 'ismap' => '',
1411 'istask' => '',
1412 'iswin' => '',
1413 'openmap' => '',
1414 'setlight' => 'S',
1415 'setmapmode' => 'S',
1416 'setregion' => 'S',
1417 'setregionnum' => 'N',
1418 'setroomnum' => ';N',
1419 'setroomtagged' => ';S',
1420 'timestamp' => '',
1421 # Axmud-dependent functions (returning a string value)
1422 'abbrevdir$' => 'S',
1423 'clientdate$' => '',
1424 'clientname$' => '',
1425 'clientversion$' => '',
1426 'findtask$' => 'S',
1427 'getexit$' => 'N',
1428 'getexitdrawn$' => 'N',
1429 'getexitstatus$' => 'N',
1430 'getexittype$' => 'N',
1431 'getlight$' => '',
1432 'getmapmode$' => '',
1433 'getobject$' => 'N',
1434 'getobjectnoun$' => 'N',
1435 'getobjecttype$' => 'N',
1436 'getregion$' => '',
1437 'getroomdescrip$' => ';S',
1438 'getroomguild$' => '',
1439 'getroomsource$' => '',
1440 'getroomtag$' => '',
1441 'getroomtitle$' => '',
1442 'iface$' => '',
1443 'ifacedata$' => '',
1444 'ifacename$' => '',
1445 'ifacepop$' => '',
1446 'ifaceselect$' => 'N',
1447 'ifaceshift$' => '',
1448 'ifacetext$' => '',
1449 'ifacetype$' => '',
1450 'popup$' => 'SSS',
1451 'scriptname$' => '',
1452 'showprofile$' => '',
1453 'unabbrevdir$' => 'S',
1454 },
1455 # While parsing the script (in $self->parse), we need to keep track of whether the line
1456 # being parsed is within a subroutine declaration, or whether it is outside one - so
1457 # that we can detect subroutines declared inside another, and respond to correctly to
1458 # an END SUB
1459 # While parsing statements inside a subroutine declaration, this IV stores the
1460 # LA::Subroutine. While parsing statements outside a subroutine declaration (but
1461 # inside the implied *main subroutine), this IV is set to 'undef'
1462 currentParseSub => undef,
1463  
1464 # Needed by a few statements (most notably PRINT statements) to display stuff in the
1465 # way that BASIC is accustomed to displaying stuff
1466 column => 0,
1467  
1468 # Variable declare mode
1469 # When $self->parse processes a variable, it calls LA::Variable->lookup to find the
1470 # LA::Variable in LA::Subroutine's ->localScalarHash / ->localArrayHash. If it's not
1471 # there, it looks in this object's ->globalScalarHash / ->globalArrayHash
1472 # If the variable isn't there, it hasn't been declared yet. In execution mode
1473 # 'no_line_num', variables must be declared with GLOBAL or LOCAL statements before
1474 # being used. (In execution mode 'line_num' (primitive line numbers), all variables
1475 # are global and don't have to be declared, so this IV is ignored)
1476 # This IV is normally set to 'default'. When $self->parse is processing a GLOBAL
1477 # statement, it is briefly set to 'global_scalar' (for scalars) or 'global_array' (for
1478 # arrays). When processing a LOCAL or SUB statement, it is briefly set to
1479 # 'local_scalar' (for scalars) or 'local_array' (for arrays). When processing a PEEK
1480 # or PEEK... statement, it is briefly set to 'peek_scalar' (for scalars) or
1481 # 'peek_array' (for arrays)
1482 # When processing a FOR EACH, POP, PUSH, SHIFT or UNSHIFT statement, or a SORT statement
1483 # (or similar), it is set to 'simple'
1484 # A non-'default' value tells ->lookup to declare the variable by adding it one of the
1485 # declared variable hashes. Otherwise, when the script refers to an undeclared
1486 # variable, an error is produced
1487 declareMode => 'default',
1488 # Global variable hashes
1489 # A hash of global scalar variables, in the form
1490 # $globalScalarHash{scalar_name} = blessed_reference_to_scalar_object
1491 # (The object is a LA::Variable::Scalar::String or LA::Variable::Scalar::Numeric)
1492 globalScalarHash => {},
1493 # A hash of global array variables, in the form
1494 # $globalArrayHash{array_name} = blessed_reference_to_array_object
1495 # (The object is a LA::Variable::Array::String or LA::Variable::Array::Numeric)
1496 globalArrayHash => {},
1497  
1498 # Global data list - all the values in DATA statements, which are gathered by
1499 # $self->parse and stored here until a READ statement accesses them
1500 globalDataList => [],
1501 # The same data list, with items removed by successive READ statements. The whole list
1502 # is reset to match the contents of ->globalDataList by a RESTORE statement
1503 readDataList => [],
1504  
1505 # Hash of LA::FileChannel objects, each one representing a data file opened with an
1506 # OPEN statement. Hash in the form
1507 # $fileChannelHash{number} = blessed_reference_to_file_channel_object
1508 fileChannelHash => {},
1509  
1510 # Hash of things declared with the OPTION statement
1511 # KEY VALUE
1512 # angle radians (default) / degrees
1513 #
1514 # - Specifies the type of angle measure used with trigonometric and graphics
1515 # functions
1516 #
1517 # needtask FALSE (default) / TRUE
1518 #
1519 # - Script refuses to run without a parent task, if TRUE
1520 #
1521 # nolet FALSE (default) / TRUE
1522 #
1523 # - Allows LET keyword to be omitted, if TRUE
1524 #
1525 # persist FALSE (default) / TRUE
1526 #
1527 # - Independent triggers (etc) not destroyed when the script ends, if TRUE
1528 #
1529 # pseudo 'show_all' / 'hide_complete' (default) / 'hide_system' / 'win_error'
1530 # / 'win_only'
1531 #
1532 # - Sets ->pseudoCmdMode (so value in ->optionStatementHash not used)
1533 #
1534 # redirect FALSE (default) / TRUE
1535 #
1536 # - If task window is not open, WRITEWIN statements redirect their output to the
1537 # 'main' window
1538 #
1539 # require 0 / float (e.g. 1.0)
1540 #
1541 # - Script refuses to run unless global variable $BASIC_VERSION >= float
1542 #
1543 # silent FALSE (default) / TRUE
1544 #
1545 # - Script does not display messages like 'AXBASIC: Executing 'test'' or
1546 # 'AXBASIC: Execution of 'test' complete'
1547 #
1548 # typo FALSE (default) / TRUE
1549 #
1550 # - Allows GLOBAL/LOCAL variable declarations to be omitted, if TRUE. Ignored in
1551 # scripts with line numbers
1552 optionStatementHash => {
1553 'angle' => 'radians',
1554 'needtask' => FALSE,
1555 'nolet' => FALSE,
1556 'persist' => FALSE,
1557 'pseudo' => 'hide_complete', # If changed, also set ->pseudoCmdMode
1558 'redirect' => FALSE,
1559 'require' => 0,
1560 'silent' => FALSE,
1561 'typo' => FALSE,
1562 },
1563  
1564 # List of variables which are expecting to be set by something the user types
1565 # Every item on the list is a LA::Variable to set
1566 inputList => [],
1567 # For statements that interact with Axmud directly, the pseudo command mode to use
1568 # NB If the default value is changed, the corresponding value in ->optionStatementHash
1569 # must also be changed, and so must the help for OPTION statements
1570 # pseudoCmdMode => 'show_all', # Show all client command messages
1571 pseudoCmdMode => 'hide_complete', # Ignore success messages, show errors
1572 # Flag set to TRUE when the parent task's own ->forcedWinFlag is set to TRUE (set to
1573 # FALSE otherwise)
1574 # In 'forced window' mode, the task opens an 'entry' task window before starting to
1575 # execute the script. This script then redirects the output from PRINT statements to
1576 # the task window. This allows the user to run old BASIC scripts - which obviously
1577 # don't know how to open Axmud task windows - in their own window, away from the
1578 # 'main' window
1579 forcedWinFlag => FALSE,
1580 # All Axbasic scripts must have one, and only one, END statement. When
1581 # LA::Statement::end->parse() is run, it checks that this IV is FALSE. If so, it is
1582 # set to TRUE; however, if it was already set to TRUE, we get an error
1583 endStatementFlag => FALSE,
1584 # The name of the profile (matches a key in GA::Session->profHash) which is used by
1585 # ADDTRIG (etc) and DELTRIG (etc) statements. The default value of the IV is 'undef',
1586 # which means 'use the current world profile'. Otherwise, the specified profile is
1587 # used
1588 useProfile => undef,
1589 };
1590  
1591 # Bless the object into existence
1592 bless $self, $class;
1593  
1594 # Initialise intrinsic functions
1595 if (! Language::Axbasic::Function::Intrinsic->initialise($self)) {
1596  
1597 # This error is fatal
1598 return undef;
1599 }
1600  
1601 # Download the specified raw script, if it was specified
1602 if ($rawScriptObj) {
1603  
1604 if (! $rawScriptObj->upload($self)) {
1605  
1606 # Download failed. Display any error messages
1607 if ($self->errorList) {
1608  
1609 $self->terminateExecution();
1610 }
1611  
1612 return undef;
1613  
1614 } else {
1615  
1616 # Download complete. Remember the name of the raw script and the file path from
1617 # which the script was loaded
1618 $self->ivPoke('name', $rawScriptObj->name);
1619 $self->ivPoke('filePath', $rawScriptObj->filePath);
1620  
1621 return $self;
1622 }
1623  
1624 } else {
1625  
1626 # Don't download a raw script
1627 return $self;
1628 }
1629 }
1630  
1631 ##################
1632 # Methods
1633  
1634 sub parse {
1635  
1636 # Parses the script, looping over the lines and parsing each line in turn
1637 # Can be called independently, in which it serves as a basic syntax checker, or can be
1638 # called by $self->implement, right before the script is executed
1639 #
1640 # Expected arguments
1641 # (none besides $self)
1642 #
1643 # Return values
1644 # 'undef' on improper arguments
1645 # 1 if the script is parsed without an error
1646  
1647 my ($self, $check) = @_;
1648  
1649 # Local variables
1650 my ($lineObj, $mainSubObj);
1651  
1652 # Check for improper arguments
1653 if (defined $check) {
1654  
1655 $axmud::CLIENT->writeImproper($self->_objClass . '->parse', @_);
1656 # Display the error message immediately, and halt execution
1657 return $self->terminateExecution();
1658 }
1659  
1660 # Only permit parsing of the script once
1661 if ($self->scriptStatus ne 'waiting') {
1662  
1663 $self->setDebug('Script already parsed', $self->_objClass . '->parse');
1664 # Display the error message immediately, and halt execution
1665 return $self->terminateExecution();
1666 }
1667  
1668 # Create the first subroutine (the equivalent of a main() function in many languages). This
1669 # subroutine is implied and doesn't actually appear in the Axbasic code
1670 $mainSubObj = Language::Axbasic::Subroutine->new($self, '*main');
1671 if (! $mainSubObj) {
1672  
1673 $self->setDebug('Can\'t create *main subroutine', $self->_objClass . '->parse');
1674 # Display the error message immediately, and halt execution
1675 return $self->terminateExecution();
1676  
1677 } else {
1678  
1679 # This subroutine is the first one in the stack
1680 $self->ivPush('subStackList', $mainSubObj);
1681 $self->ivPoke('subCount', 1);
1682  
1683 $mainSubObj->set_stackPosn(0);
1684  
1685 # We keep track of the current subroutine being parsed, but set this IV to 'undef' when
1686 # we're in the implied *main subroutine
1687 $self->ivUndef('currentParseSub');
1688 }
1689  
1690 # Mark the Axbasic script status as 'parsing' (being parsed/implemented), and the execution
1691 # status as 1 (being parsed, not implemented)
1692 $self->ivPoke('scriptStatus', 'parsing');
1693 $self->ivPoke('executionStatus', 'parse');
1694  
1695 # Execution begins at line 1
1696 $self->ivPoke('currentLine', 1);
1697  
1698 # Parse the script by parsing each line in turn
1699 while ($self->scriptStatus eq 'parsing') {
1700  
1701 # Get the LA::Line object which is the next to be parsed
1702 $lineObj = $self->ivShow('lineHash', $self->currentLine);
1703 if (! defined $lineObj) {
1704  
1705 $self->setDebug('Missing line object', $self->_objClass . '->parse');
1706 # Display the error message immediately, and halt execution
1707 return $self->terminateExecution();
1708 }
1709  
1710 # Parse the line
1711 if ($self->executionStatus eq 'finished' || ! defined $lineObj->parse()) {
1712  
1713 # Execution has halted, either because the script has finished, or because of an
1714 # error
1715 return $self->terminateExecution();
1716 }
1717  
1718 # Get the next line to be parsed
1719 if (! defined $self->nextLine) {
1720  
1721 if (! $self->ivExists('lineHash', ($lineObj->procLineNum + 1))) {
1722  
1723 # Parsing is complete. Check there is exactly one END statement in the script,
1724 # and return the result
1725 return $self->checkEndStatements();
1726  
1727 } else {
1728  
1729 # Use the line immediately after $lineObj
1730 $self->ivPoke('currentLine', $lineObj->procLineNum + 1);
1731 }
1732  
1733 } else {
1734  
1735 # $self->nextLine has been set by something, so parse that line on the next loop
1736 $self->ivPoke('currentLine', $self->nextLine);
1737 $self->ivUndef('nextLine');
1738 }
1739 }
1740  
1741 # Parsing is complete. Check there is exactly one END statement in the script, and return
1742 # the result
1743 return $self->checkEndStatements();
1744 }
1745  
1746 sub implement {
1747  
1748 # Implements (executes) the Axbasic script
1749 # Once this object has been created (with a call to $self->new), can be called by anything
1750 # to start implementing (executing) the script
1751 # If the script pauses for any reason (usually because it's being run by an Axmud 'Script'
1752 # task), can be called again to resume implementation
1753 # Is also called by GA::Session->taskLoop when a paused script must be resumed
1754 #
1755 # Expected arguments
1756 # (none besides $self)
1757 #
1758 # Return values
1759 # 'undef' on improper arguments, or if the first subroutine can't be created, or if any
1760 # functions called (and any functions they call in turn) return an error or if this
1761 # function is called recursively
1762 # 1 if the script is implemented (executed) without an error or if it gets paused before
1763 # encountering an error
1764  
1765 my ($self, $check) = @_;
1766  
1767 # Local variables
1768 my ($lineObj, $nextLine, $currentStatement);
1769  
1770 # Check for improper arguments
1771 if (defined $check) {
1772  
1773 $axmud::CLIENT->writeImproper($self->_objClass . '->implement', @_);
1774 # Display the error message immediately, and halt execution
1775 return $self->terminateExecution();
1776 }
1777  
1778 # If this function is called recursively, do nothing. (Don't return an error; if this
1779 # function is being called by the Script task, we want it to call again on the next task
1780 # loop)
1781 if ($self->implementFlag) {
1782  
1783 return undef;
1784  
1785 } else {
1786  
1787 # Prevent any part of the Axmud code from calling this function recursively; the flag is
1788 # set back to FALSE when this function ends
1789 $self->ivPoke('implementFlag', TRUE);
1790 }
1791  
1792 # If this function is being called for the first time (executing the script from the
1793 # start)...
1794 if ($self->scriptStatus eq 'waiting') {
1795  
1796 # Parse the script
1797 if (! $self->parse()) {
1798  
1799 # Parsing failed - cannot continue
1800 $self->ivPoke('implementFlag', FALSE);
1801 return undef;
1802 }
1803  
1804 # The script has had an opportunity to set OPTION SILENT, so we now show a confirmation
1805 if (! $self->ivShow('optionStatementHash', 'silent')) {
1806  
1807 $self->session->writeText('AXBASIC: Executing \'' . $self->name . '\'');
1808 }
1809  
1810 # Mark the Axbasic script status as 'parsing' (being parsed/implemented), and the
1811 # execution status as 2 (being implemented, not merely parsed)
1812 $self->ivPoke('scriptStatus', 'parsing');
1813 $self->ivPoke('executionStatus', 'implement');
1814  
1815 # Execution begins at line 1
1816 $self->ivPoke('currentLine', 1);
1817 $self->ivPoke('nextLine', undef);
1818 $self->ivPoke(
1819 'nextStatement',
1820 $self->ivShow('lineHash', ($self->currentLine))->firstStatement,
1821 );
1822  
1823 # Empty the stacks (but retain the implied *main subroutine which is always the first
1824 # subroutine in the stack), and set the display column to 0
1825 $self->ivPoke('subStackList', $self->ivShow('subNameHash', '*main'));
1826 $self->ivPoke('subCount', 0);
1827 $self->ivPoke('column', 0);
1828  
1829 # If this function is being called again (resuming execution after a pause)...
1830 } elsif ($self->scriptStatus eq 'paused') {
1831  
1832 # Mark the Axbasic script status as 'parsing' (being implemented)
1833 $self->ivPoke('scriptStatus', 'parsing');
1834  
1835 # If this function is being called again (resuming execution after pausing while waiting for
1836 # INPUT)
1837 } elsif ($self->scriptStatus eq 'wait_input') {
1838  
1839 # Input has been received. Mark the Axbasic script status as 1 (being implemented)
1840 $self->ivPoke('scriptStatus', 'parsing');
1841  
1842 # If this function is being called again (resuming execution after halting, while waiting
1843 # for the parent task to monitor another task)...
1844 } elsif ($self->scriptStatus eq 'wait_status') {
1845  
1846 # The parent task has finished monitoring another task. Mark the Axbasic script status
1847 # as 1 (being implemented)
1848 $self->ivPoke('scriptStatus', 'parsing');
1849  
1850 # Error: called when the Axbasic script status is 'parsing' (script is being parsed or
1851 # implemented) or when its execution status is 'implement' (actually executing the script,
1852 # don't just checking it for errors)
1853 } elsif ($self->scriptStatus eq 'parsing' && $self->executionStatus eq 'implement') {
1854  
1855 $self->setDebug(
1856 'Language::Axbasic::Script->implement called, but script status is already 1',
1857 $self->_objClass . '->implement',
1858 );
1859  
1860 # Display the error message immediately, and halt execution
1861 $self->ivPoke('implementFlag', FALSE);
1862 return $self->terminateExecution();
1863  
1864 # Error: called when execution has already finished (status = 3, 4 or 5)
1865 } else {
1866  
1867 $self->setDebug(
1868 'Language::Axbasic::Script->implement called, but execution already finished',
1869 $self->_objClass . '->implement',
1870 );
1871  
1872 # Display the error message immediately, and halt execution
1873 $self->ivPoke('implementFlag', FALSE);
1874 return $self->terminateExecution();
1875 }
1876  
1877 # When the script is being run from within a task, ->parentTask is set
1878 if ($self->parentTask) {
1879  
1880 # Count the number of steps taken (statements executed) during each task loop
1881 $self->ivPoke('stepCount', 0);
1882 }
1883  
1884 # Loop over statements for as long as there are statements to execute
1885 do {
1886  
1887 if ($self->nextStatement->implement()) {
1888  
1889 # On lines with multiple statements, use the next statement on the line if there is
1890 # one (otherwise $self->nextStatement is set to 'undef')
1891 if (defined $self->nextStatement) {
1892  
1893 $self->ivPoke('nextStatement', $self->nextStatement->nextStatement);
1894 }
1895 }
1896  
1897 if (defined $self->nextStatement) {
1898  
1899 # Set the new current line (which might be the same as the old one)
1900 $self->ivPoke('currentLine', $self->nextStatement->lineObj->procLineNum);
1901  
1902 } else {
1903  
1904 if (defined $self->nextLine) {
1905  
1906 # Some other part of the code has specified the new current line
1907 $self->ivPoke('currentLine', $self->nextLine);
1908 $self->ivUndef('nextLine');
1909 $self->ivPoke(
1910 'nextStatement',
1911 $self->ivShow('lineHash', ($self->currentLine))->firstStatement,
1912 );
1913  
1914 } elsif (
1915 $self->scriptStatus eq 'parsing'
1916 || $self->scriptStatus eq 'paused'
1917 || $self->scriptStatus eq 'wait_input'
1918 || $self->scriptStatus eq 'wait_status'
1919 ) {
1920 # Script is at the end of a line
1921 $self->ivPoke('currentLine', $self->currentLine + 1);
1922 $self->ivPoke(
1923 'nextStatement',
1924 $self->ivShow('lineHash', ($self->currentLine))->firstStatement,
1925 );
1926 }
1927 }
1928  
1929 # If the script is being run by a task (and if the script must periodically be paused
1930 # while waiting for the next task loop), check if it's time to pause (but not if the
1931 # script is already marked as paused/waiting for input or if execution has halted)
1932 if (
1933 $self->parentTask
1934 && $self->executionStatus ne 'finished'
1935 && $self->scriptStatus ne 'paused'
1936 && $self->scriptStatus ne 'wait_input'
1937 ) {
1938 # Another statement has been executed
1939 $self->ivIncrement('stepCount');
1940  
1941 if ($self->stepMax && $self->stepCount >= $self->stepMax) {
1942  
1943 # It's time to pause until the next task loop
1944 $self->ivPoke('scriptStatus', 'paused');
1945 }
1946 }
1947  
1948 # Continue implementing statements until script execution halts, pauses, halts to allow the
1949 # parent task to monitor another task (=6) or pauses while expecting INPUT
1950 } until (
1951 $self->executionStatus eq 'finished'
1952 || $self->scriptStatus eq 'paused'
1953 || $self->scriptStatus eq 'wait_input'
1954 || $self->scriptStatus eq 'wait_status'
1955 );
1956  
1957 # Implementation complete (or script paused)
1958 $self->ivPoke('implementFlag', FALSE);
1959  
1960 if ($self->executionStatus eq 'finished') {
1961  
1962 return $self->terminateExecution();
1963  
1964 } else {
1965  
1966 return 1;
1967 }
1968 }
1969  
1970 sub setError {
1971  
1972 # Can be called by any Language::Axbasic:: function whenever it notices an error caused by
1973 # the Axbasic script (errors probably caused by this Perl code should be addressed by
1974 # calling $self->setDebug)
1975 # Creates a LA::Error object to store details of the error, so that all error messages can
1976 # be displayed together when control is passed back to LA::Script
1977 # The calling function should then return 'undef', which trickles back down to
1978 # LA::Script->parse or LA::Script->implement, one of which actually displays the
1979 # error message(s) stored in LA::Script->errorList
1980 #
1981 # Expected arguments
1982 # $errorCode - The error message to display, e.g. 'syntax_error'
1983 # $func - A string describing the function that generated the error (e.g.
1984 # 'Language::Axbasic::Statement->parse')
1985 #
1986 # Optional arguments
1987 # %errorHash - A hash of arguments used to substitute words in $errorCode (see the
1988 # comments in LA::Error; can be an empty hash)
1989 #
1990 # Return values
1991 # 'undef'
1992  
1993 my ($self, $errorCode, $func, %errorHash) = @_;
1994  
1995 # Local variables
1996 my $errorObj;
1997  
1998 # Check for improper arguments
1999 if (! defined $errorCode || ! defined $func) {
2000  
2001 return $axmud::CLIENT->writeImproper($self->_objClass . '->setError', @_);
2002 }
2003  
2004 # Create an error object and add it to this script object's error list
2005 $errorObj = Language::Axbasic::Error->new(
2006 $self,
2007 'error',
2008 $errorCode,
2009 $func,
2010 $self->currentLine,
2011 %errorHash,
2012 );
2013  
2014 if ($errorObj) {
2015  
2016 $self->ivPush('errorList', $errorObj);
2017 }
2018  
2019 # Set status IVs (but only if they haven't been set by a previous error - the status IVs
2020 # only record the first error message received)
2021 if ($self->executionStatus ne 'finished') {
2022  
2023 # 5 signifies a Axbasic script error
2024 $self->ivPoke('scriptStatus', 'basic_error');
2025 # 3 signifies execution has finished
2026 $self->ivPoke('executionStatus', 'finished');
2027 }
2028  
2029 # This return value should trickle all the way down to LA::Script->parse or
2030 # LA::Script->implement, one of which actually displays the error message
2031 return undef;
2032 }
2033  
2034 sub setDebug {
2035  
2036 # Can be called by any Language::Axbasic:: function whenever it notices an error probably
2037 # caused by the Perl code (errors caused by the Axbasic script should be addressed by
2038 # calling $self->setError)
2039 # Creates a LA::Error object to store details of the error, so that all error messages can
2040 # be displayed together when control is passed back to LA::Script
2041 # The calling function should then return 'undef', which trickles back down to
2042 # LA::Script->parse or LA::Script->implement, one of which actually displays the
2043 # error message(s) stored in LA::Script->errorList
2044 #
2045 # Expected arguments
2046 # $errorCode - The error message to display, e.g. 'syntax_error'
2047 # $func - A string describing the function that generated the error (e.g.
2048 # 'Axbasic::Statement->parse')
2049 #
2050 # Optional arguments
2051 # %errorHash - A hash of arguments used to substitute words in $errorCode (see the
2052 # comments in LA::Error; can be an empty hash)
2053 #
2054 # Return values
2055 # 'undef'
2056  
2057 my ($self, $errorCode, $func, %errorHash) = @_;
2058  
2059 # Local variables
2060 my $errorObj;
2061  
2062 # Check for improper arguments
2063 if (! defined $errorCode || ! defined $func) {
2064  
2065 return $axmud::CLIENT->writeImproper($self->_objClass . '->setDebug', @_);
2066 }
2067  
2068 # Create an error object and add it to this script object's error list
2069 $errorObj = Language::Axbasic::Error->new(
2070 $self,
2071 'debug',
2072 $errorCode,
2073 $func,
2074 $self->currentLine,
2075 %errorHash,
2076 );
2077  
2078 if ($errorObj) {
2079  
2080 $self->ivPush('errorList', $errorObj);
2081 }
2082  
2083 # Set status IVs (but only if they haven't been set by a previous error - the status IVs
2084 # only record the first error message received)
2085 if ($self->executionStatus ne 'finished') {
2086  
2087 # 6 signifies a probably Perl code error
2088 $self->ivPoke('scriptStatus', 'perl_error');
2089 # 3 signifies execution has finished
2090 $self->ivPoke('executionStatus', 'finished');
2091 }
2092  
2093 # This return value should trickle all the way down to LA::Script->parse or
2094 # LA::Script->implement, one of which actually displays the error message
2095 return undef;
2096 }
2097  
2098 sub checkEndStatements {
2099  
2100 # Called by $self->parse
2101 # Check that there is exactly one END statement in the Axbasic script
2102 # (Multiple END statements are picked up by LA::Statement::end->parse, so we don't need to
2103 # check for that possibility)
2104 #
2105 # Expected arguments
2106 # (none besides $self)
2107 #
2108 # Return values
2109 # 'undef' on improper arguments or if there are no END statements in the script
2110 # 1 otherwise
2111  
2112 my ($self, $check) = @_;
2113  
2114 # Check for improper arguments
2115 if (defined $check) {
2116  
2117 return $axmud::CLIENT->writeImproper($self->_objClass . '->checkEndStatements', @_);
2118 }
2119  
2120 if (! $self->endStatementFlag) {
2121  
2122 # Because the call to this function might be the last thing that $self->parse does, if
2123 # there's an error, we have to call ->terminateExecution ourselves
2124 $self->setError(
2125 'Axbasic script does not contain an END statement',
2126 $self->_objClass . '->checkEndStatements',
2127 );
2128  
2129 return $self->terminateExecution();
2130  
2131 } else {
2132  
2133 return 1;
2134 }
2135 }
2136  
2137 sub terminateExecution {
2138  
2139 # Called by $self->parse or $self->implement after parsing/implementation halts, perhaps
2140 # because the script has finished naturally, or perhaps because of an error
2141 #
2142 # Expected arguments
2143 # (none besides $self)
2144 #
2145 # Return values
2146 # 'undef' on improper arguments, or if execution doesn't appear to have been halted, or if
2147 # execution halts after an error
2148 # 1 if execution halts successfully
2149  
2150 my ($self, $check) = @_;
2151  
2152 # Local variables
2153 my (
2154 $errorFlag,
2155 @msgList, @errorObjList,
2156 );
2157  
2158 # Check for improper arguments
2159 if (defined $check) {
2160  
2161 return $axmud::CLIENT->writeImproper($self->_objClass . '->terminateExecution', @_);
2162 }
2163  
2164 # If any interfaces were created with the ADDTRIG, ADDALIAS, ADDMACRO, ADDTIMER or ADDHOOK
2165 # statements, and if OPTION PERSIST has not been set, then all those interfaces must be
2166 # deleted
2167 $self->destroyOwnInterfaces();
2168  
2169 # Close all file channels which are open
2170 foreach my $channelObj ($self->ivValues('fileChannelHash')) {
2171  
2172 close $channelObj->fileHandle;
2173 }
2174  
2175 # Check that execution really has halted
2176 if ($self->executionStatus ne 'finished') {
2177  
2178 # Because the call to this function might be the last thing that $self->parse or
2179 # $self->implement does, if there's an error, we have to use the Axmud error function
2180 return $self->setError(
2181 'Axbasic ->executionStatus not set to \'finished\' after execution halts',
2182 $self->_objClass . '->terminateExecution',
2183 );
2184  
2185 } elsif (
2186 $self->scriptStatus eq 'waiting' || $self->scriptStatus eq 'parsing'
2187 || $self->scriptStatus eq 'paused' || $self->scriptStatus eq 'wait_input'
2188 ) {
2189 # (Likewise)
2190 return $self->setError(
2191 'Axbasic ->scriptStatus not set to correct value after execution halts',
2192 $self->_objClass . '->terminateExecution',
2193 );
2194 }
2195  
2196 # Compile a list of messages to display, @msgList
2197 if (
2198 $self->scriptStatus eq 'basic_error'
2199 || $self->scriptStatus eq 'perl_error'
2200 || $self->scriptStatus eq 'wait_status'
2201 || $self->scriptStatus eq 'wait_active'
2202 || $self->scriptStatus eq 'wait_not_active'
2203 ) {
2204 # There were errors. Import the error list. Unless ->allErrorsFlag has been set to TRUE,
2205 # we only display the first error generated
2206 @errorObjList = $self->errorList;
2207 if (! $self->allErrorsFlag && @errorObjList > 1) {
2208  
2209 @errorObjList = ($errorObjList[0]);
2210 }
2211  
2212 foreach my $errorObj (@errorObjList) {
2213  
2214 my (
2215 $category, $errorCode, $lineObj,
2216 %errorHash,
2217 );
2218  
2219 $category = $errorObj->category;
2220 $errorCode = $errorObj->errorCode;
2221 %errorHash = $errorObj->errorHash;
2222  
2223 # If any of the words in $errorCode are in capitals, they are meant to be keys in
2224 # %errorHash which should be substituted for the corresponding values
2225 # (NB This algorithm will still work if the keys aren't in capitals)
2226 if (%errorHash) {
2227  
2228 foreach my $key (keys %errorHash) {
2229  
2230 if ($errorCode =~ m/$key/) {
2231  
2232 my $value = $errorHash{$key};
2233 $errorCode =~ s/$key/$value/;
2234 }
2235 }
2236 }
2237  
2238 # For 'error' messages (but not 'debug' messages)...
2239 if ($errorObj->category eq 'error') {
2240  
2241 # For error messages caused by the Axbasic script, the message is in the form
2242 # of a string like 'lots_of_words_separated_by_underlines'
2243 # Convert $errorCode (a string like 'syntax_error') into a message in English (a
2244 # string like 'Syntax error')
2245 $errorCode =~ s/_/ /g;
2246 $errorCode = uc(substr($errorCode, 0, 1)) . substr($errorCode, 1);
2247 }
2248  
2249 # If the error was caused by a certain line in the Axbasic script, add the line to
2250 # the error message
2251 if (defined $errorObj->line) {
2252  
2253 # Get the LA::Line object for the line on which the error occurred
2254 $lineObj = $self->ivShow('lineHash', $errorObj->line);
2255 if ($lineObj) {
2256  
2257 if ($self->executionMode eq 'line_num') {
2258  
2259 # For primitive code, use the primitive line number
2260 $errorCode .= ', line #' . $lineObj->primLineNum;
2261  
2262 } elsif ($self->executionMode eq 'no_line_num') {
2263  
2264 # Otherwise use the line number in the raw (unprocessed) script stored
2265 # in LA::Script
2266 $errorCode .= ', line ' . $lineObj->origLineNum;
2267 }
2268 }
2269 }
2270  
2271 # Add the error message to the list to be displayed
2272 if ($errorObj->category eq 'error') {
2273  
2274 push (@msgList, 'AXBASIC: ERROR: ' . $errorCode);
2275  
2276 } elsif ($errorObj->category eq 'debug') {
2277  
2278 push (@msgList, 'AXBASIC: DEBUG: ' . $errorCode);
2279 if ($lineObj) {
2280  
2281 push (@msgList, ' Line: ' . $lineObj->lineText);
2282 }
2283 }
2284 }
2285 }
2286  
2287 # Add the final message to @msgList
2288 if (@msgList) {
2289  
2290 $errorFlag = TRUE;
2291  
2292 if ($self->allErrorsFlag) {
2293  
2294 if (@msgList == 1) {
2295  
2296 push (
2297 @msgList,
2298 'AXBASIC: Execution of \'' . $self->name . '\' halted (1 error)',
2299 );
2300  
2301 } else {
2302  
2303 push (
2304 @msgList,
2305 'AXBASIC: Execution of \'' . $self->name . '\' halted (' . @msgList
2306 . ' errors)',
2307 );
2308 }
2309  
2310 } else {
2311  
2312 push (@msgList, 'AXBASIC: Execution of \'' . $self->name . '\' halted');
2313 }
2314  
2315 } elsif (! $self->ivShow('optionStatementHash', 'silent')) {
2316  
2317 push (@msgList, 'AXBASIC: Execution of \'' . $self->name . '\' complete');
2318 }
2319  
2320 # If the most recent PRINT statement terminated with a comma or semicolon, the first message
2321 # in @msgList may be appended to the end of it
2322 if ($self->column != 0) {
2323  
2324 # Prepend a newline character to get around this problem
2325 $self->session->writeText(' ');
2326 }
2327  
2328 # Display the contents of @msgList
2329 foreach my $msg (@msgList) {
2330  
2331 $self->session->writeText($msg);
2332 }
2333  
2334 if ($errorFlag) {
2335 return undef;
2336 } else {
2337 return 1;
2338 }
2339 }
2340  
2341 sub download {
2342  
2343 # Called by LA::RawScript->upload, which is in turn called by $self->new
2344 # Stores the processed script in this object's IVs
2345 #
2346 # Expected arguments
2347 # $lineCount - Number of lines in the processed script
2348 # $executionMode - Stored as $self->executionMode
2349 # $lineHashRef - Reference to a hash, containing the lines of the processed script
2350 # $primHashRef - In scripts with primitive line numbers, reference to a hash which
2351 # converts those line numbers (e.g. 20 in '20 GOTO 10') into the line
2352 # number of the processed script (stored in $lineHashRef.) An empty
2353 # hash for scripts without line numbers
2354 #
2355 # Return values
2356 # 'undef' on improper arguments
2357 # 1 otherwise
2358  
2359 my ($self, $lineCount, $executionMode, $lineHashRef, $primHashRef, $check) = @_;
2360  
2361 # Check for improper arguments
2362 if (
2363 ! defined $lineCount || ! defined $executionMode || ! defined $lineHashRef
2364 || ! defined $primHashRef || defined $check
2365 ) {
2366 return $axmud::CLIENT->writeImproper($self->_objClass . '->download', @_);
2367 }
2368  
2369 # Update IVs
2370 $self->ivPoke('lineCount', $lineCount);
2371 $self->ivPoke('executionMode', $executionMode);
2372 $self->ivPoke('lineHash', %$lineHashRef);
2373 $self->ivPoke('primLineHash', %$primHashRef);
2374  
2375 return 1;
2376 }
2377  
2378 sub destroyOwnInterfaces {
2379  
2380 # Called by $self->terminateExecution or by the parent task (if there is one) when the task
2381 # itself has to stop
2382 # Destroys any independent interfaces created with the ADDTRIG, ADDALIAS, ADDMACRO, ADDTIMER
2383 # and ADDHOOK statements, if the interfaces still exist (unless OPTION PERSIST has been
2384 # specified)
2385 # Also destroys any interfaces created with the SETTRIG (etc) and WAITTRIG (etc) statements,
2386 # regardless of whether OPTION PERSIST has been specified
2387 #
2388 # Expected arguments
2389 # (none besides $self)
2390 #
2391 # Return values
2392 # 'undef' on improper arguments
2393 # 1 otherwise
2394  
2395 my ($self, $check) = @_;
2396  
2397 # Local variables
2398 my @list;
2399  
2400 # Check for improper arguments
2401 if (defined $check) {
2402  
2403 return $axmud::CLIENT->writeImproper($self->_objClass . '->destroyOwnInterfaces', @_);
2404 }
2405  
2406 # Destroy independent interfaces, if allowed
2407 if (
2408 ! $self->ivExists('optionStatementHash', 'persist')
2409 || ! $self->ivShow('optionStatementHash', 'persist')
2410 ) {
2411 @list = $self->indepInterfaceList;
2412 if (@list) {
2413  
2414 do {
2415 my ($name, $profile, $category, $cmd);
2416  
2417 $name = shift @list;
2418 $profile = shift @list;
2419 $category = shift @list;
2420  
2421 # Delete the interface
2422 $self->session->pseudoCmd(
2423 'delete' . $category . ' ' . $name . ' -d ' . $profile,
2424 $self->pseudoCmdMode,
2425 );
2426  
2427 } until (! @list);
2428 }
2429 }
2430  
2431 # Destroy dependent interfaces
2432 foreach my $interfaceName ($self->depInterfaceList) {
2433  
2434 my ($obj, $string);
2435  
2436 # If the interface still exists...
2437 if ($self->session->ivExists('interfaceHash', $interfaceName)) {
2438  
2439 # Get the interface object
2440 $obj = $self->session->ivShow('interfaceHash', $interfaceName);
2441 # Cannot use ;deletetrigger for dependent interfaces, so we must remove it
2442 # directly
2443 $self->session->removeInterface($obj);
2444 }
2445 }
2446  
2447 # Empty the lists - if the Axbasic script has a parent task, this function might be called
2448 # twice
2449 $self->ivEmpty('indepInterfaceList');
2450 $self->ivEmpty('depInterfaceList');
2451 # Reset the two accompanying IVs, just to be sure
2452 $self->ivUndef('indepInterfaceName');
2453 $self->ivUndef('depInterfaceName');
2454  
2455 return 1;
2456 }
2457  
2458 sub returnCurrentSub {
2459  
2460 # Can be called by anything
2461 # Returns the blessed reference of the current subroutine/function (if none have been called
2462 # yet, it's the '*main' subroutine)
2463 # Actually, returns the last entry in $self->subStackList
2464 #
2465 # Expected arguments
2466 # (none besides $self)
2467 #
2468 # Return values
2469 # 'undef' on improper arguments or if $self->subStackList is empty (should never happen)
2470 # Otherwise returns the blessed reference to the subroutine/function object
2471  
2472 my ($self, $check) = @_;
2473  
2474 # Check for improper arguments
2475 if (defined $check) {
2476  
2477 return $axmud::CLIENT->writeImproper($self->_objClass . '->returnCurrentSub', @_);
2478 }
2479  
2480 if (! $self->subStackList) {
2481  
2482 # There is no current subroutine or function (this should never happen)
2483 return undef;
2484  
2485 } else {
2486  
2487 return $self->ivIndex('subStackList', $self->ivLast('subStackList'));
2488 }
2489 }
2490  
2491 sub pushSubStack {
2492  
2493 # Can be called by anything
2494 # Adds a new subroutine/function to the top of the subroutine/function stack
2495 # In $self->executionMode 'no_line_num' (no line numbers), adds a LA::Subroutine
2496 # In $self->executionMode 'line_num' (primitive line numbers), not used
2497 #
2498 # Expected arguments
2499 # $obj - The object to add
2500 #
2501 # Return values
2502 # 'undef' on improper arguments
2503 # 1 otherwise
2504  
2505 my ($self, $obj, $check) = @_;
2506  
2507 # Check for improper arguments
2508 if (! defined $obj || defined $check) {
2509  
2510 return $axmud::CLIENT->writeImproper($self->_objClass . '->pushSubStack', @_);
2511 }
2512  
2513 $self->ivPush('subStackList', $obj);
2514 $self->ivIncrement('subCount');
2515  
2516 return 1;
2517 }
2518  
2519 sub popSubStack {
2520  
2521 # Can be called by anything
2522 # Removes a new subroutine/function from the top of the subroutine/function stack
2523 # In $self->executionMode 'no_line_num' (no line numbers), removes a LA::Subroutine
2524 # In $self->executionMode 'line_num' (primitive line numbers), not used
2525 #
2526 # Expected arguments
2527 # (none besides $self)
2528 #
2529 # Return values
2530 # 'undef' on improper arguments or if $self->subStackList is empty (should never happen)
2531 # The object at the top of the stack, otherwise
2532  
2533 my ($self, $check) = @_;
2534  
2535 # Check for improper arguments
2536 if (defined $check) {
2537  
2538 return $axmud::CLIENT->writeImproper($self->_objClass . '->popSubStack', @_);
2539 }
2540  
2541 $self->ivDecrement('subCount');
2542 return $self->ivPop('subStackList');
2543 }
2544  
2545 sub setInputData {
2546  
2547 # Called by the parent task's ->entryCallback method
2548 # For every item of data the user types in the entry box (while waiting for an Axbasic INPUT
2549 # statement), this function is called once to set the relevant variable
2550 #
2551 # Expected arguments
2552 # $value - The value to assign a variable
2553 #
2554 # Return values
2555 # 'undef' on improper arguments, or if the variable doesn't exist, or if
2556 # $self->inputList (a list of variables awaiting input data) is empty, or if an
2557 # error is generated while setting the variable
2558 # 1 otherwise
2559  
2560 my ($self, $value, $check) = @_;
2561  
2562 # Local variables
2563 my $varObj;
2564  
2565 # Check for improper arguments
2566 if (! defined $value || defined $check) {
2567  
2568 return $axmud::CLIENT->writeImproper($self->_objClass . '->setInputData', @_);
2569 }
2570  
2571 # Check that we're expecting some input data. This function shouldn't be called when
2572 # $self->inputList is empty
2573 if (! $self->inputList) {
2574  
2575 # Just ignore the typed value
2576 return undef;
2577 }
2578  
2579 # Set the variable
2580 $varObj = $self->ivShift('inputList')->variable;
2581 $varObj->set($value);
2582  
2583 if ($self->errorList) {
2584  
2585 # Setting the variable generated an error, so we have to halt
2586 return $self->terminateExecution();
2587  
2588 } else {
2589  
2590 # Variable set successfully
2591 return 1;
2592 }
2593 }
2594  
2595 sub interfaceNotification {
2596  
2597 # Called by the parent task's ->triggerNotifySeen (etc) when one of the dependent interfaces
2598 # created by a SETTRIG (etc) fires
2599 # Creates a LA::Notification object to store details of the fired interface, so that the
2600 # script can retrieve them when it's ready
2601 #
2602 # The format of arguments supplied by different GA::Task::Script functions is as follows:
2603 #
2604 # ->triggerNotifySeen: (line_of_text, group_string_list)
2605 #
2606 # line_of_text: The line of text sent by the world which caused the trigger to fire
2607 # group_string_list: A list of group substrings from the pattern match (the equivalent
2608 # of @_)
2609 #
2610 # ->aliasNotifySeen
2611 #
2612 # world_cmd: The world command which caused the alias to fire
2613 # group_string_list: A list of group substrings from the pattern match (the equivalent
2614 # of @_)
2615 #
2616 # ->macroNotifySeen
2617 #
2618 # keycode_string: The keycode string (combination of keypresses, e.g. 'ctrl + f1'
2619 # which caused the macro to fire
2620 # empty_list: An empty list
2621 #
2622 # ->timerNotifySeen
2623 #
2624 # session_time: The value of GA::Session->sessionTime when the timer fired (and that
2625 # caused the timer to fire)
2626 #
2627 # other_time: For timers whose stimulus is an interval, the time (matches
2628 # GA::Session->sessionTime) at which the timer was due to fire. This value will be
2629 # less than or the same as session_time. For timers whose stimulus is a clock time
2630 # (in the form HH:MM firing once a day, or 99:MM firing once an hour, at MM
2631 # minutes past the hour), the stimulus itself
2632 #
2633 # ->hookNotifySeen
2634 #
2635 # hook_event: The hook event which caused the hook to fire
2636 #
2637 # hook_var, hook_val: Zero, one or two items of additional data for this hook event.
2638 # The number of items depends on the hook event
2639 #
2640 # Expected arguments
2641 # $interfaceObj - The active interface that fired
2642 # $fireBecause - A scalar describing why the interface fired
2643 #
2644 # Optional arguments
2645 # @dataList - A list of additional data, supplied by the interface when it
2646 # fired (may be an empty list)
2647 #
2648 # Return values
2649 # 'undef' on improper arguments or if the LA::Notification object can't be created
2650 # 1 otherwise
2651  
2652 my ($self, $interfaceObj, $fireBecause, @dataList) = @_;
2653  
2654 # Local variables
2655 my $obj;
2656  
2657 # Check for improper arguments
2658 if (! defined $interfaceObj || ! defined $fireBecause) {
2659  
2660 return $axmud::CLIENT->writeImproper($self->_objClass . '->interfaceNotification', @_);
2661 }
2662  
2663 # For triggers and aliases, @fireDataList (if it is populated at all) contains the whole
2664 # matching line of text/world command, followed by any substrings. Remove the first
2665 # element, leaving us with the substrings
2666 if (
2667 ($interfaceObj->category eq 'trigger' || $interfaceObj->category eq 'alias')
2668 && @dataList
2669 ) {
2670 shift @dataList;
2671 }
2672  
2673 # Create a LA::Notification object to store the information about the fired interface
2674 $obj = Language::Axbasic::Notification->new(
2675 $self,
2676 $interfaceObj,
2677 $fireBecause,
2678 @dataList,
2679 );
2680  
2681 if (! $obj) {
2682  
2683 return undef;
2684  
2685 } else {
2686  
2687 # Add this LA::Notification to the end of the script's list
2688 $self->ivPush('notificationList', $obj);
2689 # If the list was empty, make the first notification in the list, the current one
2690 if ($self->currentNotification == -1) {
2691  
2692 $self->ivPoke('currentNotification', 0);
2693 }
2694  
2695 return 1;
2696 }
2697 }
2698  
2699 sub updateInterfaces {
2700  
2701 # Called by the parent task, when it is paused and waiting for an interface to fire before
2702 # resuming
2703 # Causes this LA::Script to update its IVs to remove the interface
2704 #
2705 # Expected arguments
2706 # $interfaceName - The name of the interface to remove
2707 #
2708 # Return values
2709 # 'undef' on improper arguments
2710 # 1 otherwise
2711  
2712 my ($self, $interfaceName, $check) = @_;
2713  
2714 # Local variables
2715 my (@ifaceList, @newList);
2716  
2717 # Check for improper arguments
2718 if (! defined $interfaceName || defined $check) {
2719  
2720 return $axmud::CLIENT->writeImproper($self->_objClass . '->updateInterfaces', @_);
2721 }
2722  
2723 @ifaceList = $self->depInterfaceList;
2724 foreach my $name (@ifaceList) {
2725  
2726 my $obj = $self->session->ivShow('interfaceHash', $name);
2727  
2728 if ($obj && $obj->name ne $interfaceName) {
2729  
2730 push (@newList, $obj->name);
2731 }
2732 }
2733  
2734 $self->ivPoke('depInterfaceList', @newList);
2735  
2736 # Update the accompanying IV, if necessary
2737 if (
2738 defined $self->depInterfaceName
2739 && $self->depInterfaceName eq $interfaceName
2740 ) {
2741 $self->ivUndef('depInterfaceName');
2742 }
2743  
2744 return 1;
2745 }
2746  
2747 ##################
2748 # Accessors - set
2749  
2750 sub set_column {
2751  
2752 # Returns 'undef' on improper arguments
2753 # Returns 1 on success
2754  
2755 my ($self, $column, $check) = @_;
2756  
2757 # Check for improper arguments
2758 if (! defined $column || defined $check) {
2759  
2760 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_column', @_);
2761 }
2762  
2763 # Update IVs
2764 $self->ivPoke('column', $column);
2765  
2766 return 1;
2767 }
2768  
2769 sub set_currentNotification {
2770  
2771 # Returns 'undef' on improper arguments
2772 # Returns 1 on success
2773  
2774 my ($self, $number, $check) = @_;
2775  
2776 # Check for improper arguments
2777 if (! defined $number || defined $check) {
2778  
2779 return $axmud::CLIENT->writeImproper(
2780 $self->_objClass . '->set_currentNotification',
2781 @_,
2782 );
2783 }
2784  
2785 # Update IVs
2786 $self->ivPoke('currentNotification', $number);
2787  
2788 return 1;
2789 }
2790  
2791 sub rmv_currentNotification {
2792  
2793 # Returns 'undef' on improper arguments
2794 # Returns 1 on success
2795  
2796 my ($self, $check) = @_;
2797  
2798 # Check for improper arguments
2799 if (defined $check) {
2800  
2801 return $axmud::CLIENT->writeImproper(
2802 $self->_objClass . '->rmv_currentNotification',
2803 @_,
2804 );
2805 }
2806  
2807 # Update IVs
2808 $self->ivSplice('notificationList', $self->currentNotification, 1);
2809  
2810 return 1;
2811 }
2812  
2813 sub inc_currentNotification {
2814  
2815 # Returns 'undef' on improper arguments
2816 # Returns 1 on success
2817  
2818 my ($self, $number, $check) = @_;
2819  
2820 # Check for improper arguments
2821 if (! defined $number || defined $check) {
2822  
2823 return $axmud::CLIENT->writeImproper(
2824 $self->_objClass . '->inc_currentNotification',
2825 @_,
2826 );
2827 }
2828  
2829 # Update IVs
2830 $self->ivIncrement('currentNotification');
2831  
2832 return 1;
2833 }
2834  
2835 sub set_currentParseSub {
2836  
2837 # Returns 'undef' on improper arguments
2838 # Returns 1 on success
2839  
2840 my ($self, $obj, $check) = @_;
2841  
2842 # Check for improper arguments
2843 if (defined $check) {
2844  
2845 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_currentParseSub', @_);
2846 }
2847  
2848 # Update IVs
2849 $self->ivPoke('currentParseSub', $obj);
2850  
2851 return 1;
2852 }
2853  
2854 sub set_declareMode {
2855  
2856 # Returns 'undef' on improper arguments
2857 # Returns 1 on success
2858  
2859 my ($self, $mode, $check) = @_;
2860  
2861 # Check for improper arguments
2862 if (! defined $mode || defined $check) {
2863  
2864 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_declareMode', @_);
2865 }
2866  
2867 # Update IVs
2868 $self->ivPoke('declareMode', $mode);
2869  
2870 return 1;
2871 }
2872  
2873 sub set_depInterfaceName {
2874  
2875 # Returns 'undef' on improper arguments
2876 # Returns 1 on success
2877  
2878 my ($self, $name, $check) = @_;
2879  
2880 # Check for improper arguments
2881 if (defined $check) {
2882  
2883 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_depInterfaceName', @_);
2884 }
2885  
2886 # Update IVs
2887 $self->ivPoke('depInterfaceName', $name);
2888  
2889 return 1;
2890 }
2891  
2892 sub set_depInterfaceList {
2893  
2894 # Returns 'undef' on improper arguments
2895 # Returns 1 on success
2896  
2897 my ($self, @list) = @_;
2898  
2899 # (No improper arguments to check - @list can be empty)
2900  
2901 $self->ivPoke('depInterfaceList', @list);
2902  
2903 return 1;
2904 }
2905  
2906 sub push_depInterfaceList {
2907  
2908 # Returns 'undef' on improper arguments
2909 # Returns 1 on success
2910  
2911 my ($self, $obj, $check) = @_;
2912  
2913 # Check for improper arguments
2914 if (! defined $obj || defined $check) {
2915  
2916 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_depInterfaceList', @_);
2917 }
2918  
2919 # Update IVs
2920 $self->ivPush('depInterfaceList', $obj);
2921  
2922 return 1;
2923 }
2924  
2925 sub set_endStatementFlag {
2926  
2927 # Returns 'undef' on improper arguments
2928 # Returns 1 on success
2929  
2930 my ($self, $flag, $check) = @_;
2931  
2932 # Check for improper arguments
2933 if (! defined $flag || defined $check) {
2934  
2935 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_endStatementFlag', @_);
2936 }
2937  
2938 if ($flag) {
2939 $self->ivPoke('endStatementFlag', TRUE);
2940 } else {
2941 $self->ivPoke('endStatementFlag', FALSE);
2942 }
2943  
2944 return 1;
2945 }
2946  
2947 sub set_executionStatus {
2948  
2949 # Returns 'undef' on improper arguments
2950 # Returns 1 on success
2951  
2952 my ($self, $status, $check) = @_;
2953  
2954 # Check for improper arguments
2955 if (! defined $status || defined $check) {
2956  
2957 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_executionStatus', @_);
2958 }
2959  
2960 # Update IVs
2961 $self->ivPoke('executionStatus', $status);
2962  
2963 return 1;
2964 }
2965  
2966 sub add_fileChannel {
2967  
2968 # Returns 'undef' on improper arguments
2969 # Returns 1 on success
2970  
2971 my ($self, $obj, $check) = @_;
2972  
2973 # Check for improper arguments
2974 if (! defined $obj || defined $check) {
2975  
2976 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_fileChannel', @_);
2977 }
2978  
2979 # Update IVs
2980 $self->ivAdd('fileChannelHash', $obj->channel, $obj);
2981  
2982 return 1;
2983 }
2984  
2985 sub del_fileChannel {
2986  
2987 # Returns 'undef' on improper arguments
2988 # Returns 1 on success
2989  
2990 my ($self, $obj, $check) = @_;
2991  
2992 # Check for improper arguments
2993 if (! defined $obj || defined $check) {
2994  
2995 return $axmud::CLIENT->writeImproper($self->_objClass . '->del_fileChannel', @_);
2996 }
2997  
2998 # Update IVs
2999 $self->ivDelete('fileChannelHash', $obj->channel);
3000  
3001 return 1;
3002 }
3003  
3004 sub set_forcedWinFlag {
3005  
3006 # Returns 'undef' on improper arguments
3007 # Returns 1 on success
3008  
3009 my ($self, $flag, $check) = @_;
3010  
3011 # Check for improper arguments
3012 if (! defined $flag || defined $check) {
3013  
3014 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_forcedWinFlag', @_);
3015 }
3016  
3017 if ($flag) {
3018 $self->ivPoke('forcedWinFlag', TRUE);
3019 } else {
3020 $self->ivPoke('forcedWinFlag', FALSE);
3021 }
3022  
3023 return 1;
3024 }
3025  
3026 sub add_funcName {
3027  
3028 # Returns 'undef' on improper arguments
3029 # Returns 1 on success
3030  
3031 my ($self, $name, $obj, $check) = @_;
3032  
3033 # Check for improper arguments
3034 if (! defined $name || ! defined $obj || defined $check) {
3035  
3036 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_funcName', @_);
3037 }
3038  
3039 # Update IVs
3040 $self->ivAdd('funcNameHash', $name, $obj);
3041  
3042 return 1;
3043 }
3044  
3045 sub add_globalArray {
3046  
3047 # Returns 'undef' on improper arguments
3048 # Returns 1 on success
3049  
3050 my ($self, $name, $obj, $check) = @_;
3051  
3052 # Check for improper arguments
3053 if (! defined $name || ! defined $obj || defined $check) {
3054  
3055 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_globalArray', @_);
3056 }
3057  
3058 # Update IVs
3059 $self->ivAdd('globalArrayHash', $name, $obj);
3060  
3061 return 1;
3062 }
3063  
3064 sub push_globalDataList {
3065  
3066 # Returns 'undef' on improper arguments
3067 # Returns 1 on success
3068  
3069 my ($self, $expression, $check) = @_;
3070  
3071 # Check for improper arguments
3072 if (! defined $expression || defined $check) {
3073  
3074 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_globalDataList', @_);
3075 }
3076  
3077 # Update IVs
3078 $self->ivPush('globalDataList', $expression);
3079  
3080 return 1;
3081 }
3082  
3083 sub add_globalScalar {
3084  
3085 # Returns 'undef' on improper arguments
3086 # Returns 1 on success
3087  
3088 my ($self, $name, $obj, $check) = @_;
3089  
3090 # Check for improper arguments
3091 if (! defined $name || ! defined $obj || defined $check) {
3092  
3093 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_globalScalar', @_);
3094 }
3095  
3096 # Update IVs
3097 $self->ivAdd('globalScalarHash', $name, $obj);
3098  
3099 return 1;
3100 }
3101  
3102 sub push_gosubStackList {
3103  
3104 # Returns 'undef' on improper arguments
3105 # Returns 1 on success
3106  
3107 my ($self, $obj, $check) = @_;
3108  
3109 # Check for improper arguments
3110 if (! defined $obj || defined $check) {
3111  
3112 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_gosubStackList', @_);
3113 }
3114  
3115 # Update IVs
3116 $self->ivPush('gosubStackList', $obj);
3117  
3118 return 1;
3119 }
3120  
3121 sub pop_gosubStackList {
3122  
3123 # Returns 'undef' on improper arguments
3124 # Returns the popped value on success (or 'undef' if the list is empty)
3125  
3126 my ($self, $check) = @_;
3127  
3128 # Check for improper arguments
3129 if (defined $check) {
3130  
3131 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_gosubStackList', @_);
3132 }
3133  
3134 # Update IVs
3135 return $self->ivPop('gosubStackList');
3136 }
3137  
3138 sub set_indepInterfaceName {
3139  
3140 # Returns 'undef' on improper arguments
3141 # Returns 1 on success
3142  
3143 my ($self, $name, $check) = @_;
3144  
3145 # Check for improper arguments
3146 if (defined $check) {
3147  
3148 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_indepInterfaceName', @_);
3149 }
3150  
3151 # Update IVs
3152 $self->ivPoke('indepInterfaceName', $name);
3153  
3154 return 1;
3155 }
3156  
3157 sub set_indepInterfaceList {
3158  
3159 # Returns 'undef' on improper arguments
3160 # Returns 1 on success
3161  
3162 my ($self, @list) = @_;
3163  
3164 # (No improper arguments to check - @list can be empty)
3165  
3166 $self->ivPoke('indepInterfaceList', @list);
3167  
3168 return 1;
3169 }
3170  
3171 sub push_indepInterfaceList {
3172  
3173 # Returns 'undef' on improper arguments
3174 # Returns 1 on success
3175  
3176 my ($self, $name, $profName, $category, $check) = @_;
3177  
3178 # Check for improper arguments
3179 if (! defined $name || ! defined $profName || ! defined $category || defined $check) {
3180  
3181 return $axmud::CLIENT->writeImproper(
3182 $self->_objClass . '->push_indepInterfaceList',
3183 @_,
3184 );
3185 }
3186  
3187 # Update IVs
3188 $self->ivPush('indepInterfaceList', $name, $profName, $category);
3189  
3190 return 1;
3191 }
3192  
3193 sub push_inputList {
3194  
3195 # Returns 'undef' on improper arguments
3196 # Returns 1 on success
3197  
3198 my ($self, $lvalue, $check) = @_;
3199  
3200 # Check for improper arguments
3201 if (! defined $lvalue || defined $check) {
3202  
3203 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_inputList', @_);
3204 }
3205  
3206 # Update IVs
3207 $self->ivPush('inputList', $lvalue);
3208  
3209 return 1;
3210 }
3211  
3212 sub set_inputList {
3213  
3214 # Returns 'undef' on improper arguments
3215 # Returns 1 on success
3216  
3217 my ($self, @list) = @_;
3218  
3219 # (No improper arguments to check - @list can be empty)
3220  
3221 $self->ivPoke('inputList', @list);
3222  
3223 return 1;
3224 }
3225  
3226 sub set_nextLine {
3227  
3228 # Returns 'undef' on improper arguments
3229 # Returns 1 on success
3230  
3231 my ($self, $number, $check) = @_;
3232  
3233 # Check for improper arguments
3234 if (! defined $number || defined $check) {
3235  
3236 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_nextLine', @_);
3237 }
3238  
3239 # Update IVs
3240 $self->ivPoke('nextLine', $number);
3241  
3242 return 1;
3243 }
3244  
3245 sub set_nextStatement {
3246  
3247 # Returns 'undef' on improper arguments
3248 # Returns 1 on success
3249  
3250 my ($self, $obj, $check) = @_;
3251  
3252 # Check for improper arguments
3253 if (defined $check) {
3254  
3255 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_nextStatement', @_);
3256 }
3257  
3258 # Update IVs
3259 $self->ivPoke('nextStatement', $obj);
3260  
3261 return 1;
3262 }
3263  
3264 sub add_optionStatement {
3265  
3266 # Returns 'undef' on improper arguments
3267 # Returns 1 on success
3268  
3269 my ($self, $key, $value, $check) = @_;
3270  
3271 # Check for improper arguments
3272 if (! defined $key || defined $check) {
3273  
3274 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_optionStatement', @_);
3275 }
3276  
3277 # Update IVs
3278 $self->ivAdd('optionStatementHash', $key, $value);
3279  
3280 return 1;
3281 }
3282  
3283 sub set_pseudoCmdMode {
3284  
3285 # Returns 'undef' on improper arguments
3286 # Returns 1 on success
3287  
3288 my ($self, $mode, $check) = @_;
3289  
3290 # Check for improper arguments
3291 if (! defined $mode || defined $check) {
3292  
3293 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_pseudoCmdMode', @_);
3294 }
3295  
3296 # Update IVs
3297 $self->ivPoke('set_pseudoCmdMode', $mode);
3298  
3299 return 1;
3300 }
3301  
3302 sub set_readDataList {
3303  
3304 # Returns 'undef' on improper arguments
3305 # Returns 1 on success
3306  
3307 my ($self, @list) = @_;
3308  
3309 # (No improper arguments to check - @list can be empty)
3310  
3311 $self->ivPoke('readDataList', @list);
3312  
3313 return 1;
3314 }
3315  
3316 sub push_readDataList {
3317  
3318 # Returns 'undef' on improper arguments
3319 # Returns 1 on success
3320  
3321 my ($self, $expression, $check) = @_;
3322  
3323 # Check for improper arguments
3324 if (! defined $expression || defined $check) {
3325  
3326 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_readDataList', @_);
3327 }
3328  
3329 # Update IVs
3330 $self->ivPush('readDataList', $expression);
3331  
3332 return 1;
3333 }
3334  
3335 sub shift_readDataList {
3336  
3337 # Returns 'undef' on improper arguments
3338 # Returns the shifted value on success (or 'undef' if the list is empty)
3339  
3340 my ($self, $check) = @_;
3341  
3342 # Check for improper arguments
3343 if (defined $check) {
3344  
3345 return $axmud::CLIENT->writeImproper($self->_objClass . '->shift_readDataList', @_);
3346 }
3347  
3348 # Update IVs
3349 return $self->ivShift('readDataList');
3350 }
3351  
3352 sub set_scriptStatus {
3353  
3354 # Returns 'undef' on improper arguments
3355 # Returns 1 on success
3356  
3357 my ($self, $status, $check) = @_;
3358  
3359 # Check for improper arguments
3360 if (! defined $status || defined $check) {
3361  
3362 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_scriptStatus', @_);
3363 }
3364  
3365 # Update IVs
3366 $self->ivPoke('scriptStatus', $status);
3367  
3368 return 1;
3369 }
3370  
3371 sub set_stepCount {
3372  
3373 # Returns 'undef' on improper arguments
3374 # Returns 1 on success
3375  
3376 my ($self, $count, $check) = @_;
3377  
3378 # Check for improper arguments
3379 if (! defined $count || defined $check) {
3380  
3381 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_stepCount', @_);
3382 }
3383  
3384 # Update IVs
3385 $self->ivPoke('stepCount', $count);
3386  
3387 return 1;
3388 }
3389  
3390 sub add_subName {
3391  
3392 # Returns 'undef' on improper arguments
3393 # Returns 1 on success
3394  
3395 my ($self, $name, $obj, $check) = @_;
3396  
3397 # Check for improper arguments
3398 if (! defined $name || ! defined $obj || defined $check) {
3399  
3400 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_subName', @_);
3401 }
3402  
3403 # Update IVs
3404 $self->ivAdd('subNameHash', $name, $obj);
3405  
3406 return 1;
3407 }
3408  
3409 sub set_task {
3410  
3411 # Returns 'undef' on improper arguments
3412 # Returns 1 on success
3413  
3414 my ($self, $obj, $check) = @_;
3415  
3416 # Check for improper arguments
3417 if (! defined $obj || defined $check) {
3418  
3419 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_task', @_);
3420 }
3421  
3422 # Update IVs
3423 $self->ivPoke('parentTask', $obj);
3424 $self->ivPoke('stepMax', $obj->stepMax);
3425  
3426 return 1;
3427 }
3428  
3429 sub set_useProfile {
3430  
3431 # Returns 'undef' on improper arguments
3432 # Returns 1 on success
3433  
3434 my ($self, $name, $check) = @_;
3435  
3436 # Check for improper arguments
3437 if (! defined $name || defined $check) {
3438  
3439 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_useProfile', @_);
3440 }
3441  
3442 # Update IVs
3443 $self->ivPoke('useProfile', $name);
3444  
3445 return 1;
3446 }
3447  
3448 ##################
3449 # Accessors - get
3450  
3451 sub session
3452 { $_[0]->{session} }
3453  
3454 sub name
3455 { $_[0]->{name} }
3456 sub filePath
3457 { $_[0]->{filePath} }
3458  
3459 sub lineHash
3460 { my $self = shift; return %{$self->{lineHash}}; }
3461 sub lineCount
3462 { $_[0]->{lineCount} }
3463 sub currentLine
3464 { $_[0]->{currentLine} }
3465 sub nextLine
3466 { $_[0]->{nextLine} }
3467 sub nextStatement
3468 { $_[0]->{nextStatement} }
3469  
3470 sub primLineHash
3471 { my $self = shift; return %{$self->{primLineHash}}; }
3472  
3473 sub executionMode
3474 { $_[0]->{executionMode} }
3475 sub allErrorsFlag
3476 { $_[0]->{allErrorsFlag} }
3477 sub implementFlag
3478 { $_[0]->{implementFlag} }
3479  
3480 sub keywordList
3481 { my $self = shift; return @{$self->{keywordList}}; }
3482 sub keywordHash
3483 { my $self = shift; return %{$self->{keywordHash}}; }
3484 sub modernKeywordList
3485 { my $self = shift; return @{$self->{modernKeywordList}}; }
3486 sub primKeywordList
3487 { my $self = shift; return @{$self->{primKeywordList}}; }
3488 sub peekPokeList
3489 { my $self = shift; return @{$self->{peekPokeList}}; }
3490 sub weakKeywordHash
3491 { my $self = shift; return %{$self->{weakKeywordHash}}; }
3492 sub equivKeywordHash
3493 { my $self = shift; return %{$self->{equivKeywordHash}}; }
3494 sub clientKeywordList
3495 { my $self = shift; return @{$self->{clientKeywordList}}; }
3496 sub taskKeywordList
3497 { my $self = shift; return @{$self->{taskKeywordList}}; }
3498 sub logicalOpList
3499 { my $self = shift; return @{$self->{logicalOpList}}; }
3500 sub regexHash
3501 { my $self = shift; return %{$self->{regexHash}}; }
3502 sub categoryList
3503 { my $self = shift; return @{$self->{categoryList}}; }
3504  
3505 sub scriptStatus
3506 { $_[0]->{scriptStatus} }
3507 sub executionStatus
3508 { $_[0]->{executionStatus} }
3509  
3510 sub parentTask
3511 { $_[0]->{parentTask} }
3512 sub stepMax
3513 { $_[0]->{stepMax} }
3514 sub stepCount
3515 { $_[0]->{stepCount} }
3516  
3517 sub indepInterfaceList
3518 { my $self = shift; return @{$self->{indepInterfaceList}}; }
3519 sub indepInterfaceName
3520 { $_[0]->{indepInterfaceName} }
3521 sub depInterfaceList
3522 { my $self = shift; return @{$self->{depInterfaceList}}; }
3523 sub depInterfaceName
3524 { $_[0]->{depInterfaceName} }
3525 sub notificationList
3526 { my $self = shift; return @{$self->{notificationList}}; }
3527 sub currentNotification
3528 { $_[0]->{currentNotification} }
3529  
3530 sub errorList
3531 { my $self = shift; return @{$self->{errorList}}; }
3532  
3533 sub subStackList
3534 { my $self = shift; return @{$self->{subStackList}}; }
3535 sub subCount
3536 { $_[0]->{subCount} }
3537 sub gosubStackList
3538 { my $self = shift; return @{$self->{gosubStackList}}; }
3539  
3540 sub subNameHash
3541 { my $self = shift; return %{$self->{subNameHash}}; }
3542 sub funcNameHash
3543 { my $self = shift; return %{$self->{funcNameHash}}; }
3544 sub funcArgHash
3545 { my $self = shift; return %{$self->{funcArgHash}}; }
3546 sub currentParseSub
3547 { $_[0]->{currentParseSub} }
3548  
3549 sub column
3550 { $_[0]->{column} }
3551  
3552 sub declareMode
3553 { $_[0]->{declareMode} }
3554 sub globalScalarHash
3555 { my $self = shift; return %{$self->{globalScalarHash}}; }
3556 sub globalArrayHash
3557 { my $self = shift; return %{$self->{globalArrayHash}}; }
3558  
3559 sub globalDataList
3560 { my $self = shift; return @{$self->{globalDataList}}; }
3561 sub readDataList
3562 { my $self = shift; return @{$self->{readDataList}}; }
3563  
3564 sub fileChannelHash
3565 { my $self = shift; return %{$self->{fileChannelHash}}; }
3566  
3567 sub optionStatementHash
3568 { my $self = shift; return %{$self->{optionStatementHash}}; }
3569  
3570 sub inputList
3571 { my $self = shift; return @{$self->{inputList}}; }
3572 sub pseudoCmdMode
3573 { $_[0]->{pseudoCmdMode} }
3574 sub forcedWinFlag
3575 { $_[0]->{forcedWinFlag} }
3576 sub endStatementFlag
3577 { $_[0]->{endStatementFlag} }
3578 sub useProfile
3579 { $_[0]->{useProfile} }
3580 }
3581  
3582 { package Language::Axbasic::Line;
3583  
3584 use strict;
3585 use warnings;
3586 use diagnostics;
3587  
3588 use Glib qw(TRUE FALSE);
3589  
3590 @Language::Axbasic::Line::ISA = qw(
3591 Language::Axbasic
3592 );
3593  
3594 ##################
3595 # Constructors
3596  
3597 sub new {
3598  
3599 # Called by LA::RawScript->upload
3600 # The class that handles a single line in the script, which contains one or more statements
3601 # (e.g. FOR A = 1 to 10 : NEXT A)
3602 # (This object has no ->implement method)
3603 #
3604 # Expected arguments
3605 # $scriptObj - Blessed reference to the parent LA::Script
3606 # $lineText - The line itself, e.g. 'FOR A = 1 to 10 : NEXT A'
3607 # $origLineNum - The number of this line in the original, unprocessed script (matches a
3608 # key in LA::RawScript->lineHash)
3609 # $procLineNum - The number of this line in the processed script stored by the parent
3610 # LA::Script (matches a key in LA::Script->lineHash)
3611 #
3612 # Optional arguments
3613 # $primLineNum - (LA::Script->executionMode 'line_num' for primitive line numbering)
3614 # the primitive line number of this line, e.g. '100 goto 50' > 100
3615 #
3616 # Return values
3617 # 'undef' on improper arguments
3618 # Blessed reference to the newly-created object on success
3619  
3620 my ($class, $scriptObj, $lineText, $origLineNum, $procLineNum, $primLineNum, $check) = @_;
3621  
3622 # Check for improper arguments
3623 if (
3624 ! defined $class || ! defined $scriptObj || ! defined $lineText
3625 || ! defined $origLineNum || ! defined $procLineNum || defined $check
3626 ) {
3627 return $axmud::CLIENT->writeImproper($class . '->new', @_);
3628 }
3629  
3630 # Setup
3631 my $self = {
3632 _objName => $class, # Name Axbasic objects after their class
3633 _objClass => $class,
3634 _parentFile => undef, # No parent file object
3635 _parentWorld => undef, # No parent file object
3636 _privFlag => TRUE, # All IVs are private
3637  
3638 # Perl object components
3639 # ----------------------
3640  
3641 # The parent LA::Script
3642 scriptObj => $scriptObj,
3643  
3644 # IVs
3645 # ----
3646  
3647 # The line of text from the script
3648 lineText => $lineText,
3649 # The number of this line in the original, unprocessed script
3650 origLineNum => $origLineNum,
3651 # The number of this line in the processed script
3652 procLineNum => $procLineNum,
3653 # For primitive line numbering - the primitive line number (not related to ->origLineNum
3654 # or ->procLineNum; may be 'undef')
3655 primLineNum => $primLineNum,
3656  
3657 # Set by $self->parse - blessed reference of the first LA::Statement on the line. Each
3658 # statement has a ->nextStatement IV which gives us a list of statements in the order
3659 # in which they should be parsed/implemented
3660 firstStatement => undef,
3661 };
3662  
3663 bless $self, $class;
3664 return $self;
3665 }
3666  
3667 ##################
3668 # Methods
3669  
3670 sub parse {
3671  
3672 # Called by LA::Script->parse and LA::Script->implement
3673 # Breaks up a line into statements, then parses the statements in order
3674 #
3675 # Expected arguments
3676 # (none besides $self)
3677 #
3678 # Return values
3679 # 'undef' on improper arguments, or if there is an error
3680 # 1 on success
3681  
3682 my ($self, $check) = @_;
3683  
3684 # Local variables
3685 my ($tokenGroup, $statement, $oldStatement);
3686  
3687 # Check for improper arguments
3688 if (defined $check) {
3689  
3690 return $axmud::CLIENT->writeImproper($self->_objClass . '->parse', @_);
3691 }
3692  
3693 # Create a token group
3694 $tokenGroup = Language::Axbasic::TokenGroup->new($self->scriptObj, $self->lineText);
3695 if (! defined $tokenGroup) {
3696  
3697 return $self->scriptObj->setDebug(
3698 'Can\'t create token group object',
3699 $self->_objClass . '->parse',
3700 );
3701 }
3702  
3703 # Break the line up into tokens, and store the tokens as a list in the token group (any
3704 # access to the tokens is through the token group's methods)
3705 if (! $tokenGroup->lex()) {
3706  
3707 return undef;
3708 }
3709  
3710 # Parse statements on this line, one after the other
3711 do {
3712  
3713 # Create a statement object, and work out what kind of statement it is ($statement will
3714 # be an object of the subclass LA::Statement::*)
3715 $statement = Language::Axbasic::Statement->new($self->scriptObj, $self, $tokenGroup);
3716 if (! defined $statement) {
3717  
3718 return $self->scriptObj->setDebug(
3719 'Can\'t create statement object',
3720 $self->_objClass . '->parse',
3721 );
3722 }
3723  
3724 # Parse the statement
3725 if (! $statement->parse()) {
3726  
3727 return undef;
3728 }
3729  
3730 # Build a list of statements in the line. The first statement on the line is stored in
3731 # $self->firstStatement; every subsequent statement is stored in the previous
3732 # statement object's ->nextStatement IV
3733 if (defined $oldStatement) {
3734  
3735 $oldStatement->set_nextStatement($statement);
3736  
3737 } else {
3738  
3739 $self->ivPoke('firstStatement', $statement);
3740 }
3741  
3742 $oldStatement = $statement;
3743  
3744 # Check the next token in the token group. If there are none, we have finished parsing
3745 # this line. If it's a 'statement_end' token, another statement follows it. If it's
3746 # some other kind of token, it's a syntax error
3747  
3748 # If there's a colon at the end of the statement, separating it from the next statement
3749 # on the line, shift the corresponding token out of the token group so that, on the
3750 # next DO..WHILE loop, the first token in the group is the beginning of the next
3751 # statement can be parsed
3752 if (! $tokenGroup->shiftTokenIfCategory('statement_end')) {
3753  
3754 # But if there are other kinds of tokens at the end of the statement, it's a syntax
3755 # error
3756 if (! defined $tokenGroup->testStatementEnd()) {
3757  
3758 return $self->scriptObj->setError(
3759 'syntax_error',
3760 $self->_objClass . '->parse',
3761 );
3762 }
3763 }
3764  
3765 } while (! defined $tokenGroup->testStatementEnd());
3766  
3767 # Parsing complete
3768 return 1;
3769 }
3770  
3771 ##################
3772 # Accessors - set
3773  
3774 ##################
3775 # Accessors - get
3776  
3777 sub scriptObj
3778 { $_[0]->{scriptObj} }
3779  
3780 sub lineText
3781 { $_[0]->{lineText} }
3782 sub origLineNum
3783 { $_[0]->{origLineNum} }
3784 sub procLineNum
3785 { $_[0]->{procLineNum} }
3786 sub primLineNum
3787 { $_[0]->{primLineNum} }
3788  
3789 sub firstStatement
3790 { $_[0]->{firstStatement} }
3791 }
3792  
3793 { package Language::Axbasic::Notification;
3794  
3795 use strict;
3796 use warnings;
3797 use diagnostics;
3798  
3799 use Glib qw(TRUE FALSE);
3800  
3801 @Language::Axbasic::Notification::ISA = qw(
3802 Language::Axbasic
3803 );
3804  
3805 ##################
3806 # Constructors
3807  
3808 sub new {
3809  
3810 # Called by LA::Script->interfaceNotification
3811 # Whenever an interface created by a SETTRIG (etc) statement fires, information about the
3812 # event is passed to LA::Script->interfaceNotification, which in turn calls this function
3813 # The new LA::Notification object stores information about a single such event, ready for
3814 # the Axbasic script to use when it is ready
3815 #
3816 # Expected arguments
3817 # $scriptObj - Blessed reference to the parent LA::Script
3818 # $interfaceObj - The active interface which has fired
3819 # $fireBecause - A scalar describing why the interface fired (see the comments in
3820 # LA::Script->interfaceNotification for a list of values)
3821 #
3822 # Optional arguments
3823 # @dataList - A list of additional data, supplied by the interface when it
3824 # fired (may be an empty list)
3825 #
3826 # Return values
3827 # 'undef' on improper arguments
3828 # Blessed reference to the newly-created object on success
3829  
3830 my ($class, $scriptObj, $interfaceObj, $fireBecause, @dataList) = @_;
3831  
3832 if (
3833 ! defined $class || ! defined $scriptObj || ! defined $interfaceObj
3834 || ! defined $fireBecause
3835 ) {
3836 return $axmud::CLIENT->writeImproper($class . '->new', @_);
3837 }
3838  
3839 # Setup
3840 my $self = {
3841 _objName => $class, # Name Axbasic objects after their class
3842 _objClass => $class,
3843 _parentFile => undef, # No parent file object
3844 _parentWorld => undef, # No parent file object
3845 _privFlag => TRUE, # All IVs are private
3846  
3847 # Perl object components
3848 # ----------------------
3849  
3850 # The parent LA::Script
3851 scriptObj => $scriptObj,
3852  
3853 # IVs
3854 # ----
3855  
3856 # The active interface
3857 interfaceObj => $interfaceObj,
3858 # IVs for quick checking
3859 category => $interfaceObj->category,
3860 number => $interfaceObj->number,
3861 name => $interfaceObj->name,
3862 time => $scriptObj->session->sessionTime,
3863  
3864 # A scalar describing why the interface fired (see the comments in
3865 # LA::Script->interfaceNotification for a list of values)
3866 fireBecause => $fireBecause,
3867 # A list of additional data, supplied by the interface when it fired (may be an empty
3868 # list)
3869 dataList => \@dataList,
3870 };
3871  
3872 # Bless the object into existence
3873 bless $self, $class;
3874 return $self;
3875 }
3876  
3877 ##################
3878 # Methods
3879  
3880 ##################
3881 # Accessors - set
3882  
3883 ##################
3884 # Accessors - get
3885  
3886 sub scriptObj
3887 { $_[0]->{scriptObj} }
3888  
3889 sub interfaceObj
3890 { $_[0]->{interfaceObj} }
3891 sub category
3892 { $_[0]->{category} }
3893 sub number
3894 { $_[0]->{number} }
3895 sub name
3896 { $_[0]->{name} }
3897 sub time
3898 { $_[0]->{time} }
3899  
3900 sub fireBecause
3901 { $_[0]->{fireBecause} }
3902 sub dataList
3903 { my $self = shift; return @{$self->{dataList}}; }
3904 }
3905  
3906 { package Language::Axbasic::TokenGroup;
3907  
3908 use strict;
3909 use warnings;
3910 use diagnostics;
3911  
3912 use Glib qw(TRUE FALSE);
3913  
3914 @Language::Axbasic::TokenGroup::ISA = qw(
3915 Language::Axbasic
3916 );
3917  
3918 ##################
3919 # Constructors
3920  
3921 sub new {
3922  
3923 # Called by LA::Line->parse (which also acts in the place of LA::Line->implement, which
3924 # doesn't exist) or by any other function which wants to convert some text into a list of
3925 # tokens
3926 #
3927 # The class that handles a group of tokens
3928 # Text from a line in the Axbasic script is lexed and turned into Axbasic tokens which are
3929 # stored in this object's ->tokenList
3930 # Any access to these tokens (including creating them) is through this object's methods
3931 # Other classes' ->parse methods will usually eat their way through the tokens in this
3932 # object's ->tokenList until the list is empty
3933 #
3934 # Expected arguments
3935 # $scriptObj - Blessed reference to the parent LA::Script
3936 #
3937 # Optional arguments
3938 # $text - The text to be lexed, e.g. 'FOR A = 1 to 10 : NEXT A' (may be a complete
3939 # line, or a partial line). If not specified, then $self->importTokens
3940 # should be called instead of $self->lex
3941 #
3942 # Return values
3943 # 'undef' on improper arguments
3944 # Blessed reference to the newly-created object on success
3945  
3946 my ($class, $scriptObj, $text, $check) = @_;
3947  
3948 # Check for improper arguments
3949 if (! defined $class || ! defined $scriptObj || defined $check) {
3950  
3951 return $axmud::CLIENT->writeImproper($class . '->new', @_);
3952 }
3953  
3954 # Setup
3955 my $self = {
3956 _objName => 'token_group',
3957 _objClass => $class,
3958 _parentFile => undef, # No parent file object
3959 _parentWorld => undef, # No parent file object
3960 _privFlag => TRUE, # All IVs are private
3961  
3962 # Perl object components
3963 # ----------------------
3964  
3965 # Blessed reference of the parent LA::Script object
3966 scriptObj => $scriptObj,
3967  
3968 # IVs
3969 # ---
3970  
3971 # The text to be lexed (if supplied)
3972 text => $text,
3973 # The list of tokens, compiled after lexing, and gradually removed (starting with the
3974 # first token in the list) by calls to this object's methods
3975 tokenList => [],
3976 # Flag set to TRUE if the token group ended with a 'short_comment' token (which was not
3977 # added to ->tokenList)
3978 shortCommentFlag => FALSE,
3979 };
3980  
3981 bless $self, $class;
3982 return $self;
3983 }
3984  
3985 ##################
3986 # Methods
3987  
3988 sub lex {
3989  
3990 # Called by LA::Line->parse (which also acts in the place of LA::Line->implement, which
3991 # doesn't exist) or by any other function which wants to convert some text into a list of
3992 # tokens
3993 # Breaks the line of Axbasic text in $self->text into LA::Token objects and puts them in
3994 # $self->tokenList, in the order in which they were encountered
3995 # Stops lexing at the first '!' token, which means the rest of the line is a comment
3996 #
3997 # Expected arguments
3998 # (none besides $self)
3999 #
4000 # Return values
4001 # 'undef' on improper arguments or if there's an error creating a LA::Token object
4002 # 1 otherwise
4003  
4004 my ($self, $check) = @_;
4005  
4006 # Local variables
4007 my $token;
4008  
4009 # Check for improper arguments
4010 if (defined $check) {
4011  
4012 return $axmud::CLIENT->writeImproper($self->_objClass . '->lex', @_);
4013 }
4014  
4015 do {
4016  
4017 $token = Language::Axbasic::Token->new($self->scriptObj, $self->text);
4018 if (! defined $token) {
4019  
4020 return undef;
4021 }
4022  
4023 if (! $token->extract()) {
4024  
4025 # An invalid token, like 'string' instead of "string"
4026 return $self->scriptObj->setError(
4027 'syntax_error',
4028 $self->_objClass . '->lex',
4029 );
4030  
4031 return undef;
4032  
4033 } else {
4034  
4035 if ($token->category eq 'short_comment') {
4036  
4037 # ! encountered, meaning that the rest of the line is a comment. Set a flag, so
4038 # that LA::Statement->refine knows this line did contain a valid token
4039 $self->ivPoke('shortCommentFlag', TRUE);
4040  
4041 } else {
4042  
4043 # LA::Token was able to extract a token from $self->text
4044 # Add the LA::Token object to this object's token list and, on the next loop,
4045 # search for tokens on the remainder of $self->text
4046 $self->ivPush('tokenList', $token);
4047 $self->ivPoke('text', $token->remainText);
4048 }
4049 }
4050  
4051 # Continue until there are no tokens left
4052 } until ($self->text eq '' || $token->category eq 'short_comment');
4053  
4054 return 1;
4055 }
4056  
4057 sub lookAhead {
4058  
4059 # Can be called by anything (all access to tokens is via LA::TokenGroup's methods)
4060 # Returns the first token in the group without removing it from the group, but returns
4061 # 'undef' if there are no more tokens left
4062 #
4063 # Expected arguments
4064 # (none besides $self)
4065 #
4066 # Return values
4067 # 'undef' on improper arguments or if there are no tokens left in $self->tokenList
4068 # Otherwise returns the blessed reference of the token
4069  
4070 my ($self, $check) = @_;
4071  
4072 # Check for improper arguments
4073 if (defined $check) {
4074  
4075 return $axmud::CLIENT->writeImproper($self->_objClass . '->lookAhead', @_);
4076 }
4077  
4078 if ($self->tokenList) {
4079  
4080 return $self->ivFirst('tokenList');
4081  
4082 } else {
4083  
4084 return undef;
4085 }
4086 }
4087  
4088 sub shiftToken {
4089  
4090 # Can be called by anything (all access to tokens is via LA::TokenGroup's methods)
4091 # Removes the first token in the group and returns its blessed reference, but returns
4092 # 'undef' if there are no more tokens left
4093 #
4094 # Expected arguments
4095 # (none besides $self)
4096 #
4097 # Return values
4098 # 'undef' on improper arguments or if there are no tokens left in $self->tokenList
4099 # Otherwise returns the blessed reference of the removed token
4100  
4101 my ($self, $check) = @_;
4102  
4103 # Check for improper arguments
4104 if (defined $check) {
4105  
4106 return $axmud::CLIENT->writeImproper($self->_objClass . '->shiftToken', @_);
4107 }
4108  
4109 if ($self->tokenList) {
4110  
4111 return $self->ivShift('tokenList');
4112  
4113 } else {
4114  
4115 return undef;
4116 }
4117 }
4118  
4119 sub shiftMatchingToken {
4120  
4121 # Can be called by anything (all access to tokens is via LA::TokenGroup's methods)
4122 # If the text of the first token in the group (LA::Token->tokenText) matches the specified
4123 # argument (exactly), removes it from the group and returns its blessed reference
4124 # Otherwise returns 'undef'
4125 #
4126 # Expected arguments
4127 # $matchString - The string to match
4128 #
4129 # Return values
4130 # 'undef' on improper arguments, if there are no tokens left in $self->tokenList or if
4131 # the first token doesn't match the specified string
4132 # Otherwise returns the blessed reference of the removed token
4133  
4134 my ($self, $matchString, $check) = @_;
4135  
4136 # Check for improper arguments
4137 if (! defined $matchString || defined $check) {
4138  
4139 return $axmud::CLIENT->writeImproper($self->_objClass . '->shiftMatchingToken', @_);
4140 }
4141  
4142 if ($self->tokenList && $self->ivFirst('tokenList')->tokenText eq $matchString) {
4143  
4144 return $self->ivShift('tokenList');
4145  
4146 } else {
4147  
4148 return undef;
4149 }
4150 }
4151  
4152 sub shiftTokenIfCategory {
4153  
4154 # Can be called by anything (all access to tokens is via LA::TokenGroup's methods)
4155 # If the first token in the group is of the specified category, removes the token from the
4156 # group and returns its blessed reference
4157 # Otherwise returns 'undef'
4158 #
4159 # Expected arguments
4160 # $category - The category to match
4161 #
4162 # Return values
4163 # 'undef' on improper arguments, if there are no tokens left in $self->tokenList or if
4164 # the first token doesn't match the specified category
4165 # Otherwise returns the blessed reference of the removed token
4166  
4167 my ($self, $category, $check) = @_;
4168  
4169 # Local variables
4170 my $token;
4171  
4172 # Check for improper arguments
4173 if (! defined $category || defined $check) {
4174  
4175 return $axmud::CLIENT->writeImproper($self->_objClass . '->shiftTokenIfCategory', @_);
4176 }
4177  
4178 if (! $self->tokenList) {
4179  
4180 # No tokens left to compare
4181 return undef;
4182  
4183 # Because the assignment operator '=' and the relational operator '=' are identical, we need
4184 # special tests for them
4185 } elsif (
4186 $category eq 'relational_operator'
4187 && $self->ivFirst('tokenList')->category eq 'assignment_operator'
4188 ) {
4189 $token = $self->ivShift('tokenList');
4190 # Change it from an assignment to a relational operator
4191 $token->ivPoke('category', 'relational_operator');
4192 $token->set_category('relational_operator');
4193  
4194 return $token;
4195  
4196 } elsif (
4197 $category eq 'assignment_operator'
4198 && $self->ivFirst('tokenList')->category eq 'relational_operator'
4199 ) {
4200 $token = $self->ivShift('tokenList');
4201 # Change it from an assignment to a relational operator
4202 $token->set_category('assignment_operator');
4203  
4204 return $token;
4205  
4206 } elsif ($self->tokenList && $self->ivFirst('tokenList')->category eq $category) {
4207  
4208 # Token categories match (exactly)
4209 return $self->ivShift('tokenList');
4210  
4211 } else {
4212  
4213 # Token categories don't match at all
4214 return undef;
4215 }
4216 }
4217  
4218 sub importTokens {
4219  
4220 # Can be called by anything (all access to tokens is via LA::TokenGroup's methods)
4221 # Imports tokens into this group from another group until the other group has no more
4222 # tokens, or until the other group's first token has ->tokenText that matches a specified
4223 # string exactly (in which case, the token is not imported, and remains as the first token
4224 # in the other group)
4225 #
4226 # Expected arguments
4227 # $otherGroup - The other LA::TokenGroup from which to taken tokens
4228 #
4229 # Optional arguments
4230 # @matchList - A list of strings. If any of them match the other group's ->tokenText,
4231 # that ends the process. (If the list is empty, all tokens to the end
4232 # of the group are taken)
4233 #
4234 # Return values
4235 # 'undef' on improper arguments, if there are no tokens left in $otherGroup's token list
4236 # or if the first token in $otherGroup's token list matches a string in @matchList
4237 # exactly
4238 # Otherwise returns the number of tokens imported (which might be 0)
4239  
4240 my ($self, $otherGroup, @matchList) = @_;
4241  
4242 # Local variables
4243 my ($token, $count);
4244  
4245 # Check for improper arguments
4246 if (! defined $otherGroup) {
4247  
4248 return $axmud::CLIENT->writeImproper($self->_objClass . '->importTokens', @_);
4249 }
4250  
4251 # Import tokens
4252 $count = 0;
4253 do {
4254  
4255 $token = undef;
4256  
4257 if ($otherGroup->tokenList) {
4258  
4259 $token = $otherGroup->ivFirst('tokenList');
4260 if (defined $token) {
4261  
4262 foreach my $matchString (@matchList) {
4263  
4264 if ($token->tokenText eq $matchString) {
4265  
4266 # Stop importing. Return the number of imported tokens (may be 0)
4267 return $count;
4268 }
4269 }
4270  
4271 # Otherwise, import this token
4272 $self->ivPush('tokenList', $token);
4273 $otherGroup->shiftToken();
4274 $count++;
4275 }
4276 }
4277  
4278 } until (! defined $token);
4279  
4280 # Return the number of imported tokens (may be 0)
4281 return $count;
4282 }
4283  
4284 sub testStatementEnd {
4285  
4286 # Tests whether the statement being parsed has finished (ie there are no tokens left in the
4287 # token group, or the first token in the group is a colon)
4288 #
4289 # Expected arguments
4290 # (none besides $self)
4291 #
4292 # Return values
4293 # 'undef' on improper arguments or if the first token in the group isn't a statement
4294 # separator (ie colon)
4295 # 1 if there are no tokens left in the group, or the first token in the group is a
4296 # statement separator
4297  
4298 my ($self, $check) = @_;
4299  
4300 # Local variables
4301 my $token;
4302  
4303 # Check for improper arguments
4304 if (defined $check) {
4305  
4306 return $axmud::CLIENT->writeImproper($self->_objClass . '->testStatementEnd', @_);
4307 }
4308  
4309 $token = $self->lookAhead();
4310 if (! $token || $token->category eq 'statement_end') {
4311  
4312 # No more tokens left, or we've reached the end of a statement
4313 return 1;
4314  
4315 } else {
4316  
4317 return undef;
4318 }
4319 }
4320  
4321 ##################
4322 # Accessors - set
4323  
4324 sub set_tokenList {
4325  
4326 # Returns 'undef' on improper arguments
4327 # Returns 1 on success
4328  
4329 my ($self, @list) = @_;
4330  
4331 # (No improper arguments to check - @list can be empty)
4332  
4333 $self->ivPoke('tokenList', @list);
4334  
4335 return 1;
4336 }
4337  
4338 ##################
4339 # Accessors - get
4340  
4341 sub scriptObj
4342 { $_[0]->{scriptObj} }
4343  
4344 sub text
4345 { $_[0]->{text} }
4346 sub tokenList
4347 { my $self = shift; return @{$self->{tokenList}}; }
4348 sub shortCommentFlag
4349 { $_[0]->{shortCommentFlag} }
4350  
4351 }
4352  
4353 { package Language::Axbasic::Token;
4354  
4355 use strict;
4356 use warnings;
4357 use diagnostics;
4358  
4359 use Glib qw(TRUE FALSE);
4360  
4361 @Language::Axbasic::Token::ISA = qw(
4362 Language::Axbasic
4363 );
4364  
4365 ##################
4366 # Constructors
4367  
4368 sub new {
4369  
4370 # Called by LA::TokenGroup->lex
4371 # The class that handles a single token. The calling function calls ->new to create the
4372 # LA::Token object, then ->extract to extract the token from the specified Axbasic text,
4373 # leaving a shorter piece of text
4374 #
4375 # Expected arguments
4376 # $scriptObj - Blessed reference to the parent LA::Script
4377 # $text - Some Axbasic text, which may be a complete or partial line
4378 # eg 'FOR A = 1 to 10 : NEXT A'
4379 #
4380 # Return values
4381 # 'undef' on improper arguments
4382 # Blessed reference to the newly-created object on success
4383  
4384 my ($class, $scriptObj, $text, $check) = @_;
4385  
4386 # Check for improper arguments
4387 if (! defined $class || ! defined $scriptObj || ! defined $text || defined $check) {
4388  
4389 return $axmud::CLIENT->writeImproper($class . '->new', @_);
4390 }
4391  
4392 # Setup
4393 my $self = {
4394 _objName => 'token',
4395 _objClass => $class,
4396 _parentFile => undef, # No parent file object
4397 _parentWorld => undef, # No parent file object
4398 _privFlag => TRUE, # All IVs are private
4399  
4400 # Perl object components
4401 # ----------------------
4402  
4403 # Blessed reference of the parent LA::Script object
4404 scriptObj => $scriptObj,
4405  
4406 # IVs
4407 # ---
4408  
4409 # The text from which the token will be extracted - may be a complete or partial line of
4410 # BASIC
4411 text => $text,
4412 # The remaining text in ->text, after the token has been extracted
4413 remainText => undef,
4414  
4415 # The extracted text of the token (e.g. 'print' or '$a'), converted to lower case
4416 # (except for string constants), and with leading whitespace removed
4417 tokenText => undef,
4418 # The extracted text of the token, before conversion, and with leading whitespace intact
4419 origTokenText => undef,
4420 # The leading whitespace (don't know why we need it, but still...)
4421 leadSpace => undef,
4422  
4423 # The category of this token - one of the elements in ->categoryList
4424 category => undef,
4425 };
4426  
4427 bless $self, $class;
4428 return $self;
4429 }
4430  
4431 ##################
4432 # Methods
4433  
4434 sub extract {
4435  
4436 # Called by LA::TokenGroup->lex
4437 # Extracts a single token from the beginning of $self->text. Reduces text outside of quotes
4438 # strings to lowercase, and saves the results in this object's instance variables, so
4439 # LA::TokenGroup->lex can access them
4440 #
4441 # Expected arguments
4442 # (none besides self)
4443 #
4444 # Return values
4445 # 'undef' on improper arguments, if no token can be extracted from $self->line or if the
4446 # whole of the rest of the line is a comment starting with !
4447 # 1 otherwise
4448  
4449 my ($self, $check) = @_;
4450  
4451 # Local variables
4452 my $text;
4453  
4454 # Check for improper arguments
4455 if (defined $check) {
4456  
4457 return $axmud::CLIENT->writeImproper($self->_objClass . '->extract', @_);
4458 }
4459  
4460 # The text from which the token is to be extracted
4461 $text = $self->text;
4462  
4463 # Check each category of token in order. (The order is important, because the category
4464 # 'identifier' matches nearly all alphanumeric text)
4465 OUTER: foreach my $category ($self->scriptObj->categoryList) {
4466  
4467 my ($regex, $origSpace, $tokenText);
4468  
4469 if (! $self->scriptObj->ivExists('regexHash', $category)) {
4470  
4471 return $self->scriptObj->setDebug(
4472 'Missing regex for token category \'' . $category . '\'',
4473 $self->_objClass . '->extract',
4474 );
4475 }
4476  
4477 $regex = $self->scriptObj->ivShow('regexHash', $category);
4478  
4479 if ($text =~ s/^(\s*)($regex)//) {
4480  
4481 # Found a token matching $category
4482 $origSpace = $1;
4483 $tokenText = $2;
4484  
4485 # (Special case: PRINT$ can be used as a variable, but PRINT obviously cannot.
4486 # The $regex picks up the PRINT portion of PRINT$ as a keyword, so we need to
4487 # convert it)
4488 if ($category eq 'keyword' && $text =~ s/^\$//) {
4489  
4490 $category = 'identifier';
4491 $tokenText .= '$';
4492 }
4493  
4494 # Most tokens are case-insensitive
4495 if ($category ne 'string_constant') {
4496  
4497 $tokenText = lc($tokenText);
4498 }
4499  
4500 # Store details of the token in this object's IVs
4501 $self->ivPoke('origTokenText', $origSpace . $tokenText);
4502 $self->ivPoke('leadSpace', $origSpace);
4503 $self->ivPoke('tokenText', $tokenText);
4504 $self->ivPoke('category', $category);
4505 $self->ivPoke('remainText', $text);
4506  
4507 # Stop after finding the first bit of text matching a category of token in
4508 # @categoryList
4509 last OUTER;
4510 }
4511 }
4512  
4513 if (! defined $self->category) {
4514  
4515 # No tokens found
4516 return undef;
4517  
4518 } else {
4519  
4520 # Token found
4521 return 1;
4522 }
4523 }
4524  
4525 ##################
4526 # Accessors - set
4527  
4528 sub set_category {
4529  
4530 # Returns 'undef' on improper arguments
4531 # Returns 1 on success
4532  
4533 my ($self, $category, $check) = @_;
4534  
4535 # Check for improper arguments
4536 if (! defined $category || defined $check) {
4537  
4538 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_category', @_);
4539 }
4540  
4541 # Update IVs
4542 $self->ivPoke('category', $category);
4543  
4544 return 1;
4545 }
4546  
4547 sub set_tokenText {
4548  
4549 # Called by LA::Statement::if->parse to convert an ELSEIF keyword token into an IF
4550 # Returns 'undef' on improper arguments
4551 # Returns 1 on success
4552  
4553 my ($self, $tokenText, $check) = @_;
4554  
4555 # Check for improper arguments
4556 if (! defined $tokenText || defined $check) {
4557  
4558 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_tokenText', @_);
4559 }
4560  
4561 # Update IVs
4562 $self->ivPoke('tokenText', $tokenText);
4563  
4564 return 1;
4565 }
4566  
4567 ##################
4568 # Accessors - get
4569  
4570 sub scriptObj
4571 { $_[0]->{scriptObj} }
4572  
4573 sub text
4574 { $_[0]->{text} }
4575 sub remainText
4576 { $_[0]->{remainText} }
4577  
4578 sub tokenText
4579 { $_[0]->{tokenText} }
4580 sub origTokenText
4581 { $_[0]->{origTokenText} }
4582 sub leadSpace
4583 { $_[0]->{leadSpace} }
4584  
4585 sub category
4586 { $_[0]->{category} }
4587 }
4588  
4589 { package Language::Axbasic::FileChannel;
4590  
4591 use strict;
4592 use warnings;
4593 use diagnostics;
4594  
4595 use Glib qw(TRUE FALSE);
4596  
4597 @Language::Axbasic::FileChannel::ISA = qw(
4598 Language::Axbasic
4599 );
4600  
4601 ##################
4602 # Constructors
4603  
4604 sub new {
4605  
4606 # Called by LA::Statement::open->implement
4607 # The class that handles a file channel, opened with an OPEN statement and closed with a
4608 # CLOSE statement
4609 #
4610 # Expected arguments
4611 # $scriptObj - Blessed reference to the parent LA::Script
4612 # $channel - The file channel number (an integer, 1 or above)
4613 # $filePath - The file opened for reading/writing
4614 # $fileHandle - The filehandle used to read/write from the file
4615 # $orgType - In Axbasic, always set to 'text'
4616 # $createType - 'new', 'old' or 'newold'
4617 # $accessType - 'outin', 'input' or 'output'
4618 #
4619 # Return values
4620 # 'undef' on improper arguments
4621 # Blessed reference to the newly-created object on success
4622  
4623 my (
4624 $class, $scriptObj, $channel, $filePath, $fileHandle, $orgType, $createType,
4625 $accessType, $check
4626 ) = @_;
4627  
4628 # Check for improper arguments
4629 if (
4630 ! defined $class || ! defined $scriptObj || ! defined $channel || ! defined $filePath
4631 || ! defined $fileHandle || ! defined $orgType || ! defined $createType
4632 || defined $check
4633 ) {
4634 return $axmud::CLIENT->writeImproper($class . '->new', @_);
4635 }
4636  
4637 # Setup
4638 my $self = {
4639 _objName => 'file_channel',
4640 _objClass => $class,
4641 _parentFile => undef, # No parent file object
4642 _parentWorld => undef, # No parent file object
4643 _privFlag => TRUE, # All IVs are private
4644  
4645 # Perl object components
4646 # ----------------------
4647  
4648 # Blessed reference of the parent LA::Script object
4649 scriptObj => $scriptObj,
4650  
4651 # IVs
4652 # ---
4653  
4654 # The file channel number
4655 channel => $channel,
4656 # The file opened for reading/writing
4657 filePath => $filePath,
4658 # The filehandle used to read/write from the file
4659 fileHandle => $fileHandle,
4660  
4661 # OPEN statement options
4662 # For 'ORGANIZATION TEXT' (default), set to 'text'. Axbasic does not implement True
4663 # BASIC's 'ORGANIZATION STREAM', 'ORGANIZATION RANDOM', 'ORGANIZATION RECORD' or
4664 # 'ORGANIZATION BYTE'
4665 orgType => $orgType,
4666 # For 'CREATE NEW', 'CREATE OLD' (default) or 'CREATE NEWOLD', set to 'new', 'old' or
4667 # 'newold'
4668 createType => $createType,
4669 # For 'ACCESS OUTIN' (default), 'ACCESS INPUT' or 'ACCESS OUTPUT', set to 'outin',
4670 # 'input' or 'output'
4671 accessType => $accessType,
4672 };
4673  
4674 bless $self, $class;
4675 return $self;
4676 }
4677  
4678 ##################
4679 # Methods
4680  
4681 ##################
4682 # Accessors - set
4683  
4684 ##################
4685 # Accessors - get
4686  
4687 sub scriptObj
4688 { $_[0]->{scriptObj} }
4689  
4690 sub channel
4691 { $_[0]->{channel} }
4692 sub filePath
4693 { $_[0]->{filePath} }
4694 sub fileHandle
4695 { $_[0]->{fileHandle} }
4696  
4697 sub orgType
4698 { $_[0]->{orgType} }
4699 sub createType
4700 { $_[0]->{createType} }
4701 sub accessType
4702 { $_[0]->{accessType} }
4703 }
4704  
4705 # Package must return a true value
4706 1