Built-in functions and modules
1. Functions
type_str
: fn(all) -> str : return the type of an object in a stringerror
: fn(str, int) -> error : create an error with given message and number indicating its typeclone
: fn(any) -> any: returns a clone of the given objectdoc
: fn(any) -> str: returns the doc string for a given objectprint
: fn(any) -> str: returns the doc string for a given objecteprint
: fn(any) -> str: returns the doc string for a given objectinput_line
: fn(str) -> str: returns the string from the standard input deviceversion
: fn() -> [int]: returns the version as an array of 3 integers, [major, minor, patch]arg
: fn() -> [str]: returns the command line arguments as an array of stringformat
: fn(str, all) -> str: format a stringrun
: fn(str) -> null: run the script in the given filenamerun_once
: fn(str) -> null: run the script in the given filename once over the lifetime of the overall programexit
: fn(int) -> null: exit the program/interpreter with given error codescope_names
: fn() -> [str]: returns the list of names of identifiers visible in the current scopearray
: fn() -> [all]: create an arrayarray_double
: fn() -> [double]: create an array of doublesarray_complex
: fn() -> [complex]: create an array of complexcomplex
: fn(double, double) -> complex: create a complex numberdict
: fn() -> {all:all}: create an empty dictionaryset
: fn() -> {all}: create an empty setrange
: fn(int) -> range: create a range with given upper boundrange
: fn(int, int) -> range: create a range with given lower and upper boundrange
: fn(int, int, int) -> range: create a range with given lower, upper bound and stride
1.1 Array functions
append
: fn([all], all) -> [all]: append an object to an arrayslice
: fn([all], int,int ) -> [all]: return a slice of an arrayslice
: fn([all], range ) -> [all]: return a slice of an array defined by a rangeupdate
: fn([all], int, all ) -> [all]: update an element in the array with a new objectrotate
: fn([all], int ) -> [all]: rotate an array with given amountreverse
: fn([all]) -> [all]: reverse an array, returns a reference to selfsort
: fn([all]) -> bool: sort an array, returns true when sorting was succesfulsort
: fn([all], fn(all,all) -> bool) -> bool: sort an array given provided comparison functionreversed
: fn([all]) -> [all]: reverse a copy of the given array but reversedrotated
: fn([all], int) -> [all]: rotate a copy of the given array by given amountsorted
: fn([all]) -> [all]: provide a copy of the given array but sortedsorted
: fn([all], fn(all,all) -> bool) -> [all]: return a copy of the array sorted by provided comparison functionis_sorted
: fn([all]) -> bool: returns true when given array is sortedis_sorted
: fn([all], fn(all,all) -> bool) -> bool: returns true when the given array is sorted by provided comparison function
1.2 Dictionary functions
values
: fn({all:all}) -> [all]: returns the values of a given dictionarykeys
: fn({all:all}) -> [all]: return the keys of a given dictionary
1.3 String conversion
to_bool
: fn(str) -> bool: interprets the given str and returns the boolean valueto_int
: fn(str) -> int: interpret the given str to an intto_double
: fn(str) -> double: interpret the given str to a double
1.3 I/O functions
open
: fn(str, str) -> io: open the given filename with given mode and return the IO object
2. Module overview
error_type: working with error types
json: serializing and deserializing to json
math: mathematical functions
os: communication with the OS and file system
regex: regular expression
time: working with time
threading: threading support
typing: working with types
3. error_type
Constants:
* undefined_error
: int : unknown error
* type_error
: int : error against the type system
* const_error
: int : error in const correctness
* identifier_not_found
: int : error when trying to find an identifier
* identifier_already_exists
: int : error when trying to define an identifier that is already existant within scope
* value_error
: int : error when trying to produce a value
* key_error
: int : error when assuming a key exist within a set or dictionary and it does not
* index_error
: int : error when trying to index beyond the allowed ranges
* import_error
: int : error when trying to import a module
* syntax_error
: int : error when trying to parse a fragment of source
* os_error
: int : error when trying to open a file or any OS related errors
Example:
import error_type;
try
{
let a : int = 3.0;
}
except ( e )
{
if (e.error_type() == error_type.type_error)
{
print("type error on assignment");
}
else
{
print("unknown error");
}
}
4. json
load
: fn(str) -> all : take a JSON string and convert it into an objectdump
: fn(all) -> str : take an object and dump it into a JSON string
Example:
let d : [all] = [1, 2.0, "three"];
let s = json.load(json.dump(d));
print("s =", s);
let e : {str:all} = {
"a" : 1,
"b" : 2.0,
"c" : "three",
};
let t = json.load(json.dump(e));
print("e =", e);
print("t =", t);
5. math
abs
: fn(double) -> double : take absolute value of doubleacos
: fn(double) -> double: arc cosine of doubleasin
: fn(double) -> double: arc sine of doubleatan
: fn(double) -> double: arc tangent of doublecbrt
: fn(double) -> double: cube root of doublecos
: fn(double) -> double: cosine of doubleerfc
: fn(double) -> double: complementary error function of doubleexp
: fn(double) -> double: e raised to power of given doublelgamma
: fn(double) -> double: natural logarithm of the gamma functionlog
: fn(double) -> double: logarithm of a doublelog10
: fn(double) -> double: logarithm of a double with base 10round
: fn(double) -> double: round to nearest integersin
: fn(double) -> double: sine of a doublesqrt
: fn(double) -> double: square root of a doubletan
: fn(double) -> double: tangent of a doubletgamma
: fn(double) -> double: gamma of a doubletrunc
: fn(double) -> double: truncated value of a doublepow
: fn(double, double) -> double: raise a double by a power given by another double
6. os
absolute
: fn(str) -> str: convert a path defined in a string into an absolute pathcanonical
: fn(str) -> str: convert a path defined in a string into a canonical pathweakly_canonical
: fn(str) -> str: convert a path defined in a string into a weakly canonical pathcurrent_path
: fn() -> str: return the current pathtemp_directory_path
: fn() -> str: return a path to a temporary directoryexists
: fn(str) -> bool: returns true when a given path existscreate_directory
: fn(str) -> bool: create the provided directory, true when succesfulcreate_directories
: fn(str) -> bool: create the provided hierarchical directory path, true when succesfulremove
: fn(str) -> bool: remove the provided path, true when succesfulremove_all
: fn(str) -> bool: remove the provided path including subdirectories, true when succesfulcopy
: fn(str, str) -> null: copy the given file from source to destinationrename
: fn(str, str) -> null: rename the given file from source to destinationlist_dir
: fn(str) -> [str]: list the content of the given directory name and return the list of encountered fileslist_dir_recursively
: fn(str) -> [str]: list the content of the given directory name, iterate through subdirectories and return the list of encountered filesget_env
: fn(str) -> str: returns the value of the given environment variablesystem
: fn(str) -> int: execute the provided command as a system call and return the return code of the process after execution
Example:
6.1 os::path
join
: fn([str]) -> str: join an array of path elements into a single pathroot_name
: fn(str) -> str:root_directory
: fn(str) -> str:root_path
: fn(str) -> str:relative_path
: fn(str) -> str:parent_path
: fn(str) -> str:filename
: fn(str) -> str:stem
: fn(str) -> str:extension
: fn(str) -> str:is_relative
: fn(str) -> bool:is_absolute
: fn(str) -> bool:
Example using the os::path functionality:
import os;
import os::path;
print("Current working directory:", os::current_path());
7. regex
Functions:
regex
: fn() -> regex: create a regex objectmatch
: fn(regex, str) -> <null, [str]>: return the matches of the regex given the stringsearch
: fn(regex, str) -> <null, [str]>: return the found values of the regex given the stringreplace
: fn(regex, str, str) -> str: replace all the occurences in the string provided by the other string using the regex for pattern matching
Constants:
icase
: intnosubs
: intoptimize
: intcollate
: intECMAscript
: intbasic
: intextended
: intawk
: intgrep
: integrep
: int
Example:
import regex;
let re = regex::regex;
print( regex::match(re("[a-z]+\\.txt"), "foo.txt") );
print( regex::match(re("[a-z]+\\.txt"), "bar.dat") );
print( regex::replace(re("a|e|i|o|u"), "Quick brown fox", "[$&]"), "Q[u][i]ck br[o]wn f[o]x" );
8. time
Functions:
time
: fn() -> double: gives the time since the epoch (system dependent).
Example:
import time
import threading
let start_time : double = time.time();
threading.sleep(2.0);
let time_elapsed : double = time.time() - start_time;
print("Time elapsed= ", time_elapsed);
9. threading
thread
: fn( fn() -> all ) -> thread: create a thread object and execute the specified function in itsleep
: fn(double) -> null: sleep the current thread for the given amount of seconds
Example:
import threading;
let f = fn(b : int) {
let val = b;
for (i in [0,1,2,3,4])
{
val = val + i;
threading::sleep(0.01);
}
return val;
};
let threads = [];
for (i in range(4))
{
threads.push_back(threading::thread(f,i));
}
for (thread in threads) {
thread.start();
}
let sum = 0;
for (thread in threads) {
thread.join();
sum += thread.value();
}
print("Sum from all threads=", sum);
10. typing
is_compatible_type_str
: fn(str, str) -> bool: compares two type strings and returns true when the first is compatible with the second one
Example:
import typing;
print("Types are compatible", typing::is_compatible_type_str("double", "<double,int>"));