Builtins Reference
Logos provides a comprehensive set of built-in functions that are always available without any imports. These cover I/O, file operations, string manipulation, math, HTTP, and more.
I/O
Basic input and output functions for terminal interaction.
| Function | Description |
|---|---|
print(value) | Prints value to stdout with newline |
input() | Reads a line from stdin |
prompt(message) | Prints message and reads input |
clear() | Clears the terminal screen |
Example
print("Hello, World!")
let name = prompt("What is your name? ")
print("Hello, " + name)
clear() // Clear the screenColor Output
Functions for colored terminal output. Each returns a formatted string.
| Function | Description |
|---|---|
colorRed(text) | Returns text in red |
colorGreen(text) | Returns text in green |
colorYellow(text) | Returns text in yellow |
colorBlue(text) | Returns text in blue |
colorMagenta(text) | Returns text in magenta |
colorCyan(text) | Returns text in cyan |
colorWhite(text) | Returns text in white |
colorBold(text) | Returns text in bold |
Example
print(colorRed("Error: Something went wrong"))
print(colorGreen("Success!"))
print(colorBold(colorYellow("Warning: Proceed with caution")))Type Functions
Functions for type inspection.
| Function | Description |
|---|---|
type(value) | Returns type name as string ("int", "float", "string", "bool", "array", "table", "function", "null") |
len(value) | Returns length of string, array, or table |
Example
print(type(42)) // "int"
print(type("hello")) // "string"
print(type([1,2,3])) // "array"
print(toStr(len("hello"))) // 5
print(toStr(len([1,2,3]))) // 3File Operations
Comprehensive file system operations. Most file functions return a result object with {ok, value, error} for safe error handling.
| Function | Description |
|---|---|
fileRead(path) | Reads entire file as string |
fileWrite(path, content) | Writes content to file (overwrites) |
fileAppend(path, content) | Appends content to file |
fileExists(path) | Returns true if file exists |
fileDelete(path) | Deletes a file |
fileDeleteAll(path) | Recursively deletes path |
fileRename(old, new) | Renames/moves a file |
fileMkdir(path) | Creates directory (with parents) |
fileRmdir(path) | Removes empty directory |
fileReadDir(path) | Lists directory contents as array |
fileCopy(src, dst) | Copies a file |
fileMove(src, dst) | Moves a file |
fileChmod(path, mode) | Changes file permissions |
fileGlob(pattern) | Returns files matching glob pattern |
fileExt(path) | Returns file extension |
Example
// Write and read files with error handling
let writeRes = fileWrite("data.txt", "Hello, World!")
if !writeRes.ok {
print("Write failed: " + writeRes.error)
}
let readRes = fileRead("data.txt")
if readRes.ok {
print(readRes.value) // Hello, World!
} else {
print("Read failed: " + readRes.error)
}
// Append to file
fileAppend("log.txt", "Log entry\n")
// Check existence (returns bool directly)
if fileExists("config.json") {
let config = fileRead("config.json")
if config.ok {
print(config.value)
}
}
// Directory operations
fileMkdir("output/reports")
let dirRes = fileReadDir(".")
if dirRes.ok {
for file in dirRes.value {
print(file)
}
}
// Glob pattern matching
let globRes = fileGlob("*.lgs")
if globRes.ok {
for script in globRes.value {
print("Found: " + script)
}
}String Operations
String manipulation functions.
| Function | Description |
|---|---|
upper(s) | Converts string to uppercase |
lower(s) | Converts string to lowercase |
trim(s) | Removes leading/trailing whitespace |
replace(s, old, new) | Replaces all occurrences |
split(s, sep) | Splits string by separator, returns array |
join(arr, sep) | Joins array elements with separator |
contains(s, sub) | Checks if string contains substring |
startsWith(s, prefix) | Checks if string starts with prefix |
endsWith(s, suffix) | Checks if string ends with suffix |
indexOf(s, sub) | Returns index of substring (-1 if not found) |
repeat(s, n) | Repeats string n times |
slice(s, start, end) | Returns substring from start to end |
format(template, ...args) | Formats string with placeholders |
Example
print(upper("hello")) // HELLO
print(lower("WORLD")) // world
print(trim(" spaces ")) // "spaces"
let words = split("a,b,c", ",")
print(join(words, "-")) // a-b-c
print(toStr(contains("hello", "ell"))) // true
print(toStr(startsWith("hello", "he"))) // true
print(repeat("ab", 3)) // ababab
print(slice("hello", 1, 4)) // ell
print(format("Hello, {}!", "World")) // Hello, World!Type Conversion
Functions for converting between types.
| Function | Description |
|---|---|
toInt(value) | Converts to integer |
toFloat(value) | Converts to float |
toBool(value) | Converts to boolean |
toStr(value) | Converts to string |
Example
let num = toInt("42")
let pi = toFloat("3.14")
let flag = toBool("true")
let text = toStr(123)
print(type(num)) // int
print(type(pi)) // float
print(type(flag)) // bool
print(type(text)) // stringArray Operations
Functions for array manipulation.
| Function | Description |
|---|---|
push(arr, value) | Adds element to end, returns new array |
pop(arr) | Removes last element, returns new array |
first(arr) | Returns first element |
last(arr) | Returns last element |
tail(arr) | Returns array without first element |
prepend(arr, value) | Adds element to start, returns new array |
reverse(arr) | Returns reversed array |
sort(arr) | Returns sorted array |
Example
let nums = [1, 2, 3]
nums = push(nums, 4) // [1, 2, 3, 4]
nums = prepend(nums, 0) // [0, 1, 2, 3, 4]
print(toStr(first(nums))) // 0
print(toStr(last(nums))) // 4
let rest = tail(nums) // [1, 2, 3, 4]
let rev = reverse(nums) // [4, 3, 2, 1, 0]
let unsorted = [3, 1, 4, 1, 5]
let sorted = sort(unsorted) // [1, 1, 3, 4, 5]Table Operations
Functions for working with tables (key-value maps).
| Function | Description |
|---|---|
keys(table) | Returns array of keys |
values(table) | Returns array of values |
has(table, key) | Checks if key exists |
tableDelete(table, key) | Removes key, returns new table |
merge(table1, table2) | Merges tables, returns new table |
Example
let user = table{
"name": "Alice",
"age": 30,
"role": "admin"
}
let k = keys(user) // ["name", "age", "role"]
let v = values(user) // ["Alice", 30, "admin"]
print(toStr(has(user, "name"))) // true
print(toStr(has(user, "email"))) // false
let updated = tableDelete(user, "age")
let merged = merge(user, table{ "email": "alice@example.com" })JSON
JSON parsing and serialization.
| Function | Description |
|---|---|
parseJson(str) | Parses JSON string to value |
toJson(value) | Converts value to JSON string |
Example
let jsonStr = '{"name": "Bob", "scores": [95, 87, 92]}'
let data = parseJson(jsonStr)
print(data["name"]) // Bob
print(toStr(data["scores"][0])) // 95
let obj = table{ "status": "ok", "count": 42 }
let json = toJson(obj)
print(json) // {"count":42,"status":"ok"}Math
Mathematical functions and constants.
| Function | Description |
|---|---|
mathAbs(n) | Returns absolute value |
mathPow(base, exp) | Returns base raised to exp |
mathSqrt(n) | Returns square root |
mathFloor(n) | Rounds down to nearest integer |
mathCeil(n) | Rounds up to nearest integer |
mathRound(n) | Rounds to nearest integer |
mathMin(a, b) | Returns smaller value |
mathMax(a, b) | Returns larger value |
mathRandom() | Returns random float between 0 and 1 |
mathRandomInt(min, max) | Returns random integer in range |
mathPi() | Returns value of pi |
Example
print(toStr(mathAbs(-42))) // 42
print(toStr(mathPow(2, 10))) // 1024
print(toStr(mathSqrt(16))) // 4
print(toStr(mathFloor(3.7))) // 3
print(toStr(mathCeil(3.2))) // 4
print(toStr(mathRound(3.5))) // 4
print(toStr(mathMin(5, 3))) // 3
print(toStr(mathMax(5, 3))) // 5
let dice = mathRandomInt(1, 6)
print("Rolled: " + toStr(dice))
print(toStr(mathPi())) // 3.141592653589793OS/System
System interaction and process control.
| Function | Description |
|---|---|
pwd() | Returns current working directory |
cd(path) | Changes current directory |
env(name) | Gets environment variable |
setenv(name, value) | Sets environment variable |
args() | Returns command line arguments as array |
exit(code) | Exits with status code |
sleep(ms) | Pauses execution for milliseconds |
osname() | Returns OS name ("linux", "darwin", "windows") |
run(cmd, ...args) | Runs command with args, returns output |
shell(cmd) | Runs command in shell, returns output |
Example
print(pwd()) // /home/user/project
cd("/tmp")
let home = env("HOME")
setenv("MY_VAR", "hello")
let cliArgs = args()
for arg in cliArgs {
print(arg)
}
print(osname()) // linux
// Run commands
let output = run("ls", "-la")
print(output)
let result = shell("echo $HOME && pwd")
print(result)
sleep(1000) // Wait 1 second
exit(0)Time
Time and date functions.
| Function | Description |
|---|---|
timeNow() | Returns current Unix timestamp (seconds) |
timeMs() | Returns current time in milliseconds |
timeStr() | Returns current time as HH:MM:SS |
dateStr() | Returns current date as YYYY-MM-DD |
dateTimeStr() | Returns current datetime |
timeFormat(timestamp, format) | Formats timestamp with Go layout |
Example
print(toStr(timeNow())) // 1709395200
print(toStr(timeMs())) // 1709395200123
print(timeStr()) // 14:30:00
print(dateStr()) // 2024-03-02
print(dateTimeStr()) // 2024-03-02 14:30:00
// Custom formatting (Go time layout)
let ts = timeNow()
print(timeFormat(ts, "Jan 2, 2006")) // Mar 2, 2024HTTP
HTTP client functions for making web requests. All HTTP functions return a result object:
// Result structure:
{
ok: true/false,
value: { body: "...", status: 200 },
error: ""
}| Function | Description |
|---|---|
httpGet(url) | Makes GET request |
httpPost(url, body) | Makes POST request with JSON body |
httpPatch(url, body) | Makes PATCH request with JSON body |
httpDelete(url) | Makes DELETE request |
Example
// GET request with error handling
let res = httpGet("https://api.example.com/users")
if res.ok {
let users = parseJson(res.value.body)
for user in users {
print(user["name"])
}
print("Status: " + toStr(res.value.status))
} else {
print("Request failed: " + res.error)
}
// POST with JSON body
let payload = toJson(table{
"name": "Alice",
"email": "alice@example.com"
})
let postRes = httpPost("https://api.example.com/users", payload)
if postRes.ok {
print("Created! Status: " + toStr(postRes.value.status))
}
// PATCH request
let update = toJson(table{ "name": "Alice Smith" })
let patchRes = httpPatch("https://api.example.com/users/1", update)
// DELETE request
let delRes = httpDelete("https://api.example.com/users/1")
if delRes.ok {
print("Deleted successfully")
}