2018-11-17 18:13:05 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2018-11-17 18:56:52 +00:00
|
|
|
using Upsilon.Binder;
|
2018-11-17 18:13:05 +00:00
|
|
|
|
|
|
|
namespace Upsilon.BaseTypes
|
|
|
|
{
|
2018-11-18 13:18:24 +00:00
|
|
|
public class LuaTable : LuaType, IIndexable
|
2018-11-17 18:13:05 +00:00
|
|
|
{
|
2018-11-18 13:18:24 +00:00
|
|
|
private readonly Dictionary<string, VariableSymbol> _variableLookup;
|
|
|
|
private readonly Dictionary<VariableSymbol, LuaType> _map;
|
|
|
|
public int EvaluatorIdentifier { get; }
|
2018-11-17 18:13:05 +00:00
|
|
|
|
2018-11-18 13:18:24 +00:00
|
|
|
public LuaTable(Dictionary<VariableSymbol, LuaType> map, int scopeIdentifier)
|
2018-11-17 18:13:05 +00:00
|
|
|
{
|
|
|
|
_map = map;
|
2018-11-18 13:18:24 +00:00
|
|
|
EvaluatorIdentifier = scopeIdentifier;
|
2018-11-17 18:56:52 +00:00
|
|
|
_variableLookup = map.ToDictionary(x => x.Key.Name, x => x.Key);
|
2018-11-17 18:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Type Type => Type.Table;
|
|
|
|
public override object ToCSharpObject()
|
|
|
|
{
|
2018-11-17 18:56:52 +00:00
|
|
|
return _map.Where(x => !x.Key.Local).ToDictionary(x => x.Key.Name, x => x.Value.ToCSharpObject());
|
2018-11-17 18:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-18 13:18:24 +00:00
|
|
|
public LuaType Get(string s, int scopeIdentifier)
|
|
|
|
{
|
|
|
|
if (!_variableLookup.TryGetValue(s, out var variableSymbol))
|
|
|
|
return new LuaNull();
|
|
|
|
if (EvaluatorIdentifier != scopeIdentifier && variableSymbol.Local)
|
|
|
|
return new LuaNull();
|
|
|
|
return _map[variableSymbol];
|
|
|
|
}
|
|
|
|
|
2018-11-17 18:13:05 +00:00
|
|
|
}
|
|
|
|
}
|