Standard Library

TinyLang provides 60+ built-in functions organized into 6 modules. All functions are available globally without importing.

Quick Navigation

I/O | Math | Strings | Arrays | Types | Utils

I/O Module

Functions for input and output.

print(...args)

Output values to the console, separated by spaces.

print(value1, value2, ...) → null
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(value1, value2, ...) → null
println("Line 1")
println("Line 2")

input(prompt?)

Read user input from stdin. Optionally display a prompt.

input(prompt?: string) → string
let name = input("Enter your name: ")
print("Hello, " + name)

Math Module

Mathematical operations and constants.

Constants

NameValueDescription
PI3.141592653589793Ratio of circumference to diameter
E2.718281828459045Euler's number
TAU6.2831853071795862 * PI (full circle in radians)
INFINITYInfinityPositive infinity

abs(n)

Returns the absolute value of a number.

abs(n: number) → number
abs(-5)   // 5
abs(3)    // 3
abs(0)    // 0

floor(n)

Rounds down to the nearest integer.

floor(n: number) → number
floor(3.7)   // 3
floor(-2.3)  // -3

ceil(n)

Rounds up to the nearest integer.

ceil(n: number) → number
ceil(3.2)   // 4
ceil(-2.7)  // -2

round(n)

Rounds to the nearest integer.

round(n: number) → number
round(3.5)   // 4
round(3.4)   // 3

sqrt(n)

Returns the square root. Throws an error for negative numbers.

sqrt(n: number) → number
sqrt(16)   // 4
sqrt(2)    // 1.4142135623730951

pow(base, exp)

Returns base raised to the power of exp.

pow(base: number, exp: number) → number
pow(2, 10)  // 1024
pow(3, 3)   // 27

random()

Returns a random number between 0 (inclusive) and 1 (exclusive).

random() → number
let r = random()  // e.g. 0.7234...

randomInt(min, max)

Returns a random integer between min and max (both inclusive).

randomInt(min: number, max: number) → number
randomInt(1, 6)  // e.g. 4 (dice roll)

min(...args)

Returns the smallest value. Accepts multiple numbers or an array.

min(...numbers) → number
min(3, 1, 4)      // 1
min([5, 2, 8])     // 2

max(...args)

Returns the largest value. Accepts multiple numbers or an array.

max(...numbers) → number
max(3, 1, 4)      // 4
max([5, 2, 8])     // 8

sin(n)

Returns the sine of a number (in radians).

sin(n: number) → number
sin(0)         // 0
sin(PI / 2)   // 1

cos(n)

Returns the cosine of a number (in radians).

cos(n: number) → number
cos(0)    // 1
cos(PI)   // -1

tan(n)

Returns the tangent of a number (in radians).

tan(n: number) → number
tan(0)         // 0
tan(PI / 4)   // ~1

log(n)

Returns the natural logarithm (base e). Throws for non-positive numbers.

log(n: number) → number
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(value: string | array | object) → number
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(str: string, delimiter: string) → array
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(arr: array, delimiter: string) → string
join(["a", "b", "c"], ",")  // "a,b,c"
join([1, 2, 3], " - ")     // "1 - 2 - 3"

upper(str)

Converts a string to uppercase.

upper(str: string) → string
upper("hello")  // "HELLO"

lower(str)

Converts a string to lowercase.

lower(str: string) → string
lower("HELLO")  // "hello"

trim(str)

Removes whitespace from both ends of a string.

trim(str: string) → string
trim("  hello  ")  // "hello"

contains(str, substr)

Returns true if the string contains the substring.

contains(str: string, substr: string) → boolean
contains("hello world", "world")  // true
contains("hello", "xyz")          // false

replace(str, pattern, replacement)

Replaces all occurrences of pattern with replacement.

replace(str: string, pattern: string, replacement: string) → string
replace("hello world", "world", "TinyLang")
// "hello TinyLang"

charAt(str, index)

Returns the character at the specified index.

charAt(str: string, index: number) → string
charAt("hello", 0)  // "h"
charAt("hello", 4)  // "o"

startsWith(str, prefix)

Returns true if the string starts with the given prefix.

startsWith(str: string, prefix: string) → boolean
startsWith("hello", "hel")  // true
startsWith("hello", "xyz")  // false

endsWith(str, suffix)

