CPU::Z80::Assembler::Expr - Represents one assembly expression to be computed at link time


  use CPU::Z80::Assembler::Expr;
  my $node = CPU::Z80::Assembler::Expr->new( type => "sb" );
  $new_expr = $expr->build($expr_text);
  $value = $expr->evaluate($address, \%symbol_table);
  $bytes = $expr->bytes($address, \%symbol_table);


This module defines the class that represents one assembly expression to be computed at link time.





Creates a new object, see Class::Struct.


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:


for signed byte - a 8 bit signed value. A larger value is truncated and a warning is issued.


for unsigned byte - a 8 bit unsigned value. A larger value is truncated and a warning is issued.


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.


List of tokens composing the expression.


Get/set the line - text, file name and line number where the token was read.



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.


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


  $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 = $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.


See CPU::Z80::Assembler.


CPU::Z80::Assembler Asm::Preproc::Line Class::Struct


See CPU::Z80::Assembler.