Standard Library Overview
The Animal language comes with a comprehensive standard library of built-in functions and capabilities that extend the core language. This page provides an overview of the various modules and functions available.
Core Components
The standard library is organized into several categories:
Math Functions - Mathematical operations and utilities
List Functions - Tools for working with and manipulating lists
String Functions - String manipulation and processing
I/O Functions - Reading from and writing to console and files
Audio Functions - Lightweight helpers for loading and controlling sounds
Random Functions - Random number generation and randomization tools
How to Use the Standard Library
All standard library functions are available globally without requiring any import statements. Simply call them in your code:
:: Using math function
result -> max(5, 10)
:: Using list function
items -> [1, 2, 3]
shuffled -> tumble(items)
:: Using I/O function
content -> fetch("data.txt")
Standard Library Reference
Math Functions
Function |
Description |
|---|---|
|
Returns the larger of two numbers |
|
Returns the smaller of two numbers |
|
Returns the absolute value of a number |
|
Converts a number to string in a specified base |
|
Converts a string to a number in a specified base |
See Math Functions for detailed documentation.
List Functions
Function |
Description |
|---|---|
|
Clamps a number between min and max values |
|
Merges two lists or concatenates two strings |
|
Creates a list of n nil elements |
|
Returns all permutations of a list |
|
Flattens a nested list |
|
Finds the index of an item in a list |
|
Repeats element x n times into a list |
|
Creates a running sum of list elements |
|
Creates prefixes of a list |
|
Repeats a value as a string |
|
Returns all indices where item appears |
|
Nests a value to the specified depth |
See List Functions for detailed documentation.
String Functions
Function |
Description |
|---|---|
|
Joins two strings (function form of purr) |
|
Repeats a value as a string |
|
Converts text to uppercase |
|
Converts text to lowercase |
|
Trims leading and trailing occurrences of symbol |
|
Returns a substring from text |
|
Checks whether needle occurs in text |
|
Splits text into a list using delimiter |
|
Joins a list of strings into one string |
See String Functions for detailed documentation.
I/O Functions
Function |
Description |
|---|---|
|
Prints values to the console |
|
Reads a line from the console |
|
Reads a file and returns its contents |
|
Writes content to a file |
|
Appends content to a file |
|
Checks if a file exists |
|
Reads and parses a JSON file |
|
Reads and parses a CSV file |
See Input/Output Functions for detailed documentation.
Audio Functions
Function |
Description |
|---|---|
|
Loads an audio file and returns a handle |
|
Starts playback |
|
Stops playback and rewinds |
|
Sets volume (0.0–1.0) |
|
Enables or disables looping |
|
Seeks to a position in seconds |
|
Returns whether playback is active |
|
Returns the total length |
|
Returns the current playback time |
|
Returns whether looping is enabled |
See Audio Functions for detailed documentation.
Random Functions
Function |
Description |
|---|---|
|
Generates a random integer in range |
|
Returns a random element from a list |
|
Returns a randomly shuffled list |
These functions are included in the Math Functions documentation.
Extending the Standard Library
The Animal language standard library is designed to be extensible. If you’re implementing your own Animal interpreter or contributing to the project, you can add new standard library functions by:
Implementing the function in the appropriate Go file in the
core/std/directoryRegistering the function in
core/std/register.goAdding appropriate tests and documentation
Example of a standard library function implementation:
// In core/std/example.go
package std
import (
"fmt"
)
// AnimalExampleFunction is a standard library function
func AnimalExampleFunction(args []interface{}) interface{} {
if len(args) != 1 {
return fmt.Errorf("example_function expects 1 argument")
}
// Function implementation
// ...
return result
}
// In core/std/register.go
func RegisterStandardLibrary(symbolTable *core.SymbolTable) {
// ...existing registrations...
// Register the new function
symbolTable.Set("example_function", AnimalExampleFunction)
}