Conghao97's picture tcftrees's picture
ProgramGen release
d086025
|
Raw History Blame Contribute Delete
2.44 kB

Standard library

Modules are loaded with import and used through their name (for example math.sqrt(...)). Built-in functions listed below need no import.


Built-in functions

Function Purpose
show(...) Print values without a trailing newline.
showln(...) Print values as a line.
format(template, ...args) Format values with {} placeholders and style options such as {:.2f}, {:>6}.
typeof(value) Runtime type name, or class name for instances.
str(v), int(v), float(v), bool(v) Convert between types.
len(collection) Length of a list, map or string.
sleep(seconds) Pause execution.
argv Command-line arguments passed after the script name.

Methods available on values: list.push(v), list.map(f), list.filter(f), list.reduce(f, init), list.sort(), map.set(key, value), str.trim() / .upper() / .lower() / .split(sep) / .contains(sub) / .replace(old, new).


Modules

math

Numeric and math utilities.

import math

math.sqrt(144)        // 12
math.abs(-7)          // 7
math.floor(9.8)       // 9
math.range(5)         // [0, 1, 2, 3, 4]
math.range(1, 20)     // 1..19
PI                    // constant, also available unqualified

json

import json

text = json.encode({ name: "Ember", level: 42 })
data = json.decode(text)
showln(data.name, data.level)

file and path

Read, write and inspect files, and manipulate path strings (join, split, extension, existence checks).

os

Environment variables and system information.

time

Dates, timers and sleeping.

net

HTTP client and server utilities.

crypto

Hashing (SHA-2, MD5), HMAC, PBKDF2 and AES-GCM encryption.

zip

Create and extract ZIP archives.

xml

Parse XML documents.

uuid

Generate UUIDs.

pdf, font

Render PDF documents and font/text output.

thread

import thread

thread.spawn(worker, [1])   // run worker(1) concurrently

ffi, struct, mem

Native interop with C libraries: call into shared objects (ffi), describe C data layouts (struct), and work with raw memory (mem).


Importing your own modules

Scripts can include other .dgn files; when a script is compiled with dragon -c, everything it imports is packaged into the produced executable, so the result runs standalone on another machine.