Modules
Modules are collections of operations exposed to the language by the host program.
Definition
In the Rust implementation, system calls implement the Module
trait and are exposed through the Suzu::module()
method.
let mut program = Suzu::new();
program.module("print", Print{});
Initialization
Initialization of system calls is performed before the start of program execution.
System calls are not automatically reset between executions, so initialization should be defined for any stateful implementation.
Methods
Methods map identifier names to call identifiers.
fn method(&self, name:&str) -> Option<usize>
{
match name {
"hex" => Some(1),
_ => None,
}
}
Parameters
The parameter list is used in type matching against supplied arguments.
fn params(&self, id:usize) -> Vec<Descriptor>
{
match id {
0 => vec![Natural!()],
1 => vec![Natural!()],
_ => Vec::new(),
}
}
Const-ness
Const-ness tells the Suzu runtime whether a call can be resolved at compile-time.
fn is_const(&self, _id:usize) -> bool
{
false
}
Execution
A system call's procedure is defined by the Module::call()
method.
The method accepts a slice of parameter-compatible objects and returns either an object or string error.
The call is mutable, so the system call may be stateful.
fn call(&mut self, id:usize, args:&[Any]) -> Result<Any,String>
{
match id {
0 => println!("{}", args[0].to_string());
1 => println!("{}", to_hex(args[0])),
_ => Err("not defined".to_string()),
}
}
Usage
Built-In
The library defines a few standard system calls, which can be found under the module
namespace.
Writes a stringified objects to the console.
print A
print.hex B
Expect
Reports an error if parameters are not string-equivalent.
expect(A, B)