parrotcode: object to build (PIR) code segments | |
Contents | Compilers |
CodeString - object to build (PIR) code segments
.local pmc code
code = new 'PGE::CodeString'
code.emit(" $P0 = %0 * %1", '$P21', '3')
code.emit(" %l:", 'l' => 'label')
print code
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.
_onload()
CodeString
class.emit(string fmt [, pmc args ] [, pmc hash ])
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.) %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
unique([string fmt])
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)
str
suitable for including in PIR. If the string contains any non-ASCII characters, then it's prefixed with 'unicode:'.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).
|