The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Language::Basic::Expression - Package to handle string, numeric, and boolean expressions.

SYNOPSIS

See Language::Basic 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 Language::Basic::Syntax for details on the way expressions are built.

DESCRIPTION

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).

The LBE hierarchy

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.

subclass methods

Each subclass has (at least) three methods:

new

The "new" method takes a class and a Token::Group (and possibly some other args). It eats one or more Tokens from it, parsing them, creating a new object of that class I, and setting various fields in that object, which it returns. If the tokens don't match the class, "new" returns undef.

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.

evaluate

Actually calculates the value of the expression. For a string or numeric constant or variable, that just means taking the stored value of that object. For other Expressions, you actually need to do math.

output_perl

Gives a string with the Perl equivalent to a BASIC expression. "1+2" is converted to "1+2", but "A" becomes "$a", "A$" becomes "$a_str", and function calls may be even more complicated.

LBE subclasses

The hierarchical list of subclasses follows:

Arithmetic

An arithmetic expression is a set of multiplicative expressions connected by plus or minus signs. (String expressions can only be connected by plus, which is the BASIC concatenation operator.)

Multiplicative

a set of unary expressions connected by '*' or '/'.

Unary

a variable, a function, a string or numeric constant, or an arithmetic expression in parentheses, potentially with a unary minus sign.

Constant

a string or numeric constant, like "17" or 32.4

Lvalue

a settable expression: a variable, X, or one cell in an array, A(17,Q). The "variable" method returns the actual LB::Variable::Scalar object referenced by this Lvalue.

Function

Either an Intrinsic or a User-Defined function.

Arglist

a list of arguments to an array or function

Logical_Or

a set of Logical_And expressions connected by "OR"

Logical_And

a set of Relational expressions connected by "AND"

Relational

A relational expression, like "A>B+C", optionally with a NOT in front of it.