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

filter: Statementstmt 0.0% 0 / 936 Branchbran 0.0% 0 / 284 Conditioncond 0.0% 0 / 213 MC/DCmcdc 0.0% 0 / 107 Subroutinesub 0.0% 0 / 162 Podpod 0.0% 0 / 69 totaltotal 0.0% 0 / 1771 CC 143 SCAR 39.0
CC
143
Cov
0%
new
306.0
new
156.0
new
110.0
CRAP 49.4
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::Expression, based on Language::Basic by Amir Karger
16  
17 { package Language::Axbasic::Expression;
18  
19 use strict;
20 use warnings;
21 # use diagnostics;
22  
23 use Glib qw(TRUE FALSE);
24  
25 @Language::Axbasic::Expression::ISA = qw(
26 Language::Axbasic
27 );
28  
29 # Paraphrased from Language::Basic:
30 # Every Axbasic statement is made up of keywords (like 'data', 'print', 'if' etc) and
31 # expressions
32 # Expressions include not just the standard arithmetic and boolean expressions (like 1 + 2), but
33 # also lvalues (scalar variables or arrays), functions and constants
34 # Axbasic expressions are represented by various objects of subclasses of LA::Expression
35 # Most LA::Expressions are in turn made up of other LA::Expressions
36 # For example, a LA::Expression::Arithmetic may be made up of two
37 # LA::Expression::Multiplicatives and a 'plus'
38 # 'Atoms' (indivisible LA::Expressions) include things like LA::Expression::Constants and
39 # LA::Expression:Lvalues (variables)
40 #
41 # The LA::Expression hierarchy
42 #
43 # A bunch of LA::Expression subclasses represent various kinds of BASIC expressions. These
44 # subclasses closely follow the BASIC syntax diagram.
45 # Expressions can be classified in two ways, which are sort of vertical and horizontal. One
46 # classification method is what subexpressions (if any) an expression is made of. For example,
47 # an Arith. Exp. is made up of one or more Mult. Exps. connected by plus or minus signs, while
48 # a Mult. Exp. is made up of one or more Unary Exps. This is a hierarchical (or vertical)
49 # distinction, important for building up the tree of objects that represent a BASIC
50 # expression.
51 #
52 # (Note that not all levels in the hierarchy have to be filled. We don't bother making an
53 # Arith. Exp. which contains just one Mult. Exp. which contains just one Unary Exp.
54 # Instead, we just use the Unary Exp. itself (when it's safe to do so!)
55 #
56 # The second way of classifying expressions is by their return type. A String Exp. is a string
57 # constant, a string variable, a string function, or some other expression whose value when
58 # evaluated will be a string. A Numeric Exp. evaluates to a number, and a Boolean to a True or
59 # False value. This distinction is important for typechecking and finding syntax errors in
60 # BASIC code. (Note that in BASIC -- unlike Perl or C -- you can't "cast" a boolean value
61 # into an integer or string. This actually makes parsing more difficult.)
62 #
63 # Some expressions don't exactly fit any of these distinctions. For example, an ArgList
64 # evaluates to a list of expressions, each of which may be Numeric or Boolean.
65 #
66 # Subclass methods
67 #
68 # Each subclass has (at least) two methods:
69 #
70 # ->new
71 # - Takes a class and a LA::TokenGroup (and possibly some other args). It shifts one or
72 # more tokens from the token group, parsing them, creating a new object of that class,
73 # and setting various IVs in the object, which it returns
74 # - If the tokens don't match the class, ->new returns 'undef'
75 # - If an expression contains just one subexpression often we'll just return the
76 # subexpression. So if an Arith. Exp. contains just one Mult. Exp., we'll just
77 # return the Axbasic:Expression::Multiplicative object and not a
78 # LA::Expression:::Arithmetic object.
79 #
80 # ->evaluate
81 # - Actually calculates the value of the expression. For a string or numeric constant or
82 # variable, that just means taking the stored value of that object. For other
83 # Expressions, you actually need to do some calculations
84  
85 # Sub-classes
86 {
87 package Language::Axbasic::Expression::LogicalOr;
88 package Language::Axbasic::Expression::LogicalAnd;
89 package Language::Axbasic::Expression::Relational;
90  
91 package Language::Axbasic::Expression::Arithmetic;
92 package Language::Axbasic::Expression::Multiplicative;
93 package Language::Axbasic::Expression::Unary;
94  
95 package Language::Axbasic::Expression::Lvalue;
96 package Language::Axbasic::Expression::ArgList;
97 package Language::Axbasic::Expression::Function;
98 package Language::Axbasic::Expression::Subroutine;
99 package Language::Axbasic::Expression::Constant;
100  
101 package Language::Axbasic::Expression::Numeric;
102 package Language::Axbasic::Expression::String;
103 package Language::Axbasic::Expression::Boolean;
104 }
105  
106 ##################
107 # Constructors
108  
109 # sub new {} # Each subclass must have its own ->new
110  
111 sub setReturnType {
112  
113 # Paraphrased from Language::Basic:
114 # Most expressions have a "return type" that's String, Boolean, or Numeric.
115 # (Arglists don't, since they hold a list of expressions.)
116 #
117 # An arithmetic expression is a LA::Expression::Arithmetic::Numeric if it's made up of
118 # LA::Expression::Multiplicative::Numeric expressions, but
119 # LA::Expression::Arithmetic::String if it's got a LA::Unary::String in it. We never mix
120 # expression types (except within Arglists)
121 # This sub therefore blesses an object to its String/Numeric/Boolean subclass depending on
122 # the type of the sub-expression (and returns the newly blessed object.)
123 #
124 # Usually the sub-expression is itself a LA::Expression, but not always. We test for subexps
125 # of, e.g., LA::String rather than LA::Expression::String, because we may be setting
126 # return type based on a LA::Variable::String or LA::Function::String, which aren't
127 # LA::Expressions.
128 #
129 # Expected arguments
130 # $subExp - The subexpression
131 #
132 # Return values
133 # 'undef' on improper arguments
134 # Blessed reference to the newly-blessed object on success
135  
136 my ($self, $subExp, $check) = @_;
137  
138 # Local variables
139 my (
140 $class, $subClass, $type,
141 @typeList,
142 );
143  
144 # Check for improper arguments
145 if (! defined $subExp || defined $check) {
146  
147 return $axmud::CLIENT->writeImproper($self->_objClass . '->setReturnType', @_);
148 }
149  
150 $class = $self->_objClass;
151 if ($class ne ref($self)) {
152  
153 return $self->scriptObj->setDebug(
154 'Error refining class ' . $class . ' (mismatched class)',
155 $self->_objClass . '->setReturnType',
156 );
157 }
158  
159 # Don't re-bless this object if it is already blessed
160 @typeList = qw(String Numeric Boolean);
161 foreach my $word (@typeList) {
162  
163 if ($self->isa('Language::Axbasic::Expression::' . $word)) {
164  
165 return $self;
166 }
167 }
168  
169 # Get the return type
170 OUTER: foreach my $word (@typeList) {
171  
172 # LA::Function::String
173 if ($subExp->isa('Language::Axbasic::' . $word)) {
174  
175 $type = $word;
176 last OUTER;
177 }
178 }
179  
180 if (! defined $type) {
181  
182 return $self->scriptObj->setDebug(
183 'Error refining class ' . $class,
184 $self->_objClass . '->setReturnType',
185 );
186 }
187  
188 # Bless the subclass
189 $subClass = $class . '::' . $type;
190 bless $self, $subClass;
191  
192 return $self;
193 }
194  
195 ##################
196 # Methods
197  
198 # sub evaluate {} # Each subclass must have its own ->evaluate
199  
200 ##################
201 # Accessors - set
202  
203 ##################
204 # Accessors - get
205 }
206  
207 { package Language::Axbasic::Expression::Arithmetic;
208  
209 use strict;
210 use warnings;
211 # use diagnostics;
212  
213 use Glib qw(TRUE FALSE);
214  
215 @Language::Axbasic::Expression::Arithmetic::ISA = qw(
216 Language::Axbasic::Expression
217 );
218  
219 ##################
220 # Constructors
221  
222 sub new {
223  
224 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
225 #
226 # Paraphrased from Language::Basic:
227 # An arithmetic expression is a set of multiplicative expressions connected by plus or minus
228 # signs. (String expressions can only be connected by plus, which is the Axbasic
229 # concatenation operator.)
230 #
231 # In Axbasic, Arithmetic expressions can't contain Boolean expressions. However, parentheses
232 # can confuse things.
233 # LA::Expression::Relational is one of:
234 # (1) LA::Expression::Arithmetic Rel. Op. LA::Expression::Arithmetic
235 # (2) (Logical Or)
236 # It calls LA::Expression::Arithmetic->new with 'maybe_boolean' sometimes, to tell
237 # LA::Expression::Arithemetic->new that if it finds a (parenthesisesed) Boolean
238 # expression, it's just case #2 above. (Otherwise, a Boolean subexpression is an error.)
239 #
240 # Expected arguments
241 # $scriptObj - Blessed reference to the parent LA::Script
242 # $tokenGroupObj - Blessed reference of the line's token group
243 #
244 # Optional arguments
245 # $maybeBoolean - Set to 'maybe_boolean' or 'undef'
246 #
247 # Return values
248 # 'undef' on improper arguments, or for an error
249 # Otherwise returns either the blessed reference to this object, or the blessed reference
250 # to a multiplicative expression object
251  
252 my ($class, $scriptObj, $tokenGroupObj, $maybeBoolean, $check) = @_;
253  
254 # Local variables
255 my (
256 $expression, $token,
257 @expList, @opList,
258 );
259  
260 # Check for improper arguments
261 if (
262 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
263 || (defined $maybeBoolean && $maybeBoolean ne 'maybe_boolean')
264 || defined $check
265 ) {
266 return $axmud::CLIENT->writeImproper($class . '->new', @_);
267 }
268  
269 # Test the expression to see if it evaluates to a simple Boolean value. If $maybeBoolean is
270 # set, return the Boolean value; otherwise Boolean values constitute an error
271 $expression = Language::Axbasic::Expression::Multiplicative->new(
272 $scriptObj,
273 $tokenGroupObj,
274 $maybeBoolean,
275 );
276  
277 if (! defined $expression) {
278  
279 return undef;
280  
281 } elsif ($expression->isa('Language::Axbasic::Expression::Boolean')) {
282  
283 if ($maybeBoolean) {
284  
285 return $expression;
286  
287 } else {
288  
289 return $scriptObj->setError(
290 'expected_non-boolean_expression',
291 $class . '->new',
292 );
293 }
294 }
295  
296 # The do..until loop is necessary in case we have an expression like 1+2+3
297 # It will effectively evaluate the +, - operators left to right
298 push (@expList, $expression);
299  
300 # An arithmetic expression is a set of multiplicative expressions connected by plus or minus
301 # signs. Convert the expression into a series of multiplicative expressions and
302 # corresponding operators (we already have the first multiplicative expression in the
303 # sequence, $expressionressions[0], so the first token to look for is an operator)
304 do {
305  
306 $token = $tokenGroupObj->shiftTokenIfCategory('arithmetic_operator');
307  
308 if (defined $token) {
309  
310 # Push the operator (represented by $token) and the expression that follows it into
311 # the lists @opList and @expList
312 push (@opList, $token->tokenText);
313  
314 $expression = Language::Axbasic::Expression::Multiplicative->new(
315 $scriptObj,
316 $tokenGroupObj,
317 );
318  
319 if (! defined $expression) {
320  
321 return undef;
322  
323 } elsif ($expression->isa('Language::Axbasic::Expression::Boolean')) {
324  
325 return $scriptObj->setError(
326 'expected_non-boolean_expression',
327 $class . '->new',
328 );
329 }
330  
331 push (@expList, $expression);
332 }
333  
334 } until (! defined $token);
335  
336 # If there's only one multiplicative expression, don't bother creating an arithmetic
337 # expression object. Just return the multiplicative expression
338 if (! @opList) {
339  
340 return $expression;
341 }
342  
343 # Otherwise, create an arithmetic expression object
344  
345 # Setup
346 my $self = {
347 _objName => $class, # Name Axbasic objects after their class
348 _objClass => $class,
349 _parentFile => undef, # No parent file object
350 _parentWorld => undef, # No parent file object
351 _privFlag => TRUE, # All IVs are private
352  
353 # Perl object components
354 # ----------------------
355  
356 # The parent LA::Script
357 scriptObj => $scriptObj,
358  
359 # IVs
360 # ----
361  
362 expList => \@expList,
363 opList => \@opList,
364 };
365  
366 bless $self, $class;
367 # Bless to LA::Expression::String or ::Numeric
368 $self->setReturnType($expression);
369  
370 return $self;
371 }
372  
373 ##################
374 # Methods
375  
376 # sub evaluate {} # Each subclass must have its own ->evaluate
377  
378 ##################
379 # Accessors - set
380  
381 ##################
382 # Accessors - get
383  
384 sub scriptObj
385 { $_[0]->{scriptObj} }
386  
387 sub expList
388 { my $self = shift; return @{$self->{expList}}; }
389 sub opList
390 { my $self = shift; return @{$self->{opList}}; }
391 }
392  
393 { package Language::Axbasic::Expression::Arithmetic::String;
394  
395 use strict;
396 use warnings;
397 # use diagnostics;
398  
399 use Glib qw(TRUE FALSE);
400  
401 @Language::Axbasic::Expression::Arithmetic::String::ISA = qw(
402 Language::Axbasic::Expression::Arithmetic
403 Language::Axbasic::Expression::String
404 );
405  
406 ##################
407 # Constructors
408  
409 # sub new {} # Each subclass must have its own ->new
410  
411 ##################
412 # Methods
413  
414 sub evaluate {
415  
416 # Called by LA::Statement::xxx->implement
417 # Sub-class of LA::Expression::Arithmetic whose return type has been set to 'String'
418 #
419 # Expected arguments
420 # (none besides $self)
421 #
422 # Return values
423 # 'undef' on improper arguments, or for an error
424 # Otherwise returns the value of the expression
425  
426 my ($self, $check) = @_;
427  
428 # Local variables
429 my (
430 $obj, $expression, $operator, $nextExpression,
431 @expList, @opList,
432 );
433  
434 # Check for improper arguments
435 if (defined $check) {
436  
437 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
438 }
439  
440 # Import the lists of expressions and operators
441 @expList = $self->expList;
442 # (From Language::Basic: operators ought to be all pluses, since that's all BASIC can do)
443 @opList = $self->opList;
444  
445 # Evaluate the first expression, than add each successive operator-expression pair
446 $obj = shift @expList;
447 $expression = $obj->evaluate();
448 if (! defined $expression) {
449  
450 return undef;
451 }
452  
453 while ($operator = shift @opList) {
454  
455 $obj = shift @expList;
456 $nextExpression = $obj->evaluate();
457 if (! defined $nextExpression) {
458  
459 return undef;
460 }
461  
462 if ($operator eq '&') {
463  
464 $expression .= $nextExpression;
465  
466 } else {
467  
468 return $self->scriptObj->setError(
469 'illegal_operator_in_arithmetic_expression',
470 $self->_objClass . '->evaluate',
471 );
472 }
473 }
474  
475 # Return the value of the whole expression
476 return $expression;
477 }
478  
479 ##################
480 # Accessors - set
481  
482 ##################
483 # Accessors - get
484 }
485  
486 { package Language::Axbasic::Expression::Arithmetic::Numeric;
487  
488 use strict;
489 use warnings;
490 # use diagnostics;
491  
492 use Glib qw(TRUE FALSE);
493  
494 @Language::Axbasic::Expression::Arithmetic::Numeric::ISA = qw(
495 Language::Axbasic::Expression::Arithmetic
496 Language::Axbasic::Expression::Numeric
497 );
498  
499 ##################
500 # Constructors
501  
502 # sub new {} # Each subclass must have its own ->new
503  
504 ##################
505 # Methods
506  
507 sub evaluate {
508  
509 # Called by LA::Statement::xxx->implement
510 # Sub-class of LA::Expression::Arithmetic whose return type has been set to 'Numeric'
511 #
512 # Expected arguments
513 # (none besides $self)
514 #
515 # Return values
516 # 'undef' on improper arguments, or for an error
517 # Otherwise returns the value of the expression
518  
519 my ($self, $check) = @_;
520  
521 # Local variables
522 my (
523 $obj, $expression, $operator, $nextExpression,
524 @expList, @opList,
525 );
526  
527 # Check for improper arguments
528 if (defined $check) {
529  
530 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
531 }
532  
533 # Import the lists of expressions and operators
534 @expList = $self->expList;
535 @opList = $self->opList;
536  
537 # Evaluate the first expression, than add each successive operator-expression pair
538 $obj = shift @expList;
539 $expression = $obj->evaluate();
540 if (! defined $expression) {
541  
542 return undef;
543 }
544  
545 # Need to check that $expression is numeric, or we'll get a funny Perl error when we try to
546 # add/subtract from it
547 # NB We don't check the remaining operands because they're treated as string concatenations
548 # by Axbasic
549 if (! Scalar::Util::looks_like_number($expression)) {
550  
551 return $self->scriptObj->setError(
552 'type_mismatch_error',
553 $self->_objClass . '->evaluate',
554 );
555 }
556  
557 while ($operator = shift @opList) {
558  
559 $obj = shift @expList;
560 $nextExpression = $obj->evaluate;
561 if (! defined $nextExpression || ! Scalar::Util::looks_like_number($nextExpression)) {
562  
563 return undef;
564 }
565  
566 if ($operator eq '+') {
567  
568 $expression = $expression + $nextExpression;
569  
570 } elsif ($operator eq '-') {
571  
572 $expression = $expression - $nextExpression;
573  
574 } else {
575  
576 return $self->scriptObj->setError(
577 'illegal_operator_in_arithmetic_expression',
578 $self->_objClass . '->evaluate',
579 );
580 }
581 }
582  
583 # Return the value of the whole expression
584 return $expression;
585 }
586  
587 ##################
588 # Accessors - set
589  
590 ##################
591 # Accessors - get
592 }
593  
594 { package Language::Axbasic::Expression::Multiplicative;
595  
596 use strict;
597 use warnings;
598 # use diagnostics;
599  
600 use Glib qw(TRUE FALSE);
601  
602 @Language::Axbasic::Expression::Multiplicative::ISA = qw(
603 Language::Axbasic::Expression
604 );
605  
606 ##################
607 # Constructors
608  
609 sub new {
610  
611 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
612 #
613 # Paraphrased from Language::Basic:
614 # An multiplicative expression is a set of unary expressions connected by '*' or '/'
615 #
616 # Expected arguments
617 # $scriptObj - Blessed reference to the parent LA::Script
618 # $tokenGroupObj - Blessed reference of the line's token group
619 #
620 # Optional arguments
621 # $maybeBoolean - Set to 'maybe_boolean' or 'undef'
622 #
623 # Return values
624 # 'undef' on improper arguments, or for an error
625 # Otherwise returns either the blessed reference to this object, or the blessed reference
626 # to a LA::Expression::Boolean object
627  
628 my ($class, $scriptObj, $tokenGroupObj, $maybeBoolean, $check) = @_;
629  
630 # Local variables
631 my (
632 $expression, $token,
633 @expList, @opList,
634 );
635  
636 # Check for improper arguments
637 if (
638 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
639 || (defined $maybeBoolean && $maybeBoolean ne 'maybe_boolean')
640 || defined $check
641 ) {
642 return $axmud::CLIENT->writeImproper($class . '->new', @_);
643 }
644  
645 # Test the expression to see if it evaluates to a simple Boolean value. If $maybeBoolean is
646 # set, return the Boolean value; otherwise Boolean values constitute an error
647 $expression
648 = Language::Axbasic::Expression::Unary->new($scriptObj, $tokenGroupObj, $maybeBoolean);
649  
650 if (! defined $expression) {
651  
652 return undef;
653  
654 } elsif ($expression->isa('Language::Axbasic::Expression::Boolean')) {
655  
656 if ($maybeBoolean) {
657  
658 return $expression;
659  
660 } else {
661  
662 return $scriptObj->setError(
663 'expected_non-boolean_expression',
664 $class . '->new',
665 );
666 }
667 }
668  
669 # The while loop is necessary in case we have an expression like 1 * 2 * 3
670 # It will effectively evaluate the *, / operators left to right
671 push (@expList, $expression);
672  
673 # An arithmetic expression is a set of multiplicative expressions connected by plus or minus
674 # signs. Convert the expression into a series of multiplicative expressions and
675 # corresponding operators (we already have the first multiplicative expression in the
676 # sequence, $expList[0], so the first token to look for is an operator)
677 do {
678  
679 $token = $tokenGroupObj->shiftTokenIfCategory('multiplicative_operator');
680  
681 if (defined $token) {
682  
683 # Push the operator (represented by $token) and the expression that follows it into
684 # the lists @opList and @expList
685 push (@opList, $token->tokenText);
686  
687 $expression = Language::Axbasic::Expression::Unary->new($scriptObj, $tokenGroupObj);
688 if (! defined $expression) {
689  
690 return undef;
691  
692 } elsif ($expression->isa('Language::Axbasic::Expression::Boolean')) {
693  
694 return $scriptObj->setError(
695 'expected_non-boolean_expression',
696 $class . '->new',
697 );
698 }
699  
700 push (@expList, $expression);
701 }
702  
703 } until (! defined $token);
704  
705 # If there's only one unary expression, don't bother creating a multiplicative expression
706 # object. Just return the unary expression. (This will definitely happen in $expression
707 # is a string)
708 if (! @opList) {
709  
710 return $expression;
711 }
712  
713 # Otherwise, create an multiplicative expression object
714  
715 # Setup
716 my $self = {
717 _objName => $class, # Name Axbasic objects after their class
718 _objClass => $class,
719 _parentFile => undef, # No parent file object
720 _parentWorld => undef, # No parent file object
721 _privFlag => TRUE, # All IVs are private
722  
723 # Perl object components
724 # ----------------------
725  
726 # The parent LA::Script
727 scriptObj => $scriptObj,
728  
729 # IVs
730 # ----
731  
732 expList => \@expList,
733 opList => \@opList,
734 };
735  
736 bless $self, $class;
737 # Bless to LA::Expression::String or ::Numeric
738 $self->setReturnType($expression);
739  
740 return $self;
741 }
742  
743 ##################
744 # Methods
745  
746 # sub evaluate {} # Each subclass must have its own ->evaluate
747  
748 ##################
749 # Accessors - set
750  
751 ##################
752 # Accessors - get
753  
754 sub scriptObj
755 { $_[0]->{scriptObj} }
756  
757 sub expList
758 { my $self = shift; return @{$self->{expList}}; }
759 sub opList
760 { my $self = shift; return @{$self->{opList}}; }
761 }
762  
763 { package Language::Axbasic::Expression::Multiplicative::String;
764  
765 use strict;
766 use warnings;
767 # use diagnostics;
768  
769 use Glib qw(TRUE FALSE);
770  
771 @Language::Axbasic::Expression::Multiplicative::String::ISA = qw(
772 Language::Axbasic::Expression::Multiplicative
773 Language::Axbasic::Expression::String
774 );
775  
776 # Paraphrased from Language::Basic:
777 # There cannot possibly a LA::Expression::Multiplicative::String
778 # LA::Expression::Multiplicative->new will just return a LA::Expression::Unary, since there are
779 # no string multiplying operators to find
780 }
781  
782 { package Language::Axbasic::Expression::Multiplicative::Numeric;
783  
784 use strict;
785 use warnings;
786 # use diagnostics;
787  
788 use Glib qw(TRUE FALSE);
789  
790 @Language::Axbasic::Expression::Multiplicative::Numeric::ISA = qw(
791 Language::Axbasic::Expression::Multiplicative
792 Language::Axbasic::Expression::Numeric
793 );
794  
795 ##################
796 # Constructors
797  
798 # sub new {} # Each subclass must have its own ->new
799  
800 ##################
801 # Methods
802  
803 sub evaluate {
804  
805 # Called by LA::Statement::xxx->implement
806 # Sub-class of LA::Expression::Multiplicative whose return type has been set to 'Numeric'
807 #
808 # Expected arguments
809 # (none besides $self)
810 #
811 # Return values
812 # 'undef' on improper arguments, or for an error
813 # Otherwise returns the value of the expression, $exp
814  
815 my ($self, $check) = @_;
816  
817 # Local variables
818 my (
819 $obj, $expression, $operator, $nextExpression,
820 @expList, @opList,
821 );
822  
823 # Check for improper arguments
824 if (defined $check) {
825  
826 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
827 }
828  
829 # Import the lists of expressions and operators
830 @expList = $self->expList;
831 @opList = $self->opList;
832  
833 # Evaluate the first expression, than add each successive operator-expression pair
834 $obj = shift @expList;
835 $expression = $obj->evaluate();
836 if (! defined $expression) {
837  
838 return undef;
839 }
840  
841 # Need to check that $expression is numeric, or we'll get a funny Perl error when we try to
842 # multiply/divide it
843 # N.B. We don't the remaining expressions in @expList because, as described in the
844 # opening comments for LA::Expression::Multiplicative::String, there are no string
845 # multiplications - a LA::Expression::Unary is returned instead
846 if (! Scalar::Util::looks_like_number($expression)) {
847  
848 return $self->scriptObj->setError(
849 'type_mismatch_error',
850 $self->_objClass . '->evaluate',
851 );
852 }
853  
854 while ($operator = shift @opList) {
855  
856 $obj = shift @expList;
857 $nextExpression = $obj->evaluate();
858 if (! defined $nextExpression || ! Scalar::Util::looks_like_number($nextExpression)) {
859  
860 return undef;
861 }
862  
863 if ($operator eq '*') {
864  
865 $expression = $expression * $nextExpression;
866  
867 } elsif ($operator eq '/') {
868  
869 # Need to check for division by zero
870 if ($nextExpression == 0) {
871  
872 return $self->scriptObj->setError(
873 'division_by_zero',
874 $self->_objClass . '->evaluate',
875 );
876  
877 } else {
878  
879 $expression = $expression / $nextExpression;
880 }
881  
882 } elsif ($operator eq '^') {
883  
884 $expression = $expression ** $nextExpression;
885  
886 } else {
887  
888 return $self->scriptObj->setError(
889 'illegal_operator_in_multiplicative_expression',
890 $self->_objClass . '->evaluate',
891 );
892 }
893 }
894  
895 # Return the value of the whole expression
896 return $expression;
897 }
898  
899 ##################
900 # Accessors - set
901  
902 ##################
903 # Accessors - get
904 }
905  
906 { package Language::Axbasic::Expression::Unary;
907  
908 use strict;
909 use warnings;
910 # use diagnostics;
911  
912 use Glib qw(TRUE FALSE);
913  
914 @Language::Axbasic::Expression::Unary::ISA = qw(
915 Language::Axbasic::Expression
916 );
917  
918 ##################
919 # Constructors
920  
921 sub new {
922  
923 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
924 #
925 # Paraphrased from Language::Basic:
926 # A unary expression is a variable, a function, a string or numeric constant, or an
927 # arithmetic expression in parentheses, potentially with a unary minus sign
928 # If we're inside a relational expression, then a parenthetical expression may be either
929 # Boolean or non-Boolean. Otherwise, it must be non-Boolean
930 #
931 # Expected arguments
932 # $scriptObj - Blessed reference to the parent LA::Script
933 # $tokenGroupObj - Blessed reference of the line's token group
934 #
935 # Optional arguments
936 # $maybeBoolean - Set to 'maybe_boolean' or 'undef'
937 #
938 # Return values
939 # 'undef' on improper arguments, or for an error
940 # Otherwise returns either the blessed reference to this object, or the blessed reference
941 # to a subclass object
942  
943 my ($class, $scriptObj, $tokenGroupObj, $maybeBoolean, $check) = @_;
944  
945 # Local variables
946 my ($obj, $unary, $token);
947  
948 # Check for improper arguments
949 if (
950 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
951 || (defined $maybeBoolean && $maybeBoolean ne 'maybe_boolean')
952 || defined $check
953 ) {
954 return $axmud::CLIENT->writeImproper($class . '->new', @_);
955 }
956  
957 # Setup
958 my $self = {
959 _objName => $class, # Name Axbasic objects after their class
960 _objClass => $class,
961 _parentFile => undef, # No parent file object
962 _parentWorld => undef, # No parent file object
963 _privFlag => TRUE, # All IVs are private
964  
965 # Perl object components
966 # ----------------------
967  
968 # The parent LA::Script
969 scriptObj => $scriptObj,
970  
971 # IVs
972 # ----
973  
974 # The expression, stored by $self->new and retrieved by $self->evaluate
975 expression => undef,
976 # Flag set to TRUE if this expression contains a parenthesised expression (set to
977 # FALSE if not)
978 nestedFlag => FALSE,
979 # Set to TRUE if this expression has a unary minus in front of it (set to FALSE if not)
980 unaryMinusFlag => FALSE,
981 };
982  
983 # Is there a unary minus in the expression?
984 if (defined $tokenGroupObj->shiftMatchingToken('-')) {
985  
986 $self->{'unaryMinusFlag'} = TRUE;
987 }
988  
989 # If a parenthesis is found, (recursively) parse what's inside
990 # If $maybeBoolean is set, then a parenthesised expression might be a Boolean expression, so
991 # call LA::Expression::LogicalOr (highest level Boolean expression)
992 # However, in most cases, it'll be a non-Boolean, so call with 'maybe_arithmetic' flag,
993 # which tells LA::Expression::LogicalOr not to be surprised if it finds an arithmetic
994 # expression
995 if (defined $tokenGroupObj->shiftTokenIfCategory('left_paren')) {
996  
997 $self->{'nestedFlag'} = TRUE;
998  
999 # Recursively parse what's inside the parentheses
1000 $obj = Language::Axbasic::Expression::LogicalOr->new(
1001 $scriptObj,
1002 $tokenGroupObj,
1003 'maybe_arithmetic',
1004 );
1005  
1006 if (! defined $obj) {
1007  
1008 return undef;
1009 }
1010  
1011 # Look for the corresponding right parenthesis
1012 if (! defined $tokenGroupObj->shiftTokenIfCategory('right_paren')) {
1013  
1014 return $scriptObj->setError(
1015 'mismatched_parentheses_error',
1016 $class . '->new',
1017 );
1018 }
1019  
1020 # If we found a Boolean, make sure we're allowed to have one
1021 if ($obj->isa('Language::Axbasic::Expression::Boolean') && ! $maybeBoolean) {
1022  
1023 return $scriptObj->setError(
1024 'expected_non-boolean_expression',
1025 $class . '->new',
1026 );
1027  
1028 } else {
1029  
1030 $unary = $obj;
1031 }
1032  
1033 # However, if it's a string or numeric function...
1034 } elsif (
1035 defined (
1036 $obj = Language::Axbasic::Expression::Function->new($scriptObj, $tokenGroupObj)
1037 )
1038 ) {
1039 $unary = $obj;
1040  
1041 # ... or a string or numeric variable...
1042 } elsif (
1043 defined (
1044 $obj = Language::Axbasic::Expression::Lvalue->new($scriptObj, $tokenGroupObj)
1045 )
1046 ) {
1047 $unary = $obj;
1048  
1049 # ... or a string or numeric constant...
1050 } elsif (
1051 defined (
1052 $obj = Language::Axbasic::Expression::Constant->new($scriptObj, $tokenGroupObj)
1053 )
1054 ) {
1055 $unary = $obj;
1056  
1057 # ...or if it's something else, that's an error
1058 } else {
1059  
1060 $token = $tokenGroupObj->lookAhead();
1061 if (defined $token) {
1062  
1063 return $scriptObj->setError(
1064 'missing_or_illegal_expression',
1065 $class . '->new',
1066 );
1067  
1068 } else {
1069  
1070 return $scriptObj->setError(
1071 'expected_unary_expression',
1072 $class . '->new',
1073 );
1074 }
1075 }
1076  
1077 # If it's just an Lvalue that's not nested or minused, we can just return the Lvalue object,
1078 # rather than making a unary out if it
1079 if (! $self->{nestedFlag} && ! $self->{unaryMinusFlag}) {
1080  
1081 return $unary;
1082 }
1083  
1084 # Otherwise, save the expression as an IV, and bless the object to a
1085 # LA::Expression::Unary::String, ::Numeric or ::Boolean
1086 $self->{'expression'} = $unary;
1087 bless $self, $class;
1088  
1089 # Bless to LA::Expression::String or ::Numeric
1090 $self->setReturnType($self->expression);
1091  
1092 return $self;
1093 }
1094  
1095 ##################
1096 # Methods
1097  
1098 sub evaluate {
1099  
1100 # Called by LA::Statement::xxx->implement
1101 #
1102 # Expected arguments
1103 # (none besides $self)
1104 #
1105 # Return values
1106 # 'undef' on improper arguments
1107 # Otherwise returns the value of the expression, $value
1108  
1109 my ($self, $check) = @_;
1110  
1111 # Local variables
1112 my $value;
1113  
1114 # Check for improper arguments
1115 if (defined $check) {
1116  
1117 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
1118 }
1119  
1120 # Import the saved expression and evaluate it
1121 $value = $self->expression->evaluate();
1122  
1123 # Apply the unary minus, if set
1124 if ($self->unaryMinusFlag) {
1125  
1126 $value = -$value;
1127 }
1128  
1129 return $value;
1130 }
1131  
1132 ##################
1133 # Accessors - set
1134  
1135 ##################
1136 # Accessors - get
1137  
1138 sub scriptObj
1139 { $_[0]->{scriptObj} }
1140  
1141 sub expression
1142 { $_[0]->{expression} }
1143 sub nestedFlag
1144 { $_[0]->{nestedFlag} }
1145 sub unaryMinusFlag
1146 { $_[0]->{unaryMinusFlag} }
1147 }
1148  
1149 { package Language::Axbasic::Expression::Unary::Numeric;
1150  
1151 use strict;
1152 use warnings;
1153 # use diagnostics;
1154  
1155 use Glib qw(TRUE FALSE);
1156  
1157 @Language::Axbasic::Expression::Unary::Numeric::ISA = qw(
1158 Language::Axbasic::Expression::Unary
1159 Language::Axbasic::Expression::Numeric
1160 );
1161  
1162 # $self->evaluate is inherited from LA::Expression::Unary, since it's the same for ::Numeric,
1163 # ::String and ::Boolean
1164 }
1165  
1166 { package Language::Axbasic::Expression::Unary::String;
1167  
1168 use strict;
1169 use warnings;
1170 # use diagnostics;
1171  
1172 use Glib qw(TRUE FALSE);
1173  
1174 @Language::Axbasic::Expression::Unary::String::ISA = qw(
1175 Language::Axbasic::Expression::Unary
1176 Language::Axbasic::Expression::String
1177 );
1178  
1179 # $self->evaluate is inherited from LA::Expression::Unary, since it's the same for ::Numeric,
1180 # ::String and ::Boolean
1181 }
1182  
1183 { package Language::Axbasic::Expression::Unary::Boolean;
1184  
1185 use strict;
1186 use warnings;
1187 # use diagnostics;
1188  
1189 use Glib qw(TRUE FALSE);
1190  
1191 @Language::Axbasic::Expression::Unary::Boolean::ISA = qw(
1192 Language::Axbasic::Expression::Unary
1193 Language::Axbasic::Expression::Boolean
1194 );
1195  
1196 # $self->evaluate is inherited from LA::Expression::Unary, since it's the same for ::Numeric,
1197 # ::String and ::Boolean
1198 }
1199  
1200 { package Language::Axbasic::Expression::Constant;
1201  
1202 use strict;
1203 use warnings;
1204 # use diagnostics;
1205  
1206 use Glib qw(TRUE FALSE);
1207  
1208 @Language::Axbasic::Expression::Constant::ISA = qw(
1209 Language::Axbasic::Expression
1210 );
1211  
1212 ##################
1213 # Constructors
1214  
1215 sub new {
1216  
1217 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
1218 #
1219 # A string or a numeric constant like "17" or 32.4
1220 #
1221 # Expected arguments
1222 # $scriptObj - Blessed reference to the parent LA::Script
1223 # $tokenGroupObj - Blessed reference of the line's token group
1224 #
1225 # Return values
1226 # 'undef' on improper arguments
1227 # Otherwise, returns either a LA::Expression::Constant:: subclass, or 'undef'
1228  
1229 my ($class, $scriptObj, $tokenGroupObj, $check) = @_;
1230  
1231 # Local variables
1232 my $obj;
1233  
1234 # Check for improper arguments
1235 if (
1236 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj
1237 || defined $check
1238 ) {
1239 return $axmud::CLIENT->writeImproper($class . '->new', @_);
1240 }
1241  
1242 # Return a subclass, or return 'undef'
1243 $obj = Language::Axbasic::Expression::Constant::Numeric->new($scriptObj, $tokenGroupObj);
1244 if ($obj) {
1245  
1246 return $obj;
1247  
1248 } else {
1249  
1250 $obj = Language::Axbasic::Expression::Constant::String->new($scriptObj, $tokenGroupObj);
1251 if ($obj) {
1252 return $obj;
1253 } else {
1254 return undef;
1255 }
1256 }
1257 }
1258  
1259 ##################
1260 # Methods
1261  
1262 sub evaluate {
1263  
1264 # Called by LA::Statement::xxx->implement
1265 # NB Nothing appears to set $self->expression
1266 #
1267 # Expected arguments
1268 # (none besides $self)
1269 #
1270 # Return values
1271 # 'undef' on improper arguments
1272 # Otherwise returns the value of $self->expression
1273  
1274 my ($self, $check) = @_;
1275  
1276 # Local variables
1277 my $value;
1278  
1279 # Check for improper arguments
1280 if (defined $check) {
1281  
1282 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
1283 }
1284  
1285 # Evaluate the constant, and return it
1286 return $self->expression->evaluate;
1287 }
1288  
1289 ##################
1290 # Accessors - set
1291  
1292 ##################
1293 # Accessors - get
1294 }
1295  
1296 { package Language::Axbasic::Expression::Constant::Numeric;
1297  
1298 use strict;
1299 use warnings;
1300 # use diagnostics;
1301  
1302 use Glib qw(TRUE FALSE);
1303  
1304 @Language::Axbasic::Expression::Constant::Numeric::ISA = qw(
1305 Language::Axbasic::Expression::Constant
1306 Language::Axbasic::Expression::Numeric
1307 );
1308  
1309 ##################
1310 # Constructors
1311  
1312 sub new {
1313  
1314 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
1315 #
1316 # Expected arguments
1317 # $scriptObj - Blessed reference to the parent LA::Script
1318 # $tokenGroupObj - Blessed reference of the line's token group
1319 #
1320 # Return values
1321 # 'undef' on improper arguments
1322 # Otherwise, returns either a LA::Expression::Constant::Numeric object, or 'undef'
1323  
1324 my ($class, $scriptObj, $tokenGroupObj, $check) = @_;
1325  
1326 # Local variables
1327 my $token;
1328  
1329 # Check for improper arguments
1330 if (
1331 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj
1332 || defined $check
1333 ) {
1334 return $axmud::CLIENT->writeImproper($class . '->new', @_);
1335 }
1336  
1337 $token = $tokenGroupObj->shiftTokenIfCategory('numeric_constant');
1338 if (! defined $token) {
1339  
1340 return undef;
1341 }
1342  
1343 # Setup
1344 my $self = {
1345 _objName => $class, # Name Axbasic objects after their class
1346 _objClass => $class,
1347 _parentFile => undef, # No parent file object
1348 _parentWorld => undef, # No parent file object
1349 _privFlag => TRUE, # All IVs are private
1350  
1351 # Perl object components
1352 # ----------------------
1353  
1354 # The parent LA::Script
1355 scriptObj => $scriptObj,
1356  
1357 # IVs
1358 # ----
1359  
1360 # The expression, retrieved by LA::Expression::Constant
1361 expression => undef,
1362 # The value of the expression, stored by $self->new and retrieved by $self->evaluate
1363 value => $token->tokenText + 0,
1364 };
1365  
1366 bless $self, $class;
1367 return $self;
1368 }
1369  
1370 ##################
1371 # Methods
1372  
1373 sub evaluate {
1374  
1375 # Called by LA::Statement::xxx->implement
1376 #
1377 # Expected arguments
1378 # (none besides $self)
1379 #
1380 # Return values
1381 # 'undef' on improper arguments
1382 # Otherwise returns $self->value
1383  
1384 my ($self, $check) = @_;
1385  
1386 # Check for improper arguments
1387 if (defined $check) {
1388  
1389 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
1390 }
1391  
1392 # Return the stored value
1393 return $self->value;
1394 }
1395  
1396 ##################
1397 # Accessors - set
1398  
1399 ##################
1400 # Accessors - get
1401  
1402 sub scriptObj
1403 { $_[0]->{scriptObj} }
1404  
1405 sub expression
1406 { $_[0]->{expression} }
1407 sub value
1408 { $_[0]->{value} }
1409 }
1410  
1411 { package Language::Axbasic::Expression::Constant::String;
1412  
1413 use strict;
1414 use warnings;
1415 # use diagnostics;
1416  
1417 use Glib qw(TRUE FALSE);
1418  
1419 @Language::Axbasic::Expression::Constant::String::ISA = qw(
1420 Language::Axbasic::Expression::Constant
1421 Language::Axbasic::Expression::String
1422 );
1423  
1424 ##################
1425 # Constructors
1426  
1427 sub new {
1428  
1429 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
1430 #
1431 # Expected arguments
1432 # $scriptObj - Blessed reference to the parent LA::Script
1433 # $tokenGroupObj - Blessed reference of the line's token group
1434 #
1435 # Return values
1436 # 'undef' on improper arguments
1437 # Otherwise, returns either a LA::Expression::Constant::String object, or 'undef'
1438  
1439 my ($class, $scriptObj, $tokenGroupObj, $check) = @_;
1440  
1441 # Local variables
1442 my ($token, $tokenText);
1443  
1444 # Check for improper arguments
1445 if (
1446 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj || defined $check
1447 ) {
1448 return $axmud::CLIENT->writeImproper($class . '->new', @_);
1449 }
1450  
1451 $token = $tokenGroupObj->shiftTokenIfCategory('string_constant');
1452 if (! defined $token) {
1453  
1454 return undef;
1455  
1456 } else {
1457  
1458 $tokenText = $token->tokenText;
1459 # Remove the double quotes "..." from the token, to get the actual string constant
1460 # itself
1461 $tokenText =~ s/^"(.*?)"/$1/;
1462 }
1463  
1464 # Setup
1465 my $self = {
1466 _objName => $class, # Name Axbasic objects after their class
1467 _objClass => $class,
1468 _parentFile => undef, # No parent file object
1469 _parentWorld => undef, # No parent file object
1470 _privFlag => TRUE, # All IVs are private
1471  
1472 # Perl object components
1473 # ----------------------
1474  
1475 # The parent LA::Script
1476 scriptObj => $scriptObj,
1477  
1478 # IVs
1479 # ----
1480  
1481 # The expression, retrieved by LA::Expression::Constant
1482 expression => undef,
1483 # The value of the expression, stored by $self->new and retrieved by $self->evaluate
1484 value => $tokenText,
1485 };
1486  
1487 bless $self, $class;
1488 return $self;
1489 }
1490  
1491 ##################
1492 # Methods
1493  
1494 sub evaluate {
1495  
1496 # Called by LA::Statement::xxx->implement
1497 #
1498 # Expected arguments
1499 # (none besides $self)
1500 #
1501 # Return values
1502 # 'undef' on improper arguments
1503 # Otherwise returns $self->value
1504  
1505 my ($self, $check) = @_;
1506  
1507 # Check for improper arguments
1508 if (defined $check) {
1509  
1510 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
1511 }
1512  
1513 # Return the stored value
1514 return $self->value;
1515 }
1516  
1517 ##################
1518 # Accessors - set
1519  
1520 ##################
1521 # Accessors - get
1522  
1523 sub scriptObj
1524 { $_[0]->{scriptObj} }
1525  
1526 sub expression
1527 { $_[0]->{expression} }
1528 sub value
1529 { $_[0]->{value} }
1530 }
1531  
1532 { package Language::Axbasic::Expression::Lvalue;
1533  
1534 use strict;
1535 use warnings;
1536 # use diagnostics;
1537  
1538 use Glib qw(TRUE FALSE);
1539  
1540 @Language::Axbasic::Expression::Lvalue::ISA = qw(
1541 Language::Axbasic::Expression
1542 );
1543  
1544 ##################
1545 # Constructors
1546  
1547 sub new {
1548  
1549 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
1550 #
1551 # Paraphrased from Language::Basic:
1552 # A settable expression: a variable, X, or one cell in an array, A(17, Q)
1553 # The ->variable method returns the actual LB::Variable::Scalar object referenced by this
1554 # Lvalue
1555 #
1556 # Expected arguments
1557 # $scriptObj - Blessed reference to the parent LA::Script
1558 # $tokenGroupObj - Blessed reference of the line's token group
1559 #
1560 # Optional arguments
1561 # $specialFlag - TRUE when called by a DIM or REDIM statement, in which case we attempt
1562 # to extract an LA::Expression::SpecialArgList after the identifier. FALSE (or
1563 # 'undef') when called by anything else, in which case we attempt to extract a normal
1564 # LA::Expression::SpecialArgList after the identifier
1565 #
1566 # Return values
1567 # 'undef' on improper arguments, or for an error
1568 # Otherwise returns either a LA::Expression::Lvalue:: subclass, or 'undef'
1569  
1570 my ($class, $scriptObj, $tokenGroupObj, $specialFlag, $check) = @_;
1571  
1572 # Local variables
1573 my ($token, $varName, $argListObj, $varObj);
1574  
1575 # Check for improper arguments
1576 if (
1577 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj
1578 || defined $check
1579 ) {
1580 return $axmud::CLIENT->writeImproper($class . '->new', @_);
1581 }
1582  
1583 # Setup
1584 my $self = {
1585 _objName => $class, # Name Axbasic objects after their class
1586 _objClass => $class,
1587 _parentFile => undef, # No parent file object
1588 _parentWorld => undef, # No parent file object
1589 _privFlag => TRUE, # All IVs are private
1590  
1591 # Perl object components
1592 # ----------------------
1593  
1594 # The parent LA::Script
1595 scriptObj => $scriptObj,
1596  
1597 # IVs
1598 # ----
1599  
1600 # The variable (identifier) name (e.g. A, a$, array)
1601 varName => undef,
1602 # For an identifier that refers to a cell in an array (e.g. A(5,6) ), blessed reference
1603 # to the LA::Expression::Arglist object (representing the (5,6) )
1604 argListObj => undef,
1605 # Blessed reference of the corresponding LA::Variable::Scalar::xxx or
1606 # LA::Variable::Array::xxx object
1607 varObj => undef,
1608 };
1609  
1610 # Extract a token comprising a whole variable identifier (e.g. A, a$, array)
1611 $token = $tokenGroupObj->shiftTokenIfCategory('identifier');
1612 if (! defined $token) {
1613  
1614 # Not an identifier
1615 return undef;
1616 }
1617  
1618 # Import the variable name from the token (ie A, a$, array)
1619 $varName = $token->tokenText;
1620 if ($scriptObj->ivExists('funcArgHash', $varName)) {
1621  
1622 # Using names of intrinsic functions as variables is not allowed (using names of
1623 # defined functions is allowed, but the variable's value can never be retrieved)
1624 return undef;
1625 }
1626  
1627 # Test whether the variable refers to a cell in an array (e.g. A(5,6) ) by trying to extract
1628 # an argument list
1629 if (! $specialFlag) {
1630  
1631 $argListObj = Language::Axbasic::Expression::ArgList->new(
1632 $scriptObj,
1633 $tokenGroupObj,
1634 );
1635  
1636 } else{
1637  
1638 $argListObj = Language::Axbasic::Expression::SpecialArgList->new(
1639 $scriptObj,
1640 $tokenGroupObj,
1641 );
1642 }
1643  
1644 # Lookup the variable name in variable storage (and create it, if it doesn't exist)
1645 $varObj = Language::Axbasic::Variable->lookup($scriptObj, $varName, $argListObj);
1646 if (! defined $varObj) {
1647  
1648 return undef;
1649  
1650 } else {
1651  
1652 # Store these values so that $self->evaluate can retrieve them
1653 $self->{'varName'} = $varName;
1654 $self->{'argListObj'} = $argListObj;
1655 $self->{'varObj'} = $varObj;
1656 }
1657  
1658 bless $self, $class;
1659 # Is this a string or numeric lvalue?
1660 $self->setReturnType($varObj);
1661  
1662 return $self;
1663 }
1664  
1665 ##################
1666 # Methods
1667  
1668 sub evaluate {
1669  
1670 # Called by LA::Statement::xxx->implement
1671 #
1672 # Expected arguments
1673 # (none besides $self)
1674 #
1675 # Return values
1676 # 'undef' on improper arguments, or if the cell doesn't seem to exist
1677 # Otherwise returns the value of the expression, $self->variable->value
1678  
1679 my ($self, $check) = @_;
1680  
1681 # Local variables
1682 my $var;
1683  
1684 # Check for improper arguments
1685 if (defined $check) {
1686  
1687 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
1688 }
1689  
1690 # Check the specified cell actually exists, before asking it for the variable it holds
1691 $var = $self->variable();
1692 if (! defined $var) {
1693  
1694 return undef;
1695  
1696 } else {
1697  
1698 # This automatically gets the correct cell, if necessary
1699 return $var->value;
1700 }
1701 }
1702  
1703 sub variable {
1704  
1705 # Called by various statements which need to look up a variable stored in a particular cell
1706 # of an array
1707 # Returns a blessed reference to a variable object. If it's a scalar, returns that object;
1708 # if it's an array, returns the object specified by the argument list
1709 # (e.g. the value of array(5,6) )
1710 # Note that this function always returns a LA::Variable::Scalar object. If the variable in
1711 # this expression is an array, it returns one cell from the array
1712 #
1713 # Expected arguments
1714 # (none besides $self)
1715 #
1716 # Return values
1717 # 'undef' on improper arguments
1718 # Otherwise returns the blessed reference of the variable object
1719  
1720 my ($self, $check) = @_;
1721  
1722 # Local variables
1723 my $varObj;
1724  
1725 # Check for improper arguments
1726 if (defined $check) {
1727  
1728 return $axmud::CLIENT->writeImproper($self->_objClass . '->variable', @_);
1729 }
1730  
1731 # If the variable has an argument list ($self->argListObj), evaluate each Arithemetic
1732 # expression in the argument list, and get the specified cell from the array
1733 if (defined $self->argListObj) {
1734  
1735 $varObj = $self->varObj->getCell($self->argListObj->evaluate());
1736  
1737 } else {
1738  
1739 $varObj = $self->varObj;
1740 }
1741  
1742 return $varObj;
1743 }
1744  
1745 ##################
1746 # Accessors - set
1747  
1748 ##################
1749 # Accessors - get
1750  
1751 sub scriptObj
1752 { $_[0]->{scriptObj} }
1753  
1754 sub varName
1755 { $_[0]->{varName} }
1756 sub argListObj
1757 { $_[0]->{argListObj} }
1758 sub varObj
1759 { $_[0]->{varObj} }
1760 }
1761  
1762 { package Language::Axbasic::Expression::Lvalue::Numeric;
1763  
1764 use strict;
1765 use warnings;
1766 # use diagnostics;
1767  
1768 use Glib qw(TRUE FALSE);
1769  
1770 @Language::Axbasic::Expression::Lvalue::Numeric::ISA = qw(
1771 Language::Axbasic::Expression::Lvalue
1772 Language::Axbasic::Expression::Numeric
1773 );
1774 }
1775  
1776 { package Language::Axbasic::Expression::Lvalue::String;
1777  
1778 use strict;
1779 use warnings;
1780 # use diagnostics;
1781  
1782 use Glib qw(TRUE FALSE);
1783  
1784 @Language::Axbasic::Expression::Lvalue::String::ISA = qw(
1785 Language::Axbasic::Expression::Lvalue
1786 Language::Axbasic::Expression::String
1787 );
1788 }
1789  
1790 { package Language::Axbasic::Expression::Subroutine;
1791  
1792 use strict;
1793 use warnings;
1794 # use diagnostics;
1795  
1796 use Glib qw(TRUE FALSE);
1797  
1798 @Language::Axbasic::Expression::Subroutine::ISA = qw(
1799 Language::Axbasic::Expression
1800 );
1801  
1802 # Sub-classes
1803 {
1804 package Language::Axbasic::Expression::Subroutine::Numeric;
1805 package Language::Axbasic::Expression::Subroutine::String;
1806 }
1807  
1808 ##################
1809 # Constructors
1810  
1811 sub new {
1812  
1813 # Called by LA::Statement::sub->parse
1814 #
1815 # Expected arguments
1816 # $scriptObj - Blessed reference to the parent LA::Script
1817 # $tokenGroupObj - Blessed reference of string being parsed
1818 #
1819 # Return values
1820 # 'undef' on improper arguments, or if the first token in $tokenGroupObj isn't a true
1821 # subroutine name (could be an lvalue)
1822 # Blessed reference to the newly-created object on success
1823  
1824 my ($class, $scriptObj, $tokenGroupObj, $check) = @_;
1825  
1826 # Local variables
1827 my ($token, $tokenText, $subObj, $argListObj);
1828  
1829 # Check for improper arguments
1830 if (
1831 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj || defined $check
1832 ) {
1833 return $axmud::CLIENT->writeImproper($class . '->new', @_);
1834 }
1835  
1836 # Setup
1837 my $self = {
1838 _objName => $class, # Name Axbasic objects after their class
1839 _objClass => $class,
1840 _parentFile => undef, # No parent file object
1841 _parentWorld => undef, # No parent file object
1842 _privFlag => TRUE, # All IVs are private
1843  
1844 # Perl object components
1845 # ----------------------
1846  
1847 # The parent LA::Script
1848 scriptObj => $scriptObj,
1849  
1850 # IVs
1851 # ----
1852  
1853 # The LA::Subroutine object handling this function
1854 subObj => undef,
1855 # Reference to the list of arguments
1856 argListObj => undef,
1857 };
1858  
1859 # Don't extract the first token in the token group if it's not a true subroutine name (could
1860 # be an lvalue)
1861 $token = $tokenGroupObj->lookAhead();
1862 if (! defined $token || $token->category ne 'identifier') {
1863  
1864 return undef;
1865  
1866 } else {
1867  
1868 $tokenText = $token->tokenText;
1869 }
1870  
1871 # Compare the proposed subroutine name, $tokenText, with the list of existing keywords and
1872 # subroutine names, and also filter out any invalid names
1873 if (
1874 defined $scriptObj->ivFind('keywordList', $tokenText)
1875 || ! ($tokenText =~ m/^[A-Z][A-Z0-9_]*$/i)
1876 ) {
1877 return $scriptObj->setError(
1878 'illegal_subroutine_name',
1879 $class . '->new',
1880 );
1881  
1882 } elsif ($scriptObj->ivExists('subNameHash', $tokenText)) {
1883  
1884 return $scriptObj->setError(
1885 'redefined_subroutine_error',
1886 $class . '->new',
1887 );
1888 }
1889  
1890 # Create a LA::Subroutine for this subroutine, which adds a new key-value pair to
1891 # $scriptObj->subNameHash
1892 $subObj = Language::Axbasic::Subroutine->new(
1893 $scriptObj,
1894 $tokenText, # Subroutine name
1895 );
1896  
1897 $self->{'subObj'} = $subObj;
1898  
1899 # Now we know that the first token in the token group is a subroutine, extract it
1900 $tokenGroupObj->shiftToken();
1901  
1902 # Extract the argument list. The TRUE argument means that an empty token group is treated
1903 # the same an empty arglist, in this case (e.g. 'SUB hello ()' and 'SUB hello' are both
1904 # acceptable)
1905 $argListObj
1906 = Language::Axbasic::Expression::ArgList->new($scriptObj, $tokenGroupObj, TRUE);
1907 if (! defined $argListObj) {
1908  
1909 return $scriptObj->setError(
1910 'missing_or_invalid_argument_list',
1911 $class . '->new',
1912 );
1913 }
1914  
1915 # Declare the number and type of arguments in the subroutine...
1916 $subObj->set_argListObj($argListObj);
1917 # ...and save a local copy
1918 $self->{'argListObj'} = $argListObj;
1919  
1920 # Bless the object into existence
1921 bless $self, $class;
1922 # Is it a String or Numeric Subroutine?
1923 $self->setReturnType($subObj);
1924  
1925 return $self;
1926 }
1927  
1928 ##################
1929 # Methods
1930  
1931 # sub evaluate {} # Each subclass must have its own ->evaluate
1932  
1933 ##################
1934 # Accessors - set
1935  
1936 ##################
1937 # Accessors - get
1938  
1939 sub scriptObj
1940 { $_[0]->{scriptObj} }
1941  
1942 sub subObj
1943 { $_[0]->{subObj} }
1944 sub argListObj
1945 { $_[0]->{argListObj} }
1946 }
1947  
1948 { package Language::Axbasic::Expression::Subroutine::Numeric;
1949  
1950 use strict;
1951 use warnings;
1952 # use diagnostics;
1953  
1954 use Glib qw(TRUE FALSE);
1955  
1956 # Set ISA for 'return type' classes
1957 @Language::Axbasic::Expression::Subroutine::Numeric::ISA = qw(
1958 Language::Axbasic::Expression::Subroutine
1959 Language::Axbasic::Expression::Numeric
1960 );
1961 }
1962  
1963 { package Language::Axbasic::Expression::Subroutine::String;
1964  
1965 use strict;
1966 use warnings;
1967 # use diagnostics;
1968  
1969 use Glib qw(TRUE FALSE);
1970  
1971 # Set ISA for 'return type' classes
1972 @Language::Axbasic::Expression::Subroutine::String::ISA = qw(
1973 Language::Axbasic::Expression::Subroutine
1974 Language::Axbasic::Expression::String
1975 );
1976 }
1977  
1978 { package Language::Axbasic::Expression::Function;
1979  
1980 use strict;
1981 use warnings;
1982 # use diagnostics;
1983  
1984 use Glib qw(TRUE FALSE);
1985  
1986 @Language::Axbasic::Expression::Function::ISA = qw(
1987 Language::Axbasic::Expression
1988 );
1989  
1990 # Sub-classes
1991 {
1992 package Language::Axbasic::Expression::Function::Numeric;
1993 package Language::Axbasic::Expression::Function::String;
1994 }
1995  
1996 ##################
1997 # Constructors
1998  
1999 sub new {
2000  
2001 # Called by LA::Statement::def->parse
2002 #
2003 # Expected arguments
2004 # $scriptObj - Blessed reference to the parent LA::Script
2005 # $tokenGroupObj - Blessed reference of string being parsed
2006 #
2007 # Optional arguments
2008 # $defining - If defined (actually set to the string 'defining_flag') we're in a
2009 # DEF statement, so if the function doesn't exist, we should create it
2010 # rather than returning 'undef'. Otherwise set to 'undef'
2011 #
2012 # Return values
2013 # 'undef' on improper arguments, or if the first token in $tokenGroupObj isn't a true
2014 # function name (could be an lvalue)
2015 # Blessed reference to the newly-created object on success
2016  
2017 my ($class, $scriptObj, $tokenGroupObj, $defining, $check) = @_;
2018  
2019 # Local variables
2020 my (
2021 $token, $tokenText, $funcObj, $argListObj,
2022 @args,
2023 );
2024  
2025 # Check for improper arguments
2026 if (
2027 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj
2028 || (defined $defining && $defining ne 'defining_flag')
2029 || defined $check
2030 ) {
2031 return $axmud::CLIENT->writeImproper($class . '->new', @_);
2032 }
2033  
2034 # Setup
2035 my $self = {
2036 _objName => $class, # Name Axbasic objects after their class
2037 _objClass => $class,
2038 _parentFile => undef, # No parent file object
2039 _parentWorld => undef, # No parent file object
2040 _privFlag => TRUE, # All IVs are private
2041  
2042 # Perl object components
2043 # ----------------------
2044  
2045 # The parent LA::Script
2046 scriptObj => $scriptObj,
2047  
2048 # IVs
2049 # ----
2050  
2051 # The LA::Function object handling this function
2052 funcObj => undef,
2053 # Reference to the list of arguments
2054 argListObj => undef,
2055 };
2056  
2057 # Don't extract the first token in the token group if it's not a true function name (could
2058 # be an lvalue)
2059 # NB To preserve backwards compatibility with True BASIC, 'angle' can be either a keyword
2060 # or a built-in function. This is true for all 'weak' keywords, but 'angle' is the only
2061 # built-in function that is also a keyword
2062 $token = $tokenGroupObj->lookAhead();
2063 if (! defined $token) {
2064  
2065 return undef;
2066  
2067 } elsif (
2068 $token->category eq 'keyword'
2069 && $scriptObj->ivExists('weakKeywordHash', $token->tokenText)
2070 ) {
2071 # Change the token type; it's the built-in function Angle(), not the keyword ANGLE
2072 $token->{category} = 'identifier';
2073 $tokenText = $token->tokenText;
2074  
2075 } elsif ($token->category ne 'identifier') {
2076  
2077 return undef;
2078  
2079 } else {
2080  
2081 $tokenText = $token->tokenText;
2082 }
2083  
2084 # Look up the function name. If it doesn't exist, the word is a variable or something
2085 # Alternatively, if $defining is defined, then we're in a 'def' statement and should
2086 # create the function
2087 if (defined $defining) {
2088  
2089 $funcObj = Language::Axbasic::Function::Defined->new($scriptObj, $tokenText);
2090  
2091 } else {
2092  
2093 if (! $scriptObj->ivExists('funcNameHash', $tokenText)) {
2094  
2095 # Not a function, so must be a variable name or something
2096 return undef;
2097  
2098 } else {
2099  
2100 $funcObj = $scriptObj->ivShow('funcNameHash', $tokenText);
2101 }
2102 }
2103  
2104 $self->{'funcObj'} = $funcObj;
2105  
2106 # Now we know that the first token in the token group is a function, extract it
2107 $tokenGroupObj->shiftToken();
2108  
2109 # Read and extract the argument list. If we're defining the function...
2110 if (defined $defining) {
2111  
2112 # Empty parentheses aren't allowed! (and whitespace has been removed by lexing)
2113 $token = $tokenGroupObj->shiftTokenIfCategory('left_paren');
2114 if (! defined $token) {
2115  
2116 return $scriptObj->setError(
2117 'function_called_without_arguments',
2118 $class . '->new',
2119 );
2120 }
2121  
2122 # Extract all the arguments (separated by commas, if there is more than one)
2123 do {
2124  
2125 my $lvalueObj
2126 = Language::Axbasic::Expression::Lvalue->new($scriptObj, $tokenGroupObj);
2127  
2128 if (! defined $lvalueObj) {
2129 return undef;
2130 } else {
2131 push (@args, $lvalueObj);
2132 }
2133  
2134 } while ($tokenGroupObj->shiftMatchingToken(','));
2135  
2136 $token = $tokenGroupObj->shiftTokenIfCategory('right_paren');
2137 if (! defined $token) {
2138  
2139 return $scriptObj->setError(
2140 'mismatched_parentheses_error',
2141 $class . '->new',
2142 );
2143 }
2144  
2145 # Declare the number and type of arguments in the function
2146 if (! $funcObj->declare(@args)) {
2147  
2148 return $scriptObj->setDebug(
2149 'Couldn\'t declare function arguments',
2150 $class . '->new',
2151 );
2152 }
2153  
2154 # Or if the function is already defined...
2155 } else {
2156  
2157 # Pre-defined functions are allowed to have no arguments, in some cases. We might have
2158 # to extract an arglist, or not
2159 # e.g. These statements are both valid, because Pi() expects no arguments
2160 # PRINT Pi()
2161 # PRINT Pi
2162 # e.g. These statements are not valid, because Int() expects an argument
2163 # PRINT Int()
2164 # PRINT Int
2165 $argListObj = Language::Axbasic::Expression::ArgList->new($scriptObj, $tokenGroupObj);
2166  
2167 if (! defined $argListObj) {
2168  
2169 if (! $scriptObj->ivExists('funcArgHash', $tokenText)) {
2170  
2171 # A custom function, created in a DEF statement, has been called without a
2172 # parseable arglist
2173 return $scriptObj->setError(
2174 'function_called_without_arguments',
2175 $class . '->new',
2176 );
2177  
2178 } elsif ($scriptObj->ivShow('funcArgHash', $tokenText)) {
2179  
2180 # A pre-defined function that expects at least one argument, but has been called
2181 # without a parseable arglist
2182 return $scriptObj->setError(
2183 'wrong_number_of_arguments',
2184 $class . '->new',
2185 );
2186 }
2187 }
2188  
2189 if (
2190 ! $scriptObj->ivExists('funcArgHash', $tokenText) # Custom
2191 || ( $argListObj && $scriptObj->ivShow('funcArgHash', $tokenText) ) # Pre-defined
2192 ) {
2193 # A parseable arglist has been found (even if it is empty)
2194 # Check that the arguments are of the correct number and type
2195 if (! $funcObj->checkArgs($argListObj)) {
2196  
2197 return $scriptObj->setError(
2198 'function_called_with_invalid_arguments',
2199 $class . '->new',
2200 );
2201  
2202 } else {
2203  
2204 $self->{'argListObj'} = $argListObj;
2205 }
2206 }
2207  
2208 }
2209  
2210 # Bless the object into existence
2211 bless $self, $class;
2212 # Is it a String or Numeric Function?
2213 $self->setReturnType($funcObj);
2214  
2215 return $self;
2216 }
2217  
2218 ##################
2219 # Methods
2220  
2221 sub evaluate {
2222  
2223 # Can be called by anything
2224 # Evaluates the function (the LA::Function object stored in $self->funcObj) using the
2225 # arguments stored in $self->argRefList
2226 #
2227 # Expected arguments
2228 # (none besides $self)
2229 #
2230 # Return values
2231 # 'undef' on improper arguments or if there's an error evaluating the function
2232 # Otherwise returns the result of evaluating the function
2233  
2234 my ($self, $check) = @_;
2235  
2236 # Local variables
2237 my (
2238 $result,
2239 @args,
2240 );
2241  
2242 # Check for improper arguments
2243 if (defined $check) {
2244  
2245 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
2246 }
2247  
2248 # Evaluate the arguments in the argument list (if there are any)
2249 if ($self->argListObj) {
2250  
2251 @args = $self->argListObj->evaluate();
2252 }
2253  
2254 # Evaluate the function using those arguments
2255 $result = $self->funcObj->evaluate(@args);
2256  
2257 if (! defined $result) {
2258  
2259 # If the function returned 'undef', there was an error (e.g. trying to take the square
2260 # root of a negative number)
2261 return $self->scriptObj->setError(
2262 'undefined_function_error',
2263 $self->_objClass . '->evaluate',
2264 );
2265  
2266 } else {
2267  
2268 return $result;
2269 }
2270 }
2271  
2272 ##################
2273 # Accessors - set
2274  
2275 ##################
2276 # Accessors - get
2277  
2278 sub scriptObj
2279 { $_[0]->{scriptObj} }
2280  
2281 sub funcObj
2282 { $_[0]->{funcObj} }
2283 sub argListObj
2284 { $_[0]->{argListObj} }
2285 }
2286  
2287 { package Language::Axbasic::Expression::Function::Numeric;
2288  
2289 use strict;
2290 use warnings;
2291 # use diagnostics;
2292  
2293 use Glib qw(TRUE FALSE);
2294  
2295 # Set ISA for 'return type' classes
2296 @Language::Axbasic::Expression::Function::Numeric::ISA = qw(
2297 Language::Axbasic::Expression::Function
2298 Language::Axbasic::Expression::Numeric
2299 );
2300 }
2301  
2302 { package Language::Axbasic::Expression::Function::String;
2303  
2304 use strict;
2305 use warnings;
2306 # use diagnostics;
2307  
2308 use Glib qw(TRUE FALSE);
2309  
2310 # Set ISA for 'return type' classes
2311 @Language::Axbasic::Expression::Function::String::ISA = qw(
2312 Language::Axbasic::Expression::Function
2313 Language::Axbasic::Expression::String
2314 );
2315 }
2316  
2317 { package Language::Axbasic::Expression::ArgList;
2318  
2319 use strict;
2320 use warnings;
2321 # use diagnostics;
2322  
2323 use Glib qw(TRUE FALSE);
2324  
2325 @Language::Axbasic::Expression::ArgList::ISA = qw(
2326 Language::Axbasic::Expression
2327 );
2328  
2329 ##################
2330 # Constructors
2331  
2332 sub new {
2333  
2334 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
2335 #
2336 # Expected arguments
2337 # $scriptObj - Blessed reference to the parent LA::Script
2338 # $tokenGroupObj - Blessed reference of the line's token group
2339 #
2340 # Optional arguments
2341 # $implicitFlag - If TRUE, a non-existent arglist is acceptable, i.e. a token group
2342 # with no more tokens is treated the same as an empty arglist token,
2343 # namely '()'. If FALSE or 'undef', an empty arglist (at the very
2344 # least) is expected
2345 #
2346 # Return values
2347 # 'undef' on improper arguments
2348 # Returns either the LA::Expression::Arglist created, or 'undef'
2349  
2350 my ($class, $scriptObj, $tokenGroupObj, $implicitFlag, $check) = @_;
2351  
2352 # Local variables
2353 my (
2354 $token,
2355 @args,
2356 );
2357  
2358 # Check for improper arguments
2359 if (
2360 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj || defined $check
2361 ) {
2362 return $axmud::CLIENT->writeImproper($class . '->new', @_);
2363 }
2364  
2365 # Setup
2366 my $self = {
2367 _objName => $class, # Name Axbasic objects after their class
2368 _objClass => $class,
2369 _parentFile => undef, # No parent file object
2370 _parentWorld => undef, # No parent file object
2371 _privFlag => TRUE, # All IVs are private
2372  
2373 # Perl object components
2374 # ----------------------
2375  
2376 # The parent LA::Script
2377 scriptObj => $scriptObj,
2378  
2379 # IVs
2380 # ---
2381  
2382 # The arguments of the arglist, stored as a Perl list
2383 argList => [],
2384 };
2385  
2386 if (! $implicitFlag || $tokenGroupObj->lookAhead()) {
2387  
2388 # The arglist, if explicitly specified, must start with a left parenthesis
2389 $token = $tokenGroupObj->shiftTokenIfCategory('left_paren');
2390 if (! defined $token) {
2391  
2392 # Not an arglist
2393 return undef;
2394 }
2395  
2396 # Check for an empty arglist
2397 $token = $tokenGroupObj->shiftTokenIfCategory('right_paren');
2398 if (! defined $token) {
2399  
2400 # Go through the list, extracting arguments
2401 do {
2402  
2403 my $arg = Language::Axbasic::Expression::Arithmetic->new(
2404 $scriptObj,
2405 $tokenGroupObj,
2406 );
2407  
2408 if (! defined $arg) {
2409  
2410 return undef;
2411 }
2412  
2413 push (@args, $arg);
2414  
2415 } while ($tokenGroupObj->shiftMatchingToken(','));
2416  
2417 # The arglist must end with a right parenthesis
2418 $token = $tokenGroupObj->shiftTokenIfCategory('right_paren');
2419 if (! defined $token) {
2420  
2421 return $scriptObj->setError(
2422 'mismatched_parentheses_error',
2423 $class . '->new',
2424 );
2425 }
2426 }
2427  
2428 # Store the arglist, and bless this object
2429 $self->{'argList'} = \@args;
2430 }
2431  
2432 # Bless the object into existence
2433 bless $self, $class;
2434 return $self;
2435 }
2436  
2437 ##################
2438 # Methods
2439  
2440 sub evaluate {
2441  
2442 # Called by LA::Statement::xxx->implement
2443 #
2444 # Expected arguments
2445 # (none besides $self)
2446 #
2447 # Return values
2448 # An empty list on improper arguments
2449 # Otherwise returns a list of values from the arglist. The values will be 'undef' for any
2450 # expression that could not be evaluated; it's up to the calling function to check for
2451 # that
2452  
2453 my ($self, $check) = @_;
2454  
2455 # Local variables
2456 my (@emptyList, @values);
2457  
2458 # Check for improper arguments
2459 if (defined $check) {
2460  
2461 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
2462 }
2463  
2464 # Evaluate each argument, transforming the arglist into a list of values
2465 foreach my $arg ($self->argList) {
2466  
2467 push (@values, $arg->evaluate());
2468 }
2469  
2470 # Return the list of values
2471 return @values;
2472 }
2473  
2474 ##################
2475 # Accessors - set
2476  
2477 ##################
2478 # Accessors - get
2479  
2480 sub scriptObj
2481 { $_[0]->{scriptObj} }
2482  
2483 sub argList
2484 { my $self = shift; return @{$self->{argList}}; }
2485 }
2486  
2487 { package Language::Axbasic::Expression::SpecialArgList;
2488  
2489 use strict;
2490 use warnings;
2491 # use diagnostics;
2492  
2493 use Glib qw(TRUE FALSE);
2494  
2495 @Language::Axbasic::Expression::SpecialArgList::ISA = qw(
2496 Language::Axbasic::Expression
2497 );
2498  
2499 ##################
2500 # Constructors
2501  
2502 sub new {
2503  
2504 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
2505 # A modified form of LA::Expression::ArgList, for use with DIM statements
2506 # The arglist can be a simple list of expressions (A, B, C...). Each expression is expected
2507 # to be an integer
2508 # Any of the expressions can be replaced with a pair of numeric expressions representing the
2509 # lower and upper bounds of the dimension. The pair is expressed in the form 'lower TO
2510 # upper'. Both expressions are expected to be integers, and 'lower' is expected to be less
2511 # than 'upper'
2512 # Thus we could use any of the following special arglists in a DIM statement:
2513 #
2514 # DIM data (2, 5, 10)
2515 # DIM data (0 TO 2, 0 TO 5, 10 TO 20)
2516 # DIM data (2, 5, 10 TO 20)
2517 #
2518 # Expected arguments
2519 # $scriptObj - Blessed reference to the parent LA::Script
2520 # $tokenGroupObj - Blessed reference of the line's token group
2521 #
2522 # Return values
2523 # 'undef' on improper arguments
2524 # Returns either the LA::Expression::Arglist created, or 'undef'
2525  
2526 my ($class, $scriptObj, $tokenGroupObj, $check) = @_;
2527  
2528 # Local variables
2529 my (
2530 $token,
2531 @args,
2532 );
2533  
2534 # Check for improper arguments
2535 if (
2536 ! defined $class || ! defined $scriptObj || ! defined $tokenGroupObj || defined $check
2537 ) {
2538 return $axmud::CLIENT->writeImproper($class . '->new', @_);
2539 }
2540  
2541 # Setup
2542 my $self = {
2543 _objName => $class, # Name Axbasic objects after their class
2544 _objClass => $class,
2545 _parentFile => undef, # No parent file object
2546 _parentWorld => undef, # No parent file object
2547 _privFlag => TRUE, # All IVs are private
2548  
2549 # Perl object components
2550 # ----------------------
2551  
2552 # The parent LA::Script
2553 scriptObj => $scriptObj,
2554  
2555 # IVs
2556 # ---
2557  
2558 # The arguments of the special arglist. Each single argument (or pair of arguments) is
2559 # stored as a list reference
2560 argList => [],
2561 };
2562  
2563 # The special arglist must start with a left parenthesis
2564 $token = $tokenGroupObj->shiftTokenIfCategory('left_paren');
2565 if (! defined $token) {
2566  
2567 # Not an arglist
2568 return undef;
2569 }
2570  
2571 # Check for an empty arglist
2572 $token = $tokenGroupObj->shiftTokenIfCategory('right_paren');
2573 if (! defined $token) {
2574  
2575 # Go through the list, extracting single arguments orpairs of arguments
2576 do {
2577  
2578 my (
2579 $arg, $arg2,
2580 @miniList,
2581 );
2582  
2583 $arg = Language::Axbasic::Expression::Arithmetic->new(
2584 $scriptObj,
2585 $tokenGroupObj,
2586 );
2587  
2588 if (! defined $arg || ! (ref ($arg) =~ m/Numeric/)) {
2589 return undef;
2590 } else {
2591 push (@miniList, $arg);
2592 }
2593  
2594 if (defined $tokenGroupObj->shiftMatchingToken('to')) {
2595  
2596 $arg2 = Language::Axbasic::Expression::Arithmetic->new(
2597 $scriptObj,
2598 $tokenGroupObj,
2599 );
2600  
2601 if (! defined $arg2 || ! (ref ($arg2) =~ m/Numeric/)) {
2602 return undef;
2603 } else {
2604 push (@miniList, $arg2);
2605 }
2606 }
2607  
2608 push (@args, \@miniList);
2609  
2610 } while ($tokenGroupObj->shiftMatchingToken(','));
2611  
2612 # The arglist must end with a right parenthesis
2613 $token = $tokenGroupObj->shiftTokenIfCategory('right_paren');
2614 if (! defined $token) {
2615  
2616 return $scriptObj->setError(
2617 'mismatched_parentheses_error',
2618 $class . '->new',
2619 );
2620 }
2621 }
2622  
2623 # Store the arglist, and bless this object
2624 $self->{'argList'} = \@args;
2625  
2626  
2627 # Bless the object into existence
2628 bless $self, $class;
2629 return $self;
2630 }
2631  
2632 ##################
2633 # Methods
2634  
2635 sub evaluate {
2636  
2637 # Called by LA::Statement::xxx->implement
2638 #
2639 # Expected arguments
2640 # (none besides $self)
2641 #
2642 # Return values
2643 # An empty list on improper arguments
2644 # Otherwise evaluates the expressions in the arglist, and returns them as a list with the
2645 # same structure (a list of list references, with each list reference containing
2646 # either a single value or a pair of values). The value(s) will be 'undef' for any
2647 # expression that could not be evaluated; it's up to the calling function to check for
2648 # that
2649  
2650 my ($self, $check) = @_;
2651  
2652 # Local variables
2653 my (@emptyList, @values);
2654  
2655 # Check for improper arguments
2656 if (defined $check) {
2657  
2658 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
2659 }
2660  
2661 # Evaluate each expression, transforming the arglist into a list of values
2662 foreach my $listRef ($self->argList) {
2663  
2664 my @miniList;
2665  
2666 foreach my $arg (@$listRef) {
2667  
2668 push (@miniList, $arg->evaluate());
2669 }
2670  
2671 push (@values, \@miniList);
2672 }
2673  
2674 # Return the list of values
2675 return @values;
2676 }
2677  
2678 ##################
2679 # Accessors - set
2680  
2681 ##################
2682 # Accessors - get
2683  
2684 sub scriptObj
2685 { $_[0]->{scriptObj} }
2686  
2687 sub argList
2688 { my $self = shift; return @{$self->{argList}}; }
2689 }
2690  
2691 { package Language::Axbasic::Expression::LogicalOr;
2692  
2693 use strict;
2694 use warnings;
2695 # use diagnostics;
2696  
2697 use Glib qw(TRUE FALSE);
2698  
2699 @Language::Axbasic::Expression::LogicalOr::ISA = qw(
2700 Language::Axbasic::Expression::Boolean
2701 );
2702  
2703 ##################
2704 # Constructors
2705  
2706 sub new {
2707  
2708 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
2709 #
2710 # Paraphrased from Language::Basic:
2711 # Booleans don't care whether the stuff in them is String or Numeric, so no sub-packages are
2712 # needed
2713 #
2714 # A set of LogicalAnd expressions connected by the 'or' operator
2715 #
2716 # In Axbasic, Boolean expressions can't contain non-Boolean expressions except for
2717 # Relational expressions (which have two Arithmetic expressions separated by a Relational
2718 # operator)
2719 # However, parentheses can confuse things.
2720 # LA::Expression::Unary is one of:
2721 # (1) A constant, variable, function, etc.
2722 # (2) (Arithmetic Exp.)
2723 # (3) (Logical Or)
2724 # LA::Expression::Unary::new calls LA::Expression::LogicalOr->new with 'maybe_arithmetic'
2725 # sometimes, to tell LA::Expression::LogicalOr that if it finds a (parenthesised)
2726 # non-Boolean expression, it's just case #2 above. (Otherwise, a non-Boolean subexpression
2727 # is an error.)
2728 #
2729 # Expected arguments
2730 # $scriptObj - Blessed reference to the parent LA::Script
2731 # $tokenGroupObj - Blessed reference of the line's token group
2732 #
2733 # Optional arguments
2734 # $maybeArithmetic - Set to 'maybe_arithmetic' or 'undef'
2735 #
2736 # Return values
2737 # 'undef' on improper arguments, or for an error
2738 # Otherwise returns either the blessed reference to this object, or a LA::Expression or
2739 # 'undef'
2740  
2741 my ($class, $scriptObj, $tokenGroupObj, $maybeArithmetic, $check) = @_;
2742  
2743 # Local variables
2744 my (
2745 $expression,
2746 @expList,
2747 );
2748  
2749 # Check for improper arguments
2750 if (
2751 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
2752 || (defined $maybeArithmetic && $maybeArithmetic ne 'maybe_arithmetic')
2753 || defined $check
2754 ) {
2755 return $axmud::CLIENT->writeImproper($class . '->new', @_);
2756 }
2757  
2758 # Don't bother creating a LogicalOr object if the expression evaluates to a boolean value;
2759 # just return the LogicalAnd object instead
2760 $expression = Language::Axbasic::Expression::LogicalAnd->new(
2761 $scriptObj,
2762 $tokenGroupObj,
2763 $maybeArithmetic,
2764 );
2765  
2766 if (! defined $expression) {
2767  
2768 return undef;
2769  
2770 } elsif (! $expression->isa('Language::Axbasic::Expression::Boolean')) {
2771  
2772 if ($maybeArithmetic) {
2773  
2774 return $expression;
2775  
2776 } else {
2777  
2778 return $scriptObj->setError(
2779 'expected_boolean_expression',
2780 $class . '->new',
2781 );
2782 }
2783 }
2784  
2785 # Add the first expression to @expList, and then add any following pairs of operators and
2786 # expressions
2787 push (@expList, $expression);
2788  
2789 while (defined $tokenGroupObj->shiftMatchingToken('or')) {
2790  
2791 $expression
2792 = Language::Axbasic::Expression::LogicalAnd->new($scriptObj, $tokenGroupObj);
2793  
2794 if (! defined $expression) {
2795  
2796 return undef;
2797  
2798 } elsif (! $expression->isa('Language::Axbasic::Expression::Boolean')) {
2799  
2800 return $scriptObj->setError(
2801 'expected_boolean_expression',
2802 $class . '->new',
2803 );
2804 }
2805  
2806 push (@expList, $expression);
2807 }
2808  
2809 # Don't bother making a LogicalOr object if there's just one LogicalAnd
2810 if (@expList == 1) {
2811  
2812 return $expression;
2813 }
2814  
2815 # Setup
2816 my $self = {
2817 _objName => $class, # Name Axbasic objects after their class
2818 _objClass => $class,
2819 _parentFile => undef, # No parent file object
2820 _parentWorld => undef, # No parent file object
2821 _privFlag => TRUE, # All IVs are private
2822  
2823 # Perl object components
2824 # ----------------------
2825  
2826 # The parent LA::Script
2827 scriptObj => $scriptObj,
2828  
2829 # IVs
2830 # ----
2831  
2832 # The arguments from the arglist, stored as a Perl list
2833 expList => \@expList,
2834 };
2835  
2836 bless $self, $class;
2837 return $self;
2838 }
2839  
2840 ##################
2841 # Methods
2842  
2843 sub evaluate {
2844  
2845 # Called by LA::Statement::xxx->implement
2846 #
2847 # Expected arguments
2848 # (none besides $self)
2849 #
2850 # Return values
2851 # 'undef' on improper arguments
2852 # Otherwise returns the value of the expression list, $self->expList
2853  
2854 my ($self, $check) = @_;
2855  
2856 # Local variables
2857 my (
2858 $obj, $expression, $nextExpression,
2859 @expList,
2860 );
2861  
2862 # Check for improper arguments
2863 if (defined $check) {
2864  
2865 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
2866 }
2867  
2868 # Import the stored expression list
2869 @expList = $self->expList;
2870  
2871 # Evaluate the first expression in the list
2872 $obj = shift @expList;
2873 $expression = $obj->evaluate;
2874 if (! defined $expression) {
2875  
2876 return undef;
2877 }
2878  
2879 # Perform logical OR operation on each subsequent expression
2880 while ($nextExpression = shift @expList) {
2881  
2882 $expression = ($expression || $nextExpression->evaluate);
2883 }
2884  
2885 # Return the value of the whole logical OR expression list
2886 return $expression;
2887 }
2888  
2889 ##################
2890 # Accessors - set
2891  
2892 ##################
2893 # Accessors - get
2894  
2895 sub scriptObj
2896 { $_[0]->{scriptObj} }
2897  
2898 sub expList
2899 { my $self = shift; return @{$self->{expList}}; }
2900 }
2901  
2902 { package Language::Axbasic::Expression::LogicalAnd;
2903  
2904 use strict;
2905 use warnings;
2906 # use diagnostics;
2907  
2908 use Glib qw(TRUE FALSE);
2909  
2910 @Language::Axbasic::Expression::LogicalAnd::ISA = qw(
2911 Language::Axbasic::Expression::Boolean
2912 );
2913  
2914 ##################
2915 # Constructors
2916  
2917 sub new {
2918  
2919 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
2920 #
2921 # Paraphrased from Language::Basic:
2922 # Booleans don't care whether the stuff in them is String or Numeric, so no sub-packages are
2923 # needed
2924 #
2925 # A set of Relational expressions connected by the 'and' operator
2926 #
2927 # Expected arguments
2928 # $scriptObj - Blessed reference to the parent LA::Script
2929 # $tokenGroupObj - Blessed reference of the line's token group
2930 #
2931 # Optional arguments
2932 # $maybeArithmetic - Set to 'maybe_arithmetic' or 'undef'
2933 #
2934 # Return values
2935 # 'undef' on improper arguments, or for an error
2936 # Otherwise returns either the blessed reference to this object, a LA::Expression or
2937 # 'undef'
2938  
2939 my ($class, $scriptObj, $tokenGroupObj, $maybeArithmetic, $check) = @_;
2940  
2941 # Local variables
2942 my (
2943 $expression,
2944 @expList,
2945 );
2946  
2947 # Check for improper arguments
2948 if (
2949 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
2950 || (defined $maybeArithmetic && $maybeArithmetic ne 'maybe_arithmetic')
2951 || defined $check
2952 ) {
2953 return $axmud::CLIENT->writeImproper($class . '->new', @_);
2954 }
2955  
2956 # Don't bother creating a LogicalAnd object if the expression evaluates to a boolean value;
2957 # just return the Relational object instead
2958 $expression = Language::Axbasic::Expression::Relational->new(
2959 $scriptObj,
2960 $tokenGroupObj,
2961 $maybeArithmetic,
2962 );
2963  
2964 if (! defined $expression) {
2965  
2966 return undef;
2967  
2968 } elsif (! $expression->isa('Language::Axbasic::Expression::Boolean')) {
2969  
2970 if ($maybeArithmetic) {
2971  
2972 return $expression;
2973  
2974 } else {
2975  
2976 return $scriptObj->setError(
2977 'expected_boolean_expression',
2978 $class . '->new',
2979 );
2980 }
2981 }
2982  
2983 # Add the first expression to @expList, and then add any following pairs of operators and
2984 # expressions
2985 push (@expList, $expression);
2986  
2987 while (defined $tokenGroupObj->shiftMatchingToken('and')) {
2988  
2989 $expression
2990 = Language::Axbasic::Expression::Relational->new($scriptObj, $tokenGroupObj);
2991  
2992 if (! defined $expression) {
2993  
2994 return undef;
2995  
2996 } elsif (! $expression->isa('Language::Axbasic::Expression::Boolean')) {
2997  
2998 return $scriptObj->setError(
2999 'expected_boolean_expression',
3000 $class . '->new',
3001 );
3002 }
3003  
3004 push (@expList, $expression);
3005 }
3006  
3007 # Don't bother making a LogicalAnd object if there's just one Relational
3008 if (@expList == 1) {
3009  
3010 return $expression;
3011 }
3012  
3013 # Setup
3014 my $self = {
3015 _objName => $class, # Name Axbasic objects after their class
3016 _objClass => $class,
3017 _parentFile => undef, # No parent file object
3018 _parentWorld => undef, # No parent file object
3019 _privFlag => TRUE, # All IVs are private
3020  
3021 # Perl object components
3022 # ----------------------
3023  
3024 # The parent LA::Script
3025 scriptObj => $scriptObj,
3026  
3027 # IVs
3028 # ----
3029  
3030 # The arguments from the arglist, stored as a Perl list
3031 expList => \@expList,
3032 };
3033  
3034 bless $self, $class;
3035 return $self;
3036 }
3037  
3038 ##################
3039 # Methods
3040  
3041 sub evaluate {
3042  
3043 # Called by LA::Statement::xxx->implement
3044 #
3045 # Expected arguments
3046 # (none besides $self)
3047 #
3048 # Return values
3049 # 'undef' on improper arguments
3050 # Otherwise returns the value of the expression list, $self->expList
3051  
3052 my ($self, $check) = @_;
3053  
3054 # Local variables
3055 my (
3056 $obj, $expression, $nextExpression,
3057 @expList,
3058 );
3059  
3060 # Check for improper arguments
3061 if (defined $check) {
3062  
3063 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
3064 }
3065  
3066 # Import the stored expression list
3067 @expList = $self->expList;
3068  
3069 # Evaluate the first expression in the list
3070 $obj = shift @expList;
3071 $expression = $obj->evaluate;
3072 if (! defined $expression) {
3073  
3074 return undef;
3075 }
3076  
3077 # Perform logical AND operation on each subsequent expression
3078 while ($nextExpression = shift @expList) {
3079  
3080 $expression = ($expression && $nextExpression->evaluate);
3081 }
3082  
3083 # Return the value of the whole logical OR expression list
3084 return $expression;
3085 }
3086  
3087 ##################
3088 # Accessors - set
3089  
3090 ##################
3091 # Accessors - get
3092  
3093 sub scriptObj
3094 { $_[0]->{scriptObj} }
3095  
3096 sub expList
3097 { my $self = shift; return @{$self->{expList}}; }
3098 }
3099  
3100 { package Language::Axbasic::Expression::Relational;
3101  
3102 use strict;
3103 use warnings;
3104 # use diagnostics;
3105  
3106 use Glib qw(TRUE FALSE);
3107  
3108 @Language::Axbasic::Expression::Relational::ISA = qw(
3109 Language::Axbasic::Expression::Boolean
3110 );
3111  
3112 ##################
3113 # Constructors
3114  
3115 sub new {
3116  
3117 # Called by LA::Statement::xxx->parse or LA::Statement::xxx->implement
3118 #
3119 # Paraphrased from Language::Basic:
3120 # Usually, an LA::Expression::Relational is just LA::Expression::Arithmetic
3121 # Rel. Op. LA::Expression::Arithmetic
3122 # However, if the first sub-expression in the LA::Expression::Relational is parenthesised,
3123 # it could be either
3124 # (1) (Logical Or Exp.) - e.g. IF (A>B OR C>D) THEN...
3125 # (2) (Arith. Exp.) - e.g. IF (A+1)>B THEN...
3126 # So we call the first LA::Expression::Arithmetic->new with 'maybe_boolean', so that it
3127 # knows it may find a Boolean sub-expression
3128 # Note that in case (1), we don't need to look for a Rel. Op., because
3129 # IF (A > B OR C > D) > 2 is illegal.
3130 #
3131 # Rel. Exp. usually has two expressions in the 'expressions' field, and an operator in the
3132 # "operator' field. However, in case (1) above, there will only be one (Boolean)
3133 # expression, and no op.
3134 #
3135 # Expected arguments
3136 # $scriptObj - Blessed reference to the parent LA::Script
3137 # $tokenGroupObj - Blessed reference of the line's token group
3138 #
3139 # Optional arguments
3140 # $maybeArithmetic - Set to 'maybe_arithmetic' or 'undef'
3141 #
3142 # Return values
3143 # 'undef' on improper arguments, or for an error
3144 # Returns either the blessed reference to this object, or 'undef'
3145  
3146 my ($class, $scriptObj, $tokenGroupObj, $maybeArithmetic, $check) = @_;
3147  
3148 # Local variables
3149 my (
3150 $numericOpRef, $stringOpRef, $type, $token, $op, $transform, $expression,
3151 $nextExpression, $perlOp,
3152 @expList,
3153 );
3154  
3155 # Check for improper arguments
3156 if (
3157 ! defined $class || ! defined $tokenGroupObj || ! defined $scriptObj
3158 || (defined $maybeArithmetic && $maybeArithmetic ne 'maybe_arithmetic')
3159 || defined $check
3160 ) {
3161 return $axmud::CLIENT->writeImproper($class . '->new', @_);
3162 }
3163  
3164 # Setup
3165 my $self = {
3166 _objName => $class, # Name Axbasic objects after their class
3167 _objClass => $class,
3168 _parentFile => undef, # No parent file object
3169 _parentWorld => undef, # No parent file object
3170 _privFlag => TRUE, # All IVs are private
3171  
3172 # Perl object components
3173 # ----------------------
3174  
3175 # The parent LA::Script
3176 scriptObj => $scriptObj,
3177  
3178 # IVs
3179 # ----
3180  
3181 # Is there a 'not' in the expression? 'undef' if there isn't, otherwise set to the token
3182 # containing the 'not'
3183 notToken => undef,
3184 # The Perl equivalent of the operator
3185 perlOp => undef,
3186 # What kind of operator it is ('string' or 'numeric')
3187 perlOpType => undef,
3188 # A list of expressions (max 2), one either side of the operator
3189 expList => [],
3190 };
3191  
3192 # Is there a 'not' in the expression?
3193 $token = $tokenGroupObj->shiftMatchingToken('not');
3194 if (defined $token) {
3195  
3196 $self->{notToken} = $token;
3197 }
3198  
3199 # Get the first expression
3200 $expression = Language::Axbasic::Expression::Arithmetic->new(
3201 $scriptObj,
3202 $tokenGroupObj,
3203 'maybe_boolean',
3204 );
3205  
3206 if (! defined $expression) {
3207  
3208 return $scriptObj->setError(
3209 'illegal_relational_expression',
3210 $class . '->new',
3211 );
3212 }
3213  
3214 push (@expList, $expression);
3215  
3216 # If we can find a parenthesised Boolean expression, just return it
3217 # (Don't even look for a relational operator, since that would be illegal)
3218 if ($expression->isa('Language::Axbasic::Expression::Boolean')) {
3219  
3220 bless $self, $class;
3221 $self->ivPoke('expList', @expList);
3222  
3223 return $self;
3224 }
3225  
3226 # Otherwise, read the Relational operator
3227 $token = $tokenGroupObj->shiftTokenIfCategory('relational_operator');
3228 if (! defined $token) {
3229  
3230 # Found a parenthesised Arithmetic expression?
3231 if ($maybeArithmetic) {
3232  
3233 # Don't bother blessing and returning $self
3234 return $expression;
3235  
3236 } else {
3237  
3238 return $scriptObj->setError(
3239 'illegal_relational_expression',
3240 $class . '->new',
3241 );
3242 }
3243 }
3244  
3245 $op = $token->tokenText;
3246  
3247 # NB $nextExpression isn't allowed to be Arithmetic, so no $maybeArithmetic arg
3248 $nextExpression
3249 = Language::Axbasic::Expression::Arithmetic->new($scriptObj, $tokenGroupObj);
3250  
3251 if (! defined $nextExpression) {
3252  
3253 return $scriptObj->setError(
3254 'illegal_relational_expression',
3255 $class . '->new',
3256 );
3257 }
3258  
3259 push (@expList, $nextExpression);
3260  
3261 # Convert Axbasic operators to Perl Operators
3262 $stringOpRef = {
3263 "=" => "eq",
3264 ">" => "gt",
3265 "<" => "lt",
3266 ">=" => "ge",
3267 "<=" => "le",
3268 "<>" => "ne",
3269 };
3270  
3271 $numericOpRef = {
3272 "=" => "==",
3273 ">" => ">",
3274 "<" => "<",
3275 ">=" => ">=",
3276 "<=" => "<=",
3277 "<>" => "!=",
3278 };
3279  
3280 if ($expression->isa('Language::Axbasic::Expression::String')) {
3281  
3282 $transform = $stringOpRef;
3283 $type = 'string';
3284  
3285 } else {
3286  
3287 $transform = $numericOpRef;
3288 $type = 'numeric';
3289 }
3290  
3291 $perlOp = $transform->{$op};
3292 if (! defined $perlOp) {
3293  
3294 return $scriptObj->setError(
3295 'illegal_operator_STRING_in_relational_expression',
3296 $class . '->new()',
3297 'STRING', $op,
3298 );
3299 }
3300  
3301 bless $self, $class;
3302  
3303 # Store the equivalent Perl operator and bless the object into existence
3304 $self->ivPoke('perlOp', $perlOp);
3305 $self->ivPoke('perlOpType', $type);
3306 $self->ivPoke('expList', @expList);
3307  
3308 return $self;
3309 }
3310  
3311 ##################
3312 # Methods
3313  
3314 sub evaluate {
3315  
3316 # Called by LA::Statement::xxx->implement
3317 #
3318 # Expected arguments
3319 # (none besides $self)
3320 #
3321 # Return values
3322 # 'undef' on improper arguments
3323 # Otherwise returns the value of the expression
3324  
3325 my ($self, $check) = @_;
3326  
3327 # Local variables
3328 my (
3329 $expression, $value, $nextExpression, $nextValue, $perlExpression, $finalValue,
3330 @expList,
3331 );
3332  
3333 # Check for improper arguments
3334 if (defined $check) {
3335  
3336 return $axmud::CLIENT->writeImproper($self->_objClass . '->evaluate', @_);
3337 }
3338  
3339 # Import the stored expression list
3340 @expList = $self->expList;
3341  
3342 # Evaluate the first expression in the list
3343 $expression = shift @expList;
3344 $value = $expression->evaluate;
3345  
3346 if (! $expression->isa('Language::Axbasic::Expression::Boolean')) {
3347  
3348 $nextExpression = shift @expList;
3349 $nextValue = $nextExpression->evaluate();
3350  
3351 # Paraphrased from Language::Basic:
3352 # We're assuming that Perl eval will get the same result as BASIC would
3353 # Using \Q in case we say IF a$ = "\", which should really compare with \\
3354 if ($self->perlOpType eq 'string') {
3355  
3356 $perlExpression = "\"\Q$value\E\" " . $self->perlOp . " \"\Q$nextValue\E\"";
3357  
3358 } elsif ($self->perlOpType eq 'numeric') {
3359  
3360 # # v0.6.042 - removed \E as it was generating Perl warnings
3361 # $perlExpression = "$value\E " . $self->perlOp . " $nextValue\E";
3362 $perlExpression = "$value " . $self->perlOp . " $nextValue";
3363 }
3364  
3365 $finalValue = eval $perlExpression;
3366  
3367 } else {
3368  
3369 # $expression has a nested Boolean in it. There is no $nextExpression
3370 $finalValue = $value;
3371 }
3372  
3373 # Apply the 'not' operator, if it was present when $self->parse checked for it
3374 if ($self->notToken) {
3375  
3376 $finalValue = ! $finalValue;
3377 }
3378  
3379 return $finalValue;
3380 }
3381  
3382 ##################
3383 # Accessors - set
3384  
3385 ##################
3386 # Accessors - get
3387  
3388 sub scriptObj
3389 { $_[0]->{scriptObj} }
3390  
3391 sub perlOp
3392 { $_[0]->{perlOp} }
3393 sub perlOpType
3394 { $_[0]->{perlOpType} }
3395 sub notToken
3396 { $_[0]->{notToken} }
3397 sub expList
3398 { my $self = shift; return @{$self->{expList}}; }
3399 }
3400  
3401 { package Language::Axbasic::Expression::Numeric;
3402  
3403 use strict;
3404 use warnings;
3405 # use diagnostics;
3406  
3407 use Glib qw(TRUE FALSE);
3408  
3409 @Language::Axbasic::Expression::Numeric::ISA = qw(
3410 Language::Axbasic::Expression
3411 Language::Axbasic::Numeric
3412 );
3413 }
3414  
3415 { package Language::Axbasic::Expression::String;
3416  
3417 use strict;
3418 use warnings;
3419 # use diagnostics;
3420  
3421 use Glib qw(TRUE FALSE);
3422  
3423 @Language::Axbasic::Expression::String::ISA = qw(
3424 Language::Axbasic::Expression
3425 Language::Axbasic::String
3426 );
3427 }
3428  
3429 { package Language::Axbasic::Expression::Boolean;
3430  
3431 use strict;
3432 use warnings;
3433 # use diagnostics;
3434  
3435 use Glib qw(TRUE FALSE);
3436  
3437 @Language::Axbasic::Expression::Boolean::ISA = qw(
3438 Language::Axbasic::Expression
3439 Language::Axbasic::Boolean
3440 );
3441 }
3442  
3443 # Package must return a true value
3444 1