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

#include <rego.hh>

Public Member Functions

 BuiltInDef (Location name_, std::size_t arity_, BuiltInBehavior behavior_)
 
virtual void clear ()
 

Static Public Member Functions

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

Public Attributes

Location name
 
std::size_t arity
 
BuiltInBehavior behavior
 

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));
}
Definition rego.hh:311
Node scalar()
BigInt get_int(const Node &node)
double get_double(const Node &node)
Node unwrap_arg(const Nodes &args, const UnwrapOpt &options)

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("add"), 2, add))
static BuiltIn create(const Location &name, std::size_t arity, BuiltInBehavior behavior)

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

Constructor & Destructor Documentation

◆ BuiltInDef()

rego::BuiltInDef::BuiltInDef ( Location name_,
std::size_t arity_,
BuiltInBehavior behavior_ )

Constructor.

Member Function Documentation

◆ clear()

virtual void rego::BuiltInDef::clear ( )
virtual

Called to clear any persistent state or caching.

◆ create()

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

Creates a new built-in.

BuiltIn is a pointer to a BuiltInDef.

Parameters
nameThe name of the built-in.
arityThe number of arguments expected by the built-in.
behaviorThe function which will be called when the built-in is evaluated.
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.

◆ behavior

BuiltInBehavior rego::BuiltInDef::behavior

The function which will be called when the built-in is evaluated.

◆ name

Location rego::BuiltInDef::name

The name used to match against expression calls in the rego program.


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