NAME ^

CodeString - object to build (PIR) code segments

SYNOPSIS ^

    .local pmc code
    code = new 'CodeString'

    code.emit("   $P0 = %0 * %1", '$P21', '3')
    code.emit("  %l:", 'l' => 'label')

    print code

DESCRIPTION ^

CodeString is a class intended to simplify the process of emitting code strings. Ideally this will eventually become a form of "CodeBuffer" that is more efficient than string concatenation, but for now it works well enough for me.

The primary method for CodeString objects is emit, which appends a line (or lines) of code to the string according to a format parameter. The line can contain substitution markers (ala printf) that indicate where other parameters to the call should be placed.

Note that CodeString is just a subclass of Parrot's native String class, so it's easy to combine CodeString objects with other strings outside of the emit method.

Functions ^

_onload()

Initializes the CodeString class.

emit(string fmt [, pmc args ] [, pmc hash ])

Add a line to a CodeString object according to fmt. The fmt string can contain any number of "%-replacements" which are replaced by the corresponding values from args or hash prior to being appended to the string. (Here args is a slurpy array, and hash is a slurpy hash.)

The currently defined replacements include:

    %0 %1 ... %9     the value from the args array at index 0..9
    %,               the values of the args array separated by commas
    %%               a percent sign
A percent-sign followed by any other character that is a hash key receives the value of the hash element.

A newline is automatically added to the end of the fmt.

unique([string fmt])

Each call to unique returns a unique number, or if a fmt parameter is given it returns a unique string beginning with fmt. (This may eventually be generalized to allow uniqueness anywhere in the string.) The function starts counting at 10 (so that the values 0..9 can be considered "safe").

escape(string str)

   Returns an escaped value of C<str> suitable for including in PIR.
   If the string contains any non-ASCII characters, then it's
   prefixed with 'unicode:'.

AUTHOR ^

Kevin Tew (tewky@yahoo.com is the author and maintainer of the C PMC version. Patrick Michaud (pmichaud@pobox.com) is the author and maintainer. Patches and suggestions should be sent to the Perl 6 compiler list (perl6-compiler@perl.org).


parrot