|
Language::Basic::Expression - Package to handle string, numeric, and boolean expressions. |
Language::Basic::Expression - Package to handle string, numeric, and boolean expressions.
See the Language::Basic manpage for the overview of how the Language::Basic module works. This pod page is more technical.
# Given an LB::Token::Group, create an expression I<and> parse it
my $exp = new LB::Expression::Arithmetic $token_group;
# What's the value of the expression?
print $exp->evaluate;
# Perl equivalent of the BASIC expression
print $exp->output_perl;
Expressions are basically the building blocks of Statements, in that every BASIC statement is made up of keywords (like GOTO, TO, STEP) and expressions. So expressions include not just the standard arithmetic and boolean expressions (like 1 + 2), but also lvalues (scalar variables or arrays), functions, and constants. See the Language::Basic::Syntax manpage for details on the way expressions are built.
BASIC expressions are represented by various objects of subclasses of Language::Basic::Expression. Most LB::Expressions are in turn made up of other LB::Expressions. For example an LBE::Arithmetic may be made up of two LBE::Multiplicative and a ``plus''. ``Atoms'' (indivisible LBE's) include things like LBE::Constants and LBE::Lvalues (variables).
A bunch of LBE subclasses represent various kinds of BASIC expressions. These subclasses closely follow the BASIC syntax diagram.
Expressions can be classified in two ways, which are sort of vertical and horizontal. One classification method is what subexpressions (if any) an expression is made of. For example, an Arith. Exp. is made up of one or more Mult. Exps. connected by plus or minus signs, while a Mult. Exp. is made up of one or more Unary Exps. This is a hierarchical (or vertical) distinction, important for building up the tree of objects that represent a BASIC expression.
(Note that not all levels in the hierarchy have to be filled. We don't bother making an Arith. Exp. which contains just one Mult. Exp. which contains just one Unary Exp. Instead, we just use the Unary Exp. itself (when it's safe to do so!)
The second way of classifying expressions is by their return type. A String Exp. is a string constant, a string variable, a string function, or some other expression whose value when evaluated will be a string. A Numeric Exp. evaluates to a number, and a Boolean to a True or False value. This distinction is important for typechecking and finding syntax errors in BASIC code. (Note that in BASIC -- unlike Perl or C -- you can't ``cast'' a boolean value into an integer or string. This actually makes parsing more difficult.)
Some expressions don't exactly fit any of these distinctions. For example, an Arglist evaluates to a list of expressions, each of which may be Numeric or Boolean.
Each subclass has (at least) three methods:
If an expression contains just one subexpression often we'll just return the subexpression. So if an Arith. Exp. contains just one Mult. Exp., we'll just return the LBE::Multiplicative object and not an LBE::Arithmetic object.
The hierarchical list of subclasses follows:
|
Language::Basic::Expression - Package to handle string, numeric, and boolean expressions. |