NAME

Language::Basic::Expression - Package to handle string, numeric, and conditional 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 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 includes not just the standard arithmetic expressions (like 1 + 2), but also lvalues (scalar variables or arrays), functions, and constants. See the Syntax file included with the Language::Basic distribution for details on the way expressions are built.

DESCRIPTION

Each subclass has (at least) three methods:

parse

takes a Token::Group and eats one or more Tokens from it (or returns undef if the string's syntax doesn't match that subclass), setting various fields in the 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.

The Expression subclasses closely follow the BASIC grammar. Most subclasses can have their own String and Numeric subclasses in turn. This allows us to make sure that you don't try to multiply strings together, etc.

Expression subclasses:

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 '/'. A String multiplicative expression is just a String unary expression.

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

Arglist

A conditional expression, like "A>B+C". Note that in BASIC, (unlike Perl or C) you can't change a conditional expression into an integer or string.