Trine's library
Some of Trine's functions are written in Trine itself (src/std/*.tr), so they behave the same in every engine by construction. Nothing needs importing: a program that uses one of a group's names gets the whole group added. A program that also defines one of the group's names is an error (rename yours), except that a program defining every name it uses keeps its own (its own enumerate wins). trine caps, trine doc and trine lint leave library functions out; a message about library code names its file, <library>/json.tr.
JSON (json.tr)
type Json = JNull | JBool(Bool) | JNum(Float) | JStr(Str) | JArr(List[Json]) | JObj(Map[Str, Json])
parse_json(text: Str) -> Result[Json, Str] # errors: "line 2, column 8: expected ':'"
to_json(j: Json) -> Str # compact: {"a":[1,2]}
to_json_pretty(j: Json) -> Str # two-space indentation
json_get(j, key) -> Option[Json] # a field of an object
json_at(j, i) -> Option[Json] # an element of an array
json_str(j) -> Option[Str] json_num(j) -> Option[Float] json_int(j) -> Option[Int]
json_bool(j) -> Option[Bool] json_list(j) -> Option[List[Json]]
- Numbers are Floats (as in JavaScript): integers up to 2^53 are exact and print without a fraction (
36, not36.0);json_intgivesNonefor a fraction or a bigger number. - Objects keep their fields in order (a Map does); a repeated field keeps the last value.
- Strings: every escape (
\n,๐, ...); a lone surrogate becomes U+FFFD. Output escapes",\, and control characters. - Nesting deeper than 500 levels is an error rather than a crash.
let j = parse_json(text)?
let name = json_str(json_get(j, "name") ?? JNull) ?? "anonymous"
print(to_json_pretty(JObj({"name": JStr(name), "tags": JArr([JStr(t) for t in tags])})))
Time (time.tr)
Timestamps are Floats: seconds since 1970-01-01 00:00 UTC, as now(sys) gives. Everything is UTC (local time zones need the operating system's tables, which would make results differ by machine).
time_parts(t) -> (year, month, day, hour, minute, second) # Ints; month 1..12
make_time(year, month, day, hour, minute, second) -> Float
weekday(t) -> Int # 0 = Monday ... 6 = Sunday
format_time(t, fmt) -> Str
parse_time(text, fmt) -> Option[Float] # None unless text matches fmt exactly
Format codes: %Y year, %y two digits, %m month, %d day, %e day padded with a space, %H hour, %I hour 1-12, %p AM/PM, %M minute, %S second, %f microseconds, %j day of the year, %a/%A weekday name, %b/%B month name, %F = %Y-%m-%d, %T = %H:%M:%S, %s seconds since 1970, %z +0000 (parsing: Z, +hhmm or +hh:mm), %Z UTC, %%. Dates use the proleptic Gregorian calendar (years 1 to 9999 round-trip). Parsing checks the date (2026-02-29 is None).
print(format_time(now(sys), "%a %d %b %Y %H:%M")) # Mon 30 Nov 2026 22:10
let t = parse_time("30/Nov/2026:13:00:00 +0100", "%d/%b/%Y:%H:%M:%S %z")
Files, input and HTTP (io.tr)
read_lines(d: Dir, name) -> Result[List[Str], Str] # a file's lines, without line endings
each_line(r: Lines) yields Str # the rest of a file opened with open_lines
stdin_lines(sys) yields Str # standard input, a line at a time
http_get(sys, url) -> Result[Str, Str] # the body of a 2xx response
http_post(sys, url, body, content_type) -> Result[Str, Str]
for line in stdin_lines(sys):
if "ERROR" in line: errors += 1
Python objects (python.tr, 37b)
PyObj (a handle to an object in the Python process py_start(sys) started), py_import, py_get, py_invoke, py_invoke_kw, py_method, py_index, py_value, py_ref, py_json, py_text, py_free: written in Trine over py_call and the JSON library. See docs/PYTHON.md.
Lists (enumerate.tr, zip.tr)
enumerate(xs) is [(0, x0), (1, x1), ...]; zip(xs, ys) pairs elements up to the shorter list.
Examples
examples/json_library.tr, examples/time_library.tr, and three small tools in examples/tools/: a to-do list kept as JSON (todo.tr), an access-log analyzer (log_analyzer.tr) and an HTTP fetcher (fetch.tr). tests/golden.rs runs the tools with grants, input and a local server on every engine.