#include <stdint.h>
Go to the source code of this file.
Defines | |
#define | nu_Scope_NULL -1 |
no scope (NULL) | |
#define | nu_TypeId_NULL -1 |
no type (NULL) | |
Typedefs | |
typedef int | nu_Scope |
A scope handle. | |
typedef struct nu_TypeSystemStruct * | nu_TypeSystem |
The type system object. | |
typedef int | nu_TypeId |
Id of a type. | |
Enumerations | |
enum | nu_ErrorCode { NU_OK = 0, NU_ERROR_NOMEM = 1, NU_ERROR_CONFLICT_RULE = 2, NU_ERROR_CONFLICT_DECISION = 3, NU_ERROR_CONFLICT_DUPLICATE_DEF = 4, NU_ERROR_SYNTAX = 5 } |
Possible error codes of libnaru. More... | |
Functions | |
nu_TypeSystem | nu_createTypeSystem (void) |
Constructor of a type system. | |
void | nu_destroyTypeSystem (nu_TypeSystem ts) |
Destructor of a type system. | |
nu_Scope | nu_beginScope (nu_TypeSystem ts) |
Declare the start of a new scope (open bracket). Returns Scope_NULL in case of failure, the built scope in case of success. | |
nu_Scope | nu_declareScope (nu_TypeSystem ts) |
Declare a declaration step in the current scope. This is done to define an order of declarations in the scope. Something declared with a following declareScope is accessible for following declaration in the same scope, but not vice versa. | |
int | nu_endScope (nu_TypeSystem ts) |
Declare the end of a scope 'scope' (close bracket). | |
int | nu_cmpScope (nu_TypeSystem ts, nu_Scope A, nu_Scope B) |
Compares two scopes. Decides wheter A covers B or B covers A or wheter they are disjunct. | |
int | nu_sameScope (nu_TypeSystem ts, nu_Scope A, nu_Scope B) |
Test two scopes to be the same. They are considered to be the same if opened with the same 'beginScope' action. Scopes derived from it with 'declareScope' are also considered to be the same. | |
int | nu_scopeId (nu_TypeSystem ts, nu_Scope scope) |
Returns an identifier for a scope unique for all declarations in the same scope. | |
nu_TypeId | nu_declareType (nu_TypeSystem ts, nu_Scope scope, const char *id, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode *errcode) |
Declares a type identified by 'id' in the scope 'scope'. With tc/tcarg you declare how a result is built when this type has been used in a resolve expression query. It is part of the constructor of a resolved expression. | |
nu_TypeId | nu_getType (nu_TypeSystem ts, nu_Scope scope, const char *id) |
Find the type and its constructor identified by 'id' in the scope 'scope'. | |
char * | nu_getTypeName (nu_TypeSystem ts, nu_TypeId type) |
Returns the name of the type as passed with parameter 'id' when declaring the type with 'declareType'. | |
uintptr_t | nu_getTypeConstructor (nu_TypeSystem ts, nu_TypeId type, uintptr_t *tcarg) |
Returns the constructor of the type as passed with parameter 'tc' when declaring the type with 'declareType'. | |
int | nu_declareRule (nu_TypeSystem ts, nu_Scope scope, nu_TypeId src, nu_TypeId dest, unsigned int cstord, unsigned int cst, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode *errcode) |
Declares a rule to deduce the type 'dest' from 'src' with the costs 'cstord/cst' and a constructor. The value of the type 'dest' after applying the rule can be built, when iterating through the result of a resolved expression. With tc/tcarg you declare how a result is built when this rule has been applied when resolving an expression. It is part of the constructor of a resolved expression. | |
int | nu_declareExpression (nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId *tps, nu_TypeId restype, uintptr_t tc, uintptr_t tcarg, nu_ErrorCode *errcode) |
Declares an expression as tuple of types that evaluates to the type 'restype'. The value of the expression can be built when iterating through the result of a 'resolveExpression'. | |
nu_TypeId | nu_getExpressionType (nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId *tps, uintptr_t *tc, uintptr_t *tcarg) |
Finds the expression adressed by 'tps[]' declared in a scope containing the scope 'scope' that is an exact match. So no rules are involved and there is no result constructed. This function is needed for implementing concepts. Concepts are sets of conditions that an object must meet in order to be of a certain type. | |
int | nu_resolveExpression (nu_TypeSystem ts, nu_Scope scope, unsigned int nof_tps, nu_TypeId *tps, nu_TypeId *resulttype, nu_ErrorCode *errcode) |
Finds the expression adressed by 'tps[]' declared in a scope containing the scope 'scope' that is the nearest in the evaluation path. Nearest means that the expression found by applying rules to elements in the tuple 'tps' (the unifier of the query expression and the defined expression) has the minimal sum of costs of all matching expressions and their rule evaluation paths. | |
int | nu_getRuleConflict (nu_TypeSystem ts, nu_TypeId **path1, nu_TypeId **path2) |
Get both deduction paths of the conflicts when nu_resolveExpression returned errcode=NU_ERROR_CONFLICT_RULE in the last call. path1[] and path2[] are TypeId_NULL terminated. | |
int | nu_getDecisionConflict (nu_TypeSystem ts, nu_TypeId **expr1, nu_TypeId **expr2) |
Get both conflicting expressions when nu_resolveExpression returned errcode=NU_ERROR_CONFLICT_DECISION in the last call. expr1[] and expr2[] are TypeId_NULL terminated. | |
int | nu_getElementConstructor (nu_TypeSystem ts, int result_handle, unsigned int tps_idx, int iter, uintptr_t *tc, uintptr_t *tcarg) |
Function to iterate through a result of 'resolveExpression'. 'iter' is starting with 0, and incremented until -1 is returned. Gets the constructors of the element unifiers. The function returns the constructor 'tc' of a type or rule declared with 'declareType' or 'declareRule'. It returns also the arguments of the constructor functor 'tc' in 'tcarg'. The expression element result is constructed by applying the constructors got with this function in the ascending order of 'iter' starting with 0. | |
int | nu_getResultConstructor (nu_TypeSystem ts, int result_handle, uintptr_t *tc, uintptr_t *tcarg) |
Get the constructor of the expression of the last result of 'resolveExpression'. The value of the expression can be constructed by applying the constructor functor on the elements built from the result of 'getElementConstructor'. Returns -1 when the result handle passed is invalid. | |
void | nu_clearResultBuffer (nu_TypeSystem ts) |
Clear the result buffer. The handles of results will counted again from 0. |
typedef struct nu_TypeSystemStruct* nu_TypeSystem |
enum nu_ErrorCode |
Possible error codes of libnaru.
nu_Scope nu_beginScope | ( | nu_TypeSystem | ts | ) |
Declare the start of a new scope (open bracket). Returns Scope_NULL in case of failure, the built scope in case of success.
ts | type system |
void nu_clearResultBuffer | ( | nu_TypeSystem | ts | ) |
Clear the result buffer. The handles of results will counted again from 0.
ts | type system |
int nu_cmpScope | ( | nu_TypeSystem | ts, | |
nu_Scope | A, | |||
nu_Scope | B | |||
) |
Compares two scopes. Decides wheter A covers B or B covers A or wheter they are disjunct.
ts | type system | |
A | scope identifier declared in ts. | |
B | scope identifier declared in ts. |
nu_TypeSystem nu_createTypeSystem | ( | void | ) |
Constructor of a type system.
int nu_declareExpression | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
unsigned int | nof_tps, | |||
nu_TypeId * | tps, | |||
nu_TypeId | restype, | |||
uintptr_t | tc, | |||
uintptr_t | tcarg, | |||
nu_ErrorCode * | errcode | |||
) |
Declares an expression as tuple of types that evaluates to the type 'restype'. The value of the expression can be built when iterating through the result of a 'resolveExpression'.
ts | type system | |
scope | scope identifier declared in ts. The scope of the declared expression. | |
nof_tps | number of elements in tps[] | |
tps | array of types defining the expression as tuple of types. | |
restype | the result type of the expression | |
tc | type constructor function identifier of this expression. | |
tcarg | type constructor function argument identifier of this expression | |
errcode | possible error returned in case of failure (0 returned) |
int nu_declareRule | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
nu_TypeId | src, | |||
nu_TypeId | dest, | |||
unsigned int | cstord, | |||
unsigned int | cst, | |||
uintptr_t | tc, | |||
uintptr_t | tcarg, | |||
nu_ErrorCode * | errcode | |||
) |
Declares a rule to deduce the type 'dest' from 'src' with the costs 'cstord/cst' and a constructor. The value of the type 'dest' after applying the rule can be built, when iterating through the result of a resolved expression. With tc/tcarg you declare how a result is built when this rule has been applied when resolving an expression. It is part of the constructor of a resolved expression.
ts | type system | |
scope | scope identifier declared in ts. The scope of the declared rule. | |
src | type identifier declared in ts. The type this deduction rule is applied on. | |
dest | type identifier declared in ts. The type this deduction rule results in. | |
cstord | number between 0..3 meaning the order of costs. Costs can have different order. A sum of costs of higher order is allways bigger than any sum of costs of lower order. | |
cst | costs of the rule. The costs are part of the costs of a unifier of two expressions. The summation of all costs of the rules used in the unifier is used to decide what is the matching expression and its unifier when calling 'resolveExpression'. It is the expression that has the unifier with minimal sum of costs. | |
tc | type constructor function identifier of this rule. | |
tcarg | type constructor function argument identifier of this rule | |
errcode | possible error returned in case of failure (0 returned) |
nu_Scope nu_declareScope | ( | nu_TypeSystem | ts | ) |
Declare a declaration step in the current scope. This is done to define an order of declarations in the scope. Something declared with a following declareScope is accessible for following declaration in the same scope, but not vice versa.
ts | type system |
nu_TypeId nu_declareType | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
const char * | id, | |||
uintptr_t | tc, | |||
uintptr_t | tcarg, | |||
nu_ErrorCode * | errcode | |||
) |
Declares a type identified by 'id' in the scope 'scope'. With tc/tcarg you declare how a result is built when this type has been used in a resolve expression query. It is part of the constructor of a resolved expression.
ts | type system | |
scope | scope identifier declared in ts. The scope of the declared type. | |
id | identifier of the declared type | |
tc | type constructor function identifier | |
tcarg | type constructor function argument identifier | |
errcode | possible error returned in case of failure (TypeId_NULL returned) |
void nu_destroyTypeSystem | ( | nu_TypeSystem | ts | ) |
Destructor of a type system.
int nu_endScope | ( | nu_TypeSystem | ts | ) |
Declare the end of a scope 'scope' (close bracket).
ts | type system |
int nu_getDecisionConflict | ( | nu_TypeSystem | ts, | |
nu_TypeId ** | expr1, | |||
nu_TypeId ** | expr2 | |||
) |
Get both conflicting expressions when nu_resolveExpression returned errcode=NU_ERROR_CONFLICT_DECISION in the last call. expr1[] and expr2[] are TypeId_NULL terminated.
ts | type system | |
expr1[] | returned conflicting expression 1 | |
expr2[] | returned conflicting expression 2 |
int nu_getElementConstructor | ( | nu_TypeSystem | ts, | |
int | result_handle, | |||
unsigned int | tps_idx, | |||
int | iter, | |||
uintptr_t * | tc, | |||
uintptr_t * | tcarg | |||
) |
Function to iterate through a result of 'resolveExpression'. 'iter' is starting with 0, and incremented until -1 is returned. Gets the constructors of the element unifiers. The function returns the constructor 'tc' of a type or rule declared with 'declareType' or 'declareRule'. It returns also the arguments of the constructor functor 'tc' in 'tcarg'. The expression element result is constructed by applying the constructors got with this function in the ascending order of 'iter' starting with 0.
ts | type system | |
result_handle | handle of the result got as return value of 'resolveExpression'. | |
tps_idx | which element of the query structure to build. | |
iter | iterator to visit all element constructors of the query structure element unifier. | |
tc | returned type constructor function identifier of the query structure element unifier. | |
tcarg | returned type constructor function argument identifier of the query structure element unifier |
nu_TypeId nu_getExpressionType | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
unsigned int | nof_tps, | |||
nu_TypeId * | tps, | |||
uintptr_t * | tc, | |||
uintptr_t * | tcarg | |||
) |
Finds the expression adressed by 'tps[]' declared in a scope containing the scope 'scope' that is an exact match. So no rules are involved and there is no result constructed. This function is needed for implementing concepts. Concepts are sets of conditions that an object must meet in order to be of a certain type.
ts | type system | |
scope | scope identifier declared in ts. The query scope. The scope of the result must cover the query scope and be minimal. The result scope is the innermost scope of all matching expression definitions. | |
nof_tps | number of elements in tps[] | |
tps | array of types defining the expression as tuple of types. | |
tc | returned type constructor function identifier of the found expression. | |
tcarg | returned type constructor function argument identifier of the found expression |
int nu_getResultConstructor | ( | nu_TypeSystem | ts, | |
int | result_handle, | |||
uintptr_t * | tc, | |||
uintptr_t * | tcarg | |||
) |
Get the constructor of the expression of the last result of 'resolveExpression'. The value of the expression can be constructed by applying the constructor functor on the elements built from the result of 'getElementConstructor'. Returns -1 when the result handle passed is invalid.
ts | type system | |
result_handle | handle of the result got as return value of 'resolveExpression'. | |
tc | returned type constructor function identifier of the matching expression. | |
tcarg | returned type constructor function argument identifier of the matching expression. |
int nu_getRuleConflict | ( | nu_TypeSystem | ts, | |
nu_TypeId ** | path1, | |||
nu_TypeId ** | path2 | |||
) |
Get both deduction paths of the conflicts when nu_resolveExpression returned errcode=NU_ERROR_CONFLICT_RULE in the last call. path1[] and path2[] are TypeId_NULL terminated.
ts | type system | |
path1[] | returned first deduction path of types the first element is the last type in the chain, the last element is the starting point. | |
path2[] | returned alternative deduction path of types the first element is the last type in the chain, the last element is the starting point. |
nu_TypeId nu_getType | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
const char * | id | |||
) |
Find the type and its constructor identified by 'id' in the scope 'scope'.
ts | type system | |
scope | scope identifier declared in ts. The query scope. The scope of the result type must cover the query scope and be minimal. The result scope is the innermost scope of all matching type definitions. | |
id | identifier of the type to search |
uintptr_t nu_getTypeConstructor | ( | nu_TypeSystem | ts, | |
nu_TypeId | type, | |||
uintptr_t * | tcarg | |||
) |
Returns the constructor of the type as passed with parameter 'tc' when declaring the type with 'declareType'.
ts | type system | |
type | identifier of the type | |
tcarg | returned constructor arument ('tcarg') of 'type' |
char* nu_getTypeName | ( | nu_TypeSystem | ts, | |
nu_TypeId | type | |||
) |
Returns the name of the type as passed with parameter 'id' when declaring the type with 'declareType'.
ts | type system | |
type | identifier of the type |
int nu_resolveExpression | ( | nu_TypeSystem | ts, | |
nu_Scope | scope, | |||
unsigned int | nof_tps, | |||
nu_TypeId * | tps, | |||
nu_TypeId * | resulttype, | |||
nu_ErrorCode * | errcode | |||
) |
Finds the expression adressed by 'tps[]' declared in a scope containing the scope 'scope' that is the nearest in the evaluation path. Nearest means that the expression found by applying rules to elements in the tuple 'tps' (the unifier of the query expression and the defined expression) has the minimal sum of costs of all matching expressions and their rule evaluation paths.
ts | type system | |
scope | scope identifier declared in ts. The query scope. The scope of the result must cover the query scope and be minimal. All applied rules must cover the query scope too. The result scope is the innermost scope of all matching expression definitions. | |
nof_tps | number of elements in tps[] | |
tps | array of types defining the expression as tuple of types. | |
resulttype | returned type of the result. | |
errcode | possible error returned in case of failure (-1 returned). In case of errors because of conflicts, you can get the conflict reason with: nu_getDecisionConflict in case of NU_ERROR_CONFLICT_DECISION (conflicting expressions) nu_getRuleConflict in case of NU_ERROR_CONFLICT_RULE (2 type unifiers with same costs) |
int nu_sameScope | ( | nu_TypeSystem | ts, | |
nu_Scope | A, | |||
nu_Scope | B | |||
) |
Test two scopes to be the same. They are considered to be the same if opened with the same 'beginScope' action. Scopes derived from it with 'declareScope' are also considered to be the same.
ts | type system | |
A | scope identifier declared in ts. | |
B | scope identifier declared in ts. |
int nu_scopeId | ( | nu_TypeSystem | ts, | |
nu_Scope | scope | |||
) |
Returns an identifier for a scope unique for all declarations in the same scope.
ts | type system | |
scope | scope identifier declared in ts. |