Upsilon/Upsilon/BaseTypes/LuaTable.cs

42 lines
1.3 KiB
C#
Raw Normal View History

2018-11-19 11:17:21 +00:00
using System.Collections;
2018-11-17 18:13:05 +00:00
using System.Collections.Generic;
using System.Linq;
using Upsilon.Binder;
2018-11-19 11:17:21 +00:00
using Upsilon.Evaluator;
2018-11-17 18:13:05 +00:00
namespace Upsilon.BaseTypes
{
2018-11-19 11:17:21 +00:00
internal class LuaTable : LuaType, IIndexable, IScopeOwner
2018-11-17 18:13:05 +00:00
{
2018-11-18 13:18:24 +00:00
private readonly Dictionary<string, VariableSymbol> _variableLookup;
2018-11-19 11:17:21 +00:00
public EvaluationScope EvaluationScope { get; }
2018-11-17 18:13:05 +00:00
2018-11-19 11:17:21 +00:00
public LuaTable(EvaluationScope scope)
2018-11-17 18:13:05 +00:00
{
2018-11-19 11:17:21 +00:00
EvaluationScope = scope;
_variableLookup = scope.Variables.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-19 11:17:21 +00:00
return EvaluationScope.Variables.Where(x => !x.Key.Local)
.ToDictionary(x => x.Key.Name, x => x.Value.ToCSharpObject());
2018-11-17 18:13:05 +00:00
}
2018-11-19 11:17:21 +00:00
public LuaType Get(string s, EvaluationScope scope)
2018-11-18 13:18:24 +00:00
{
if (!_variableLookup.TryGetValue(s, out var variableSymbol))
return new LuaNull();
2018-11-19 11:17:21 +00:00
if (variableSymbol.Local && scope != EvaluationScope)
2018-11-18 13:18:24 +00:00
return new LuaNull();
2018-11-19 11:17:21 +00:00
if (EvaluationScope.TryGet(variableSymbol, out var o))
{
return o;
}
return new LuaNull();
2018-11-18 13:18:24 +00:00
}
2018-11-17 18:13:05 +00:00
}
}