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 pathdefinition.h
275 lines (234 loc) · 8.68 KB
/
definition.h
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
// 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
#ifndef LIBCURV_DEFINITION_H
#define LIBCURV_DEFINITION_H
#include <libcurv/analyser.h>
#include <libcurv/meanings.h>
namespace curv {
struct Pattern;
struct Recursive_Scope;
// Syntactically,
// '<definition>' is a recursive definition
// 'local <definition>' is a sequential definition
// In both cases, a <definition> phrase is represented by a Definition object.
struct Definition : public Shared_Base
{
Shared<const Phrase> syntax_;
Definition(
Shared<const Phrase> syntax)
:
syntax_(move(syntax))
{}
virtual void analyse_sequential(Environ&) = 0;
virtual Shared<Operation> add_to_sequential_scope(Scope&) = 0;
virtual void add_to_recursive_scope(Recursive_Scope&) = 0;
};
// A unitary definition is one that occupies a single node in the dependency
// graph that is constructed while analysing a recursive scope. It can define
// multiple names.
struct Unitary_Definition : public Definition
{
Unitary_Definition(
Shared<const Phrase> syntax)
:
Definition(syntax)
{}
virtual void analyse_recursive(Environ&) = 0;
virtual Shared<Operation> make_setter(slot_t module_slot) = 0;
};
// A function definition is `f=<lambda>` or `f x=<expr>`.
// Only `=` style function definitions belong to this class, so it's potentially
// a recursive definition.
// Only a single name is bound. There is no pattern matching in the definiendum.
struct Function_Definition : public Unitary_Definition
{
Shared<const Identifier> name_;
Shared<Lambda_Phrase> lambda_phrase_;
slot_t slot_; // initialized by add_to_recursive_scope()
Shared<Lambda> lambda_; // initialized by analyse()
Shared<Operation> lambda_expr_; // initialized by analyse_sequential()
Function_Definition(
Shared<const Phrase> syntax,
Shared<const Identifier> name,
Shared<Lambda_Phrase> lambda_phrase)
:
Unitary_Definition(syntax),
name_(move(name)),
lambda_phrase_(move(lambda_phrase))
{}
virtual void analyse_sequential(Environ&) override;
virtual Shared<Operation> add_to_sequential_scope(Scope&) override;
virtual void add_to_recursive_scope(Recursive_Scope&) override;
virtual void analyse_recursive(Environ&) override;
virtual Shared<Operation> make_setter(slot_t module_slot) override;
};
// A data definition is `pattern = expr` where `expr` is not a lambda.
// Data definitions cannot be recursive, and they can use pattern matching
// to bind multiple names.
struct Data_Definition : public Unitary_Definition
{
Shared<Pattern> pattern_;
Shared<Phrase> definiens_phrase_;
Shared<Operation> definiens_expr_; // initialized by analyse()
Data_Definition(
Shared<const Phrase> syntax,
Shared<Pattern> pattern,
Shared<Phrase> definiens)
:
Unitary_Definition(syntax),
pattern_(pattern),
definiens_phrase_(move(definiens))
{}
virtual void analyse_sequential(Environ&) override;
virtual Shared<Operation> add_to_sequential_scope(Scope&) override;
virtual void add_to_recursive_scope(Recursive_Scope&) override;
virtual void analyse_recursive(Environ&) override;
virtual Shared<Operation> make_setter(slot_t module_slot) override;
};
struct Include_Definition : public Unitary_Definition
{
Shared<Phrase> arg_;
Shared<Include_Setter> setter_; // initialized by add_to_recursive_scope()
Include_Definition(
Shared<const Phrase> syntax,
Shared<Phrase> arg)
:
Unitary_Definition(syntax),
arg_(move(arg))
{}
virtual void analyse_sequential(Environ&) override;
Shared<Include_Setter> add_to_scope(Scope& scope);
virtual Shared<Operation> add_to_sequential_scope(Scope&) override;
virtual void add_to_recursive_scope(Recursive_Scope&) override;
virtual void analyse_recursive(Environ&) override;
virtual Shared<Operation> make_setter(slot_t module_slot) override;
};
struct Test_Definition : public Unitary_Definition
{
Shared<Phrase> arg_;
Shared<Operation> setter_; // initialized by analyse()
Test_Definition(
Shared<const Phrase> syntax,
Shared<Phrase> arg)
:
Unitary_Definition(syntax),
arg_(move(arg))
{}
virtual void analyse_sequential(Environ&) override;
virtual Shared<Operation> add_to_sequential_scope(Scope&) override;
virtual void add_to_recursive_scope(Recursive_Scope&) override;
virtual void analyse_recursive(Environ&) override;
virtual Shared<Operation> make_setter(slot_t module_slot) override;
};
// A compound definition is `statement1;statement2;...` where each statement
// is an action or definition, and there is at least one definition.
struct Compound_Definition_Base : public Definition
{
Compound_Definition_Base(Shared<const Phrase> syntax)
: Definition(move(syntax)) {}
virtual void analyse_sequential(Environ&) override;
virtual Shared<Operation> add_to_sequential_scope(Scope&) override;
virtual void add_to_recursive_scope(Recursive_Scope&) override;
TAIL_ARRAY_MEMBERS(Shared<Definition>)
void resize(size_t n) { size_ = n; }
};
struct Compound_Definition : public Tail_Array<Compound_Definition_Base>
{
using Tail_Array<Compound_Definition_Base>::Tail_Array;
};
// A Scope is set of user-defined variables defined over a lexical scope.
struct Scope : public Environ
{
struct Binding {
slot_t slot_index_;
unsigned unit_index_;
Shared<Scoped_Variable> variable_;
Binding(slot_t slot, unsigned unit, Shared<Scoped_Variable> var)
:
slot_index_(slot),
unit_index_(unit),
variable_(var)
{}
};
Symbol_Map<Binding> dictionary_ = {};
Scope(Environ& parent)
:
Environ(&parent)
{
frame_nslots_ = parent.frame_nslots_;
frame_maxslots_ = parent.frame_maxslots_;
}
// Environ protocol
virtual Shared<Meaning> single_lookup(const Identifier&) override;
virtual Unique<const Locative> single_lvar_lookup(const Identifier&) override;
// Scope protocol. Default implementation is for a sequential scope.
virtual unsigned add_unit(Shared<Unitary_Definition>) { return 0; }
virtual std::pair<slot_t,Shared<const Scoped_Variable>> add_binding(
Symbol_Ref, const Phrase&, unsigned unit);
};
struct Recursive_Scope : public Scope
{
struct Unit {
enum State {
k_not_analysed, k_analysis_in_progress, k_analysed
};
Shared<Unitary_Definition> def_;
State state_ = k_not_analysed;
int scc_ord_ = -1; // -1 until SCC assigned
int scc_lowlink_ = -1;
Symbol_Map<Shared<Operation>> nonlocals_ = {};
Unit(Shared<Unitary_Definition> def) : def_(def) {}
bool is_function() {
return cast<Function_Definition>(def_) != nullptr;
}
};
struct Function_Environ : public Environ {
Recursive_Scope& scope_;
Unit& unit_;
Function_Environ(Recursive_Scope& scope, Unit& unit)
:
Environ(scope.parent_),
scope_(scope),
unit_(unit)
{
frame_nslots_ = scope.frame_nslots_;
frame_maxslots_ = scope.frame_maxslots_;
assert(unit.is_function());
}
virtual Shared<Meaning> single_lookup(const Identifier&) override;
};
Shared<const Phrase> syntax_;
bool target_is_module_;
Scope_Executable executable_ = {};
std::vector<Unit> units_ = {};
int scc_count_ = 0;
std::vector<Unit*> scc_stack_ = {};
std::vector<Unit*> analysis_stack_ = {};
Recursive_Scope(
Environ& parent, bool target_is_module, Shared<const Phrase> syntax)
:
Scope(parent),
syntax_(move(syntax)),
target_is_module_(target_is_module)
{
if (target_is_module)
executable_.module_slot_ = make_slot();
}
// Environ
virtual Shared<Meaning> single_lookup(const Identifier&) override;
virtual Unique<const Locative> single_lvar_lookup(const Identifier&) override;
// Scope
virtual std::pair<slot_t,Shared<const Scoped_Variable>> add_binding(
Symbol_Ref, const Phrase&, unsigned unit) override;
// Recursive_Scope (add_to_recursive_scope protocol)
void analyse(Definition&);
virtual unsigned add_unit(Shared<Unitary_Definition>) override;
void analyse();
private:
void analyse_unit(Unit&, const Identifier*);
Shared<Operation> make_function_setter(size_t nunits, Unit** units);
};
Shared<Module_Expr> analyse_module(Definition&, Environ&);
} // namespace
#endif // header guard