cctools
jx.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2015- 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  struct jx *key;
80  struct jx *value;
81  unsigned line;
82  struct jx_pair *next;
83 };
84 
85 typedef enum {
86  JX_OP_EQ,
87  JX_OP_NE,
88  JX_OP_LE,
89  JX_OP_LT,
90  JX_OP_GE,
91  JX_OP_GT,
92  JX_OP_ADD,
93  JX_OP_SUB,
94  JX_OP_MUL,
95  JX_OP_DIV,
96  JX_OP_MOD,
97  JX_OP_AND,
98  JX_OP_OR,
99  JX_OP_NOT,
100  JX_OP_LOOKUP,
101  JX_OP_CALL,
102  JX_OP_SLICE,
103  JX_OP_INVALID,
104 } jx_operator_t;
105 
106 struct jx_operator {
107  jx_operator_t type;
108  unsigned line;
109  struct jx *left;
110  struct jx *right;
111 };
112 
115 struct jx {
117  unsigned line;
118  union {
120  jx_int_t integer_value;
121  double double_value;
122  char * string_value;
123  char * symbol_name;
124  struct jx_item *items;
125  struct jx_pair *pairs;
126  struct jx_operator oper;
127  struct jx *err;
128  } u;
129 };
130 
132 struct jx * jx_null();
133 
135 struct jx * jx_boolean( int boolean_value );
136 
138 struct jx * jx_integer( jx_int_t integer_value );
139 
141 struct jx * jx_double( double double_value );
142 
144 struct jx * jx_string( const char *string_value );
145 
147 struct jx * jx_string_nocopy( char *string_value );
148 
150 struct jx * jx_format( const char *fmt, ... );
151 
154 struct jx * jx_symbol( const char *symbol_name );
155 
157 struct jx * jx_error( struct jx *err );
158 
160 struct jx * jx_array( struct jx_item *items );
161 
163 struct jx * jx_arrayv( struct jx *value, ... );
164 
166 struct jx * jx_object( struct jx_pair *pairs );
167 
178 struct jx * jx_objectv( const char *key, struct jx *value, ... );
179 
181 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
182 
184 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
185 
187 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
188 
196 struct jx_comprehension *jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next);
197 
199 int jx_istype( struct jx *j, jx_type_t type );
200 
202 int jx_isatomic( struct jx *j );
203 
205 int jx_istrue( struct jx *j );
206 
207 int jx_comprehension_equals(struct jx_comprehension *j, struct jx_comprehension *k);
208 int jx_item_equals(struct jx_item *j, struct jx_item *k);
209 int jx_pair_equals(struct jx_pair *j, struct jx_pair *k);
210 
213 int jx_equals( struct jx *j, struct jx *k );
214 
216 int jx_array_length( struct jx *array );
217 
218 struct jx_comprehension *jx_comprehension_copy(struct jx_comprehension *c);
219 struct jx_item *jx_item_copy(struct jx_item *i);
220 struct jx_pair *jx_pair_copy(struct jx_pair *p);
221 
224 struct jx * jx_copy( struct jx *j );
225 
227 void jx_delete( struct jx *j );
228 
230 void jx_pair_delete( struct jx_pair *p );
231 
233 void jx_item_delete( struct jx_item *i );
234 
236 void jx_comprehension_delete(struct jx_comprehension *comp);
237 
239 struct jx * jx_remove( struct jx *object, struct jx *key );
240 
242 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
243 
245 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
246 
248 void jx_insert_boolean( struct jx *object, const char *key, int value );
249 
251 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
252 
254 void jx_insert_double( struct jx *object, const char *key, double value );
255 
257 void jx_insert_string( struct jx *object, const char *key, const char *value );
258 
260 struct jx * jx_lookup( struct jx *object, const char *key );
261 
262 /* Like @ref jx_lookup, but found is set to 1 when the key is found. Useful for when value is false. */
263 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
264 
266 const char * jx_lookup_string( struct jx *object, const char *key );
267 
269 char * jx_lookup_string_dup( struct jx *object, const char *key );
270 
272 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
273 
275 int jx_lookup_boolean( struct jx *object, const char *key );
276 
278 double jx_lookup_double( struct jx *object, const char *key );
279 
281 void jx_array_insert( struct jx *array, struct jx *value );
282 
284 void jx_array_append( struct jx *array, struct jx *value );
285 
287 struct jx * jx_array_index( struct jx *array, int nth );
288 
290 struct jx *jx_array_concat( struct jx *array, ...);
291 
295 struct jx *jx_array_shift(struct jx *array);
296 
298 int jx_is_constant( struct jx *j );
299 
301 void jx_export( struct jx *j );
302 
320 struct jx * jx_iterate_array(struct jx *j, void **i);
321 
340 struct jx * jx_iterate_values(struct jx *j, void **i);
341 
360 const char *jx_iterate_keys(struct jx *j, void **i);
361 
362 /* Get the current key while iterating over an object.
363  * The iteration variable must have been passed to jx_iterate_keys
364  * or jx_iterate_values. This directly fetches the current key rather than
365  * doing a lookup from the beginning, so it takes constant time and
366  * can handle repeated keys.
367  */
368 const char *jx_get_key(void **i);
369 
370 /* Get the current value while iterating over an object.
371  * The iteration variable must have been passed to jx_iterate_keys
372  * or jx_iterate_values. This directly fetches the current value rather than
373  * doing a lookup from the beginning, so it takes constant time and
374  * can handle repeated keys.
375  */
376 struct jx *jx_get_value(void **i);
377 
378 
380 struct jx *jx_merge(struct jx *j, ...);
381 
382 #endif
383 
384 /*vim: set noexpandtab tabstop=4: */
int jx_insert(struct jx *object, struct jx *key, struct jx *value)
Insert a key-value pair into an object.
null value
Definition: jx.h:44
struct jx * jx_remove(struct jx *object, struct jx *key)
Remove a key-value pair from an object.
unsigned line
line where this value was defined
Definition: jx.h:117
struct jx * jx_object(struct jx_pair *pairs)
Create a JX object.
indicates failed evaluation
Definition: jx.h:53
struct jx_item * jx_item(struct jx *value, struct jx_item *next)
Create a JX array item.
struct jx_item * next
pointer to next item
Definition: jx.h:73
int jx_istrue(struct jx *j)
Test an expression for the boolean value TRUE.
true or false
Definition: jx.h:45
int jx_istype(struct jx *j, jx_type_t type)
Test an expression&#39;s type.
char * string_value
value of JX_STRING
Definition: jx.h:122
void jx_insert_double(struct jx *object, const char *key, double value)
Insert a double value into an object.
void jx_array_insert(struct jx *array, struct jx *value)
Insert an item at the beginning of an array.
int jx_lookup_boolean(struct jx *object, const char *key)
Search for a boolean item in an object.
struct jx * jx_objectv(const char *key, struct jx *value,...)
Create a JX object.
struct jx * jx_format(const char *fmt,...)
Create a JX string value using prinf style formatting.
floating point value
Definition: jx.h:47
object containing key-value pairs
Definition: jx.h:51
JX item linked-list used by JX_ARRAY and jx::items.
Definition: jx.h:69
struct jx * key
key of this pair
Definition: jx.h:79
char * symbol_name
value of JX_SYMBOL
Definition: jx.h:123
void jx_pair_delete(struct jx_pair *p)
Delete a key-value pair.
void jx_insert_string(struct jx *object, const char *key, const char *value)
Insert a string value into an object.
void jx_delete(struct jx *j)
Delete an expression recursively.
struct jx * jx_boolean(int boolean_value)
Create a JX boolean value.
double jx_lookup_double(struct jx *object, const char *key)
Search for a double item in an object.
struct jx * jx_merge(struct jx *j,...)
Merge an arbitrary number of JX_OBJECTs into a single new one.
struct jx_pair * next
pointer to next pair
Definition: jx.h:82
struct jx * jx_error(struct jx *err)
Create a JX_ERROR.
int boolean_value
value of JX_BOOLEAN
Definition: jx.h:119
struct jx * jx_iterate_array(struct jx *j, void **i)
Iterate over the values in an array.
int jx_array_length(struct jx *array)
Get the length of an array.
double double_value
value of JX_DOUBLE
Definition: jx.h:121
void jx_insert_boolean(struct jx *object, const char *key, int value)
Insert a boolean value into an object.
struct jx * jx_string_nocopy(char *string_value)
Create a JX string value without copying (uncommon).
struct jx * elements
items for list comprehension
Definition: jx.h:62
Definition: jx.h:106
int jx_is_constant(struct jx *j)
Determine if an expression is constant.
struct jx * value
value of this pair
Definition: jx.h:80
array containing values
Definition: jx.h:50
struct jx * jx_array_index(struct jx *array, int nth)
Get the nth item in an array.
struct jx * jx_integer(jx_int_t integer_value)
Create a JX integer value.
struct jx * jx_copy(struct jx *j)
Duplicate an expression.
struct jx * value
value of this item
Definition: jx.h:71
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...
struct jx * jx_operator(jx_operator_t oper, struct jx *left, struct jx *right)
Create a JX binary expression,.
struct jx * err
error value of JX_ERROR
Definition: jx.h:127
struct jx * jx_string(const char *string_value)
Create a JX string value.
operator on multiple values.
Definition: jx.h:52
struct jx * jx_array(struct jx_item *items)
Create a JX array.
int jx_isatomic(struct jx *j)
Test for an atomic value.
integer value
Definition: jx.h:46
const char * jx_lookup_string(struct jx *object, const char *key)
Search for a string item in an object.
void jx_export(struct jx *j)
Export a jx object as a set of environment variables.
Definition: jx.h:59
variable identifier
Definition: jx.h:49
struct jx * jx_iterate_values(struct jx *j, void **i)
Iterate over the values in an object.
struct jx_pair * pairs
value of JX_OBJECT
Definition: jx.h:125
struct jx * condition
condition for filtering list comprehension
Definition: jx.h:63
jx_int_t integer_value
value of JX_INTEGER
Definition: jx.h:120
char * variable
variable for comprehension
Definition: jx.h:61
struct jx * jx_symbol(const char *symbol_name)
Create a JX symbol.
void jx_insert_integer(struct jx *object, const char *key, jx_int_t value)
Insert an integer value into an object.
void jx_item_delete(struct jx_item *i)
Delete an array item.
JX value representing any expression type.
Definition: jx.h:115
const char * jx_iterate_keys(struct jx *j, void **i)
Iterate over the keys in an object.
jx_type_t type
type of this value
Definition: jx.h:116
struct jx_operator oper
value of JX_OPERATOR
Definition: jx.h:126
struct jx * jx_array_shift(struct jx *array)
Remove and return the first element in the array.
struct jx * jx_lookup(struct jx *object, const char *key)
Search for a arbitrary item in an object.
struct jx * jx_array_concat(struct jx *array,...)
Concatenate the given arrays into a single array.
struct jx_comprehension * jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next)
Create a JX comprehension.
struct jx_pair * jx_pair(struct jx *key, struct jx *value, struct jx_pair *next)
Create a JX key-value pair.
struct jx * jx_double(double double_value)
Create a JX floating point value.
struct jx * jx_arrayv(struct jx *value,...)
Create a JX array with inline items.
JX key-value pairs used by JX_OBJECT and jx::pairs.
Definition: jx.h:78
jx_int_t jx_lookup_integer(struct jx *object, const char *key)
Search for an integer item in an object.
jx_type_t
JX atomic type.
Definition: jx.h:43
void jx_comprehension_delete(struct jx_comprehension *comp)
Delete a comprehension.
string value
Definition: jx.h:48
void jx_array_append(struct jx *array, struct jx *value)
Append an item at the end of an array.
struct jx_item * items
value of JX_ARRAY
Definition: jx.h:124
struct jx * jx_null()
Create a JX null value.
char * jx_lookup_string_dup(struct jx *object, const char *key)
Search for a string item in an object.
int jx_equals(struct jx *j, struct jx *k)
Test two expressions for equality.