25#include <json-c/json.h> 46static Value json_to_fun(json_object *j) {
48 enum json_type
t = json_object_get_type(j);
52 case json_type_boolean:
53 return make_bool(json_object_get_boolean(j));
54 case json_type_double:
57 return make_int((int64_t)json_object_get_int64(j));
58 case json_type_string:
60 case json_type_array: {
61 size_t n = json_object_array_length(j);
67 for (
size_t i = 0; i <
n; ++i) {
68 json_object *item = json_object_array_get_idx(j, (
int)i);
69 vals[i] = json_to_fun(item);
72 for (
size_t i = 0; i <
n; ++i)
77 case json_type_object: {
79 json_object_object_foreach(j, key,
val) {
99static json_object *fun_to_json(
const Value *
v) {
102 return json_object_new_null();
104 return json_object_new_boolean(
v->i ? 1 : 0);
106 return json_object_new_int64(
v->i);
108 return json_object_new_double(
v->d);
110 return json_object_new_string(
v->s ?
v->s :
"");
112 json_object *
arr = json_object_new_array();
114 for (
int i = 0; i <
n; ++i) {
117 json_object_array_add(
arr, fun_to_json(&item));
120 json_object_array_add(
arr, json_object_new_null());
126 json_object *obj = json_object_new_object();
130 for (
int i = 0; i < kn; ++i) {
136 json_object_object_add(obj,
k.s, fun_to_json(&
val));
139 json_object_object_add(obj,
k.s, json_object_new_null());
149 return json_object_new_string(
"<unsupported>");
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Value make_map_empty(void)
Construct a new empty map Value.
int map_get_copy(const Value *vm, const char *key, Value *out)
Look up a key and copy the stored value into out.
Value map_keys_array(const Value *vm)
Return all map keys as an array of strings.
Tagged union representing a Fun value.
Value make_bool(int v)
Construct a boolean Value.
Value make_nil(void)
Construct a nil Value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
int array_length(const Value *v)
Get the element count of an array Value.
void free_value(Value v)
Free dynamic storage owned by a Value.
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Value make_float(double v)
Construct a Value representing a double-precision float.
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Defines the Value type and associated functions for the Fun VM.
Core virtual machine data structures and public VM API.