jx.h
Go to the documentation of this file.
/*
Copyright (C) 2022 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#ifndef JX_H
#define JX_H
#include <stdint.h>
#include <inttypes.h>
typedef enum {
JX_NULL = 0,
JX_BOOLEAN,
JX_INTEGER,
JX_DOUBLE,
JX_STRING,
JX_SYMBOL,
JX_ARRAY,
JX_OBJECT,
JX_OPERATOR,
JX_ERROR,
} jx_type_t;
typedef int64_t jx_int_t;
#define PRIiJX PRIi64
struct jx_comprehension {
unsigned line;
char *variable;
struct jx *elements;
struct jx *condition;
struct jx_comprehension *next;
};
struct jx_item {
unsigned line;
struct jx *value;
struct jx_comprehension *comp;
struct jx_item *next;
};
struct jx_pair {
unsigned line;
struct jx *key;
struct jx *value;
struct jx_comprehension *comp;
struct jx_pair *next;
};
typedef enum {
JX_OP_EQ,
JX_OP_NE,
JX_OP_LE,
JX_OP_LT,
JX_OP_GE,
JX_OP_GT,
JX_OP_ADD,
JX_OP_SUB,
JX_OP_MUL,
JX_OP_DIV,
JX_OP_MOD,
JX_OP_AND,
JX_OP_OR,
JX_OP_NOT,
JX_OP_LOOKUP,
JX_OP_CALL,
JX_OP_SLICE,
JX_OP_DOT,
JX_OP_INVALID,
} jx_operator_t;
struct jx_operator {
jx_operator_t type;
unsigned line;
struct jx *left;
struct jx *right;
};
struct jx {
jx_type_t type;
unsigned line;
union {
int boolean_value;
jx_int_t integer_value;
double double_value;
char * string_value;
char * symbol_name;
struct jx_item *items;
struct jx_pair *pairs;
struct jx_operator oper;
struct jx *err;
} u;
};
struct jx * jx_null();
struct jx * jx_boolean( int boolean_value );
struct jx * jx_integer( jx_int_t integer_value );
struct jx * jx_double( double double_value );
struct jx * jx_string( const char *string_value );
struct jx * jx_string_nocopy( char *string_value );
struct jx * jx_format( const char *fmt, ... );
struct jx * jx_symbol( const char *symbol_name );
struct jx * jx_error( struct jx *err );
struct jx * jx_array( struct jx_item *items );
struct jx * jx_arrayv( struct jx *value, ... );
struct jx * jx_object( struct jx_pair *pairs );
struct jx * jx_objectv( const char *key, struct jx *value, ... );
struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
struct jx_item * jx_item( struct jx *value, struct jx_item *next );
struct jx_comprehension *jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next);
int jx_istype( struct jx *j, jx_type_t type );
int jx_isatomic( struct jx *j );
int jx_istrue( struct jx *j );
int jx_isfalse( struct jx *j );
int jx_comprehension_equals(struct jx_comprehension *j, struct jx_comprehension *k);
int jx_item_equals(struct jx_item *j, struct jx_item *k);
int jx_pair_equals(struct jx_pair *j, struct jx_pair *k);
int jx_equals( struct jx *j, struct jx *k );
int jx_array_length( struct jx *array );
struct jx_comprehension *jx_comprehension_copy(struct jx_comprehension *c);
struct jx_item *jx_item_copy(struct jx_item *i);
struct jx_pair *jx_pair_copy(struct jx_pair *p);
struct jx * jx_copy( struct jx *j );
void jx_delete( struct jx *j );
void jx_pair_delete( struct jx_pair *p );
void jx_item_delete( struct jx_item *i );
void jx_comprehension_delete(struct jx_comprehension *comp);
struct jx * jx_remove( struct jx *object, struct jx *key );
int jx_insert( struct jx *object, struct jx *key, struct jx *value );
int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
void jx_insert_boolean( struct jx *object, const char *key, int value );
void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
void jx_insert_double( struct jx *object, const char *key, double value );
void jx_insert_string( struct jx *object, const char *key, const char *value );
struct jx * jx_lookup( struct jx *object, const char *key );
/* Like @ref jx_lookup, but found is set to 1 when the key is found. Useful for when value is false. */
struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
const char * jx_lookup_string( struct jx *object, const char *key );
char * jx_lookup_string_dup( struct jx *object, const char *key );
jx_int_t jx_lookup_integer( struct jx *object, const char *key );
int jx_lookup_boolean( struct jx *object, const char *key );
double jx_lookup_double( struct jx *object, const char *key );
void jx_array_insert( struct jx *array, struct jx *value );
void jx_array_append( struct jx *array, struct jx *value );
struct jx * jx_array_index( struct jx *array, int nth );
struct jx *jx_array_concat( struct jx *array, ...);
struct jx *jx_array_shift(struct jx *array);
int jx_is_constant( struct jx *j );
void jx_export( struct jx *j );
struct jx * jx_iterate_array(struct jx *j, void **i);
struct jx * jx_iterate_values(struct jx *j, void **i);
const char *jx_iterate_keys(struct jx *j, void **i);
/* Get the current key while iterating over an object.
* The iteration variable must have been passed to jx_iterate_keys
* or jx_iterate_values. This directly fetches the current key rather than
* doing a lookup from the beginning, so it takes constant time and
* can handle repeated keys.
*/
const char *jx_get_key(void **i);
/* Get the current value while iterating over an object.
* The iteration variable must have been passed to jx_iterate_keys
* or jx_iterate_values. This directly fetches the current value rather than
* doing a lookup from the beginning, so it takes constant time and
* can handle repeated keys.
*/
struct jx *jx_get_value(void **i);
struct jx *jx_merge(struct jx *j, ...);
#endif
/*vim: set noexpandtab tabstop=8: */
jx_objectv
struct jx * jx_objectv(const char *key, struct jx *value,...)
Create a JX object.
jx_remove
struct jx * jx_remove(struct jx *object, struct jx *key)
Remove a key-value pair from an object.
jx_insert_boolean
void jx_insert_boolean(struct jx *object, const char *key, int value)
Insert a boolean value into an object.
jx_insert_integer
void jx_insert_integer(struct jx *object, const char *key, jx_int_t value)
Insert an integer value into an object.
jx_error
struct jx * jx_error(struct jx *err)
Create a JX_ERROR.
jx_item_delete
void jx_item_delete(struct jx_item *i)
Delete an array item.
jx_symbol
struct jx * jx_symbol(const char *symbol_name)
Create a JX symbol.
jx_array_shift
struct jx * jx_array_shift(struct jx *array)
Remove and return the first element in the array.
jx_isfalse
int jx_isfalse(struct jx *j)
Test an expression for the boolean value FALSE.
jx_string
struct jx * jx_string(const char *string_value)
Create a JX string value.
jx_export
void jx_export(struct jx *j)
Export a jx object into the current environment using setenv().
jx_lookup_string
const char * jx_lookup_string(struct jx *object, const char *key)
Search for a string item in an object.
jx_insert
int jx_insert(struct jx *object, struct jx *key, struct jx *value)
Insert a key-value pair into an object.
jx_operator
struct jx * jx_operator(jx_operator_t oper, struct jx *left, struct jx *right)
Create a JX binary expression,.
jx_lookup_string_dup
char * jx_lookup_string_dup(struct jx *object, const char *key)
Search for a string item in an object.
jx_lookup_double
double jx_lookup_double(struct jx *object, const char *key)
Search for a double item in an object.
jx_pair
struct jx_pair * jx_pair(struct jx *key, struct jx *value, struct jx_pair *next)
Create a JX key-value pair.
jx_double
struct jx * jx_double(double double_value)
Create a JX floating point value.
jx_pair_delete
void jx_pair_delete(struct jx_pair *p)
Delete a key-value pair.
jx_array_length
int jx_array_length(struct jx *array)
Get the length of an array.
jx_istrue
int jx_istrue(struct jx *j)
Test an expression for the boolean value TRUE.
jx_lookup_integer
jx_int_t jx_lookup_integer(struct jx *object, const char *key)
Search for an integer item in an object.
jx_comprehension_delete
void jx_comprehension_delete(struct jx_comprehension *comp)
Delete a comprehension.
jx_arrayv
struct jx * jx_arrayv(struct jx *value,...)
Create a JX array with inline items.
jx_item
struct jx_item * jx_item(struct jx *value, struct jx_item *next)
Create a JX array item.
jx_comprehension
struct jx_comprehension * jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next)
Create a JX comprehension.
jx_array_append
void jx_array_append(struct jx *array, struct jx *value)
Append an item at the end of an array.
jx_null
struct jx * jx_null()
Create a JX null value.
jx_istype
int jx_istype(struct jx *j, jx_type_t type)
Test an expression's type.
jx_array_concat
struct jx * jx_array_concat(struct jx *array,...)
Concatenate the given arrays into a single array.
jx_type_t
jx_type_t
JX atomic type.
Definition jx.h:43
JX_INTEGER
@ JX_INTEGER
integer value
Definition jx.h:46
JX_ERROR
@ JX_ERROR
indicates failed evaluation
Definition jx.h:53
JX_OBJECT
@ JX_OBJECT
object containing key-value pairs
Definition jx.h:51
JX_ARRAY
@ JX_ARRAY
array containing values
Definition jx.h:50
JX_SYMBOL
@ JX_SYMBOL
variable identifier
Definition jx.h:49
JX_STRING
@ JX_STRING
string value
Definition jx.h:48
JX_DOUBLE
@ JX_DOUBLE
floating point value
Definition jx.h:47
JX_BOOLEAN
@ JX_BOOLEAN
true or false
Definition jx.h:45
JX_OPERATOR
@ JX_OPERATOR
operator on multiple values.
Definition jx.h:52
JX_NULL
@ JX_NULL
null value
Definition jx.h:44
jx_array
struct jx * jx_array(struct jx_item *items)
Create a JX array.
jx_iterate_array
struct jx * jx_iterate_array(struct jx *j, void **i)
Iterate over the values in an array.
jx_equals
int jx_equals(struct jx *j, struct jx *k)
Test two expressions for equality.
jx_integer
struct jx * jx_integer(jx_int_t integer_value)
Create a JX integer value.
jx_iterate_values
struct jx * jx_iterate_values(struct jx *j, void **i)
Iterate over the values in an object.
jx_boolean
struct jx * jx_boolean(int boolean_value)
Create a JX boolean value.
jx_iterate_keys
const char * jx_iterate_keys(struct jx *j, void **i)
Iterate over the keys in an object.
jx_isatomic
int jx_isatomic(struct jx *j)
Test for an atomic value.
jx_string_nocopy
struct jx * jx_string_nocopy(char *string_value)
Create a JX string value without copying (uncommon).
jx_array_index
struct jx * jx_array_index(struct jx *array, int nth)
Get the nth item in an array.
jx_insert_double
void jx_insert_double(struct jx *object, const char *key, double value)
Insert a double value into an object.
jx_format
struct jx * jx_format(const char *fmt,...)
Create a JX string value using prinf style formatting.
jx_object
struct jx * jx_object(struct jx_pair *pairs)
Create a JX object.
jx_insert_string
void jx_insert_string(struct jx *object, const char *key, const char *value)
Insert a string value into an object.
jx_delete
void jx_delete(struct jx *j)
Delete an expression recursively.
jx_lookup
struct jx * jx_lookup(struct jx *object, const char *key)
Search for a arbitrary item in an object.
jx_array_insert
void jx_array_insert(struct jx *array, struct jx *value)
Insert an item at the beginning of an array.
jx_lookup_boolean
int jx_lookup_boolean(struct jx *object, const char *key)
Search for a boolean item in an object.
jx_copy
struct jx * jx_copy(struct jx *j)
Duplicate an expression.
jx_insert_unless_empty
int jx_insert_unless_empty(struct jx *object, struct jx *key, struct jx *value)
Insert a key-value pair into an object, unless the value is an empty collection, in which case delete...
jx_merge
struct jx * jx_merge(struct jx *j,...)
Merge an arbitrary number of JX_OBJECTs into a single new one.
jx_is_constant
int jx_is_constant(struct jx *j)
Determine if an expression is constant.
jx_comprehension
Definition jx.h:59
jx_comprehension::elements
struct jx * elements
items for list comprehension
Definition jx.h:62
jx_comprehension::variable
char * variable
variable for comprehension
Definition jx.h:61
jx_comprehension::condition
struct jx * condition
condition for filtering list comprehension
Definition jx.h:63
jx_item
JX item linked-list used by JX_ARRAY and jx::items.
Definition jx.h:69
jx_item::value
struct jx * value
value of this item
Definition jx.h:71
jx_item::next
struct jx_item * next
pointer to next item
Definition jx.h:73
jx_operator
Definition jx.h:108
jx_pair
JX key-value pairs used by JX_OBJECT and jx::pairs.
Definition jx.h:78
jx_pair::key
struct jx * key
key of this pair
Definition jx.h:80
jx_pair::next
struct jx_pair * next
pointer to next pair
Definition jx.h:83
jx_pair::value
struct jx * value
value of this pair
Definition jx.h:81
jx
JX value representing any expression type.
Definition jx.h:117
jx::integer_value
jx_int_t integer_value
value of JX_INTEGER
Definition jx.h:122
jx::items
struct jx_item * items
value of JX_ARRAY
Definition jx.h:126
jx::boolean_value
int boolean_value
value of JX_BOOLEAN
Definition jx.h:121
jx::pairs
struct jx_pair * pairs
value of JX_OBJECT
Definition jx.h:127
jx::oper
struct jx_operator oper
value of JX_OPERATOR
Definition jx.h:128
jx::err
struct jx * err
error value of JX_ERROR
Definition jx.h:129
jx::double_value
double double_value
value of JX_DOUBLE
Definition jx.h:123
jx::line
unsigned line
line where this value was defined
Definition jx.h:119
jx::symbol_name
char * symbol_name
value of JX_SYMBOL
Definition jx.h:125
jx::type
jx_type_t type
type of this value
Definition jx.h:118
jx::string_value
char * string_value
value of JX_STRING
Definition jx.h:124