← Architecture | Built-ins | Examples | Contributing →
Reference for all built-in functions available in Carv.
Prints arguments to stdout, space-separated, without a trailing newline.
print("hello", "world"); // hello world
print(1, 2, 3); // 1 2 3
Prints arguments to stdout, space-separated, with a trailing newline.
Convert any value to its string representation.
str(42) // "42"
str(true) // "true"
str([1,2,3]) // "[1, 2, 3]"
Convert to integer. Works with floats and booleans.
int(3.14) // 3
int(true) // 1
int(false) // 0
Convert to float.
float(42) // 42.0
Get the type of a value as a string.
type_of(42) // "INTEGER"
type_of("hello") // "STRING"
type_of([1,2,3]) // "ARRAY"
Get length of string or array.
len("hello") // 5
len([1, 2, 3]) // 3
Return new array with item appended.
let a = [1, 2];
let b = push(a, 3); // [1, 2, 3]
// a is still [1, 2]
Get first element of array.
head([1, 2, 3]) // 1
head([]) // nil
Get all elements except the first.
tail([1, 2, 3]) // [2, 3]
tail([1]) // []
Get all keys from a map.
let m = {"a": 1, "b": 2};
keys(m) // ["a", "b"]
Get all values from a map.
let m = {"a": 1, "b": 2};
values(m) // [1, 2]
Check if key exists in map.
let m = {"a": 1};
has_key(m, "a") // true
has_key(m, "b") // false
Return new map with key set to value.
let m = {"a": 1};
let m2 = set(m, "b", 2); // {"a": 1, "b": 2}
Return new map with key removed.
let m = {"a": 1, "b": 2};
let m2 = delete(m, "a"); // {"b": 2}
Split string into array.
split("a,b,c", ",") // ["a", "b", "c"]
Join array into string.
join(["a", "b", "c"], "-") // "a-b-c"
Remove leading/trailing whitespace.
trim(" hello ") // "hello"
Get substring. End is optional.
substr("hello", 0, 2) // "he"
substr("hello", 2) // "llo"
Check if string contains substring.
contains("hello", "ell") // true
Check if string starts with prefix.
starts_with("hello", "he") // true
Check if string ends with suffix.
ends_with("hello", "lo") // true
Replace all occurrences.
replace("hello", "l", "L") // "heLLo"
Find index of substring. Returns -1 if not found.
index_of("hello", "l") // 2
index_of("hello", "x") // -1
Convert to uppercase.
to_upper("hello") // "HELLO"
Convert to lowercase.
to_lower("HELLO") // "hello"
Get ASCII code of character.
ord('A') // 65
ord("A") // 65 (takes first char)
Get character from ASCII code.
chr(65) // 'A'
Get character at index.
char_at("hello", 1) // 'e'
Parse a string as an integer.
parse_int("42") // 42
parse_int("-10") // -10
Parse a string as a float.
parse_float("3.14") // 3.14
parse_float("2.0") // 2
Get command-line arguments passed to the script.
let a = args();
print(a); // ["arg1", "arg2", ...]
Run an external command. Returns the exit code.
let code = exec("echo", "hello"); // prints "hello", returns 0
Run an external command and capture output. Returns Ok(stdout) or Err(stderr).
let result = exec_output("echo", "hello");
match result {
Ok(out) => print(trim(out)),
Err(e) => print("failed: " + e),
}
Get an environment variable. Returns empty string if not set.
let home = getenv("HOME");
Set an environment variable.
setenv("MY_VAR", "hello");
Read entire file contents.
let content = read_file("data.txt");
Write string to file.
write_file("out.txt", "hello");
Append string to file. Creates the file if it doesn't exist.
append_file("log.txt", "new line\n");
Check if file exists.
if file_exists("config.txt") {
// ...
}
Create a directory (and parent directories).
mkdir("build/output");
Delete a file.
remove_file("old.txt");
Rename or move a file.
rename_file("old.txt", "new.txt");
Read directory entries and return sorted file/directory names.
let entries = read_dir(".");
println(entries);
Get current working directory.
println(cwd());
Use these via the built-in net or web module:
require "net" as net;
net and web currently expose the same low-level TCP primitives.
Create a TCP listener and return a listener handle.
let listener = net.tcp_listen("127.0.0.1", 8080);
Accept one incoming TCP connection and return a connection handle.
let conn = net.tcp_accept(listener);
Read up to max_bytes bytes from a TCP connection.
let request = net.tcp_read(conn, 4096);
Write string data to a TCP connection. Returns number of bytes written.
let wrote = net.tcp_write(conn, "hello");
Close a listener or connection handle.
net.tcp_close(conn);
net.tcp_close(listener);
Exit program with optional status code.
exit(); // exit with 0
exit(1); // exit with 1
Crash with error message.
panic("something went wrong");
← Architecture | Built-ins | Examples | Contributing →