Literals and Expressions
Literals
A literal is a fixed value written directly into the source code.
In cksp, literals can be of the following types:
Integer literal
Integer literals are whole numbers without a decimal point.
Integer literals can also be written in hexadecimal or binary form:
Hexadecimal literals start with 0x and use digits 0–9 and letters A–F:
Binary literals can be written in two orientations:
-
With a trailing
b: bits are interpreted least-significant-bit (LSB) right
-
With a leading
b: bits are interpreted least-significant-bit (LSB) left
Real (floating point) literal
Real literals represent numbers with a fractional part, indicated by a decimal point.
Real literals can also be expressed in scientific notation, allowing compact representation of very large or very small values.Scientific notation uses the letter e or E to represent powers of ten.
The syntax follows this pattern:
The exponent can be positive or negative, with or without a sign.
The following examples are all valid:
You may use uppercase E interchangeably with lowercase e:
Precision and Type Inference
All literals written in scientific notation are inferred as real values. They support the same precision and arithmetic operations as normal floating-point literals.
String literal
String literals are sequences of characters enclosed in double quotes (" ) or single quotes (').
String Literal Escape Sequences
String literals can contain escape sequences such as \" for quotes or \n for newlines.
Boolean literal
Boolean literals represent truth values and can be either true or false.
Expressions
An expression is any valid combination of literals, variables, operators, and function calls that produces a value. They can not stand alone; they must be part of a larger statement, e.g. as an l-value in assignments or as arguments in function calls.
Expressions in CKSP follow KSP semantics and support a wide range of operations.
Arithmetic Operators
The following operators can be used with integer and real values:
In addition, CKSP supports:
Increment and decrement functions are also available for integers:
Real-Specific Functions
Trigonometric functions:
Bitwise Operators
For integer values:
Bit shifting:
String Concatenation
Strings can be concatenated using the & operator:
Concatenation is evaluated left to right and can be combined with other expressions:
Logical Operators
Logical operators work only in expressions using boolean types or values:
Examples:
Type Conversion
CKSP supports explicit casts with as. The target type follows the expression:
The conversion functions remain available, so value as int and int(value) are equivalent for numeric values. The as form is particularly useful when the target is an object type, since a struct name is not a conversion function.
Cast precedence
A cast binds to the value immediately before as. Use parentheses when a complete binary expression should be converted:
Casting object IDs
Every object pointer is represented by an integer ID in the generated KSP. An object can be converted to that ID, and an integer ID can be given an object type:
This is useful when a low-level operation returns an index into a struct's storage. Generic object types are valid cast targets as well:
A cast does not validate an object ID
id as Note does not construct a Note and does not check whether the corresponding allocation slot is alive. Only cast IDs obtained from a matching object's storage or from a previously converted pointer. Reading fields through an invalid or stale ID can access the wrong storage slot.
Casts do not change reference counts
Evaluating note as int does not release note, and evaluating id as Note does not retain it by itself. Normal object assignment and return handling takes care of ownership when the result is stored as an object pointer. A raw integer ID does not keep an object alive.
When an object-valued cast is used in a string context, CKSP uses the target struct's __repr__ method just as it does for an ordinary object pointer:
A cast in parentheses can be used like any other object reference. Its fields can be read and assigned, and its methods called, without storing the pointer in a variable first:
The parentheses are required: in id as Note.pitch the part after as is read as a type name. Only a member can be assigned this way, not the cast itself, so (id as Note) := other is rejected.
Casts operate on scalar values. Arrays and other composite values cannot be used as the source or target of a cast.
f-Strings (Formatted Strings)
CKSP supports formatted strings, also known as f-strings, for embedding variables or expressions directly into a string literal.
You can also embed expressions:
Function calls are allowed as well: