-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathSymbolTable.cs
More file actions
130 lines (111 loc) · 3.8 KB
/
Copy pathSymbolTable.cs
File metadata and controls
130 lines (111 loc) · 3.8 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
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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System.Collections.Generic;
using OneScript.Contexts;
namespace OneScript.Compilation.Binding
{
public class SymbolTable
{
private class BindingRecord
{
public SymbolScope scope;
public IAttachableContext target;
}
private readonly List<BindingRecord> _bindings = new List<BindingRecord>();
public SymbolScope GetScope(int index) => _bindings[index].scope;
public IAttachableContext GetBinding(int scopeIndex) => _bindings[scopeIndex].target;
public int ScopeCount => _bindings.Count;
public int PushScope(SymbolScope scope, IAttachableContext target)
{
var idx = _bindings.Count;
_bindings.Add(new BindingRecord
{
scope = scope,
target = target
});
return idx;
}
public void PopScope()
{
_bindings.RemoveAt(_bindings.Count - 1);
}
public bool FindVariable(string name, out SymbolBinding binding)
{
for (int i = _bindings.Count - 1; i >= 0; i--)
{
var scope = _bindings[i].scope;
var idx = scope.Variables.IndexOf(name);
if (idx >= 0)
{
binding = new SymbolBinding
{
ScopeNumber = i,
MemberNumber = idx
};
return true;
}
}
binding = default;
return false;
}
public bool TryFindMethodBinding(string name, out SymbolBinding binding)
{
for (int i = _bindings.Count - 1; i >= 0; i--)
{
var scope = _bindings[i].scope;
var idx = scope.Methods.IndexOf(name);
if (idx >= 0)
{
binding = new SymbolBinding
{
ScopeNumber = i,
MemberNumber = idx
};
return true;
}
}
binding = default;
return false;
}
public bool TryFindMethod(string name, out IMethodSymbol method)
{
if (TryFindMethodBinding(name, out var binding))
{
method = GetMethod(binding);
return true;
}
method = default;
return false;
}
public SymbolBinding DefineMethod(IMethodSymbol symbol)
{
var index = _bindings[ScopeCount - 1].scope.DefineMethod(symbol);
return new SymbolBinding
{
ScopeNumber = ScopeCount - 1,
MemberNumber = index
};
}
public SymbolBinding DefineVariable(IVariableSymbol symbol)
{
var index = _bindings[ScopeCount - 1].scope.DefineVariable(symbol);
return new SymbolBinding
{
ScopeNumber = ScopeCount - 1,
MemberNumber = index
};
}
public IVariableSymbol GetVariable(SymbolBinding binding)
{
return GetScope(binding.ScopeNumber).Variables[binding.MemberNumber];
}
public IMethodSymbol GetMethod(SymbolBinding binding)
{
return GetScope(binding.ScopeNumber).Methods[binding.MemberNumber];
}
}
}