Core functions are available globally without importing a module. They cover common operations such as output, type inspection, protected calls, loading code, and working with object attributes.

print

function core

Signature

function print(...)

Description

The print function is a global function in Teascript that is used to output values to the console. If no arguments are provided, it prints a new line \n. If arguments are provided, it prints each value separated by a tab \t.

Parameters

...
the values to be printed. These can be of any Tea type.

Returns

Always nil.

Example

print()
print(1, 2, 3);                 // 1    2   3
print(true, 789.98, "hello");   // true 789.98  "hello"

print("Hello, world!")          // "Hello, world!"

var x = 5
var y = 10
print(x, y)                     // "5   10"

input

function core

Signature

function input(prompt='')

Description

The input function reads a line of text from the standard input stream of the console, and returns it as a string.

Parameters

prompt
an optional string value, defaults as an empty string "".

Returns

The text entered by the user as a Tea string.

Example

var name = input("Enter your name: ")
print("Hello, " + name + "!")

assert

function core

Signature

function assert(condition, message=nil)

Description

The assert function is used to check whether a given condition is true, and if is not, it raises an error with an optional error message.

Parameters

condition
a boolean value or an expression to be evaluated based on its truthyness.
message
an optional string to be printed as the error message if the condition arguments results false.

Returns

nil when the condition results true. It does not return if the condition is false, raising an error instead.

Example

assert(5 > 3, "5 is not greater than 3")
assert([1, 2, 3].len == 3, "The list does not have 3 elements")

error

function core

Signature

function error(message)

Description

The error function is used to raise an error with a specified string message. It is similar to the assert function, but error always raises an error.

Parameters

message
A string to be used as the error message.

Errors

Raises an error using the specified string message.

Example

function divide(x, y)
{
    if(y == 0)
        error("Cannot divide by zero")
    return x / y
}

divide(5, 0)  // Raises an error with message "Cannot divide by zero"

typeof

function core

Signature

function typeof(value)

Description

The typeof function is used to determine the type of a given value.

Parameters

value
The Tea value whose type is to be determined.

Returns

A string containing the name of the value type.

Example

print(typeof(5))  // "number"
print(typeof("hello"))  // "string"
print(typeof([1, 2, 3]))  // "list"
print(typeof({a = 1, b = 2}))  // "map"
print(type(1..10))  // "range"

tostring

function core

Signature

function tostring(value)

Description

The tostring function converts any value into its string representation. It respects an instance or userdata tostring() special method if one is defined, allowing custom formatting for these objects.

Parameters

value
The Tea value to convert to a string

Returns

A string representation of value. For plain values, the result is a readable form suitable for display or concatenation.

Example

print(tostring(123))          // "123"
print(tostring(true))         // "true"
print(tostring([1, 2, 3]))    // "[1, 2, 3]"
print(tostring({a = 1}))      // "{a = 1}"

tonumber

function core

Signature

function tonumber(value, base=10)

Description

The function tonumber converts a given Tea value into a number, or returns nil if is unable to convert it. When base is 10 (default), any value that can be coerced into a number is converted directly. For other bases (between 2 and 36), the input must be a string, which is interpreted as a number in that base.

Parameters

value
The Tea value to coerce into a number.
base
An optional number specifying the base for string parsing. Defaults to 10

Returns

The parsed number, or nil if the conversion fails.

Example

print(tonumber("123"))      // 123
print(tonumber("123.45"))   // 123.45
print(tonumber(42))         // 42
print(tonumber("ff", 16))   // 255
print(tonumber("101", 2))   // 5
print(tonumber("xyz"))      // nil

gc

function core

Signature

function gc()

Description

The gc function is used to invoke the Teascript garbage collector, which is responsible for freeing up memory that is no longer being used by the Tea program.

Returns

The number of Kbytes being collected. This is expressed as: number of bytes / 2 ** 10

Example

const collected = gc()
print('Collected ', collected, 'Kib')

eval

function core

Signature

function eval(source)

Description

The eval function compiles and executes then given source string as Tea code in the current environment, returning any result produced by the evaluated chunk.

Parameters

source
A string containing Tea source code to compile and run

Returns

The value produced by the evaluated code (if any). If code raises an error, the error is propagated to the caller and can be caught with pcall.

Example

print(eval("1 + 2 * 3"))     // 7
eval("var greeting = 'hi'")
print(greeting)              // "hi"

dump

function core

Signature