Returns true if the string ends with the given suffix.

endsWith(str: string, suffix: string) → boolean
endsWith("hello", "llo")  // true
endsWith("hello", "xyz")  // false

repeat(str, count)

Repeats a string a specified number of times.

repeat(str: string, count: number) → string
repeat("ha", 3)     // "hahaha"
repeat("-", 10)      // "----------"

padStart(str, length, padChar?)

Pads the string from the start to reach the target length.

padStart(str: string, length: number, padChar?: string) → string
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(str: string, length: number, padChar?: string) → string
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.

push(arr: array, value: any) → number
let arr = [1, 2, 3]
push(arr, 4)  // arr is now [1, 2, 3, 4]

pop(array)

Removes and returns the last element.

pop(arr: array) → any
let arr = [1, 2, 3]
let last = pop(arr)  // last = 3, arr = [1, 2]

shift(array)

Removes and returns the first element.

shift(arr: array) → any
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.

unshift(arr: array, value: any) → number
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.

slice(arr: array, start: number, end?: number) → 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(arr1: array, arr2: array) → array
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(arr: array, value: any) → number
indexOf([10, 20, 30], 20)  // 1
indexOf([10, 20, 30], 99)  // -1

includes(array, value)

Returns true if the array contains the value.

includes(arr: array, value: any) → boolean
includes([1, 2, 3], 2)  // true
includes([1, 2, 3], 9)  // false

reverse(array)

Returns a new array with elements in reversed order.

reverse(arr: array) → array
reverse([1, 2, 3])  // [3, 2, 1]

sort(array)

Returns a new sorted array. Numbers sort numerically; others sort lexicographically.

sort(arr: array) → array
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(arr: array) → array
flatten([[1, 2], [3, 4], [5]])  // [1, 2, 3, 4, 5]

zip(array1, array2)

Combines two arrays into an array of pairs.

zip(arr1: array, arr2: array) → array
zip(["a", "b"], [1, 2])  // [["a", 1], ["b", 2]]

enumerate(array)

Returns an array of [index, value] pairs.

enumerate(arr: array) → array
enumerate(["a", "b", "c"])
// [[0, "a"], [1, "b"], [2, "c"]]

unique(array)

Returns a new array with duplicate values removed.

unique(arr: array) → array
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(value: any) → 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(value: any) → string
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(value: string | boolean | null) → number
num("42")     // 42
num("3.14")   // 3.14
num(true)     // 1
num(false)    // 0

bool(value)

Converts a value to boolean (truthiness check).

bool(value: any) → boolean
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(value: any) → boolean
isNumber(42)       // true
isNumber("42")     // false

isString(value)

Returns true if the value is a string.

isString(value: any) → boolean
isString("hi")    // true
isString(42)      // false

isArray(value)

Returns true if the value is an array.

isArray(value: any) → boolean
isArray([1, 2])   // true
isArray("hi")      // false

isNull(value)

Returns true if the value is null.

isNull(value: any) → boolean
isNull(null)    // true
isNull(0)       // false

isFunction(value)

Returns true if the value is a function.

isFunction(value: any) → boolean
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(end: number) → array range(start: number, end: number, step?: number) → array
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(obj: object) → array
keys({a: 1, b: 2})  // ["a", "b"]

values(object)

Returns an array of the object's values.

values(obj: object) → array
values({a: 1, b: 2})  // [1, 2]

entries(object)

Returns an array of [key, value] pairs.

entries(obj: object) → array
entries({a: 1, b: 2})  // [["a", 1], ["b", 2]]

time()

Returns the current timestamp in milliseconds.

time() → number
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.

clone(value: any) → any
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(condition: any, message?: string) → boolean
assert(1 + 1 == 2)                    // OK
assert(1 + 1 == 3, "Math is broken")  // Error!

format(template, ...args)

String formatting with {} placeholders.

format(template: string, ...args) → string
format("Hello, {}!", "World")
// "Hello, World!"

format("{} + {} = {}", 1, 2, 3)
// "1 + 2 = 3"

sleep(ms)

Pauses execution for the specified milliseconds (educational use).

sleep(ms: number) → null
sleep(1000)  // Wait 1 second

typeof(value)

Alias for type(). Returns the type of a value as a string.

typeof(value: any) → string
typeof(42)  // "number"