apfl/src/value.h

100 lines
3 KiB
C
Raw Normal View History

#ifndef APFL_VALUE_H
#define APFL_VALUE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdio.h>
#include "apfl.h"
enum value_type {
VALUE_NIL,
VALUE_BOOLEAN,
VALUE_NUMBER,
VALUE_STRING,
VALUE_LIST,
VALUE_DICT,
// TODO: functions/closures
};
struct apfl_list_data;
typedef struct apfl_list_data *apfl_list;
struct apfl_dict_data;
typedef struct apfl_dict_data *apfl_dict;
struct apfl_value {
enum value_type type;
union {
bool boolean;
apfl_number number;
apfl_refcounted_string string;
apfl_list list;
apfl_dict dict;
};
};
bool apfl_value_eq(const struct apfl_value, const struct apfl_value);
struct apfl_value apfl_value_move(struct apfl_value *src);
struct apfl_value apfl_value_incref(struct apfl_value);
void apfl_value_print(struct apfl_value, FILE *);
void apfl_value_deinit(struct apfl_allocator, struct apfl_value *);
enum get_item_result {
GET_ITEM_OK,
GET_ITEM_KEY_DOESNT_EXIST,
GET_ITEM_NOT_A_CONTAINER,
GET_ITEM_WRONG_KEY_TYPE,
};
enum get_item_result apfl_value_get_item(struct apfl_allocator allocator, struct apfl_value container, struct apfl_value key, struct apfl_value *out);
apfl_list apfl_list_incref(apfl_list);
size_t apfl_list_len(apfl_list);
bool apfl_list_get_item(struct apfl_allocator, apfl_list, size_t index, struct apfl_value *out);
void apfl_list_unref(struct apfl_allocator, apfl_list);
apfl_dict apfl_dict_incref(apfl_dict);
bool apfl_dict_get_item(struct apfl_allocator, apfl_dict, struct apfl_value key, struct apfl_value *out);
void apfl_dict_unref(struct apfl_allocator, apfl_dict);
struct apfl_editable_list_data;
typedef struct apfl_editable_list_data *apfl_editable_list;
apfl_editable_list apfl_editable_list_new(struct apfl_allocator);
bool apfl_editable_list_append(apfl_editable_list, struct apfl_value);
bool apfl_editable_list_append_list(apfl_editable_list, apfl_list);
void apfl_editable_list_destroy(apfl_editable_list);
/* Finalize the list and return a non-editable list.
* This also destroys the editable list object, so it's no longer safe to use it.
* Returns NULL on failure
*/
apfl_list apfl_editable_list_finalize(apfl_editable_list);
struct apfl_editable_dict_data;
typedef struct apfl_editable_dict_data *apfl_editable_dict;
apfl_editable_dict apfl_editable_dict_new(struct apfl_allocator);
apfl_editable_dict apfl_editable_dict_new_from_dict(struct apfl_allocator, apfl_dict);
bool apfl_editable_dict_get(apfl_editable_dict, struct apfl_value key, struct apfl_value *out);
bool apfl_editable_dict_set(apfl_editable_dict, struct apfl_value key, struct apfl_value value);
void apfl_editable_dict_delete(apfl_editable_dict, struct apfl_value key);
void apfl_editable_dict_destroy(apfl_editable_dict);
/* Finalize the dictionary and return a non-editable dictionary.
* This also destroys the editable dictionary object, so it's no longer safe to use it.
* Returns NULL on failure
*/
apfl_dict apfl_editable_dict_finalize(apfl_editable_dict);
#ifdef __cplusplus
}
#endif
#endif