58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Upsilon.BaseTypes;
|
|
|
|
namespace Upsilon.Binder
|
|
{
|
|
internal class BoundScope
|
|
{
|
|
public readonly BoundScope ParentScope;
|
|
private readonly Dictionary<string, VariableSymbol> _variables;
|
|
|
|
public BoundScope(BoundScope parentScope)
|
|
{
|
|
ParentScope = parentScope;
|
|
_variables = new Dictionary<string, VariableSymbol>();
|
|
}
|
|
public BoundScope(Dictionary<VariableSymbol, LuaType> variables, BoundScope parentScope)
|
|
{
|
|
ParentScope = parentScope;
|
|
_variables = variables.ToDictionary(x => x.Key.Name, x => x.Key);
|
|
}
|
|
|
|
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, bool allowUpperScopes, out VariableSymbol result)
|
|
{
|
|
if (_variables.TryGetValue(key, out result))
|
|
{
|
|
return true;
|
|
}
|
|
if (ParentScope != null && allowUpperScopes)
|
|
{
|
|
return ParentScope.TryGetVariable(key, true, out result);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
}
|
|
} |