00001
00002
00003
00004
00005
00006
00007 #ifndef JX_H
00008 #define JX_H
00009
00039 #include <stdint.h>
00040
00042 typedef enum {
00043 JX_NULL = 0,
00044 JX_BOOLEAN,
00045 JX_INTEGER,
00046 JX_DOUBLE,
00047 JX_STRING,
00048 JX_SYMBOL,
00049 JX_ARRAY,
00050 JX_OBJECT,
00051 JX_OPERATOR,
00052 JX_FUNCTION,
00053 JX_ERROR,
00054 } jx_type_t;
00055
00056 typedef int64_t jx_int_t;
00057
00058 struct jx_comprehension {
00059 unsigned line;
00060 char *variable;
00061 struct jx *elements;
00062 struct jx *condition;
00063 struct jx_comprehension *next;
00064 };
00065
00068 struct jx_item {
00069 unsigned line;
00070 struct jx *value;
00071 struct jx_comprehension *comp;
00072 struct jx_item *next;
00073 };
00074
00077 struct jx_pair {
00078 struct jx *key;
00079 struct jx *value;
00080 unsigned line;
00081 struct jx_pair *next;
00082 };
00083
00084 typedef enum {
00085 JX_OP_EQ,
00086 JX_OP_NE,
00087 JX_OP_LE,
00088 JX_OP_LT,
00089 JX_OP_GE,
00090 JX_OP_GT,
00091 JX_OP_ADD,
00092 JX_OP_SUB,
00093 JX_OP_MUL,
00094 JX_OP_DIV,
00095 JX_OP_MOD,
00096 JX_OP_AND,
00097 JX_OP_OR,
00098 JX_OP_NOT,
00099 JX_OP_LOOKUP,
00100 JX_OP_CALL,
00101 JX_OP_SLICE,
00102 JX_OP_INVALID,
00103 } jx_operator_t;
00104
00105 struct jx_operator {
00106 jx_operator_t type;
00107 unsigned line;
00108 struct jx *left;
00109 struct jx *right;
00110 };
00111
00112 typedef enum {
00113 JX_BUILTIN_LAMBDA,
00114 JX_BUILTIN_RANGE,
00115 JX_BUILTIN_FORMAT,
00116 JX_BUILTIN_JOIN,
00117 JX_BUILTIN_CEIL,
00118 JX_BUILTIN_FLOOR,
00119 JX_BUILTIN_BASENAME,
00120 JX_BUILTIN_DIRNAME,
00121 JX_BUILTIN_LISTDIR,
00122 JX_BUILTIN_ESCAPE,
00123 } jx_builtin_t;
00124
00125 struct jx_function {
00126 char *name;
00127 unsigned line;
00128 struct jx_item *params;
00129 struct jx *body;
00130 jx_builtin_t builtin;
00131 };
00132
00135 struct jx {
00136 jx_type_t type;
00137 unsigned line;
00138 union {
00139 int boolean_value;
00140 jx_int_t integer_value;
00141 double double_value;
00142 char * string_value;
00143 char * symbol_name;
00144 struct jx_item *items;
00145 struct jx_pair *pairs;
00146 struct jx_operator oper;
00147 struct jx_function func;
00148 struct jx *err;
00149 } u;
00150 };
00151
00153 struct jx * jx_null();
00154
00156 struct jx * jx_boolean( int boolean_value );
00157
00159 struct jx * jx_integer( jx_int_t integer_value );
00160
00162 struct jx * jx_double( double double_value );
00163
00165 struct jx * jx_string( const char *string_value );
00166
00168 struct jx * jx_format( const char *fmt, ... );
00169
00172 struct jx * jx_symbol( const char *symbol_name );
00173
00175 struct jx * jx_error( struct jx *err );
00176
00180 struct jx *jx_function(const char *name, jx_builtin_t op,
00181 struct jx_item *params, struct jx *body);
00182
00184 struct jx * jx_array( struct jx_item *items );
00185
00187 struct jx * jx_arrayv( struct jx *value, ... );
00188
00190 struct jx * jx_object( struct jx_pair *pairs );
00191
00193 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
00194
00196 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
00197
00199 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
00200
00208 struct jx_comprehension *jx_comprehension(const char *variable, struct jx *elements, struct jx *condition, struct jx_comprehension *next);
00209
00211 int jx_istype( struct jx *j, jx_type_t type );
00212
00214 int jx_isatomic( struct jx *j );
00215
00217 int jx_istrue( struct jx *j );
00218
00219 int jx_comprehension_equals(struct jx_comprehension *j, struct jx_comprehension *k);
00220 int jx_item_equals(struct jx_item *j, struct jx_item *k);
00221 int jx_pair_equals(struct jx_pair *j, struct jx_pair *k);
00222
00225 int jx_equals( struct jx *j, struct jx *k );
00226
00228 int jx_array_length( struct jx *array );
00229
00230 struct jx_comprehension *jx_comprehension_copy(struct jx_comprehension *c);
00231 struct jx_item *jx_item_copy(struct jx_item *i);
00232 struct jx_pair *jx_pair_copy(struct jx_pair *p);
00233
00236 struct jx * jx_copy( struct jx *j );
00237
00239 void jx_delete( struct jx *j );
00240
00242 void jx_pair_delete( struct jx_pair *p );
00243
00245 void jx_item_delete( struct jx_item *i );
00246
00248 void jx_comprehension_delete(struct jx_comprehension *comp);
00249
00251 struct jx * jx_remove( struct jx *object, struct jx *key );
00252
00254 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
00255
00257 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
00258
00260 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
00261
00263 void jx_insert_double( struct jx *object, const char *key, double value );
00264
00266 void jx_insert_string( struct jx *object, const char *key, const char *value );
00267
00269 struct jx * jx_lookup( struct jx *object, const char *key );
00270
00271
00272 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
00273
00275 const char * jx_lookup_string( struct jx *object, const char *key );
00276
00278 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
00279
00281 int jx_lookup_boolean( struct jx *object, const char *key );
00282
00284 double jx_lookup_double( struct jx *object, const char *key );
00285
00287 void jx_array_insert( struct jx *array, struct jx *value );
00288
00290 void jx_array_append( struct jx *array, struct jx *value );
00291
00293 struct jx * jx_array_index( struct jx *j, int nth );
00294
00296 struct jx *jx_array_concat( struct jx *array, ...);
00297
00301 struct jx *jx_array_shift(struct jx *array);
00302
00304 int jx_is_constant( struct jx *j );
00305
00307 void jx_export( struct jx *j );
00308
00326 struct jx * jx_iterate_array(struct jx *j, void **i);
00327
00346 struct jx * jx_iterate_values(struct jx *j, void **i);
00347
00366 const char *jx_iterate_keys(struct jx *j, void **i);
00367
00368
00369
00370
00371
00372
00373
00374 const char *jx_get_key(void **i);
00375
00376
00377
00378
00379
00380
00381
00382 struct jx *jx_get_value(void **i);
00383
00384
00386 struct jx *jx_merge(struct jx *j, ...);
00387
00388 #endif