System interfaces

Table of contents

  1. Module time — time measurement
  2. Module sys — runtime system interface
  3. Module sys.stackless — avoid the hardware callstack
  4. Module fs — file system interface

Module time

Time measurement, pausing threads.

sleep(x)
Halt execution and continue after x seconds. The number x can be a float and thus fractional parts of a second are possible.
clock()
Start a new stop watch that returns time in seconds.
c = time.clock()
time.sleep(0.1)
print(c())
time()
Obtain Gregorian calendar date and UTC time.
# Format (each of type integer):
  [year, month, day, hour, minute, second]
# Example:
template = "{4(0)}-{2(0)}-{2(0)}T{2(0)}:{2(0)}:{2(0)}"
print(template % time.time())

Module sys

Interface to the runtime system.

argv
The list of command-line arguments.
exit(n)
Exit the program with integer return value n. Zero means success, other values indicate an error.
path
The list of search paths for module loading, analogous to the environment variable PATH. This list may be changed freely and may contain relative paths. But note that relative paths are problematic: they becomde invalid if the current working directory changes. That path[0] should be the absolute path of the directory where argv[0] is found.

Note: the list itself must be changed. Replacing sys.path with another list will not work.
call(n,main,*argv)
Call main with a new call stack of size n. This is used to obtain unlimited recursion depth.
use sys: call

f = |n| 0 if n==0 else f(n-1)+1

function main()
   print(f(10^5))
end

call(10^6,main)
cmd(command,argv)
Call the command with the specified argument list, the output directed to stdout. Permission denied if not in unsafe mode. Returns null if the process was successful, otherwise an integer.
cmd("sh",["-c","GET en.wikipedia.org > /tmp/0"])
s = read("/tmp/0")
eprint(*a), eput(*a)
Like print(*a) and put(*a), but print to stderr instead of stdout.
istable(x)
True if x is a table object, else false.
isclass(x)
True if x is a class object, else false.
id(x)
Memory address of a pointer object. Value null for a non-pointer object. The only non-pointer objects are null, empty and objects of type Bool, Int, Float, Complex. Note that they are considered non-pointer, even if internally stored behind a pointer. Note that the address is not stored as an integer, because an integer cannot hold an u64 value.
main()
Request whether the current module is executed as the main program or not. Returns a boolean value.

Module sys.stackless

This modules patches functions (that take callbacks) with Moss implementations to dispense with the hardware callstack. It should be used together with sys.call to enable unlimited recursion depth.

The following functions are patched:

List.map, List.filter,
Iterable.map, Iterable.filter, Iterable.all, Iterable.any,
Iterable.count, Iterable.reduce

Module fs

is_dir(path)
Return true if path leads to a directory.
is_file(path)
Return true if path leads to a file.
ls(path=".")
Return the list of nodes within that directory, but omit "." and "..".
wd()
Return the current working directory.
cd(path)
Change the current working directory.
open(path)
Open a binary file and return a file object of type File.
Type File, f: File
f.read()
Read the whole file and return it as a byte list.
f.read(n)
Read at most n bytes and return them as a byte list.