-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunlark_utils.c
172 lines (155 loc) · 4.45 KB
/
sunlark_utils.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
/* #include "utarray.h" */
#include "utstring.h"
#include "s7.h"
#include "sunlark_utils.h"
/* **************************************************************** */
EXPORT const char* sunlark_node_to_string(s7_pointer nd)
{
#if defined (DEBUG_TRACE) || defined(DEBUG_VECTORS)
log_debug("sunlark_to_string");
#endif
if (s7_is_c_object(nd)) {
struct node_s *node = s7_c_object_value(nd);
if (sealark_is_printable(node)) {
return sealark_node_printable_string(node);
} else {
return NULL;
}
}
}
EXPORT int sealark_normalize_index(struct node_s *node, int index)
{
#if defined (DEBUG_TRACE) || defined(DEBUG_VECTORS)
log_debug("sealark_normalize_index: %d", index);
#endif
struct node_s *nodes;
int subnode_ct;
int item_ct;
if(node->tid == TK_List_Expr) {
nodes = utarray_eltptr(node->subnodes, 1);
subnode_ct = utarray_len(nodes->subnodes);
item_ct = (subnode_ct + 1) / 2;
} else {
if(node->tid == TK_Arg_List) {
subnode_ct = utarray_len(node->subnodes);
item_ct = (subnode_ct + 1) / 2;
} else {
if(node->tid == TK_Package) {
item_ct = sealark_pkg_targets_count(node);
} else {
log_error("normalize idx unhandled node type: %d %s",
node->tid, TIDNAME(node));
}
}
}
/* reverse indexing */
if (index < 0) {
if (abs(index) > item_ct) {
log_error("abs(%d) > item_ct", index, item_ct);
errno = EINDEX_OUT_OF_BOUNDS;
/* return NULL; */
} else {
index = item_ct + index;
/* log_debug("recurring..."); */
/* return sealark_vector_item_for_int(node, index); */
}
}
if (index > item_ct-1) {
log_error("index > target count");
errno = EINDEX_OUT_OF_BOUNDS;
/* return NULL; */
}
#if defined (DEBUG_TRACE) || defined(DEBUG_VECTORS)
log_debug("\tnormalized: %d", index);
#endif
return index;
}
int sunlark_kwindex_to_int(s7_scheme *s7, s7_pointer arg)
{
#ifdef DEBUG_TRACE
log_debug("sunlark_kwindex_to_int: %s", s7_object_to_c_string(s7, arg));
#endif
errno = 0;
int idx;
if (s7_is_integer(arg))
return s7_integer(arg);
/* sunlark_is_nbr_kw will set errno */
return sunlark_is_nbr_kw(s7, arg);
}
int sunlark_is_nbr_kw(s7_scheme *s7, s7_pointer arg)
{
#ifdef DEBUG_TRACE
log_debug("sunlark_is_nbr_kw: %s", s7_object_to_c_string(s7, arg));
#endif
if (s7_is_keyword(arg)) {
s7_pointer sym = s7_keyword_to_symbol(s7, arg);
const char *kw = s7_symbol_name(sym);
int len = strlen(kw); // very unlikely to be more than 2
int i = (kw[0] == '-')? 1 : 0;
for ( ; i < len; i++) {
if ( isdigit((int)kw[i]) == 0 ) {
errno = EKW_NOT_NBR;
return -1;
}
}
errno = 0;
return atoi(kw);
} else {
errno = ENOT_A_KW;
return -1;
}
}
s7_pointer node_to_scheme(s7_scheme *s7, struct node_s *node)
{
#ifdef DEBUG_TRACE
log_debug("node_to_scheme tid %d %s", node->tid, TIDNAME(node));
#endif
s7_pointer val;
switch(node->tid) {
case TK_STRING: {
UT_string *buf;
utstring_new(buf);
char *br = SEALARK_STRTYPE(node->qtype);
char *q = sealark_quote_type(node);
utstring_printf(buf,
"%s%s%s%s",
br,
q,
node->s,
q);
val = s7_make_string(s7, utstring_body(buf));
utstring_free(buf);
}
break;
case TK_ID:
if (strncmp("True", node->s, 4) == 0) {
val = s7_t(s7);
} else {
if (strncmp("False", node->s, 4) == 0) {
val = s7_make_symbol(s7, "#f");
} else {
val = s7_make_symbol(s7, node->s);
}
}
break;
case TK_INT:
val = s7_make_integer(s7, atoi(node->s));
break;
case TK_Binding: {
struct node_s *v = sealark_value_for_binding(node);
return sunlark_new_node(s7, v);
}
break;
case TK_List_Expr:
log_error("dollar list expr");
default:
log_error("FIXME: deal with :$");
}
return val;
/* } */
}