Module utils

Unsurprisingly, this module is a grab-bag of various useful functions and methods the used by modules in the rest of the compiler. Some of this stuff is actually quite useful in contexts beyond writing a compiler, and should probably be moved into some of the common libraries rather than living in compiler base.

File compiler/base/utils.dylan

pretty-format[Method]

Pretty-print error messages.

Parameters
streamAn instance of <stream>.
stringAn instance of <byte-string>.
argsAn instance of <object>.

Return Values
None

Description

Used for error message printing. Turns each space in the control string into a conditional newline, and turns literal newlines into a mandatory newline and 2 space indent.


condition-format[Method]

A method that uses pretty-format.

Parameters
streamAn instance of <stream>.
control-stringAn instance of <byte-string>.
argsAn instance of <object>.

Return Values
None

Description

This method prints error messages using pretty-format. It works by shadowing the condition-format method defined on (<stream>, <string>).


write-class-name[Method]

Print an object's classname to a stream.

Parameters
thingAn instance of <object>.
streamAn instance of <object>.

Return Values
None

Description

This print's thing's object-class's name to the stream. If the class is anonymous, the default representation of classes is printed to the stream. (This should never happen until d2c learns how to make anonymous classes.)


write-address[Method]

Prints the address of an object

Parameters
thingAn instance of <object>.
streamAn instance of <object>.

Return Values
None

Description

Prints the hexadecimal address of object to the stream stream.


pprint-fields[Method]

Used to print an object and its fields

Parameters
thingAn instance of <object>.
streamAn instance of <object>.
fieldsAn instance of <object>. Plist of fieldnames and values.

Return Values
None

Description

This method is a tool used to print the constituents of an object. The #rest argument is a list of symbols alternating with values — the symbols are used as the field names for the values.


$thousand-cardinals[Variable]

Vector of English names for numbers.

Type
<vector>

Description

This vector contains the names that are prefixed by numbers less than a hundred — "thousand", "million", etc. It is used in the method integer-to-english.


$ten-cardinals[Variable]

Vector of the English tens units.

Type
<vector>

Description

This vector contains the names of multiples of ten — "twenty", "thirty", etc. It is used in the method integer-to-english.


$unit-cardinals[Variable]

Vector of English words for 0 to 20.

Type
<vector>

Description

This vector contains strings naming the integers from 0 to 20. It is used in the method integer-to-english .


$thousand-ordinals[Variable]

Like $thousand-cardinals, except to name positions.

Type
<vector>

Description

This vector lists the ordinals for numbers bigger than 1000 — "millionth", "billionth" and so on. It is used in the method integer-to-english.


$ten-ordinals[Variable]

Like $ten-cardinals, except to name positions.

Type
<vector>

Description

This vector lists the ordinals for small integers from 0 to 20 — "zeroth", "first", etc. It is used in the method integer-to-english.


integer-to-english[Method]

Convert an integer to an English string.

Parameters
intAn instance of <integer>.
as:An instance of one-of(#"cardinal", #"ordinal").

Return Values
resAn instance of <byte-string>.

Description

This method converts an <integer> object into the English string for the number it represents. If the keyword as: is set to #"ordinal", then the English string is the name of the n-th position; otherwise it is just the name of the number.