1e475a54fSMasatake YAMATO /* 2e475a54fSMasatake YAMATO Copyright (c) 2009 Masatake YAMATO 3e475a54fSMasatake YAMATO 4e475a54fSMasatake YAMATO Permission is hereby granted, free of charge, to any person obtaining a copy 5e475a54fSMasatake YAMATO of this software and associated documentation files (the "Software"), to deal 6e475a54fSMasatake YAMATO in the Software without restriction, including without limitation the rights 7e475a54fSMasatake YAMATO to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8e475a54fSMasatake YAMATO copies of the Software, and to permit persons to whom the Software is 9e475a54fSMasatake YAMATO furnished to do so, subject to the following conditions: 10e475a54fSMasatake YAMATO 11e475a54fSMasatake YAMATO The above copyright notice and this permission notice shall be included in 12e475a54fSMasatake YAMATO all copies or substantial portions of the Software. 13e475a54fSMasatake YAMATO 14e475a54fSMasatake YAMATO THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15e475a54fSMasatake YAMATO IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16e475a54fSMasatake YAMATO FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17e475a54fSMasatake YAMATO AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18e475a54fSMasatake YAMATO LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19e475a54fSMasatake YAMATO OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20e475a54fSMasatake YAMATO THE SOFTWARE. */ 21e475a54fSMasatake YAMATO 22e475a54fSMasatake YAMATO #ifndef __ES_LANG_C_STDC99_H__ 23e475a54fSMasatake YAMATO #define __ES_LANG_C_STDC99_H__ 24e475a54fSMasatake YAMATO 25e475a54fSMasatake YAMATO #ifndef _GNU_SOURCE 26e475a54fSMasatake YAMATO #define _GNU_SOURCE 27e475a54fSMasatake YAMATO #endif 28e475a54fSMasatake YAMATO #include <stdio.h> 29e475a54fSMasatake YAMATO 30e475a54fSMasatake YAMATO #include "mio.h" 31e475a54fSMasatake YAMATO 32e475a54fSMasatake YAMATO #ifdef __cplusplus 33e475a54fSMasatake YAMATO extern "C" { 34e475a54fSMasatake YAMATO #endif 35e475a54fSMasatake YAMATO 36e475a54fSMasatake YAMATO enum _EsType { 37e475a54fSMasatake YAMATO ES_TYPE_NIL, 38e475a54fSMasatake YAMATO ES_TYPE_INTEGER, 39e475a54fSMasatake YAMATO ES_TYPE_REAL, 40e475a54fSMasatake YAMATO ES_TYPE_BOOLEAN, 41e475a54fSMasatake YAMATO ES_TYPE_SYMBOL, 42e475a54fSMasatake YAMATO ES_TYPE_STRING, 43e475a54fSMasatake YAMATO ES_TYPE_CONS, 44e475a54fSMasatake YAMATO ES_TYPE_REGEX, 45e475a54fSMasatake YAMATO /* ... */ 46e475a54fSMasatake YAMATO ES_TYPE_ERROR, 47e475a54fSMasatake YAMATO /* ... */ 48e475a54fSMasatake YAMATO ES_TYPE_BUILTIN_LAST = ES_TYPE_ERROR, 49e475a54fSMasatake YAMATO ES_TYPE_FOREIGNER_START, 50e475a54fSMasatake YAMATO }; 51e475a54fSMasatake YAMATO typedef enum _EsType EsType; 52e475a54fSMasatake YAMATO 53e475a54fSMasatake YAMATO struct _EsObject; 54e475a54fSMasatake YAMATO typedef struct _EsObject EsObject; 55e475a54fSMasatake YAMATO 56b73e7385SMasatake YAMATO const char* es_type_get_name (int t); 57e475a54fSMasatake YAMATO 58b73e7385SMasatake YAMATO EsType es_object_get_type (const EsObject* object); 59e475a54fSMasatake YAMATO 60e475a54fSMasatake YAMATO 61e475a54fSMasatake YAMATO EsObject* es_object_ref (EsObject* object); 62e475a54fSMasatake YAMATO void es_object_unref (EsObject* object); 63e475a54fSMasatake YAMATO void es_object_unref_batch (EsObject* array[], 64e475a54fSMasatake YAMATO unsigned int count); 65e475a54fSMasatake YAMATO EsObject* es_object_autounref (EsObject* object); 66e475a54fSMasatake YAMATO 67e475a54fSMasatake YAMATO int es_object_equal (const EsObject* self, 68e475a54fSMasatake YAMATO const EsObject* other); 69e475a54fSMasatake YAMATO 70e475a54fSMasatake YAMATO int es_atom (const EsObject* object); 71e475a54fSMasatake YAMATO 72e475a54fSMasatake YAMATO #define ES_ERROR_MEMORY es_error_intern("MEMORY-EXHAUSTED") 73e475a54fSMasatake YAMATO #define ES_ERROR_REGEX es_error_intern("WRONG-REGEX-SYNTAX") 74e475a54fSMasatake YAMATO 75e475a54fSMasatake YAMATO /* 76e475a54fSMasatake YAMATO * Nil 77e475a54fSMasatake YAMATO */ 78e475a54fSMasatake YAMATO #define es_nil ((EsObject*)0) 79e475a54fSMasatake YAMATO int es_null (const EsObject* object); 80e475a54fSMasatake YAMATO 81e475a54fSMasatake YAMATO /* 82e475a54fSMasatake YAMATO * Integer 83e475a54fSMasatake YAMATO */ 84e475a54fSMasatake YAMATO EsObject* es_integer_new (int value); 85e475a54fSMasatake YAMATO int es_integer_p (const EsObject* object); 86e475a54fSMasatake YAMATO int es_integer_get (const EsObject* object); 87e475a54fSMasatake YAMATO 88e475a54fSMasatake YAMATO 89e475a54fSMasatake YAMATO /* 90e475a54fSMasatake YAMATO * Real 91e475a54fSMasatake YAMATO */ 92e475a54fSMasatake YAMATO EsObject* es_real_new (double value); 93e475a54fSMasatake YAMATO int es_real_p (const EsObject* object); 94e475a54fSMasatake YAMATO double es_real_get (const EsObject* object); 95e475a54fSMasatake YAMATO 96e475a54fSMasatake YAMATO 97e475a54fSMasatake YAMATO /* 98e475a54fSMasatake YAMATO * Use Integer as Real 99e475a54fSMasatake YAMATO */ 100e475a54fSMasatake YAMATO int es_number_p (const EsObject* object); 101e475a54fSMasatake YAMATO double es_number_get (const EsObject* object); 102e475a54fSMasatake YAMATO 103e475a54fSMasatake YAMATO 104e475a54fSMasatake YAMATO /* 105e475a54fSMasatake YAMATO * Boolean 106e475a54fSMasatake YAMATO */ 107e475a54fSMasatake YAMATO #define es_true (es_boolean_new(1)) 108e475a54fSMasatake YAMATO #define es_false (es_boolean_new(0)) 109e475a54fSMasatake YAMATO EsObject* es_boolean_new (int value); 110e475a54fSMasatake YAMATO int es_boolean_p (const EsObject* object); 111e475a54fSMasatake YAMATO int es_boolean_get (const EsObject* object); 112e475a54fSMasatake YAMATO 113e475a54fSMasatake YAMATO /* 114e475a54fSMasatake YAMATO * String 115e475a54fSMasatake YAMATO */ 116e475a54fSMasatake YAMATO EsObject* es_string_new (const char* value); 117*eeb45f3bSMasanari Iida EsObject* es_string_newL (const char* value, size_t length); 118e475a54fSMasatake YAMATO int es_string_p (const EsObject* object); 119e475a54fSMasatake YAMATO const char* es_string_get (const EsObject* object); 120e475a54fSMasatake YAMATO 121e475a54fSMasatake YAMATO 122e475a54fSMasatake YAMATO /* 123e475a54fSMasatake YAMATO * Symbol 124e475a54fSMasatake YAMATO */ 125e475a54fSMasatake YAMATO EsObject* es_symbol_intern (const char* name); 126e475a54fSMasatake YAMATO int es_symbol_p (const EsObject* object); 127e475a54fSMasatake YAMATO const char* es_symbol_get (const EsObject* object); 128e475a54fSMasatake YAMATO 129e475a54fSMasatake YAMATO void* es_symbol_set_data (const EsObject* object, void *data); 130e475a54fSMasatake YAMATO void* es_symbol_get_data (const EsObject* object); 131e475a54fSMasatake YAMATO 132e475a54fSMasatake YAMATO /* 133e475a54fSMasatake YAMATO * Error 134e475a54fSMasatake YAMATO */ 135e475a54fSMasatake YAMATO 136e475a54fSMasatake YAMATO EsObject* es_error_intern (const char* name); 137e475a54fSMasatake YAMATO int es_error_p (const EsObject* object); 138e475a54fSMasatake YAMATO const char* es_error_name (const EsObject* object); 139e475a54fSMasatake YAMATO EsObject* es_error_set_object (EsObject* error, EsObject* object); 140e475a54fSMasatake YAMATO EsObject* es_error_get_object (const EsObject* error); 141e475a54fSMasatake YAMATO 142e475a54fSMasatake YAMATO 143e475a54fSMasatake YAMATO /* 144e475a54fSMasatake YAMATO * Cons 145e475a54fSMasatake YAMATO */ 146e475a54fSMasatake YAMATO EsObject* es_cons (EsObject* car, EsObject* cdr); 147e475a54fSMasatake YAMATO int es_cons_p (const EsObject* object); 148e475a54fSMasatake YAMATO int es_list_p (const EsObject* object); 149e475a54fSMasatake YAMATO EsObject* es_car (const EsObject* object); 150e475a54fSMasatake YAMATO EsObject* es_cdr (const EsObject* object); 151e475a54fSMasatake YAMATO 152e475a54fSMasatake YAMATO 153e475a54fSMasatake YAMATO /* 154e475a54fSMasatake YAMATO * Regex 155e475a54fSMasatake YAMATO */ 156e475a54fSMasatake YAMATO EsObject* es_regex_compile (const char* pat, 157e475a54fSMasatake YAMATO int case_insensitive); 158e475a54fSMasatake YAMATO int es_regex_p (const EsObject* object); 159e475a54fSMasatake YAMATO EsObject* es_regex_exec (const EsObject* regex, 160e475a54fSMasatake YAMATO const EsObject* str); 161e475a54fSMasatake YAMATO 162e475a54fSMasatake YAMATO /* 163e475a54fSMasatake YAMATO * Foreign pointer 164e475a54fSMasatake YAMATO */ 165e1e0f0f5SMasatake YAMATO EsType es_type_define_pointer (const char *name, 166e475a54fSMasatake YAMATO void (* freefn) (void *), 167e475a54fSMasatake YAMATO int (* equalfn) (const void*, const void*), 168e475a54fSMasatake YAMATO void (* printfn) (const void*, MIO *)); 169e475a54fSMasatake YAMATO 170e7dc9661SMasatake YAMATO /* If the type has sized fat area, the area is filled with zero. */ 171e475a54fSMasatake YAMATO EsObject* es_pointer_new (EsType type, void *ptr); 172e475a54fSMasatake YAMATO void* es_pointer_get (const EsObject *object); 173e475a54fSMasatake YAMATO void* es_pointer_take (EsObject *object); 174e475a54fSMasatake YAMATO 175e475a54fSMasatake YAMATO /* 176e7dc9661SMasatake YAMATO * Fatptr: Foreign pointer with extra data 177e7dc9661SMasatake YAMATO * 178e7dc9661SMasatake YAMATO * init_fat () returns es_true if the initialization ends successfully. 179e7dc9661SMasatake YAMATO * In failure case, init_fat () returns an error object. 180e7dc9661SMasatake YAMATO */ 181e1e0f0f5SMasatake YAMATO EsType es_type_define_fatptr (const char *name, 182e7dc9661SMasatake YAMATO size_t fat_size, 183e7dc9661SMasatake YAMATO EsObject *(* initfat_fn) (void *fat, void * ptr, void *extra), 184e7dc9661SMasatake YAMATO void (* freefn) (void * ptr, void *fat), 185e7dc9661SMasatake YAMATO int (* equalfn) (const void* ptr_a, const void* fat_a, 186e7dc9661SMasatake YAMATO const void* ptr_b, const void* fat_b), 187e7dc9661SMasatake YAMATO void (* printfn) (const void* ptr, const void *fat, MIO *)); 188e7dc9661SMasatake YAMATO /* If initfat_fn is given in the type, the new fat area will is 189e7dc9661SMasatake YAMATO * initialized with the method. 190e7dc9661SMasatake YAMATO * If initfat_fn is not given, and extra is not NULL, the contents 191e7dc9661SMasatake YAMATO * pointed by extra is copied to the fat area. 192e7dc9661SMasatake YAMATO * If initfat_fn is not given, and extra is NULL, the fat area 193e7dc9661SMasatake YAMATO * is filled with zero as es_pointer_new does. */ 194e7dc9661SMasatake YAMATO EsObject* es_fatptr_new (EsType type, void *ptr, void *extra); 195e7dc9661SMasatake YAMATO /* Access to the fat area. Use es_pointer_get to access the pointer. */ 196e7dc9661SMasatake YAMATO void* es_fatptr_get (const EsObject *object); 197e7dc9661SMasatake YAMATO 198e7dc9661SMasatake YAMATO /* 199e475a54fSMasatake YAMATO * Print 200e475a54fSMasatake YAMATO */ 201e475a54fSMasatake YAMATO void es_print (const EsObject* object, 202e475a54fSMasatake YAMATO MIO* out); 203e475a54fSMasatake YAMATO char* es_print_to_string (EsObject* object); 204e475a54fSMasatake YAMATO 205e475a54fSMasatake YAMATO /* 206e475a54fSMasatake YAMATO * Read 207e475a54fSMasatake YAMATO */ 208e475a54fSMasatake YAMATO EsObject* es_read (MIO* in); 209e475a54fSMasatake YAMATO EsObject* es_read_from_string(const char* in, 210e475a54fSMasatake YAMATO const char** saveptr); 211e475a54fSMasatake YAMATO 212e475a54fSMasatake YAMATO #define ES_READER_ERROR es_error_intern("READ-ERROR") 213e475a54fSMasatake YAMATO #define ES_READER_EOF es_error_intern("EOF") 214e475a54fSMasatake YAMATO 215e475a54fSMasatake YAMATO /* 216e475a54fSMasatake YAMATO * Comment 217e475a54fSMasatake YAMATO */ 218e475a54fSMasatake YAMATO void es_comment (const char* comment, 219e475a54fSMasatake YAMATO MIO* out); 220e475a54fSMasatake YAMATO char* es_comment_to_string (const char* comment); 221e475a54fSMasatake YAMATO 222e475a54fSMasatake YAMATO /* 223e475a54fSMasatake YAMATO * Autounref pool 224e475a54fSMasatake YAMATO */ 225e475a54fSMasatake YAMATO void es_autounref_pool_push(void); 226e475a54fSMasatake YAMATO void es_autounref_pool_pop (void); 227e475a54fSMasatake YAMATO 228e475a54fSMasatake YAMATO 229e475a54fSMasatake YAMATO 230e475a54fSMasatake YAMATO /* 231e475a54fSMasatake YAMATO * List builders 232e475a54fSMasatake YAMATO */ 233e475a54fSMasatake YAMATO EsObject* es_list (EsObject* object,...); 234e475a54fSMasatake YAMATO EsObject* es_append (EsObject* list,...); 235e475a54fSMasatake YAMATO EsObject* es_reverse (EsObject* cons); 236e475a54fSMasatake YAMATO 237e475a54fSMasatake YAMATO #define ES_PROC_UNIMPLEMENTED es_error_intern("PROC-UNIMPLEMENTED") 238e475a54fSMasatake YAMATO EsObject* es_realize (EsObject* fmt_object,...); 239e475a54fSMasatake YAMATO EsObject* es_srealize (const char* fmt,...); 240e475a54fSMasatake YAMATO 241e475a54fSMasatake YAMATO /* The value returned from FN treated as if it is returned from 242e475a54fSMasatake YAMATO * a *_new function. es_map may call es_object_unref() for the value. 243e475a54fSMasatake YAMATO * The value returned from es_map should be treated as if it is 244e475a54fSMasatake YAMATO * returned from a *_new function. The caller must free the returned 245e475a54fSMasatake YAMATO * value. 246e475a54fSMasatake YAMATO */ 247e475a54fSMasatake YAMATO EsObject* es_map (EsObject * (*fn) (EsObject *, void *), 248e475a54fSMasatake YAMATO EsObject *list, void *user_data); 249e475a54fSMasatake YAMATO 250e76e59c9SMasatake YAMATO /* Unlike es_map, the value returnd from FN is not accumulated. 251e76e59c9SMasatake YAMATO * If FN returns a value other than #f, es_foreach stops the 252e76e59c9SMasatake YAMATO * iteration immediately and returns the value. 253e76e59c9SMasatake YAMATO */ 254e76e59c9SMasatake YAMATO EsObject* es_foreach (EsObject * (*fn) (EsObject *, void *), 255e76e59c9SMasatake YAMATO EsObject *list, void *user_data); 256e76e59c9SMasatake YAMATO 25719118124SMasatake YAMATO /* The memory management of es_map is also applicable to es_fold. */ 25819118124SMasatake YAMATO EsObject* es_fold (EsObject * (*kons) (EsObject *, EsObject *, void *), 25919118124SMasatake YAMATO EsObject * knil, EsObject * list, void *user_data); 26019118124SMasatake YAMATO 261e475a54fSMasatake YAMATO /* 262e475a54fSMasatake YAMATO * Rich element accessors 263e475a54fSMasatake YAMATO */ 264e475a54fSMasatake YAMATO int es_match (EsObject* input, EsObject* fmt_object,...); 265e475a54fSMasatake YAMATO int es_smatch (EsObject* input, const char* fmt,...); 266e475a54fSMasatake YAMATO 267e475a54fSMasatake YAMATO 268e475a54fSMasatake YAMATO EsObject* es_pget (EsObject* plist, EsObject* key, EsObject* default_value); 269e475a54fSMasatake YAMATO 270e475a54fSMasatake YAMATO #ifdef __cplusplus 271e475a54fSMasatake YAMATO } 272e475a54fSMasatake YAMATO #endif 273e475a54fSMasatake YAMATO 274e475a54fSMasatake YAMATO #endif /* Not def: __ES_LANG_C_STDC_H__ */ 275