This repository was archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathScope.h
More file actions
88 lines (54 loc) · 2.08 KB
/
Scope.h
File metadata and controls
88 lines (54 loc) · 2.08 KB
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
#pragma once
#include <memory>
#include "AST/DeclNode.h"
#include "Support/Alias.h"
#include "Support/Find.h"
class TopLevelScope;
class LocalScope;
/// 作用域
class Scope : public std::enable_shared_from_this<Scope> {
public:
Scope() = default;
virtual ~Scope() = default;
virtual void addChild(const SharedPtr<LocalScope> &scope) final;
virtual void addVariable(const String &varName, const SharedPtr<VarDeclNode> &varDecl) final;
virtual bool hasVariable(const String &name) final;
virtual bool isTopLevel() = 0;
virtual SharedPtr<TopLevelScope> getTopLevel() = 0;
virtual SharedPtr<Scope> getParent() = 0;
virtual SharedPtr<VarDeclNode> resolveVariable(const String &name) = 0;
SharedPtr<Node> host;
protected:
SharedPtrVector<LocalScope> children;
SharedPtrMap<String, VarDeclNode> variables;
};
/// 顶级作用域
class TopLevelScope : public Scope {
public:
static SharedPtr<TopLevelScope> create();
TopLevelScope() = default;
~TopLevelScope() override = default;
bool isTopLevel() override;
SharedPtr<TopLevelScope> getTopLevel() override;
SharedPtr<Scope> getParent() override;
virtual void addFunction(const String &name, const SharedPtr<FunctionDeclNode> &funcDecl) final;
virtual bool hasFunction(const String &name) final;
SharedPtr<VarDeclNode> resolveVariable(const String &name) override;
SharedPtr<FunctionDeclNode> resolveFunction(const String &name);
private:
SharedPtrMap<String, FunctionDeclNode> functions;
SharedPtrMap<String, FunctionDeclNode> libFunctions = FunctionDeclNode::getBuiltinFunctions();
};
/// 局部作用域
class LocalScope : public Scope {
public:
static SharedPtr<LocalScope> create(const SharedPtr<Scope> &parent);
explicit LocalScope(const SharedPtr<Scope> &parent);
~LocalScope() override = default;
bool isTopLevel() override;
SharedPtr<TopLevelScope> getTopLevel() override;
SharedPtr<Scope> getParent() override;
SharedPtr<VarDeclNode> resolveVariable(const String &name) override;
private:
SharedPtr<Scope> parent;
};