00001
00002
00003
00004
00005
00006
00007 #ifndef JX_H
00008 #define JX_H
00009
00039 #include <stdint.h>
00040 #include <assert.h>
00041
00043 typedef enum {
00044 JX_NULL = 0,
00045 JX_BOOLEAN,
00046 JX_INTEGER,
00047 JX_DOUBLE,
00048 JX_STRING,
00049 JX_SYMBOL,
00050 JX_ARRAY,
00051 JX_OBJECT,
00052 JX_OPERATOR,
00053 JX_FUNCTION,
00054 JX_ERROR,
00055 } jx_type_t;
00056
00057 typedef int64_t jx_int_t;
00058
00061 struct jx_item {
00062 struct jx *value;
00063 struct jx_item *next;
00064 };
00065
00068 struct jx_pair {
00069 struct jx *key;
00070 struct jx *value;
00071 struct jx_pair *next;
00072 };
00073
00074 typedef enum {
00075 JX_OP_EQ,
00076 JX_OP_NE,
00077 JX_OP_LE,
00078 JX_OP_LT,
00079 JX_OP_GE,
00080 JX_OP_GT,
00081 JX_OP_ADD,
00082 JX_OP_SUB,
00083 JX_OP_MUL,
00084 JX_OP_DIV,
00085 JX_OP_MOD,
00086 JX_OP_AND,
00087 JX_OP_OR,
00088 JX_OP_NOT,
00089 JX_OP_LOOKUP,
00090 JX_OP_INVALID,
00091 } jx_operator_t;
00092
00093 struct jx_operator {
00094 jx_operator_t type;
00095 struct jx *left;
00096 struct jx *right;
00097 };
00098
00099 typedef enum {
00100 JX_FUNCTION_INVALID = 0,
00101 JX_FUNCTION_DBG,
00102 JX_FUNCTION_RANGE,
00103 JX_FUNCTION_STR,
00104 JX_FUNCTION_FOREACH,
00105 JX_FUNCTION_JOIN,
00106 JX_FUNCTION_LET,
00107 } jx_function_t;
00108
00109 struct jx_function {
00110 jx_function_t function;
00111 struct jx *arguments;
00112 };
00113
00116 struct jx {
00117 jx_type_t type;
00118 union {
00119 int boolean_value;
00120 jx_int_t integer_value;
00121 double double_value;
00122 char * string_value;
00123 char * symbol_name;
00124 struct jx_item *items;
00125 struct jx_pair *pairs;
00126 struct jx_operator oper;
00127 struct jx_function func;
00128 struct jx *err;
00129 } u;
00130 };
00131
00133 struct jx * jx_null();
00134
00136 struct jx * jx_boolean( int boolean_value );
00137
00139 struct jx * jx_integer( jx_int_t integer_value );
00140
00142 struct jx * jx_double( double double_value );
00143
00145 struct jx * jx_string( const char *string_value );
00146
00148 struct jx * jx_format( const char *fmt, ... );
00149
00151 struct jx *jx_function( jx_function_t func, struct jx *args );
00152
00155 struct jx * jx_symbol( const char *symbol_name );
00156
00158 struct jx * jx_error( struct jx *err );
00159
00161 struct jx * jx_array( struct jx_item *items );
00162
00164 struct jx * jx_arrayv( struct jx *value, ... );
00165
00167 struct jx * jx_object( struct jx_pair *pairs );
00168
00170 struct jx * jx_operator( jx_operator_t oper, struct jx *left, struct jx *right );
00171
00173 struct jx_pair * jx_pair( struct jx *key, struct jx *value, struct jx_pair *next );
00174
00176 struct jx_item * jx_item( struct jx *value, struct jx_item *next );
00177
00179 int jx_istype( struct jx *j, jx_type_t type );
00180
00182 int jx_istrue( struct jx *j );
00183
00186 int jx_equals( struct jx *j, struct jx *k );
00187
00189 int jx_array_length( struct jx *array );
00190
00193 struct jx * jx_copy( struct jx *j );
00194
00196 void jx_delete( struct jx *j );
00197
00199 void jx_pair_delete( struct jx_pair *p );
00200
00202 void jx_item_delete( struct jx_item *i );
00203
00205 struct jx * jx_remove( struct jx *object, struct jx *key );
00206
00208 int jx_insert( struct jx *object, struct jx *key, struct jx *value );
00209
00211 int jx_insert_unless_empty( struct jx *object, struct jx *key, struct jx *value );
00212
00214 void jx_insert_integer( struct jx *object, const char *key, jx_int_t value );
00215
00217 void jx_insert_double( struct jx *object, const char *key, double value );
00218
00220 void jx_insert_string( struct jx *object, const char *key, const char *value );
00221
00223 struct jx * jx_lookup( struct jx *object, const char *key );
00224
00225
00226 struct jx * jx_lookup_guard( struct jx *j, const char *key, int *found );
00227
00229 const char * jx_lookup_string( struct jx *object, const char *key );
00230
00232 jx_int_t jx_lookup_integer( struct jx *object, const char *key );
00233
00235 int jx_lookup_boolean( struct jx *object, const char *key );
00236
00238 double jx_lookup_double( struct jx *object, const char *key );
00239
00241 void jx_array_insert( struct jx *array, struct jx *value );
00242
00244 void jx_array_append( struct jx *array, struct jx *value );
00245
00247 struct jx * jx_array_index( struct jx *j, int nth );
00248
00250 struct jx *jx_array_concat( struct jx *array, ...);
00251
00253 int jx_is_constant( struct jx *j );
00254
00256 void jx_export( struct jx *j );
00257
00275 struct jx * jx_iterate_array(struct jx *j, void **i);
00276
00295 struct jx * jx_iterate_values(struct jx *j, void **i);
00296
00315 struct jx * jx_iterate_keys(struct jx *j, void **i);
00316
00318 struct jx *jx_merge(struct jx *j, ...);
00319
00321 const char *jx_error_name(int code);
00322
00324 int jx_error_valid(struct jx *j);
00325
00326 #endif