printf
The printf
statement gets a format string and, optionally, a
list of values to print. It can be invoked using two alternative
syntaxes, which are equivalent:
printf (fmt[, value…]) printf fmt[, value…]
The format string fmt is printed verbatim to the standard
output, but for format tags which start with %
. These
format tags are interpreted especially.
Most of the format tags “consume” one of the specified values.
Every value in the list shall be described by a tag, or the compiler
will signal an error. Likewise, values without a corresponding
describing tag is an error. These tags are:
%s
Print the argument as a string.
%ibits(d|x|o|b|c)
Print the argument as a signed integer of size bits. The last letter determines how the argument is printed.
d
Print the integer in decimal.
x
Print the integer in hexadecimal.
o
Print the integer in octal.
b
Print the integer in binary.
c
Print the integer as an ASCII character. This only works with 8 bit integers.
%u
Same as %i
, but for unsigned integers.
%c
A shorter way to write %u8c
.
%v
Print the value printed representation of the argument, which can be
of any type including complex types like arrays and structs. This is
similar to the write
operation available in many Lisp systems.
This tag supports an optional numerical argument (restricted to a single digit) that specifies the maximum depth level when printing structures. Example:
(poke) printf ("%1v\n", struct { s = struct { i = 10 }, l = 20L }); struct {s=struct {…},l=0x14L}
By default, the depth level is 0
, which means no limit.
This tag also support an optional flag that specifies how the value is
printed. The supported tags are F
(for flat) and
T
(for tree). The default is flat. Example:
(poke) printf ("%Tv\n", struct { s = struct { i = 10 }, l = 20L }); struct { s=struct { i=0xa }, l=0x14L } (poke) printf ("%Fv\n", struct { s = struct { i = 10 }, l = 20L }); struct {s=struct {i=0xa},l=0x14L}
This tag is mainly intended to be used in pretty-printers.
The following format tags do not consume arguments. They support emitting styled text using the libtextstyle approach of having styling classes that you can customize in a .css file.
%<classname:
Start the styling class with name classname. The class name cannot be empty.
%>
End the last opened styling class. All styling classes should be ended before finishing the format string.
Note that styling classes can be nested, but all classes should be ended before finishing the format string.
If you use a name class, you can define how to style it in the
.css file (poke installs and uses poke-default.css but you can
set the POKE_STYLE
environment variable to point to another
css) like this:
.NAME { text-decoration: blink; color : pink; }
Examples:
(poke) printf "This is a NAME: %<NAME:xxx%>" This is a NAME: xxx (poke) printf "Name: %<string:%s%> Age: %<integer:%i32d%>, "Jose", 39 Name: Jose Age: 39