function dump(func, strip=false)

Description

The dump function serializes a Tea function into bytecode, returning the compiled representation as a string. The result can later be loaded with loadstring to reconstruct the function.

Parameters

func
The function to serialize. Must be a Tea function (not a C function)
strip
An optional boolean (or string containing the "s"). When truthy, debug information is stripped from the output, producing smaller bytecode.

Returns

A Tea string containing the serialized bytecode.

Example

function greet(name)
{
    return "Hello, " + name
}

var bytecode = dump(greet)
print(typeof(bytecode))          // "string"

var reloaded = loadstring(bytecode)
print(reloaded("world"))         // "Hello, world"

loadfile

function core

Signature

function loadfile(filename, mode='t')

Description

The loadfile function loads a Tea chunk from the specified file and returns it as a callable function, without executing it.

Parameters

filename
The path of the file to load
mode
An optional string specifying the accepted input modes: "t" for text source, "b" for binary bytecode, or "bt" for both. Defaults to "t".

Returns

A Tea function representing the compiled chunk. If the file cannot be read or compiled, an error is raised.

Example

var chunk = loadfile("script.tea")
chunk()

loadstring

function core

Signature

function loadstring(source, name='?<load>')

Description

The loadstring function compiles the given Tea source string into a callable Tea function without executing it. The optional name argument is used as the chunk name in error messages and stack traces.

Parameters

source
A string containing Tea source code
name
An optional module environment name. Defaults to "?<load>".

Returns

A Tea function representing the compiled chunk. If compilation fails, an error is raised.

Example

var chunk = loadstring("print(6 * 7)")
print(chunk())               // 42

pcall

function core

Signature

function pcall(func, ...)

Description

The pcall function calls func in protected mode, catching any error that occurs during execution. This is the primary mechanism for handling runtime errors in Tea code.

Parameters

func
The Tea function to call.
...
Any arguments to be passed to func

Returns

The function returns a two-element list. The first element is a boolean indicating whether the call succeeded. The second element is the function’s return value on success, or the error message on failure.

Example

var result = pcall(function() error("something went wrong") end)
print(result[0])             // false
print(result[1])             // "something went wrong"

var ok, res = pcall(function(a, b) return a + b end, 2, 3)
print(ok, res)          // true 5

rawequal

function core

Signature

function rawequal(a, b)

Description

The rawequal function compares two Tea values for equality without invoking any special method. Unlike the == operator, which may call operator == if defined, this performs a direct, primitive comparison.

Parameters

a
The first (left) value to compare
b
The second (right) value to compare

Returns

true if the two values are primitively equal; false otherwise.

Example

print(rawequal(1, 1))        // true
print(rawequal("a", "a"))    // true
print(rawequal([1], [1]))    // false (distinct list objects)

hasattr

function core

Signature

function hasattr(obj, name)

Description

The hasattr function checks whether the given object has an attribute with the specified name, consulting the object’s attributes and any applicable method.

Parameters

obj
The object to inspect.
name
A string containing the attribute name to look up.

Returns

true if the attribute exists; false otherwise.

Example

var point = {x = 10, y = 20}
print(hasattr(point, "x"))       // true
print(hasattr(point, "z"))       // false

getattr

function core

Signature

function getattr(obj, name)

Description

The getattr function retrieves the value of the named attribute from the given object, honoring special methods such as getters and getattr().

Parameters

obj
The object to read from.
name
A string containing the attribute name to retrieve.

Returns

The value of the attribute. If the attribute does not exist, an error is raised.

Example

var point = {x = 10, y = 20}
print(getattr(point, "x"))       // 10
print(getattr(point, "z"))       // error: attribute "z" not found

setattr

function core

Signature

function setattr(obj, name, value)

Description

The setattr function assigns a value to the named attribute of the given object, honoring special methods such as setters and setattr().

Parameters

obj
The object to modify.
name
A string containing the attribute name to set.
value
The value to assign

Returns

nil

Example

var point = {x = 10, y = 20}
setattr(point, "x", 99)
print(point.x)                   // 99

setattr(point, "z", 5)
print(point.z)                   // 5

delattr

function core

Signature

function delattr(obj, name)

Description

The delattr function removes the named attribute from the given object.

Parameters

obj
The object from which to remove the attribute.
name
A string containing the attribute name to delete.

Returns

nil

Example

var point = {x = 10, y = 20}
delattr(point, "y")
print(hasattr(point, "y"))       // false