cctools
jx.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2022 The University of Notre Dame
3 This software is distributed under the GNU General Public License.
4 See the file COPYING for details.
5 */
6 
7 #ifndef JX_H
8 #define JX_H
9 
39 #include <stdint.h>
40 #include <inttypes.h>
41 
43 typedef enum {
44  JX_NULL = 0,
54 } jx_type_t;
55 
56 typedef int64_t jx_int_t;
57 #define PRIiJX PRIi64
58 
60  unsigned line;
61  char *variable;
62  struct jx *elements;
63  struct jx *condition;
64  struct jx_comprehension *next;
65 };
66 
69 struct jx_item {
70  unsigned line;
71  struct jx *value;
72  struct jx_comprehension *comp;
73  struct jx_item *next;
74 };
75 
78 struct jx_pair {
79  unsigned line;
80  struct jx *key;
81  struct jx *value;
82  struct jx_comprehension *comp;
83  struct jx_pair *next;
84 };
85 
86 typedef enum {
87  JX_OP_EQ,
88  JX_OP_NE,
89  JX_OP_LE,
90  JX_OP_LT,
91  JX_OP_GE,
92  JX_OP_GT,
93  JX_OP_ADD,
94  JX_OP_SUB,
95  JX_OP_MUL,
96  JX_OP_DIV,
97  JX_OP_MOD,
98  JX_OP_AND,
99  JX_OP_OR,
100  JX_OP_NOT,
101  JX_OP_LOOKUP,
102  JX_OP_CALL,
103  JX_OP_SLICE,
104  JX_OP_DOT,
105  JX_OP_INVALID,
106 } jx_operator_t;
107 
108 struct jx_operator {
109  jx_operator_t type;
110  unsigned line;
111  struct jx *left;
112  struct jx *right;
113 };
114 
117 struct jx {
119  unsigned line;
120  union {
122  jx_int_t integer_value;
123  double double_value;
124  char * string_value;
125  char * symbol_name;
126  struct jx_item *items;
127  struct jx_pair *pairs;
128  struct jx_operator oper;
129  struct jx *err;
130  } u;
131 };
132 
134 struct jx * jx_null();
135 
137 struct jx * jx_boolean( int boolean_value );
138 
140 struct jx * jx_integer( jx_int_t integer_value );
141 
143 struct jx * jx_double( double double_value );
144 
146 struct jx * jx_string( const char *string_value );
147 
149 struct jx * jx_string_nocopy( char *string_value );
150 
152 struct jx * jx_format( const char *fmt, ... );
153 
159 struct jx * jx_symbol( const char *symbol_name );
160 
165 struct jx * jx_error( struct jx *err );
166 
168 struct jx * jx_array( struct jx_item *items );
169 
171 struct jx * jx_arrayv( struct jx *value, ... );
172 
174 struct jx * jx_object( struct jx_pair *pairs );
175 
186 struct jx * jx_objectv( const char *key, struct jx *value, ... );
187 
189 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
190 
192 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
193 
195 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
196 
204 struct jx_comprehension *jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next);
205 
207 int jx_istype( struct jx *j, jx_type_t type );
208 
210 int jx_isatomic( struct jx *j );
211 
213 int jx_istrue( struct jx *j );
214 
216 int jx_isfalse( struct jx *j );
217 
218 int jx_comprehension_equals(struct jx_comprehension *j, struct jx_comprehension *k);
219 int jx_item_equals(struct jx_item *j, struct jx_item *k);
220 int jx_pair_equals(struct jx_pair *j, struct jx_pair *k);
221 
224 int jx_equals( struct jx *j, struct jx *k );
225 
227 int jx_array_length( struct jx *array );
228 
229 struct jx_comprehension *jx_comprehension_copy(struct jx_comprehension *c);
230 struct jx_item *jx_item_copy(struct jx_item *i);
231 struct jx_pair *jx_pair_copy(struct jx_pair *p);
232 
235 struct jx * jx_copy( struct jx *j );
236 
238 void jx_delete( struct jx *j );
239 
241 void jx_pair_delete( struct jx_pair *p );
242 
244 void jx_item_delete( struct jx_item *i );
245 
247 void jx_comprehension_delete(struct jx_comprehension *comp);
248 
250 struct jx * jx_remove( struct jx *object, struct jx *key );
251 
253 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
254 
256 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
257 
259 void jx_insert_boolean( struct jx *object, const char *key, int value );
260 
262 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
263 
265 void jx_insert_double( struct jx *object, const char *key, double value );
266 
268 void jx_insert_string( struct jx *object, const char *key, const char *value );
269 
271 struct jx * jx_lookup( struct jx *object, const char *key );
272 
273 /* Like @ref jx_lookup, but found is set to 1 when the key is found. Useful for when value is false. */
274 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
275 
277 const char * jx_lookup_string( struct jx *object, const char *key );
278 
280 char * jx_lookup_string_dup( struct jx *object, const char *key );
281 
283 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
284 
286 int jx_lookup_boolean( struct jx *object, const char *key );
287 
289 double jx_lookup_double( struct jx *object, const char *key );
290 
292 void jx_array_insert( struct jx *array, struct jx *value );
293 
295 void jx_array_append( struct jx *array, struct jx *value );
296 
298 struct jx * jx_array_index( struct jx *array, int nth );
299 
301 struct jx *jx_array_concat( struct jx *array, ...);
302 
306 struct jx *jx_array_shift(struct jx *array);
307 
309 int jx_is_constant( struct jx *j );
310 
312 void jx_export( struct jx *j );
313 
331 struct jx * jx_iterate_array(struct jx *j, void **i);
332 
351 struct jx * jx_iterate_values(struct jx *j, void **i);
352 
371 const char *jx_iterate_keys(struct jx *j, void **i);
372 
373 /* Get the current key while iterating over an object.
374  * The iteration variable must have been passed to jx_iterate_keys
375  * or jx_iterate_values. This directly fetches the current key rather than
376  * doing a lookup from the beginning, so it takes constant time and
377  * can handle repeated keys.
378  */
379 const char *jx_get_key(void **i);
380 
381 /* Get the current value while iterating over an object.
382  * The iteration variable must have been passed to jx_iterate_keys
383  * or jx_iterate_values. This directly fetches the current value rather than
384  * doing a lookup from the beginning, so it takes constant time and
385  * can handle repeated keys.
386  */
387 struct jx *jx_get_value(void **i);
388 
389 
391 struct jx *jx_merge(struct jx *j, ...);
392 
393 #endif
394 
395 /*vim: set noexpandtab tabstop=8: */
jx_error
struct jx * jx_error(struct jx *err)
Create a JX_ERROR.
jx_item
struct jx_item * jx_item(struct jx *value, struct jx_item *next)
Create a JX array item.
jx::string_value
char * string_value
value of JX_STRING
Definition: jx.h:124
jx_string_nocopy
struct jx * jx_string_nocopy(char *string_value)
Create a JX string value without copying (uncommon).
JX_ARRAY
@ JX_ARRAY
array containing values
Definition: jx.h:50
jx_array
struct jx * jx_array(struct jx_item *items)
Create a JX array.
jx_array_length
int jx_array_length(struct jx *array)
Get the length of an array.
jx_isfalse
int jx_isfalse(struct jx *j)
Test an expression for the boolean value FALSE.
jx::boolean_value
int boolean_value
value of JX_BOOLEAN
Definition: jx.h:121
jx_comprehension::variable
char * variable
variable for comprehension
Definition: jx.h:61
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_comprehension::condition
struct jx * condition
condition for filtering list comprehension
Definition: jx.h:63
JX_OPERATOR
@ JX_OPERATOR
operator on multiple values.
Definition: jx.h:52
jx_pair_delete
void jx_pair_delete(struct jx_pair *p)
Delete a key-value pair.
jx::integer_value
jx_int_t integer_value
value of JX_INTEGER
Definition: jx.h:122
jx_insert
int jx_insert(struct jx *object, struct jx *key, struct jx *value)
Insert a key-value pair into an object.
jx_lookup_string
const char * jx_lookup_string(struct jx *object, const char *key)
Search for a string item in an object.
jx_item::next
struct jx_item * next
pointer to next item
Definition: jx.h:73
jx_lookup_integer
jx_int_t jx_lookup_integer(struct jx *object, const char *key)
Search for an integer item in an object.
jx_arrayv
struct jx * jx_arrayv(struct jx *value,...)
Create a JX array with inline items.
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_lookup_double
double jx_lookup_double(struct jx *object, const char *key)
Search for a double item in an object.
jx_lookup_boolean
int jx_lookup_boolean(struct jx *object, const char *key)
Search for a boolean item in an object.
jx_operator
Definition: jx.h:108
jx_array_insert
void jx_array_insert(struct jx *array, struct jx *value)
Insert an item at the beginning of an array.
jx_comprehension::elements
struct jx * elements
items for list comprehension
Definition: jx.h:62
jx_array_concat
struct jx * jx_array_concat(struct jx *array,...)
Concatenate the given arrays into a single array.
jx_iterate_array
struct jx * jx_iterate_array(struct jx *j, void **i)
Iterate over the values in an array.
jx_pair::value
struct jx * value
value of this pair
Definition: jx.h:81
jx::pairs
struct jx_pair * pairs
value of JX_OBJECT
Definition: jx.h:127
JX_STRING
@ JX_STRING
string value
Definition: jx.h:48
jx::oper
struct jx_operator oper
value of JX_OPERATOR
Definition: jx.h:128
JX_INTEGER
@ JX_INTEGER
integer value
Definition: jx.h:46
jx_insert_string
void jx_insert_string(struct jx *object, const char *key, const char *value)
Insert a string value into an object.
jx::items
struct jx_item * items
value of JX_ARRAY
Definition: jx.h:126
jx_delete
void jx_delete(struct jx *j)
Delete an expression recursively.
jx_item
JX item linked-list used by JX_ARRAY and jx::items.
Definition: jx.h:69
jx_insert_boolean
void jx_insert_boolean(struct jx *object, const char *key, int value)
Insert a boolean value into an object.
JX_OBJECT
@ JX_OBJECT
object containing key-value pairs
Definition: jx.h:51
jx_comprehension
Definition: jx.h:59
jx_isatomic
int jx_isatomic(struct jx *j)
Test for an atomic value.
JX_BOOLEAN
@ JX_BOOLEAN
true or false
Definition: jx.h:45
JX_NULL
@ JX_NULL
null value
Definition: jx.h:44
jx_remove
struct jx * jx_remove(struct jx *object, struct jx *key)
Remove a key-value pair from an object.
jx_comprehension_delete
void jx_comprehension_delete(struct jx_comprehension *comp)
Delete a comprehension.
jx::symbol_name
char * symbol_name
value of JX_SYMBOL
Definition: jx.h:125
jx_array_index
struct jx * jx_array_index(struct jx *array, int nth)
Get the nth item in an array.
jx::line
unsigned line
line where this value was defined
Definition: jx.h:119
jx::double_value
double double_value
value of JX_DOUBLE
Definition: jx.h:123
jx_format
struct jx * jx_format(const char *fmt,...)
Create a JX string value using prinf style formatting.
jx::type
jx_type_t type
type of this value
Definition: jx.h:118
jx_boolean
struct jx * jx_boolean(int boolean_value)
Create a JX boolean value.
jx_istype
int jx_istype(struct jx *j, jx_type_t type)
Test an expression's type.
jx_item_delete
void jx_item_delete(struct jx_item *i)
Delete an array item.
jx_string
struct jx * jx_string(const char *string_value)
Create a JX string value.
jx_object
struct jx * jx_object(struct jx_pair *pairs)
Create a JX object.
jx_pair::key
struct jx * key
key of this pair
Definition: jx.h:80
jx_export
void jx_export(struct jx *j)
Export a jx object into the current environment using setenv().
jx_pair::next
struct jx_pair * next
pointer to next pair
Definition: jx.h:83
jx_double
struct jx * jx_double(double double_value)
Create a JX floating point value.
jx_is_constant
int jx_is_constant(struct jx *j)
Determine if an expression is constant.
jx_pair
JX key-value pairs used by JX_OBJECT and jx::pairs.
Definition: jx.h:78
jx_insert_double
void jx_insert_double(struct jx *object, const char *key, double value)
Insert a double value into an object.
jx_lookup
struct jx * jx_lookup(struct jx *object, const char *key)
Search for a arbitrary item in an object.
jx_iterate_values
struct jx * jx_iterate_values(struct jx *j, void **i)
Iterate over the values in an object.
JX_DOUBLE
@ JX_DOUBLE
floating point value
Definition: jx.h:47
jx_objectv
struct jx * jx_objectv(const char *key, struct jx *value,...)
Create a JX object.
jx_type_t
jx_type_t
JX atomic type.
Definition: jx.h:43
jx_iterate_keys
const char * jx_iterate_keys(struct jx *j, void **i)
Iterate over the keys 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_array_append
void jx_array_append(struct jx *array, struct jx *value)
Append an item at the end of an array.
jx_equals
int jx_equals(struct jx *j, struct jx *k)
Test two expressions for equality.
jx_merge
struct jx * jx_merge(struct jx *j,...)
Merge an arbitrary number of JX_OBJECTs into a single new one.
jx_item::value
struct jx * value
value of this item
Definition: jx.h:71
jx_null
struct jx * jx_null()
Create a JX null value.
jx_istrue
int jx_istrue(struct jx *j)
Test an expression for the boolean value TRUE.
JX_ERROR
@ JX_ERROR
indicates failed evaluation
Definition: jx.h:53
jx_lookup_string_dup
char * jx_lookup_string_dup(struct jx *object, const char *key)
Search for a string item in an object.
jx_operator
struct jx * jx_operator(jx_operator_t oper, struct jx *left, struct jx *right)
Create a JX binary expression,.
jx
JX value representing any expression type.
Definition: jx.h:117
jx_array_shift
struct jx * jx_array_shift(struct jx *array)
Remove and return the first element in the array.
jx_symbol
struct jx * jx_symbol(const char *symbol_name)
Create a JX symbol.
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_integer
struct jx * jx_integer(jx_int_t integer_value)
Create a JX integer value.
jx_copy
struct jx * jx_copy(struct jx *j)
Duplicate an expression.
jx::err
struct jx * err
error value of JX_ERROR
Definition: jx.h:129
JX_SYMBOL
@ JX_SYMBOL
variable identifier
Definition: jx.h:49