Standard Library
Built-in functions are called like any function. Those that produce a value are used inside expressions; the others are statements. The complete table with argument lists is in the Built-in Functions reference.
Console
print(value), println(value), inputInt(), inputStr(), see Input & Output.
Conversion
n = str2int('42') # 42
f = str2float('3.5') # 3.5
s = int2str(42) # '42'
t = float2str(3.5) # '3.500000'
str2intstops the program withFunction errorif the text is not an integer (str2int('abc')).str2floatreturns0for text that is not a number.int2strneeds an integer; a float argument is aFunction error.float2straccepts an integer or a float.
Strings
strlen('hello') # 5
substr('hello world', 6, 5) # 'world' (text, start, length)
strfind('hello world', 'world') # 6, or -1 when absent
strcase('Hello', 1) # 'HELLO' (non-zero: upper, 0: lower)
trim(' hi ') # 'hi'
substr returns an empty string when start is past the end.
Random numbers
randomSeed(42)
r = random() # float in [0, 1)
dice = randomRange(1, 6) # integer from 1 to 6, inclusive
Files
assertCapability('FS')
h = openFile('log.txt')
writeFile('line one\n', h)
writeFile('line two\n', h)
println(readFile(h))
closeFile(h)
openFile(path)opens the file for reading and writing, creating it if it does not exist. Existing contents are kept. It returns a handle.writeFile(text, handle)appendstextto the end of the file and flushes it.readFile(handle)returns the whole file, from the start.closeFile(handle)closes the handle. Always close files you open.
HTTP
assertCapability('HTTP')
body = ''
status = httpRequest('GET', 'http://example.com/', '', '', &body)
println(status)
println(body)
httpRequest(method, url, headers, body, &response):
methodisGET,POST,PUTorDELETE.headersis a string ofName: valuelines separated by\n(empty for none). AContent-Typeheader sets the type of the request body.bodyis the request body (empty for GET).&responseis a reference to the variable that receives the response body.- It returns the HTTP status code, or
-1if the request could not be made, in which case the response variable holds an error message.
Capabilities
assertCapability(name), see Capabilities.
Errors
If a built-in cannot do its job (wrong argument type, unknown handle…), the program prints Function error followed by a message and stops.