NAME
CPU::Z80::Assembler::Expr - Represents one assembly expression to be computed at link time
SYNOPSIS
use CPU::Z80::Assembler::Expr;
my $node = CPU::Z80::Assembler::Expr->new( type => "sb" );
$expr->parse($input);
$new_expr = $expr->build($expr_text);
$value = $expr->evaluate($address, \%symbol_table);
$bytes = $expr->bytes($address, \%symbol_table);
DESCRIPTION
This module defines the class that represents one assembly expression to be computed at link time.
EXPORTS
Nothing.
FUNCTIONS
new
Creates a new object, see Class::Struct.
type
The type string has to be defined before the bytes
method is called, and defines how to code the value returned by evaluate
into a byte string.
Type is one of:
- "sb"
-
for signed byte - a 8 bit signed value. A larger value is truncated and a warning is issued.
- "ub"
-
for unsigned byte - a 8 bit unsigned value. A larger value is truncated and a warning is issued.
- "w"
-
for word - a 16 bit unsigned value in little endian format. A larger value is truncated, but in this case no warning is issued. The address part above 0xFFFF is considered a bank selector for memory banked systems.
A STRING value is computed in little endian format and only the first two characters are used. "ab" is encoded as ord("a")+(ord("b")<<8).
The text bytes used in defm / deft are a string of bytes in big endian format, not truncated. For example, 0x112233 is stored as the 3-byte sequence 0x11, 0x22 and 0x33.
A STRING value is encoded with the list of characters in the string. If the string is used in an expression, then the expression applies to the last character of the string. This allows expressions like "CALL"+0x80 to invert bit 7 of the last character of the string.
C-like escape sequences are expanded both in single- and double-quoted strings.
child
List of tokens composing the expression.
line
Get/set the line - text, file name and line number where the token was read.
parse
$expr->parse($input);
Parses an expression at the given $input stream (Iterator::Simple::Lookahead), leaves the stream pointer after the expression and updates the expression object. Dies if the expression cannot be parsed.
evaluate
$value = $expr->evaluate($address, $symbol_table)
Computes the value of the expression, as found at the given address and looking up any referenced symbols from the given symbol table.
The address is used to evaluate the value of '$'.
The symbol table is a hash of symbol names to values. The value is either a scalar value that is used directly in the expression, or a reference to a sub-expression that is computed recursively by calling its evaluate
method.
Exits with a fatal error if the expression cannot be evaluated (circular reference, undefined symbol or mathematical error).
build
$new_expr = $expr->build($expr_text)
$new_expr = $expr->build($expr_text, @init_args)
Build and return a new expresion object with an expression based on the current object. The expression is passed as a string and is lexed by CPU::Z80::Assembler z80lexer
. The special token '{}' is used to refer to this expression.
For example, to return a new expression object that, when evaluated, gives the double of the current expression object:
my $new_expr = $expr->build("2*{}");
@init_args
can be used to pass parameters to the constructor of the new expression object.
bytes
$bytes = $expr->bytes($address, \%symbol_table);
Calls evaluate
to compute the value of the expression, and converts the value to a one or two byte string, according to the type
.
BUGS and FEEDBACK
See CPU::Z80::Assembler.
SEE ALSO
CPU::Z80::Assembler Asm::Preproc::Line Class::Struct
AUTHORS, COPYRIGHT and LICENCE
See CPU::Z80::Assembler.