forked from asutton/waffle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.hpp
47 lines (36 loc) · 1 KB
/
scope.hpp
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
#ifndef SCOPE_HPP
#define SCOPE_HPP
#include "ast.hpp"
#include <map>
// Determines the kind of scope.
enum Scope_kind {
global_scope,
lambda_scope
};
// A scope records a set of named terms (e.g., variables), allowing
// the lookup of bound identifiers. Each scope is linked to its
// parent or enclosing scope, allowing lookup to work "outwards"
// as a declaration corresponding to that name is searched for.
struct Scope : std::map<Name*, Expr*, Expr_less> {
Scope(Scope_kind k)
: kind(k), parent(nullptr) { }
Scope(Scope_kind k, Scope* p)
: kind(k), parent(p) { }
Scope_kind kind;
Scope* parent;
};
void push_scope(Scope_kind);
void pop_scope();
Scope* current_scope();
bool in_global_scope();
bool in_lambda_scope();
Expr* declare(Name*, Expr*);
Expr* declare(Expr*);
Expr* lookup(Name*);
// A helper class that guarantees that a scope is popped
// when it goes out of scope.
struct Scope_guard {
Scope_guard(Scope_kind k) { push_scope(k); }
~Scope_guard() { pop_scope(); }
};
#endif