-
Notifications
You must be signed in to change notification settings - Fork 6
/
hklr_as_string.c
138 lines (107 loc) · 3.36 KB
/
hklr_as_string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <assert.h>
#include "hkl_value.h"
#include "hkl_string.h"
HklValue* hklr_as_string(HklValue* value);
static bool hash_build_string(HklPair* pair, void* string)
{
HklValue* temp = hkl_value_new(HKL_TYPE_REF, pair->value);
hkl_string_cat_utf8((HklString*) string, "\"");
hkl_string_cat((HklString*) string, pair->key);
hkl_string_cat_utf8((HklString*) string, "\"");
hkl_string_cat_utf8((HklString*) string, ": ");
hkl_string_cat((HklString*) string, hklr_as_string(temp)->as.string);
hkl_string_cat_utf8((HklString*) string, ", ");
return false;
}
// Make a new value object that is a cast of the value as a string
HklValue* hklr_as_string(HklValue* value)
{
HklValue* cast = hkl_value_new(HKL_TYPE_STRING, NULL);
switch (value->type)
{
case HKL_TYPE_REF:
{
HklValue* temp = hklr_object_dereference(value->as.object);
cast->as.string = hklr_as_string(temp)->as.string;
}
break;
case HKL_TYPE_NIL:
cast->as.string = hkl_string_new_from_utf8("nil");
break;
case HKL_TYPE_INT:
cast->as.string = hkl_string_new_from_integer(value->as.integer);
break;
case HKL_TYPE_TYPE:
switch (value->as.type)
{
case HKL_TYPE_NIL:
cast->as.string = hkl_string_new_from_utf8("nil");
break;
case HKL_TYPE_INT:
cast->as.string = hkl_string_new_from_utf8("int");
break;
case HKL_TYPE_TYPE:
cast->as.string = hkl_string_new_from_utf8("type");
break;
case HKL_TYPE_REAL:
cast->as.string = hkl_string_new_from_utf8("real");
break;
case HKL_TYPE_STRING:
cast->as.string = hkl_string_new_from_utf8("string");
break;
case HKL_TYPE_ARRAY:
cast->as.string = hkl_string_new_from_utf8("array");
break;
case HKL_TYPE_HASH:
cast->as.string = hkl_string_new_from_utf8("hash");
break;
case HKL_TYPE_FUNCTION:
cast->as.string = hkl_string_new_from_utf8("func");
break;
case HKL_TYPE_CFUNC:
cast->as.string = hkl_string_new_from_utf8("cfunc");
break;
case HKL_TYPE_CDATA:
cast->as.string = hkl_string_new_from_utf8("cdata");
break;
default:
assert(false);
break;
}
break;
case HKL_TYPE_REAL:
cast->as.string = hkl_string_new_from_real(value->as.real);
break;
case HKL_TYPE_STRING:
cast->as.string = hkl_string_new_from_string(value->as.string);
break;
case HKL_TYPE_ARRAY:
{
cast->as.string = hkl_string_new_from_utf8("[");
HklDeque* deque = value->as.deque;
size_t i;
for (i = 0; i < value->as.deque->size; ++i)
{
HklValue* temp = hklr_as_string((HklValue*) hkl_deque_findn(deque, i));
hkl_string_cat(cast->as.string, temp->as.string);
if (i != value->as.deque->size - 1)
hkl_string_cat_utf8(cast->as.string, ", ");
hkl_value_free(temp);
}
hkl_string_cat_utf8(cast->as.string, "]");
}
break;
case HKL_TYPE_HASH:
{
cast->as.string = hkl_string_new_from_utf8("{");
HklHash* hash = value->as.hash;
hkl_hash_traverse(hash, hash_build_string, cast->as.string);
hkl_string_cat_utf8(cast->as.string, "}");
}
break;
default:
assert(false);
break;
}
return cast;
}