Skip to content

Standard Library

torvik-lang edited this page Jul 30, 2026 · 8 revisions

Standard Library Reference

Torvik provides two layers of built-in functionality:

  1. Core Builtins — always available, no apply needed.
  2. Standard Library (std) — opt-in with apply std;

Core Builtins

Strings

  • len(s) — length of a string (or list)
  • str_concat(a, b, ...) — concatenate two or more strings, left to right (variadic since v1.3.0)
  • substr(s, start, end) — substring
  • char_at(s, i) — the character at i as a 1-character string
  • byte_at(s, i) — the raw byte value at i
  • chr(code) — the 1-character string for a byte code, inverse of byte_at (new in Torvik v1.4.0)
  • trim(s), triml(s), trimr(s)
  • upper(s), lower(s) — case conversion (also usable as weave stages: s ~> upper)
  • replace(s, old, new)
  • contains(s, sub), starts(s, prefix), ends(s, suffix)
  • find(s, sub) — byte index of the first occurrence, or -1
  • split(s, sep) — returns list<str>
  • fmt(...) — string formatting (named or positional; interpolation braces accept full expressions, e.g. fmt("sum={a + b}"))

Conversions

  • toint(s), tofloat(s)
  • tostr(x), int_to_str(n), float_to_str(f)
  • typeof(x) — the type of a variable or literal as a string (e.g. "i64", "str", "list<str>"), resolved at compile time with zero runtime cost

Math

  • abs(n), min(a, b), max(a, b)
  • randint(lo, hi), rand()

Collections

Lists (list<T>):

  • list_new()
  • push(xs, x)
  • list_insert(xs, i, x), list_remove(xs, i)
  • list_pop(xs) — remove and return the last element (typed as the element type)
  • len(xs), xs[i]
  • Element type is inferred from the first push when you omit the annotation (set xs = list_new();)
  • Iterate directly with each x in xs { ... }

Tables (table<K, V>):

  • table_new()
  • table_set(t, k, v), table_get(t, k)
  • table_keys(t) — every key as a sorted list<str>; iterate these and table_get each value (v1.3.0)
  • table_has(t, k), table_del(t, k)

Bags (bag<T>):

  • bag_new(), bag_add(b, x), bag_has(b, x), etc.

Results (result<T>)

Explicit error handling: ok(value), err(msg) / err(code, msg) construct; is_ok(r), is_err(r), unwrap(r), unwrap_or(r, default), err_msg(r), err_code(r) consume. result<i64>, result<str>, and result<f64> are supported, and try_readfile(path), try_toint(s), try_tofloat(s) are result-returning forms of the matching builtins.

Console Input

  • read(), readln(), readint(), readfloat(), readbool() — each also accepts an optional prompt argument, printed before reading (e.g. readint("age: ")); the parsing forms re-prompt until the input is valid
  • readkey() — a single keypress as its raw byte code (i64, no Enter needed); pairs with byte_at
  • readkey_str() — a single keypress as a 1-character string; pairs with char_at
  • readenv(name) — returns "" if the variable is not set

Files & System

  • readfile(path), writefile(path, content), appendline(path, line)
  • fs_exists(path), fs_mkdir(path), fs_remove(path), fs_size(path) (bytes, new in Torvik v1.4.0)
  • exit(code), sys_run(cmd)
  • args(), args_get(i)
  • sleep(ms), clear_screen()

System introspection

All chainable in expressions (e.g. check sys_os_name() == "windows" { ... }):

  • sys_os_name(), sys_os_version(), sys_arch() — strings
  • sys_hostname(), sys_username() — strings
  • sys_cpu_count(), sys_mem_total(), sys_mem_free(), sys_pid() — integers

Time

  • time_ms(), datetime_str(), etc.

Standard Library (apply std;)

Bring in additional helpers with:

apply std;                    // everything
// or a single module:
apply std::math;
apply std::strings;
apply std::list;
apply std::path;
apply std::convert;
apply std::net;

As of Torvik v1.5.0 the standard library lives in its own repository, with the full reference in its docs/STDLIB.md. It still ships with the toolchain and rune update keeps it current — the move means it can version, and make breaking changes, independently of the compiler.

The standard library has its own version, independent of the compiler (shown by torvc --version and rune version). A project can require a minimum with std = "1.2.0" in torvik.rune — a too-old installation is a clean build error pointing at rune update. Because the library versions separately, a future breaking change can be gated behind a new major library version that you opt into deliberately (e.g. std = "2.0.0"); a compiler upgrade never pulls it in automatically — rune update --std-major --yes is how you opt in, and it writes the pin into your manifest for you. A feature marked "new in Torvik v1.4.0" was simply added in that release — it doesn't imply a "std 1.4".

Modules

  • std::math: pow, gcd, lcm, clamp, factorial, is_even, is_odd, sign, isqrt, pow_checked, digit_count, at_least, at_most, ilog2
  • std::strings: join, repeat_str, pad_left, pad_right, str_eq, count_str, reverse_str, capitalize, strip_prefix, strip_suffix, is_digits, is_alpha, center, truncate
  • std::list: range(start, stop), sum(xs), list_max(xs), list_min(xs), sort(xs), sort_str(xs), reverse_list(xs), index_of(xs, v), index_of_str(xs, s), contains_int, contains_str_in, count_int, unique, take, drop, mean
  • std::path: path_base, path_dir, path_ext, path_join (both / and \ recognized on input)
  • std::convert: to_hex, to_bin, from_hex, to_int (base conversions and safe parsing; parsers return result<i64>)
  • std::net (new in Torvik v1.4.0): a minimal HTTP layer — net_listen, net_accept, net_recv, net_send, net_send_file (binary-safe), net_close (transport, provided by the compiler/runtime and dead-stripped when unused), plus http_method, http_path, url_decode, path_is_safe, mime_type, send_text, send_file_response, send_status. Enough to serve a static site or a small localhost API; powers Vefna's vefna serve.

Next Steps

  • See what's on the Roadmap for Torvik.
  • Find out how to Contribute to Torvik.
  • Have any questions? See our FAQ.
  • See the full detailed reference in the repo: docs/STDLIB.md

Clone this wiki locally