rev: 7fccfb5a02ee7da629d6860fceda7187922ac8c0 scopes/src/scope.cpp -rw-r--r-- 10.0 KiB View raw Log this file
7fccfb5a02ee — Leonard Ritter * REPL: completion suggestions are first sorted by length, then lexicographic 18 days ago
                                                                                
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
    The Scopes Compiler Infrastructure
    This file is distributed under the MIT License.
    See LICENSE.md for details.
*/

#include "scope.hpp"
#include "value.hpp"
#include "error.hpp"

#include <algorithm>
#include <unordered_set>
#include <cstring>

namespace scopes {

//------------------------------------------------------------------------------
// SCOPE
//------------------------------------------------------------------------------

Scope::Scope(const String *_doc, const Scope *_parent) :
    map(nullptr),
    index(0),
    name(ConstRef()),
    value(ValueRef()),
    doc(_doc),
    next(_parent),
    start(this) {
    if (!doc && next) {
        doc = next->header_doc();
    }
}

Scope::Scope(const ConstRef &_name, const ValueRef &_value, const String *_doc, const Scope *_next) :
    map(nullptr),
    name(_name),
    value(_value),
    doc(_doc),
    next(_next) {
    assert(_name);
    assert(_next && _next->start);
    start = _next->start;
    index = _next->index + 1;
}

const Scope *Scope::parent() const {
    return start->next;
}

const String *Scope::header_doc() const {
    return start->doc;
}

bool Scope::is_header() const {
    return start == this;
}

size_t Scope::count() const {
    return index;
}

size_t Scope::totalcount() const {
    size_t count = 0;
    const Scope *self = this;
    while (self) {
        count += self->index;
        self = self->parent();
    }
    return count;
}

size_t Scope::levelcount() const {
    size_t count = 0;
    const Scope *self = this;
    while (self) {
        count += 1;
        self = self->parent();
    }
    return count;
}

const Scope::Map &Scope::table() const {
    if (!map) {
        auto map = new Map();
        const Scope *self = this;
        while (true) {
            if (self->map) {
                // copy all elements from map
                size_t i = self->map->keys.size();
                while (i-- > 0) {
                    auto &&key = self->map->keys[i];
                    auto &&entry = self->map->values[i];
                    map->insert(key, entry);
                }
                break;
            } else if (!self->is_header()) {
                map->insert(self->name, { self->value, self->doc });
                self = self->next;
            } else {
                break;
            }
        }
        map->flip();
        this->map = map;
    }
    return *map;
}

const Scope *Scope::reparent_from(const Scope *content, const Scope *parent) {
    auto &&map = content->table();
    // we lose our scopes docstring and instead reuse the parents docstring
    const String *doc = nullptr;
    if (parent) {
        doc = parent->header_doc();
    } else {
        doc = content->header_doc();
    }
    auto self = from(doc, parent);
    #if 0
    auto count = map.keys.size();
    for (size_t i = 0; i < count; ++i) {
        auto &&key = map.keys[i];
        auto &&entry = map.values[i];
        self = bind_from(key, entry.value, entry.doc, self);
    }
    #endif
    // can reuse the map because the content is the same
    self->map = &map;
    self->index = map.keys.size();
    return self;
}

const Scope *Scope::bind_from(const ConstRef &name, const ValueRef &value, const String *doc, const Scope *next) {
    assert(value);
    return new Scope(name, value, doc, next);
}

const Scope *Scope::unbind_from(const ConstRef &name, const Scope *next) {
    // check if value is contained
    ValueRef dest;
    if (next->lookup(name, dest)) {
        return new Scope(name, ValueRef(), nullptr, next);
    }
    return next;
}

const Scope *Scope::from(const String *doc, const Scope *parent) {
    return new Scope(doc, parent);
}

std::vector<Symbol> Scope::find_closest_match(Symbol name) const {
    const String *s = name.name();
    std::unordered_set<Symbol, Symbol::Hash> done;
    std::vector<Symbol> best_syms;
    size_t best_dist = (size_t)-1;
    const Scope *self = this;
    do {
        if (self->map) {
            size_t count = self->map->keys.size();
            for (size_t i = 0; i < count; ++i) {
                auto &&key = self->map->keys[i];
                if (key->get_type() == TYPE_Symbol) {
                    Symbol sym = Symbol::wrap(key.cast<ConstInt>()->value());
                    if (!done.count(sym)) {
                        auto &&value = self->map->values[i];
                        if (value.value) {
                            size_t dist = distance(s, sym.name());
                            if (dist == best_dist) {
                                best_syms.push_back(sym);
                            } else if (dist < best_dist) {
                                best_dist = dist;
                                best_syms = { sym };
                            }
                        }
                        done.insert(sym);
                    }
                }
            }
            self = self->start;
        } else if (!self->is_header()) {
            auto &&key = self->name;
            if (key->get_type() == TYPE_Symbol) {
                Symbol sym = Symbol::wrap(key.cast<ConstInt>()->value());
                if (!done.count(sym)) {
                    if (self->value) {
                        size_t dist = distance(s, sym.name());
                        if (dist == best_dist) {
                            best_syms.push_back(sym);
                        } else if (dist < best_dist) {
                            best_dist = dist;
                            best_syms = { sym };
                        }
                    }
                    done.insert(sym);
                }
            }
        }
        self = self->next;
    } while (self);
    std::sort(best_syms.begin(), best_syms.end());
    return best_syms;
}

std::vector<Symbol> Scope::find_elongations(Symbol name) const {
    const String *s = name.name();

    std::unordered_set<Symbol, Symbol::Hash> done;
    std::vector<Symbol> found;
    const Scope *self = this;
    do {
        if (self->map) {
            size_t count = self->map->keys.size();
            for (size_t i = 0; i < count; ++i) {
                auto &&key = self->map->keys[i];
                if (key->get_type() == TYPE_Symbol) {
                    Symbol sym = Symbol::wrap(key.cast<ConstInt>()->value());
                    if (!done.count(sym)) {
                        auto &&value = self->map->values[i];
                        if (value.value) {
                            if (sym.name()->count >= s->count &&
                                    (sym.name()->substr(0, s->count) == s))
                                found.push_back(sym);
                        }
                        done.insert(sym);
                    }
                }
            }
            self = self->start;
        } else if (!self->is_header()) {
            auto &&key = self->name;
            if (key->get_type() == TYPE_Symbol) {
                Symbol sym = Symbol::wrap(key.cast<ConstInt>()->value());
                if (!done.count(sym)) {
                    if (self->value) {
                        if (sym.name()->count >= s->count &&
                                (sym.name()->substr(0, s->count) == s))
                            found.push_back(sym);
                    }
                    done.insert(sym);
                }
            }
        }
        self = self->next;
    } while (self);
    std::sort(found.begin(), found.end(),
        [](Symbol a, Symbol b){
            auto a_count = a.name()->count;
            auto b_count = b.name()->count;
            if (a_count == b_count) {
                return std::strcmp(a.name()->data, b.name()->data) < 0;
            } else {
                return a_count < b_count;
            }
        });
    return found;
}

bool Scope::lookup(const ConstRef &name, ValueRef &dest, const String *&doc, size_t depth) const {
    const Scope *self = this;
    const Scope *last_end = this;
    int iterations = 0;
    do {
        // compact when we searched too long and retry
        if (iterations == 16) {
            self = last_end;
            self->table();
            iterations = 0;
        }
        if (self->map) {
            int i = self->map->find_index(name);
            if (i != -1) {
                auto &&value = self->map->values[i];
                if (value.value) {
                    dest = value.value;
                    doc = value.doc;
                    return true;
                } else {
                    return false;
                }
            }
            self = self->start;
            last_end = self->next;
            //if (self->parent()) self->parent()->table();
        }
        if (self->is_header()) {
            if (!depth)
                break;
            depth = depth - 1;
        } else {
            iterations++;
            if (self->name == name) {
                if (self->value) {
                    dest = self->value;
                    doc = self->doc;
                    return true;
                } else {
                    return false;
                }
            }
        }
        self = self->next;
    } while (self);
    return false;
}

bool Scope::lookup(const ConstRef &name, ValueRef &dest, size_t depth) const {
    const String *doc;
    return lookup(name, dest, doc, depth);
}

bool Scope::lookup_local(const ConstRef &name, ValueRef &dest, const String *&doc) const {
    return lookup(name, dest, doc, 0);
}

bool Scope::lookup_local(const ConstRef & name, ValueRef &dest) const {
    return lookup(name, dest, 0);
}

StyledStream &Scope::stream(StyledStream &ss) const {
    size_t totalcount = this->totalcount();
    size_t count = this->count();
    size_t levelcount = this->levelcount();
    ss << Style_Keyword << "Scope" << Style_Comment << "<" << Style_None
        << format("L:%i T:%i in %i levels", count, totalcount, levelcount)->data
        << Style_Comment << ">" << Style_None;
    return ss;
}

//------------------------------------------------------------------------------

StyledStream& operator<<(StyledStream& ost, const Scope *scope) {
    scope->stream(ost);
    return ost;
}


} // namespace scopes