Standard Library
TinyLang provides 60+ built-in functions organized into 6 modules. All functions are available globally without importing.
I/O Module
Functions for input and output.
print(...args)
Output values to the console, separated by spaces.
print("Hello") // Hello
print("x =", 42) // x = 42
print(1, 2, 3) // 1 2 3
println(...args)
Same as print - outputs values followed by a newline.
println("Line 1")
println("Line 2")
input(prompt?)
Read user input from stdin. Optionally display a prompt.
let name = input("Enter your name: ")
print("Hello, " + name)
Math Module
Mathematical operations and constants.
Constants
| Name | Value | Description |
|---|---|---|
PI | 3.141592653589793 | Ratio of circumference to diameter |
E | 2.718281828459045 | Euler's number |
TAU | 6.283185307179586 | 2 * PI (full circle in radians) |
INFINITY | Infinity | Positive infinity |
abs(n)
Returns the absolute value of a number.
abs(-5) // 5
abs(3) // 3
abs(0) // 0
floor(n)
Rounds down to the nearest integer.
floor(3.7) // 3
floor(-2.3) // -3
ceil(n)
Rounds up to the nearest integer.
ceil(3.2) // 4
ceil(-2.7) // -2
round(n)
Rounds to the nearest integer.
round(3.5) // 4
round(3.4) // 3
sqrt(n)
Returns the square root. Throws an error for negative numbers.
sqrt(16) // 4
sqrt(2) // 1.4142135623730951
pow(base, exp)
Returns base raised to the power of exp.
pow(2, 10) // 1024
pow(3, 3) // 27
random()
Returns a random number between 0 (inclusive) and 1 (exclusive).
let r = random() // e.g. 0.7234...
randomInt(min, max)
Returns a random integer between min and max (both inclusive).
randomInt(1, 6) // e.g. 4 (dice roll)
min(...args)
Returns the smallest value. Accepts multiple numbers or an array.
min(3, 1, 4) // 1
min([5, 2, 8]) // 2
max(...args)
Returns the largest value. Accepts multiple numbers or an array.
max(3, 1, 4) // 4
max([5, 2, 8]) // 8
sin(n)
Returns the sine of a number (in radians).
sin(0) // 0
sin(PI / 2) // 1
cos(n)
Returns the cosine of a number (in radians).
cos(0) // 1
cos(PI) // -1
tan(n)
Returns the tangent of a number (in radians).
tan(0) // 0
tan(PI / 4) // ~1
log(n)
Returns the natural logarithm (base e). Throws for non-positive numbers.
log(E) // 1
log(10) // 2.302585...
Strings Module
String manipulation functions. These can also be called as methods on string values.
len(value)
Returns the length of a string, array, or object (number of keys).
len("hello") // 5
len([1, 2, 3]) // 3
len({a: 1, b: 2}) // 2
split(str, delimiter)
Splits a string into an array by a delimiter.
split("a,b,c", ",") // ["a", "b", "c"]
split("hello world", " ") // ["hello", "world"]
join(array, delimiter)
Joins array elements into a string with a delimiter.
join(["a", "b", "c"], ",") // "a,b,c"
join([1, 2, 3], " - ") // "1 - 2 - 3"
upper(str)
Converts a string to uppercase.
upper("hello") // "HELLO"
lower(str)
Converts a string to lowercase.
lower("HELLO") // "hello"
trim(str)
Removes whitespace from both ends of a string.
trim(" hello ") // "hello"
contains(str, substr)
Returns true if the string contains the substring.
contains("hello world", "world") // true
contains("hello", "xyz") // false
replace(str, pattern, replacement)
Replaces all occurrences of pattern with replacement.
replace("hello world", "world", "TinyLang")
// "hello TinyLang"
charAt(str, index)
Returns the character at the specified index.
charAt("hello", 0) // "h"
charAt("hello", 4) // "o"
startsWith(str, prefix)
Returns true if the string starts with the given prefix.
startsWith("hello", "hel") // true
startsWith("hello", "xyz") // false
endsWith(str, suffix)
Returns true if the string ends with the given suffix.
endsWith("hello", "llo") // true
endsWith("hello", "xyz") // false
repeat(str, count)
Repeats a string a specified number of times.
repeat("ha", 3) // "hahaha"
repeat("-", 10) // "----------"
padStart(str, length, padChar?)
Pads the string from the start to reach the target length.
padStart("5", 3, "0") // "005"
padStart("hi", 5) // " hi"
padEnd(str, length, padChar?)
Pads the string from the end to reach the target length.
padEnd("hi", 5, ".") // "hi..."
padEnd("hi", 5) // "hi "
Arrays Module
Array manipulation functions. Many also available as methods on array values.
push(array, value)
Adds an element to the end of the array. Returns the new length.
let arr = [1, 2, 3]
push(arr, 4) // arr is now [1, 2, 3, 4]
pop(array)
Removes and returns the last element.
let arr = [1, 2, 3]
let last = pop(arr) // last = 3, arr = [1, 2]
shift(array)
Removes and returns the first element.
let arr = [1, 2, 3]
let first = shift(arr) // first = 1, arr = [2, 3]
unshift(array, value)
Adds an element to the beginning. Returns the new length.
let arr = [2, 3]
unshift(arr, 1) // arr is now [1, 2, 3]
slice(array, start, end?)
Returns a shallow copy of a portion of the array.
let arr = [1, 2, 3, 4, 5]
slice(arr, 1, 3) // [2, 3]
slice(arr, 2) // [3, 4, 5]
concat(array1, array2)
Returns a new array combining both arrays.
concat([1, 2], [3, 4]) // [1, 2, 3, 4]
indexOf(array, value)
Returns the first index of the value, or -1 if not found.
indexOf([10, 20, 30], 20) // 1
indexOf([10, 20, 30], 99) // -1
includes(array, value)
Returns true if the array contains the value.
includes([1, 2, 3], 2) // true
includes([1, 2, 3], 9) // false
reverse(array)
Returns a new array with elements in reversed order.
reverse([1, 2, 3]) // [3, 2, 1]
sort(array)
Returns a new sorted array. Numbers sort numerically; others sort lexicographically.
sort([3, 1, 4, 1, 5]) // [1, 1, 3, 4, 5]
sort(["banana", "apple", "cherry"]) // ["apple", "banana", "cherry"]
flatten(array)
Flattens one level of nesting.
flatten([[1, 2], [3, 4], [5]]) // [1, 2, 3, 4, 5]
zip(array1, array2)
Combines two arrays into an array of pairs.
zip(["a", "b"], [1, 2]) // [["a", 1], ["b", 2]]
enumerate(array)
Returns an array of [index, value] pairs.
enumerate(["a", "b", "c"])
// [[0, "a"], [1, "b"], [2, "c"]]
unique(array)
Returns a new array with duplicate values removed.
unique([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
Types Module
Type checking and conversion functions.
type(value)
Returns the type of a value as a string.
type(42) // "number"
type("hello") // "string"
type(true) // "boolean"
type(null) // "null"
type([1, 2]) // "array"
type({a: 1}) // "object"
type(print) // "function"
str(value)
Converts any value to its string representation.
str(42) // "42"
str(true) // "true"
str([1, 2, 3]) // "[1, 2, 3]"
num(value)
Converts a value to a number. Throws if conversion is not possible.
num("42") // 42
num("3.14") // 3.14
num(true) // 1
num(false) // 0
bool(value)
Converts a value to boolean (truthiness check).
bool(0) // false
bool(1) // true
bool("") // false
bool("hi") // true
bool(null) // false
bool([]) // false
bool([1]) // true
isNumber(value)
Returns true if the value is a number.
isNumber(42) // true
isNumber("42") // false
isString(value)
Returns true if the value is a string.
isString("hi") // true
isString(42) // false
isArray(value)
Returns true if the value is an array.
isArray([1, 2]) // true
isArray("hi") // false
isNull(value)
Returns true if the value is null.
isNull(null) // true
isNull(0) // false
isFunction(value)
Returns true if the value is a function.
isFunction(print) // true
isFunction((x) => x) // true
isFunction(42) // false
Utils Module
General-purpose utility functions.
range(end) / range(start, end, step?)
Generates an array of numbers. Similar to Python's range().
range(5) // [0, 1, 2, 3, 4]
range(2, 6) // [2, 3, 4, 5]
range(0, 10, 2) // [0, 2, 4, 6, 8]
range(10, 0, -2) // [10, 8, 6, 4, 2]
keys(object)
Returns an array of the object's keys.
keys({a: 1, b: 2}) // ["a", "b"]
values(object)
Returns an array of the object's values.
values({a: 1, b: 2}) // [1, 2]
entries(object)
Returns an array of [key, value] pairs.
entries({a: 1, b: 2}) // [["a", 1], ["b", 2]]
time()
Returns the current timestamp in milliseconds.
let start = time()
// ... do work ...
let elapsed = time() - start
print("Took", elapsed, "ms")
clone(value)
Creates a deep copy of a value. Primitives are returned as-is.
let original = [1, [2, 3]]
let copy = clone(original)
push(copy[1], 4)
print(original) // [1, [2, 3]] (unchanged)
print(copy) // [1, [2, 3, 4]]
assert(condition, message?)
Throws an error if the condition is falsy.
assert(1 + 1 == 2) // OK
assert(1 + 1 == 3, "Math is broken") // Error!
format(template, ...args)
String formatting with {} placeholders.
format("Hello, {}!", "World")
// "Hello, World!"
format("{} + {} = {}", 1, 2, 3)
// "1 + 2 = 3"
sleep(ms)
Pauses execution for the specified milliseconds (educational use).
sleep(1000) // Wait 1 second
typeof(value)
Alias for type(). Returns the type of a value as a string.
typeof(42) // "number"