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

#include <rego.hh>

Public Member Functions

 Interpreter (bool v1_compatible=false)
 
Node add_module_file (const std::filesystem::path &path)
 
Node add_module (const std::string &name, const std::string &contents)
 
Node add_data_json_file (const std::filesystem::path &path)
 
Node add_data_json (const std::string &json)
 
Node add_data (const Node &node)
 
Node set_input_json_file (const std::filesystem::path &path)
 
Node set_input_term (const std::string &term)
 
Node set_input (const Node &node)
 
std::string query (const std::string &query_expr)
 
Node raw_query (const std::string &query_expr)
 
Interpreterdebug_path (const std::filesystem::path &prefix)
 
const std::filesystem::path & debug_path () const
 
Interpreterdebug_enabled (bool enabled)
 
bool debug_enabled () const
 
Interpreterwf_check_enabled (bool enabled)
 
bool wf_check_enabled () const
 
BuiltInsDefbuiltins () const
 

Friends

const char * regoGetError (regoInterpreter *rego)
 
void setError (regoInterpreter *rego, const std::string &error)
 
regoOutput * regoQuery (regoInterpreter *rego, const char *query_expr)
 

Detailed Description

This class forms the main interface to the Rego library.

You can use it to assemble and then execute queries, for example:

rego.add_module_file("objects.rego");
rego.add_data_json_file("data0.json");
rego.add_data_json_file("data1.json");
rego.add_input_json_file("input0.json");
std::cout << rego.query("[data.one, input.b, data.objects.sites[1]]") <<
std::endl;
Definition rego.hh:873
Definition rego.hh:17

Constructor & Destructor Documentation

◆ Interpreter()

rego::Interpreter::Interpreter ( bool v1_compatible = false)

Constructor.

Parameters
v1_compatiblewhether the Interpreter should run in rego-v1 mode.

Member Function Documentation

◆ add_data()

Node rego::Interpreter::add_data ( const Node & node)

Adds a base document to the interpreter.

Adds an AST node directly to the interpreter's data sequence. Use with caution.

Parameters
nodeThe contents of the document.

◆ add_data_json()

Node rego::Interpreter::add_data_json ( const std::string & json)

Adds a base document to the interpreter.

The document must contain a single JSON-encoded object, and will be parsed and added to the interpreter's data sequence.

Parameters
jsonThe contents of the document.
Returns
either an error node or a nullptr if the JSON is valid.

◆ add_data_json_file()

Node rego::Interpreter::add_data_json_file ( const std::filesystem::path & path)

Adds a base document to the interpreter.

This is the same as calling Interpreter::add_data_json with the contents of the file.

Parameters
moduleThe module to add.
Returns
either an error node or a nullptr if the JSON is valid.

◆ add_module()

Node rego::Interpreter::add_module ( const std::string & name,
const std::string & contents )

Adds a module (i.e. virtual document) to the interpreter.

The module will be parsed and added to the interpreter's module sequence.

Parameters
nameThe name of the module.
contentsThe contents of the module.
Returns
either an error node or a nullptr if the module is valid.

◆ add_module_file()

Node rego::Interpreter::add_module_file ( const std::filesystem::path & path)

Adds a module (i.e. virtual document) file to the interpreter.

This is the same as calling Interpreter::add_module with the contents of the file.

Parameters
pathThe path to the module file.
Returns
either an error node or a nullptr if the module is valid.

◆ builtins()

BuiltInsDef & rego::Interpreter::builtins ( ) const

The built-ins used by the interpreter.

This object can be used to register custom built-ins created using BuiltInDef::create.

◆ debug_enabled()

Interpreter & rego::Interpreter::debug_enabled ( bool enabled)

Whether debug mode is enabled.

If true, then the interpreter will output intermediary ASTs after each compiler pass to the debug directory set via Interpreter::debug_path.

◆ debug_path()

Interpreter & rego::Interpreter::debug_path ( const std::filesystem::path & prefix)

The path to the debug directory.

If set, then (when in debug mode) the interpreter will output intermediary ASTs after each compiler pass to the debug directory. If the directory does not exist, it will be created.

◆ query()

std::string rego::Interpreter::query ( const std::string & query_expr)

Executes a query against the interpreter.

This method calls Interpreter::raw_query and then converts it into a human-readable string.

Parameters
query_exprThe query expression.
Returns
The result of the query.

◆ raw_query()

Node rego::Interpreter::raw_query ( const std::string & query_expr)

Executes a query against the interpreter.

The query expression must be a valid Rego query expression. The result will be an AST node representing the result, which will either be a list of bindings and terms, or an error sequence.

Parameters
query_exprThe query expression.
Returns
The result of the query.

◆ set_input()

Node rego::Interpreter::set_input ( const Node & node)

Sets the input document to the interpreter.

Sets an AST node directly as the interpreter's input. Use with caution.

Parameters
nodeThe contents of the document.

◆ set_input_json_file()

Node rego::Interpreter::set_input_json_file ( const std::filesystem::path & path)

Sets the input document to the interpreter.

This is the same as calling Interpreter::add_input_json with the contents of the file.

Parameters
pathThe path to the input file.
Returns
either an error node or a nullptr if the input document is valid.

◆ set_input_term()

Node rego::Interpreter::set_input_term ( const std::string & term)

Sets the input term of the interpreter.

The string must contain a single valid Rego data term.

Parameters
termThe contents of the term.
Returns
either an error node or a nullptr if the input term is valid.

◆ wf_check_enabled()

Interpreter & rego::Interpreter::wf_check_enabled ( bool enabled)

Whether well-formed checks are enabled.

If true, then the interpreter will perform well-formedness checks after each compiler pass using the well-formedness definitions.

Friends And Related Symbol Documentation

◆ regoGetError

const char * regoGetError ( regoInterpreter * rego)
friend

Returns the most recently thrown error.

If an error code is returned from an interface function, more error information can be obtained by calling this function.

Parameters
regoThe interpreter.
Returns
The error message.

◆ regoQuery

regoOutput * regoQuery ( regoInterpreter * rego,
const char * query_expr )
friend

Performs a query against the current base and virtual documents.

The query expression should be a Rego query. The output of the query will be returned as a regoOutput object. The caller is responsible for freeing the output object with regoFreeOutput.

Parameters
regoThe interpreter.
query_exprThe query expression.
Returns
The output of the query.

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