NAME ^

src/pmc/codestring.pmc - CodeString PMC Class

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.

Methods ^

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 str suitable for including in PIR. If the string contains any non-ASCII characters, then it's prefixed with 'unicode:'.


parrot