blib/lib/Language/Axbasic/Subroutine.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 386 CC 22 SCAR 15.3
CC
22
Cov
0%
new
12.0
set_argListObj
6.0
push_blockStackList
6.0
CRAP 4.6
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # Lesser Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Language::Axbasic::Subroutine, based on Language::Basic by Amir Karger
16  
17 { package Language::Axbasic::Subroutine;
18  
19 use strict;
20 use warnings;
21 # use diagnostics;
22  
23 use Glib qw(TRUE FALSE);
24  
25 @Language::Axbasic::Subroutine::ISA = qw(
26 Language::Axbasic
27 );
28  
29 ##################
30 # Constructors
31  
32 sub new {
33  
34 # Called by LA::Script->parse
35 # The class that handles a subroutine
36 #
37 # Expected arguments
38 # $scriptObj - Blessed reference of the parent LA::Script
39 # $subName - A name for the subroutine
40 #
41 # Return values
42 # 'undef' on improper arguments or if a subroutine with the name $subName already exists
43 # Blessed reference to the newly-created object on success
44  
45 my ($class, $scriptObj, $subName, $check) = @_;
46  
47 # Check for improper arguments
48 if (! defined $class || ! defined $scriptObj || ! defined $subName || defined $check) {
49  
50 return $axmud::CLIENT->writeImproper($class . '->new', @_);
51 }
52  
53 # Check that a subroutine with this name doesn't already exist
54 if ($scriptObj->ivExists('subNameHash', $subName)) {
55  
56 return $scriptObj->setDebug(
57 'Can\'t create Language::Axbasic::Subroutine object; subroutine or function'
58 . ' named \'' . $subName . '\' - already exists',
59 $class . '->new',
60 );
61 }
62  
63 # Setup
64 my $self = {
65 _objName => $class, # Name Axbasic objects after their class
66 _objClass => $class,
67 _parentFile => undef, # No parent file object
68 _parentWorld => undef, # No parent file object
69 _privFlag => TRUE, # All IVs are private
70  
71 # Perl object components
72 # ----------------------
73  
74 # The parent LA::Script
75 scriptObj => $scriptObj,
76  
77 # IVs
78 # ----
79  
80 # The subroutine name
81 name => $subName,
82 # The position of this subroutine in LA::Script's subroutine/function stack (unknown at
83 # the moment)
84 stackPosn => undef,
85  
86 # Code block stack (loops such as FOR..NEXT, DO..WHILE, etc)
87 # Contains a list of LA::Statements in which the block started
88 blockStackList => [],
89  
90 # ->blockStackList is used during the ->implement stage. We also use separate lists
91 # for each kind of code block, used exclusively during the ->parse stage - mainly so
92 # that the start of a code block can be matched with the end
93 # Special stack for DO..UNTIL code blocks
94 doStackList => [],
95 # Special stack for WHILE..LOOP code blocks
96 whileStackList => [],
97 # Special stack for SELECT CASE..END SELECT code blocks
98 selectStackList => [],
99 # Special stack for multi-line IF...END IF blocks
100 ifStackList => [],
101  
102 # The SUB statement with which the subroutine was declared
103 declareStatement => undef,
104 # The END SUB statement with which the subroutine was terminated
105 terminateStatement => undef,
106 # The CALL statement from which the subroutine was called
107 callStatement => undef,
108 # The LA::Expression::Arglist with which the subroutine was declared
109 argListObj => undef,
110 # If the subroutine is called from a statement like 'LET a$ = CALL...', the variable (a$
111 # in this example) to which the subroutine's return value is assigned
112 # (In a straight 'CALL...' statement, this is set to 'undef')
113 returnVar => undef,
114 # The return value type - 'string' or 'numeric'
115 returnVarType => undef,
116  
117 # Local variable hashes
118  
119 # Local scalar variables
120 # ->localScalarHash{scalar_name} = blessed_reference_to_scalar_object;
121 # (the object is a LA::Variable::Scalar::String or LA::Variable::Scalar::Numeric)
122 localScalarHash => {},
123 # Local array variables
124 # ->localArrayHash{array_name} = blessed_reference_to_array_object;
125 # (the object is a LA::Variable::Array::String or LA::Variable::Array::Numeric)
126 localArrayHash => {},
127 };
128  
129 bless $self, $class;
130  
131 # Update the parent LA::Script
132 $scriptObj->add_subName($subName, $self);
133  
134 return $self;
135 }
136  
137 ##################
138 # Methods
139  
140 ##################
141 # Accessors - set
142  
143 sub set_argListObj {
144  
145 # Returns 'undef' on improper arguments
146 # Returns 1 on success
147  
148 my ($self, $obj, $check) = @_;
149  
150 # Check for improper arguments
151 if (! defined $obj || defined $check) {
152  
153 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_argListObj', @_);
154 }
155  
156 # Update IVs
157 $self->ivPoke('argListObj', $obj);
158  
159 return 1;
160 }
161  
162 sub set_blockStackList {
163  
164 # Returns 'undef' on improper arguments
165 # Returns 1 on success
166  
167 my ($self, @list) = @_;
168  
169 # (No improper arguments to check - @list can be empty)
170  
171 $self->ivPoke('blockStackList', @list);
172  
173 return 1;
174 }
175  
176 sub push_blockStackList {
177  
178 # Returns 'undef' on improper arguments
179 # Returns 1 on success
180  
181 my ($self, $obj, $check) = @_;
182  
183 # Check for improper arguments
184 if (! defined $obj || defined $check) {
185  
186 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_blockStackList', @_);
187 }
188  
189 # Update IVs
190 $self->ivPush('blockStackList', $obj);
191  
192 return 1;
193 }
194  
195 sub pop_blockStackList {
196  
197 # Returns 'undef' on improper arguments
198 # Returns the popped value on success (or 'undef' if the IV is empty)
199  
200 my ($self, $check) = @_;
201  
202 # Check for improper arguments
203 if (defined $check) {
204  
205 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_blockStackList', @_);
206 }
207  
208 # Update IVs
209 return $self->ivPop('blockStackList');
210 }
211  
212 sub set_callStatement {
213  
214 # Returns 'undef' on improper arguments
215 # Returns 1 on success
216  
217 my ($self, $obj, $check) = @_;
218  
219 # Check for improper arguments
220 if (! defined $obj || defined $check) {
221  
222 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_callStatement', @_);
223 }
224  
225 # Update IVs
226 $self->ivPoke('callStatement', $obj);
227  
228 return 1;
229 }
230  
231 sub set_declareStatement {
232  
233 # Returns 'undef' on improper arguments
234 # Returns 1 on success
235  
236 my ($self, $obj, $check) = @_;
237  
238 # Check for improper arguments
239 if (! defined $obj || defined $check) {
240  
241 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_declareStatement', @_);
242 }
243  
244 # Update IVs
245 $self->ivPoke('declareStatement', $obj);
246  
247 return 1;
248 }
249  
250 sub push_doStackList {
251  
252 # Returns 'undef' on improper arguments
253 # Returns 1 on success
254  
255 my ($self, $obj, $check) = @_;
256  
257 # Check for improper arguments
258 if (! defined $obj || defined $check) {
259  
260 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_doStackList', @_);
261 }
262  
263 # Update IVs
264 $self->ivPush('doStackList', $obj);
265  
266 return 1;
267 }
268  
269 sub pop_doStackList {
270  
271 # Returns 'undef' on improper arguments
272 # Returns the popped value on success (or 'undef' if the IV is empty)
273  
274 my ($self, $check) = @_;
275  
276 # Check for improper arguments
277 if (defined $check) {
278  
279 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_doStackList', @_);
280 }
281  
282 # Update IVs
283 return $self->ivPop('doStackList');
284 }
285  
286 sub push_ifStackList {
287  
288 # Returns 'undef' on improper arguments
289 # Returns 1 on success
290  
291 my ($self, $obj, $check) = @_;
292  
293 # Check for improper arguments
294 if (! defined $obj || defined $check) {
295  
296 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_ifStackList', @_);
297 }
298  
299 # Update IVs
300 $self->ivPush('ifStackList', $obj);
301  
302 return 1;
303 }
304  
305 sub pop_ifStackList {
306  
307 # Returns 'undef' on improper arguments
308 # Returns the popped value on success (or 'undef' if the IV is empty)
309  
310 my ($self, $check) = @_;
311  
312 # Check for improper arguments
313 if (defined $check) {
314  
315 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_ifStackList', @_);
316 }
317  
318 # Update IVs
319 return $self->ivPop('ifStackList');
320 }
321  
322 sub add_localArray {
323  
324 # Returns 'undef' on improper arguments
325 # Returns 1 on success
326  
327 my ($self, $name, $obj, $check) = @_;
328  
329 # Check for improper arguments
330 if (! defined $name || ! defined $obj || defined $check) {
331  
332 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_localArray', @_);
333 }
334  
335 # Update IVs
336 $self->ivAdd('localArrayHash', $name, $obj);
337  
338 return 1;
339 }
340  
341 sub set_localArrayHash {
342  
343 # Returns 'undef' on improper arguments
344 # Returns 1 on success
345  
346 my ($self, %hash) = @_;
347  
348 # (No improper arguments to check - @list can be empty)
349  
350 $self->ivPoke('localArrayHash', %hash);
351  
352 return 1;
353 }
354  
355 sub add_localScalar {
356  
357 # Returns 'undef' on improper arguments
358 # Returns 1 on success
359  
360 my ($self, $name, $obj, $check) = @_;
361  
362 # Check for improper arguments
363 if (! defined $name || ! defined $obj || defined $check) {
364  
365 return $axmud::CLIENT->writeImproper($self->_objClass . '->add_localScalar', @_);
366 }
367  
368 # Update IVs
369 $self->ivAdd('localScalarHash', $name, $obj);
370  
371 return 1;
372 }
373  
374 sub set_localScalarHash {
375  
376 # Returns 'undef' on improper arguments
377 # Returns 1 on success
378  
379 my ($self, %hash) = @_;
380  
381 # (No improper arguments to check - @list can be empty)
382  
383 $self->ivPoke('localScalarHash', %hash);
384  
385 return 1;
386 }
387  
388 sub push_selectStackList {
389  
390 # Returns 'undef' on improper arguments
391 # Returns 1 on success
392  
393 my ($self, $obj, $check) = @_;
394  
395 # Check for improper arguments
396 if (! defined $obj || defined $check) {
397  
398 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_selectStackList', @_);
399 }
400  
401 # Update IVs
402 $self->ivPush('selectStackList', $obj);
403  
404 return 1;
405 }
406  
407 sub set_returnVar {
408  
409 # Returns 'undef' on improper arguments
410 # Returns 1 on success
411  
412 my ($self, $value, $check) = @_;
413  
414 # Check for improper arguments
415 if (defined $check) {
416  
417 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_returnVar', @_);
418 }
419  
420 # Update IVs
421 $self->ivPoke('returnVar', $value);
422  
423 return 1;
424 }
425  
426 sub set_returnVarType {
427  
428 # Returns 'undef' on improper arguments
429 # Returns 1 on success
430  
431 my ($self, $value, $check) = @_;
432  
433 # Check for improper arguments
434 if (! defined $value || defined $check) {
435  
436 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_returnVarType', @_);
437 }
438  
439 # Update IVs
440 $self->ivPoke('returnVarType', $value);
441  
442 return 1;
443 }
444  
445 sub pop_selectStackList {
446  
447 # Returns 'undef' on improper arguments
448 # Returns the popped value on success (or 'undef' if the IV is empty)
449  
450 my ($self, $check) = @_;
451  
452 # Check for improper arguments
453 if (defined $check) {
454  
455 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_selectStackList', @_);
456 }
457  
458 # Update IVs
459 return $self->ivPop('selectStackList');
460 }
461  
462 sub set_stackPosn {
463  
464 # Returns 'undef' on improper arguments
465 # Returns 1 on success
466  
467 my ($self, $num, $check) = @_;
468  
469 # Check for improper arguments
470 if (! defined $num || defined $check) {
471  
472 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_stackPosn', @_);
473 }
474  
475 # Update IVs
476 $self->ivPoke('stackPosn', $num);
477  
478 return 1;
479 }
480  
481 sub set_terminateStatement {
482  
483 # Returns 'undef' on improper arguments
484 # Returns 1 on success
485  
486 my ($self, $obj, $check) = @_;
487  
488 # Check for improper arguments
489 if (! defined $obj || defined $check) {
490  
491 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_terminateStatement', @_);
492 }
493  
494 # Update IVs
495 $self->ivPoke('terminateStatement', $obj);
496  
497 return 1;
498 }
499  
500 sub push_whileStackList {
501  
502 # Returns 'undef' on improper arguments
503 # Returns 1 on success
504  
505 my ($self, $obj, $check) = @_;
506  
507 # Check for improper arguments
508 if (! defined $obj || defined $check) {
509  
510 return $axmud::CLIENT->writeImproper($self->_objClass . '->push_whileStackList', @_);
511 }
512  
513 # Update IVs
514 $self->ivPush('whileStackList', $obj);
515  
516 return 1;
517 }
518  
519 sub pop_whileStackList {
520  
521 # Returns 'undef' on improper arguments
522 # Returns the popped value on success (or 'undef' if the IV is empty)
523  
524 my ($self, $check) = @_;
525  
526 # Check for improper arguments
527 if (defined $check) {
528  
529 return $axmud::CLIENT->writeImproper($self->_objClass . '->pop_whileStackList', @_);
530 }
531  
532 # Update IVs
533 return $self->ivPop('whileStackList');
534 }
535  
536 ##################
537 # Accessors - get
538  
539 sub scriptObj
540 { $_[0]->{scriptObj} }
541  
542 sub name
543 { $_[0]->{name} }
544 sub stackPosn
545 { $_[0]->{stackPosn} }
546  
547 sub blockStackList
548 { my $self = shift; return @{$self->{blockStackList}}; }
549  
550 sub doStackList
551 { my $self = shift; return @{$self->{doStackList}}; }
552 sub whileStackList
553 { my $self = shift; return @{$self->{whileStackList}}; }
554 sub selectStackList
555 { my $self = shift; return @{$self->{selectStackList}}; }
556 sub ifStackList
557 { my $self = shift; return @{$self->{ifStackList}}; }
558  
559 sub declareStatement
560 { $_[0]->{declareStatement} }
561 sub terminateStatement
562 { $_[0]->{terminateStatement} }
563 sub callStatement
564 { $_[0]->{callStatement} }
565 sub argListObj
566 { $_[0]->{argListObj} }
567 sub returnVar
568 { $_[0]->{returnVar} }
569 sub returnVarType
570 { $_[0]->{returnVarType} }
571  
572 sub localScalarHash
573 { my $self = shift; return %{$self->{localScalarHash}}; }
574 sub localArrayHash
575 { my $self = shift; return %{$self->{localArrayHash}}; }
576 }
577  
578 { package Language::Axbasic::Subroutine::String;
579  
580 use strict;
581 use warnings;
582 # use diagnostics;
583  
584 use Glib qw(TRUE FALSE);
585  
586 @Language::Axbasic::Subroutine::String::ISA = qw(
587 Language::Axbasic::Subroutine
588 Language::Axbasic::String
589 );
590 }
591  
592 { package Language::Axbasic::Subroutine::Numeric;
593  
594 use strict;
595 use warnings;
596 # use diagnostics;
597  
598 use Glib qw(TRUE FALSE);
599  
600 @Language::Axbasic::Subroutine::Numeric::ISA = qw(
601 Language::Axbasic::Subroutine
602 Language::Axbasic::Numeric
603 );
604 }
605  
606 # Package must return a true value
607 1