This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathlist.cc
295 lines (275 loc) · 8.23 KB
/
list.cc
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2016-2021 Doug Moen
// Licensed under the Apache License, version 2.0
// See accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0
#include <libcurv/list.h>
#include <libcurv/context.h>
#include <libcurv/exception.h>
#include <libcurv/meanings.h>
#include <libcurv/reactive.h>
#include <libcurv/type.h>
namespace curv {
Generic_List::Generic_List(Value val)
{
if (val.is_ref()) {
list_ = share(val.to_ref_unsafe());
switch (list_->type_) {
case Ref_Value::ty_abstract_list:
return;
case Ref_Value::ty_reactive:
{
auto* rx = (Reactive_Value*)(&*list_);
if (rx->sctype_.is_array())
return;
}
}
}
list_ = nullptr;
}
Generic_List::Generic_List(Value val, Fail fl, const Context& cx)
: Generic_List(val)
{
if (list_ == nullptr && fl == Fail::hard)
throw Exception(cx, stringify(val, " is not a list"));
}
bool Generic_List::has_elem_type(const Type& type, const At_Syntax& cx) const
{
if (is_abstract_list()) {
const auto& list = get_abstract_list();
size_t n = list.size();
for (size_t i = 0; i < n; ++i)
if (!type.contains(list.val_at(i), cx)) return false;
return true;
} else {
return Type::equal(*get_reactive_value().sctype_.elem_type(), type);
}
}
size_t Generic_List::size() const noexcept
{
if (is_abstract_list())
return get_abstract_list().size();
else
return get_reactive_value().sctype_.count();
}
void Generic_List::assert_size(size_t sz, const Context& cx) const
{
if (size() != sz)
throw Exception(cx,
stringify("list ",Value{list_}," does not have ",sz," elements"));
}
Value Generic_List::val_at(size_t i, const At_Syntax& cx) const
{
if (is_abstract_list())
return get_abstract_list().val_at(i);
else {
auto& rx = get_reactive_value();
auto ph = share(cx.syntax());
return {make<Reactive_Expression>(
rx.sctype_.elem_type(),
make<Index_Expr>(
ph,
rx.expr(),
make<Constant>(ph, Value(double(i)))),
cx)};
}
}
void Generic_List::amend_at(size_t i, Value newval, const At_Syntax& cx)
{
if (this->is_boxed_list()) {
if (list_->use_count > 1) {
auto& bl = get_boxed_list();
list_ = copy_tail_array<List>(bl.begin(), bl.size());
}
get_boxed_list().at(i) = newval;
}
else if (this->is_string()) {
if (newval.is_char()) {
if (list_->use_count > 1) {
auto& str = get_string();
list_ = make_string(str.data(), str.size());
}
get_string().at(i) = newval.to_char_unsafe();
} else {
auto& str = get_string();
auto li = make_tail_array<List>(str.size());
for (unsigned j = 0; j < str.size(); ++j)
li->at(j) = Value{str[j]};
li->at(i) = newval;
list_ = move(li);
}
}
else if (this->is_reactive_value())
throw Exception(cx, "Generic_List: can't amend symbolic list");
else
throw Exception(cx, "Generic_List: internal error in amend_at");
}
Ternary Abstract_List::aequal(Abstract_List& a, const Context& cx) const
{
if (size() != a.size()) return Ternary::False;
for (unsigned i = 0; i < size(); ++i) {
Ternary t = val_at(i).equal(a.val_at(i), cx);
if (t != Ternary::True) return t;
}
return Ternary::True;
}
const char List_Base::name[] = "list";
const char Abstract_List::name[] = "list";
void
List_Base::assert_size(size_t sz, const Context& cx)
const
{
if (size() != sz)
throw Exception(cx,
stringify("list ",*this," does not have ",sz," elements"));
}
void
List_Base::print_string(std::ostream& out) const
{
bool in_string = true;
bool first_after_left_bracket = false;
for (auto e : *this) {
if (e.is_char()) {
if (!in_string) {
out << ']';
in_string = true;
}
out << e.to_char_unsafe();
} else {
if (in_string) {
out << '[';
in_string = false;
first_after_left_bracket = true;
}
if (!first_after_left_bracket)
out << ',';
e.print_repr(out, Prec::item);
first_after_left_bracket = false;
}
}
if (!in_string)
out << ']';
}
void
List_Base::print_repr(std::ostream& out, Prec rprec) const
{
open_paren(out, rprec, Prec::sum);
enum {begin, in_string, in_list} state = begin;
bool first_after_left_bracket = false;
for (size_t i = 0; i < size(); ++i) {
Value e = array_[i];
if (e.is_char()) {
switch (state) {
case begin: out << '"'; break;
case in_string: break;
case in_list: out << "]++\""; break;
}
state = in_string;
char next = 0;
if (i + 1 < size() && array_[i+1].is_char())
next = array_[i+1].to_char_unsafe();
write_curv_char(e.to_char_unsafe(), next, 0, out);
} else {
switch (state) {
case begin:
out << '[';
first_after_left_bracket = true;
break;
case in_string:
out << "\"++[";
first_after_left_bracket = true;
break;
case in_list:
break;
}
state = in_list;
if (!first_after_left_bracket)
out << ',';
first_after_left_bracket = false;
e.print_repr(out, Prec::item);
}
}
switch (state) {
case begin: out << "[]"; break;
case in_string: out << '"'; break;
case in_list: out << ']'; break;
}
close_paren(out, rprec, Prec::sum);
}
Ternary List_Base::equal(const List_Base& list, const Context& cx) const
{
if (size() != list.size())
return Ternary::False;
Ternary result = Ternary::True;
for (size_t i = 0; i < size(); ++i) {
Ternary ter = array_[i].equal(list.array_[i], cx);
if (ter == Ternary::False) return Ternary::False;
result &= ter;
}
return result;
}
void List_Builder::push_back(Value val)
{
if (in_string_) {
if (val.is_char()) {
string_.push_back(val.to_char_unsafe());
return;
}
for (auto c : string_)
list_.push_back({c});
in_string_ = false;
}
list_.push_back(val);
}
void List_Builder::concat(Value val, const Context& cx)
{
if (auto strval = val.maybe<String>()) {
// Strings can't be empty.
if (in_string_)
string_ += strval->c_str();
else {
for (auto c : *strval)
list_.push_back({c});
}
} else if (auto listval = val.maybe<List>()) {
if (listval->empty()) return;
// A non-empty List is unlikely to contain only characters,
// so we switch out of string mode. If this assumption is wrong,
// then we generate a denormalized string. TODO?
if (in_string_) {
for (auto c : string_)
list_.push_back({c});
in_string_ = false;
}
list_.insert(list_.end(), listval->begin(), listval->end());
} else {
throw Exception(cx, stringify(val, "is not a list"));
}
}
Value List_Builder::get_value()
{
if (in_string_) {
if (string_.empty())
return {make_tail_array<List>(0)};
return {make_string(string_)};
}
Shared<List> result = move_tail_array<List>(list_);
return {result};
}
Shared<List> List_Base::clone() const
{
return copy_tail_array<List>(array_, size_);
}
Value* List_Base::ref_element(Value index, bool need_value, const Context& cx)
{
auto index_list = index.to<List>(cx);
index_list->assert_size(1, cx);
int i = index_list->at(0).to_int(0, int(size_)-1, cx);
(void)need_value;
return &array_[i];
}
Value* List_Base::ref_lens(Value lens, bool need_value, const Context& cx)
{
int i = lens.to_int(0, int(size_)-1, cx);
(void)need_value;
return &array_[i];
}
} // namespace curv