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 
42 typedef enum {
43  JX_NULL = 0,
54 } jx_type_t;
55 
56 typedef int64_t jx_int_t;
57 
59  unsigned line;
60  char *variable;
61  struct jx *elements;
62  struct jx *condition;
63  struct jx_comprehension *next;
64 };
65 
68 struct jx_item {
69  unsigned line;
70  struct jx *value;
71  struct jx_comprehension *comp;
72  struct jx_item *next;
73 };
74 
77 struct jx_pair {
78  struct jx *key;
79  struct jx *value;
80  unsigned line;
81  struct jx_pair *next;
82 };
83 
84 typedef enum {
85  JX_OP_EQ,
86  JX_OP_NE,
87  JX_OP_LE,
88  JX_OP_LT,
89  JX_OP_GE,
90  JX_OP_GT,
91  JX_OP_ADD,
92  JX_OP_SUB,
93  JX_OP_MUL,
94  JX_OP_DIV,
95  JX_OP_MOD,
96  JX_OP_AND,
97  JX_OP_OR,
98  JX_OP_NOT,
99  JX_OP_LOOKUP,
100  JX_OP_CALL,
101  JX_OP_SLICE,
102  JX_OP_INVALID,
103 } jx_operator_t;
104 
105 struct jx_operator {
106  jx_operator_t type;
107  unsigned line;
108  struct jx *left;
109  struct jx *right;
110 };
111 
112 typedef enum {
113  JX_BUILTIN_LAMBDA,
114  JX_BUILTIN_RANGE,
115  JX_BUILTIN_FORMAT,
116  JX_BUILTIN_JOIN,
117  JX_BUILTIN_CEIL,
118  JX_BUILTIN_FLOOR,
119  JX_BUILTIN_BASENAME,
120  JX_BUILTIN_DIRNAME,
121  JX_BUILTIN_LISTDIR,
122  JX_BUILTIN_ESCAPE,
123 } jx_builtin_t;
124 
125 struct jx_function {
126  char *name;
127  unsigned line;
128  struct jx_item *params;
129  struct jx *body;
130  jx_builtin_t builtin;
131 };
132 
135 struct jx {
137  unsigned line;
138  union {
140  jx_int_t integer_value;
141  double double_value;
142  char * string_value;
143  char * symbol_name;
144  struct jx_item *items;
145  struct jx_pair *pairs;
146  struct jx_operator oper;
147  struct jx_function func;
148  struct jx *err;
149  } u;
150 };
151 
153 struct jx * jx_null();
154 
156 struct jx * jx_boolean( int boolean_value );
157 
159 struct jx * jx_integer( jx_int_t integer_value );
160 
162 struct jx * jx_double( double double_value );
163 
165 struct jx * jx_string( const char *string_value );
166 
168 struct jx * jx_format( const char *fmt, ... );
169 
172 struct jx * jx_symbol( const char *symbol_name );
173 
175 struct jx * jx_error( struct jx *err );
176 
180 struct jx *jx_function(const char *name, jx_builtin_t op,
181  struct jx_item *params, struct jx *body);
182 
184 struct jx * jx_array( struct jx_item *items );
185 
187 struct jx * jx_arrayv( struct jx *value, ... );
188 
190 struct jx * jx_object( struct jx_pair *pairs );
191 
193 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
194 
196 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
197 
199 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
200 
208 struct jx_comprehension *jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next);
209 
211 int jx_istype( struct jx *j, jx_type_t type );
212 
214 int jx_isatomic( struct jx *j );
215 
217 int jx_istrue( struct jx *j );
218 
219 int jx_comprehension_equals(struct jx_comprehension *j, struct jx_comprehension *k);
220 int jx_item_equals(struct jx_item *j, struct jx_item *k);
221 int jx_pair_equals(struct jx_pair *j, struct jx_pair *k);
222 
225 int jx_equals( struct jx *j, struct jx *k );
226 
228 int jx_array_length( struct jx *array );
229 
230 struct jx_comprehension *jx_comprehension_copy(struct jx_comprehension *c);
231 struct jx_item *jx_item_copy(struct jx_item *i);
232 struct jx_pair *jx_pair_copy(struct jx_pair *p);
233 
236 struct jx * jx_copy( struct jx *j );
237 
239 void jx_delete( struct jx *j );
240 
242 void jx_pair_delete( struct jx_pair *p );
243 
245 void jx_item_delete( struct jx_item *i );
246 
248 void jx_comprehension_delete(struct jx_comprehension *comp);
249 
251 struct jx * jx_remove( struct jx *object, struct jx *key );
252 
254 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
255 
257 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
258 
260 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
261 
263 void jx_insert_double( struct jx *object, const char *key, double value );
264 
266 void jx_insert_string( struct jx *object, const char *key, const char *value );
267 
269 struct jx * jx_lookup( struct jx *object, const char *key );
270 
271 /* Like @ref jx_lookup, but found is set to 1 when the key is found. Useful for when value is false. */
272 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
273 
275 const char * jx_lookup_string( struct jx *object, const char *key );
276 
278 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
279 
281 int jx_lookup_boolean( struct jx *object, const char *key );
282 
284 double jx_lookup_double( struct jx *object, const char *key );
285 
287 void jx_array_insert( struct jx *array, struct jx *value );
288 
290 void jx_array_append( struct jx *array, struct jx *value );
291 
293 struct jx * jx_array_index( struct jx *j, int nth );
294 
296 struct jx *jx_array_concat( struct jx *array, ...);
297 
301 struct jx *jx_array_shift(struct jx *array);
302 
304 int jx_is_constant( struct jx *j );
305 
307 void jx_export( struct jx *j );
308 
326 struct jx * jx_iterate_array(struct jx *j, void **i);
327 
346 struct jx * jx_iterate_values(struct jx *j, void **i);
347 
366 const char *jx_iterate_keys(struct jx *j, void **i);
367 
368 /* Get the current key while iterating over an object.
369  * The iteration variable must have been passed to jx_iterate_keys
370  * or jx_iterate_values. This directly fetches the current key rather than
371  * doing a lookup from the beginning, so it takes constant time and
372  * can handle repeated keys.
373  */
374 const char *jx_get_key(void **i);
375 
376 /* Get the current value while iterating over an object.
377  * The iteration variable must have been passed to jx_iterate_keys
378  * or jx_iterate_values. This directly fetches the current value rather than
379  * doing a lookup from the beginning, so it takes constant time and
380  * can handle repeated keys.
381  */
382 struct jx *jx_get_value(void **i);
383 
384 
386 struct jx *jx_merge(struct jx *j, ...);
387 
388 #endif
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:43
struct jx * jx_remove(struct jx *object, struct jx *key)
Remove a key-value pair from an object.
Definition: jx.h:125
unsigned line
line where this value was defined
Definition: jx.h:137
struct jx * jx_object(struct jx_pair *pairs)
Create a JX object.
indicates failed evaluation
Definition: jx.h:53
struct jx * jx_function(const char *name, jx_builtin_t op, struct jx_item *params, struct jx *body)
Create a JX_FUNCTION.
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:72
int jx_istrue(struct jx *j)
Test an expression for the boolean value TRUE.
true or false
Definition: jx.h:44
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:142
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_format(const char *fmt,...)
Create a JX string value using prinf style formatting.
floating point value
Definition: jx.h:46
object containing key-value pairs
Definition: jx.h:50
JX item linked-list used by JX_ARRAY and jx::items.
Definition: jx.h:68
struct jx * key
key of this pair
Definition: jx.h:78
char * symbol_name
value of JX_SYMBOL
Definition: jx.h:143
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:81
struct jx * jx_error(struct jx *err)
Create a JX_ERROR.
int boolean_value
value of JX_BOOLEAN
Definition: jx.h:139
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:141
struct jx * elements
items for list comprehension
Definition: jx.h:61
Definition: jx.h:105
int jx_is_constant(struct jx *j)
Determine if an expression is constant.
struct jx * value
value of this pair
Definition: jx.h:79
array containing values
Definition: jx.h:49
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:70
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:148
struct jx * jx_string(const char *string_value)
Create a JX string value.
operator on multiple values.
Definition: jx.h:51
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:45
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:58
struct jx_function func
value of JX_FUNCTION
Definition: jx.h:147
variable identifier
Definition: jx.h:48
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:145
struct jx * condition
condition for filtering list comprehension
Definition: jx.h:62
jx_int_t integer_value
value of JX_INTEGER
Definition: jx.h:140
char * variable
variable for comprehension
Definition: jx.h:60
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:135
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:136
struct jx_operator oper
value of JX_OPERATOR
Definition: jx.h:146
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:77
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:42
struct jx * jx_array_index(struct jx *j, int nth)
Get the nth item in an array.
function definition
Definition: jx.h:52
void jx_comprehension_delete(struct jx_comprehension *comp)
Delete a comprehension.
string value
Definition: jx.h:47
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:144
struct jx * jx_null()
Create a JX null value.
int jx_equals(struct jx *j, struct jx *k)
Test two expressions for equality.