Catspeak Reference


location

When compiling programs, CatspeakParser will embed diagnostic information in the generated IR, such as the line and column numbers of an expression or statement. This information can be used by failing Catspeak programs to offer clearer error messages.

§ catspeak_location_create

function catspeak_location_create(
  row : Real,
  column : Real,
) -> Real

Encodes the line and column numbers of a source location into a 32-bit integer. The first 20 least-significant bits are reserved for the row number, with the remaining 12 bits used for the (less important) column number.

Because a lot of diagnostic information may be created for any given Catspeak program, it is important that this information has zero memory impact; hence, the line and column numbers are encoded into a 32-bit integer--which can be created and discarded without allocating memory--instead of as a struct.

Mask layout

| 00000000000011111111111111111111 |
| <--column--><-------line-------> |
📝 Note

Because of this, the maximum line number is 1,048,576 and the maximum column number is 4,096. Any line/column counts beyond this will raise an exception in debug mode, and just be garbage data in release mode.

Arguments

  • row

    The row number of the source location.

  • column

    The column number of the source location. This is the number of Unicode codepoints since the previous new-line character. As a result, tabs are considered a single column, not 2, 4, 8, etc. columns.

Returns Real

§ catspeak_location_get_column

function catspeak_location_get_column(
  location : Real,
) -> Real

Gets the column component of a Catspeak source location. This is stored as a 12-bit unsigned integer within the most significant bits of the supplied Catspeak location handle.

Arguments

  • location

    A 32-bit integer representing the diagnostic information of a Catspeak program.

Returns Real

§ catspeak_location_get_row

function catspeak_location_get_row(
  location : Real,
) -> Real

Gets the line component of a Catspeak source location. This is stored as a 20-bit unsigned integer within the least-significant bits of the supplied Catspeak location handle.

Arguments

  • location

    A 32-bit integer representing the diagnostic information of a Catspeak program.

Returns Real