rego-cpp 1.0.0
A C++ implementation of the Rego language and runtime
Loading...
Searching...
No Matches
rego::BuiltInDef Struct Reference

Struct which defines a built-in function. More...

#include <rego.hh>

Public Member Functions

 BuiltInDef (Location name_, Node decl_, BuiltInBehavior behavior_, bool available_)
 Constructor.
 
virtual void clear ()
 Called to clear any persistent state or caching.
 

Static Public Member Functions

static BuiltIn create (const Location &name, Node decl, BuiltInBehavior behavior)
 Creates a new built-in.
 
static BuiltIn create (const Location &name, size_t arity, BuiltInBehavior behavior)
 Creates a new built-in.
 
static BuiltIn placeholder (const Location &name, Node decl, const std::string &message)
 Creates a placeholder for a built-in which is not available on this platform.
 

Public Attributes

Location name
 The name used to match against expression calls in the rego program.
 
Node decl
 The declaration node which adheres to the builtins::wf_decl well-formedness definition.
 
std::size_t arity
 The number of expected arguments.
 
BuiltInBehavior behavior
 The function which will be called when the built-in is evaluated.
 
bool available
 Whether the builtin is available.
 

Detailed Description

Struct which defines a built-in function.

You can extend Rego by registering your own built-ins. A built-in is a function which is called by Rego during evaluation. Built-ins are called with a vector of Nodes, and return a Node. The vector of Nodes contains the arguments passed to the built-in. The Node returned by the built-in is the result of the built-in's evaluation.

Here is an example built-in which performs addition:

Node add(const Nodes& args)
{
Node a = unwrap_arg(args, UnwrapOpt(0).types({Int, Float}));
if(a.type() == Error){
return a;
}
Node b = unwrap_arg(args, UnwrapOpt(1).types({Int, Float}));
if(b.type() == Error){
return b;
}
if(a.type() == Int && b.type() == Int)
{
return scalar(get_int(a) + get_int(b));
}
return scalar(get_double(a) + get_double(b));
}
Node add_decl =
bi::Decl << (bi::ArgSeq << (bi::Arg << (bi::Name ^ "a") <<
bi::Description
<< (bi::Type << bi::Number))
<< (bi::Arg << (bi::Name ^ "b") <<
bi::Description
<< (bi::Type << bi::Number)))
<< (bi::Result << (bi::Name ^ "result")
<< (bi::Description ^ "The sum of `a` and `b`.")
<< (bi::Type << bi::Number));
Options for unwrapping an argument.
Definition rego.hh:419
Node scalar()
Creates a null scalar.
BigInt get_int(const Node &node)
Extracts the value of a node as an integer.
double get_double(const Node &node)
Extracts the value of a node as an double.
Node unwrap_arg(const Nodes &args, const UnwrapOpt &options)
Unwraps an argument from the provided vector of nodes.

Note that there are several helper methods and objects to aid in writing managing nodes and wrapping/unwrapping them into basic types. Once a method like the above has been written, use BuiltInDef::create following way:

builtins.register_builtin(BuiltInDef::create(Location("myadd"), add_decl,
add))
static BuiltIn create(const Location &name, Node decl, BuiltInBehavior behavior)
Creates a new built-in.

Then, during evaluation, any call to the built-in myadd will be handled by the myadd function.

Member Function Documentation

◆ create() [1/2]

static BuiltIn rego::BuiltInDef::create ( const Location & name,
Node decl,
BuiltInBehavior behavior )
static

Creates a new built-in.

The decl node adheres to the builtins::wf_decl well-formedness definition. BuiltIn is a pointer to a BuiltInDef.

Parameters
nameThe name of the built-in.
declMetadata and documentation for the built-in.
behaviorThe function which will be called when the built-in is evaluated.
Returns
The built-in.

◆ create() [2/2]

static BuiltIn rego::BuiltInDef::create ( const Location & name,
size_t arity,
BuiltInBehavior behavior )
static

Creates a new built-in.

An empty decl node with the correct arity and a return type of Any will be automatically created from the arity argument. ///

Deprecated
Parameters
nameThe name of the built-in.
arityNumber of arguments to the builtin
behaviorThe function which will be called when the built-in is evaluated.
Returns
The built-in.

◆ placeholder()

static BuiltIn rego::BuiltInDef::placeholder ( const Location & name,
Node decl,
const std::string & message )
static

Creates a placeholder for a built-in which is not available on this platform.

The decl node adheres to the builtins::wf_decl well-formedness definition. BuiltIn is a pointer to a BuiltInDef.

Parameters
nameThe name of the built-in.
declMetadata and documentation for the built-in.
messageThe message to include in the error when the built-in is called.
Returns
The built-in.

Member Data Documentation

◆ arity

std::size_t rego::BuiltInDef::arity

The number of expected arguments.

If any number of arguments can be provided, use the constant AnyArity.


The documentation for this struct was generated from the following file: