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
173
174
|
#include "write.h"
#include "function.h"
#include "syntax.h"
static lsp_status_t write_number(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
return lsp_out_stream_write_int(s, lsp_mem_get_number(m, addr));
}
static lsp_status_t write_pair(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_status_t status = lsp_out_stream_write(s, '(');
if (status != LSP_SUCCESS)
return status;
lsp_bool_t write_space = false;
while (addr != m->nil) {
if (write_space) {
lsp_out_stream_write(s, ' ');
} else {
write_space = true;
}
lsp_addr_t first = lsp_mem_get_pair_first(m, addr);
lsp_addr_t second = lsp_mem_get_pair_second(m, addr);
status = lsp_write(m, s, first);
if (status != LSP_SUCCESS)
return status;
if (!lsp_mem_is_pair(m, second)) {
status = lsp_out_stream_write_str(s, " . ");
if (status != LSP_SUCCESS)
return status;
status = lsp_write(m, s, second);
if (status != LSP_SUCCESS)
return status;
break;
}
addr = second;
}
return lsp_out_stream_write(s, ')');
}
static lsp_status_t write_string(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_uint16_t len = lsp_mem_get_string_len(m, addr);
lsp_status_t status = lsp_out_stream_write(s, '"');
for (lsp_uint16_t i = 0; i < len; ++i) {
lsp_uint8_t c = lsp_mem_get_string_data(m, addr, i);
if (c == '"') {
status = lsp_out_stream_write(s, '\\');
if (status != LSP_SUCCESS)
return status;
}
status = lsp_out_stream_write(s, c);
if (status != LSP_SUCCESS)
return status;
}
return lsp_out_stream_write(s, '"');
}
static lsp_status_t write_symbol(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_uint16_t len = lsp_mem_get_symbol_len(m, addr);
for (lsp_uint16_t i = 0; i < len; ++i) {
lsp_uint8_t c = lsp_mem_get_symbol_name(m, addr, i);
lsp_status_t status = lsp_out_stream_write(s, c);
if (status != LSP_SUCCESS)
return status;
}
return LSP_SUCCESS;
}
static lsp_status_t write_builtin_function(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_status_t status = lsp_out_stream_write_str(s, "#<builtin-function-");
if (status != LSP_SUCCESS)
return status;
status = lsp_out_stream_write_str(
s, lsp_functions[lsp_mem_get_builtin_index(m, addr)].name);
if (status != LSP_SUCCESS)
return status;
return lsp_out_stream_write(s, '>');
}
static lsp_status_t write_builtin_syntax(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_status_t status = lsp_out_stream_write_str(s, "#<builtin-syntax-");
if (status != LSP_SUCCESS)
return status;
status = lsp_out_stream_write_str(
s, lsp_syntaxes[lsp_mem_get_builtin_index(m, addr)].name);
if (status != LSP_SUCCESS)
return status;
return lsp_out_stream_write(s, '>');
}
static lsp_status_t write_function(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_status_t status = lsp_out_stream_write_str(s, "#<function-");
if (status != LSP_SUCCESS)
return status;
status = lsp_out_stream_write_int(s, addr);
if (status != LSP_SUCCESS)
return status;
return lsp_out_stream_write(s, '>');
}
static lsp_status_t write_syntax(lsp_mem_t *m, lsp_out_stream_t *s,
lsp_addr_t addr) {
lsp_status_t status = lsp_out_stream_write_str(s, "#<syntax-");
if (status != LSP_SUCCESS)
return status;
status = lsp_out_stream_write_int(s, addr);
if (status != LSP_SUCCESS)
return status;
return lsp_out_stream_write(s, '>');
}
lsp_status_t lsp_write(lsp_mem_t *m, lsp_out_stream_t *s, lsp_addr_t addr) {
if (lsp_mem_is_number(m, addr))
return write_number(m, s, addr);
if (lsp_mem_is_pair(m, addr))
return write_pair(m, s, addr);
if (lsp_mem_is_string(m, addr))
return write_string(m, s, addr);
if (lsp_mem_is_symbol(m, addr))
return write_symbol(m, s, addr);
if (lsp_mem_is_builtin_function(m, addr))
return write_builtin_function(m, s, addr);
if (lsp_mem_is_builtin_syntax(m, addr))
return write_builtin_syntax(m, s, addr);
if (lsp_mem_is_function(m, addr))
return write_function(m, s, addr);
if (lsp_mem_is_syntax(m, addr))
return write_syntax(m, s, addr);
return LSP_ERR_WRITE;
}
|