builtins
Python's always-available built-ins — no import needed. The compiler auto-includes this module and resolves bare calls like len("x") to builtins::len. (The math-flavored built-ins abs/min/max/round/pow live in the math module.)
import io
io.print(len("purr")) # 4
io.print(chr(65), ord("A")) # A 65
io.print(hex(255)) # 0xff
io.print(int("42") + 1) # 43What's here
Length —
len(any sized container or string).Characters —
ord,chr.Base representations —
hex,oct,bin.Repr —
ascii(printable-ASCII, escaped, single-quoted).Conversions —
bool,int,float(string and numeric overloads).Hashing —
hash.Growable lists —
append(list, x)/xs.append(x)(in-place push).Indexing & slicing —
index(seq, i)(seq[i]; negative indices; a string index yields a length-1 string) andslice(seq, lo, hi)(seq[lo:hi]); the compiler lowersseq[i]/seq[i:j]to these.String predicates —
startswith,endswith,contains(usable as methods:s.startswith("…")).
Every template here is constrained by a concept (e.g. the baseline Value, Sized, convertible_to), so misuse fails with a named error, not template spam.
Scalar-returning built-ins (len/ord/to_bool/hash/…) don't allocate; the string-building ones (chr/hex/oct/bin/ascii) allocate their result, and the string-parsing to_int/to_float allocate a temporary std::string.
Per-function docs (parameters, runtime complexity, heap behavior) are in builtins.hpp. Tested in ../tests/builtins_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).
Classes
Error— A raised error: akindnaming what went wrong and a humanmessage.Finally— Runs its action when the scope ends, however it ends — the body of afinally.
Functions
len · 2 overloads
Length of a C-string / string literal.
Returns the byte length of the view; any embedded NUL bytes are counted (the length comes from the view, not from a terminating NUL).
s | the string. |
its byte length.
O(1).
none.
CheatahBuiltins.LenOrdChrBuiltinsCompileRun.LenStdlibE2E.Builtins SystemApps.EventLog SystemApps.GradeReport …ord · 2 overloads
Code point of the first byte.
Returns the unsigned value of s[0] (0–255), ignoring trailing bytes; an empty string yields 0 rather than throwing.
s | a one-character string. |
its byte value (0 if empty).
O(1).
none.
CheatahBuiltins.LenOrdChrBuiltinsCompileRun.OrdStdlibE2E.BuiltinsCharacter for a code point.
Builds a one-byte string from the low 8 bits of codepoint (it is narrowed to char), so values outside 0–255 wrap modulo 256 rather than producing multi-byte output.
codepoint | a byte value. |
the one-character string.
O(1).
none (1-char small-string optimization).
CheatahBuiltins.LenOrdChrBuiltinsCompileRun.ChrStdlibE2E.BuiltinsHex representation.
Formats value in base 16 with lowercase digits and a 0x prefix; negatives are rendered as a leading - before the prefix (e.g. -0x1f), and 0 is 0x0.
value | the integer. |
"0x…" (with sign).
O(log value).
allocates the result string, built from a temporary digits buffer.
CheatahBuiltins.BaseReprsBuiltinsCompileRun.HexStdlibE2E.BuiltinsOctal representation.
Formats value in base 8 with a 0o prefix; negatives get a leading - before the prefix (e.g. -0o17), and 0 is 0o0.
value | the integer. |
"0o…" (with sign).
O(log value).
allocates the result string, built from a temporary digits buffer.
CheatahBuiltins.BaseReprsBuiltinsCompileRun.OctStdlibE2E.BuiltinsBinary representation.
Formats value in base 2 with a 0b prefix; negatives get a leading - before the prefix (e.g. -0b101), and 0 is 0b0.
value | the integer. |
"0b…" (with sign).
O(log value).
allocates the result string, built from a temporary digits buffer.
CheatahBuiltins.BaseReprsBuiltinsCompileRun.BinStdlibE2E.BuiltinsPrintable-ASCII repr (non-printables/\\endiskip/' escaped, single-quoted).
Wraps s in single quotes, passing through printable ASCII (bytes 32–126) verbatim while escaping \\endiskip and ' as \\/\' and emitting any other byte as a two-digit \xHH hex escape.
s | input. |
the quoted repr.
O(n).
allocates the result string, plus a temporary ostringstream per escaped byte.
CheatahBuiltins.AsciiBuiltinsCompileRun.AsciiStdlibE2E.Builtinsto_bool · 2 overloads
Truthiness of a string.
Truthy iff non-empty; a whitespace-only or "0"/"false" string is still truthy (only emptiness is false).
s | input. |
false iff s is empty.
O(1).
none.
CheatahBuiltins.ConversionsBuiltinsCompileRun.BoolFromStringStdlibE2E.Builtinsto_int · 2 overloads
Parse a base-10 integer.
Parses leading whitespace and an optional sign followed by decimal digits via std::stoll; it stops at the first non-digit (so trailing junk is ignored), throws on no parseable digits, and throws on out-of-range values.
s | the integer string. |
its value (throws on bad input).
O(n).
allocates a temporary std::string for the parse.
CheatahBuiltins.ConversionsBuiltinsCompileRun.IntFromStringStdlibE2E.Builtinsto_float · 2 overloads
Parse a float.
Parses leading whitespace and a floating-point literal via std::stod, accepting decimal, scientific (1e9), inf, and nan forms; it stops at the first unparsed character, throws when nothing parses, and throws on overflow.
s | a floating-point string. |
its value (throws on bad input).
O(n).
allocates a temporary std::string for the parse.
CheatahBuiltins.ConversionsBuiltinsCompileRun.FloatFromStringStdlibE2E.Builtinshash · 2 overloads
Content hash of a string.
Hashes the bytes via std::hash<std::string_view> (equal contents hash equally); the value is implementation-defined and unstable across runs and compilers (do not persist it).
s | input. |
a std::size_t hash.
O(n).
none.
CheatahBuiltins.HashStdlibE2E.BuiltinsNo
: compile-run coverage is intentionally skipped because the hash value is implementation-defined and has no portable expected stdout.operator== · 4 overloads
Compare an error against a string — by MESSAGE, so e == "boom" reads the way it did when a handler bound a bare string.
Compare e.kind() when you mean the kind.
e | the error. |
s | the message to compare against. |
true when the error's message is exactly s.
O(min(len)).
none.
Stream an error as its MESSAGE — the kind would be noise in output that wanted the sentence.
os | the destination stream. |
e | the error. |
os, for chaining.
O(message).
none beyond the stream's own.
The error currently being handled, normalized to an Error.
Called from inside a catch (...), where throw; re-raises the in-flight exception so it can be inspected by type. This is what lets ONE handler shape cover a raised Error, a std::exception from any C++ library, and a throw of some type we have never heard of — the last of which used to travel straight past every handler and abort the process.
the in-flight exception as an Error: a raised Error verbatim, a std::out_of_range as kind "index", a std::domain_error as "arithmetic", any other std::exception as "error", and anything else as "unknown".
O(1) plus the message copy.
copies the kind and message.
PurrcPipeline.CompilesAndRunsTryExceptRaiseBuild a scope guard around f — how finally { … } lowers.
f | the callable to run when the enclosing scope ends. |
the guard; keep it alive for the scope you want covered.
O(1).
moves f into the returned guard.
str · 5 overloads
Python str(): stringify any streamable value (an always-available built-in, so it needs no import — bare str(x) resolves here, like int()/float()/bool()).
Renders value via its operator<< into a fresh ostringstream, so the text matches whatever that stream insertion produces (e.g. default 6-significant-digit float precision), agreeing with io.print/io.str.
value | the value to render. |
value formatted as text.
O(n) in the output length.
allocates the result string (via an ostringstream).
CheatahBuiltins.StrBuiltinsCompileRun.StrStdlibE2E.BuiltinsTrue division — the cheatah / operator (like Python 3): always floating-point, so 6 / 2 is 3.0, not 3, and integer operands never silently truncate.
Use the // operator (floordiv) when you want integer/floor division.
a | numerator. |
b | denominator. |
double(a) / double(b).
O(1).
none.
CheatahBuiltins.DivisionBuiltinsCompileRun.TrueDivisionStdlibE2E.Builtinsfloordiv · 2 overloads
Floor division — the cheatah // operator (like Python): the quotient floored toward −∞.
Integer operands give an integer (7 // 2 == 3, -7 // 2 == -4, flooring the way Python does, not truncating toward zero like raw C++); a floating operand gives a floored double (7.0 // 2 == 3.0).
a | numerator. |
b | denominator; |
floor(a / b), integral for integral operands.
O(1).
none.
BuiltinsCompileRun.FloorDivisionStdlibE2E.BuiltinsAppend x to list v in place (Python list.append).
Grows v by one, converting x to the list's element type. Usable as a method (xs.append(x)) or a bare call (append(xs, x)); the list is taken by reference, so the caller's list is mutated.
v | the list to grow. |
x | the value to append. |
amortized O(1).
reallocates v when it outgrows its capacity.
CheatahBuiltins.AppendLangFeatures.AppendAndDictMutationStdlibE2E.BuiltinsWhether s begins with prefix (Python str.startswith).
s | the string. |
prefix | the prefix to test. |
true iff s starts with prefix.
O(len(prefix)).
none.
CheatahBuiltins.StringPredicatesLangFeatures.MethodPredicatesStdlibE2E.BuiltinsWhether s ends with suffix (Python str.endswith).
s | the string. |
suffix | the suffix to test. |
true iff s ends with suffix.
O(len(suffix)).
none.
CheatahBuiltins.StringPredicatesLangFeatures.MethodPredicatesStdlibE2E.Builtinscontains · 3 overloads
Whether sub occurs anywhere in s (Python sub in s).
s | the string to search. |
sub | the substring to find. |
true iff sub is a substring of s.
O(len(s) · len(sub)) worst case.
none.
CheatahBuiltins.StringPredicatesLangFeatures.MethodPredicatesStdlibE2E.Builtinsmod · 2 overloads
Python FLOOR-mod for integers: the result takes the DIVISOR's sign (-7 % 3 == 2), unlike raw C++ %.
Backs the % operator.
a | the dividend. |
b | the divisor; |
a mod b with the sign of b (Python floor-mod semantics).
O(1).
none.
LangFeatures.Moduloindex · 7 overloads
Element at i of a string — a length-1 string (Python s[i]).
Negative i counts from the end; out-of-range throws std::out_of_range.
s | the string. |
i | the index (may be negative). |
the one-character string at i.
O(1).
none (1-char small-string optimization).
CheatahBuiltins.IndexStringLangFeatures.StringSlicingAndIndexStdlibE2E.Builtinsslice · 2 overloads
Substring s[lo:hi] (Python slice semantics: clamped, negatives from the end).
s | the string. |
lo | start index (default 0 at the call site). |
hi | end index, or slice_end for "to the end". |
the slice (empty if lo >= hi after clamping).
O(hi-lo).
the result string.
CheatahBuiltins.SliceStringLangFeatures.StringSlicingAndIndexStdlibE2E.BuiltinsConstants & variables
Conventional kinds raised from the language core. Libraries are free to define their own.
raise "msg" — unclassified
subscript out of range
dict key absent
divide / modulo by zero
a throw of a type we cannot inspect
Sentinel for an omitted slice upper bound (s[a:]): "to the end".
