Lots of work on type binding
This commit is contained in:
51
Upsilon/Binder/BoundScope.cs
Normal file
51
Upsilon/Binder/BoundScope.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Upsilon.Binder
|
||||
{
|
||||
public class BoundScope
|
||||
{
|
||||
private readonly BoundScope _parentScope;
|
||||
private readonly Dictionary<string, VariableSymbol> _variables;
|
||||
|
||||
public BoundScope(BoundScope parentScope)
|
||||
{
|
||||
_parentScope = parentScope;
|
||||
_variables = new Dictionary<string, VariableSymbol>();
|
||||
}
|
||||
|
||||
public void SetVariable(VariableSymbol var)
|
||||
{
|
||||
if (_variables.ContainsKey(var.Name))
|
||||
_variables[var.Name] = var;
|
||||
else
|
||||
_variables.Add(var.Name, var);
|
||||
}
|
||||
|
||||
public void SetGlobalVariable(VariableSymbol var)
|
||||
{
|
||||
if (_parentScope == null)
|
||||
{
|
||||
SetVariable(var);
|
||||
}
|
||||
else
|
||||
{
|
||||
_parentScope.SetGlobalVariable(var);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool TryGetVariable(string key, out VariableSymbol result)
|
||||
{
|
||||
if (_variables.TryGetValue(key, out result))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (_parentScope != null)
|
||||
{
|
||||
return _parentScope.TryGetVariable(key, out result);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user