Upsilon/Upsilon/BaseTypes/SimpleScriptTable.cs

65 lines
1.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Evaluator;
using Upsilon.Text;
namespace Upsilon.BaseTypes
{
internal class SimpleScriptTable : ScriptType, IIndexable
{
private readonly IList<ScriptType> _objects;
public SimpleScriptTable(IList<ScriptType> objects)
{
_objects = objects;
}
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
{
int i;
switch (index.Type.Type)
{
case BaseTypes.Type.String:
{
var str = (ScriptString)index;
i = int.Parse(str.Value) - 1;
break;
}
case BaseTypes.Type.Number:
{
var ind = (Number.ScriptNumberLong) index;
i = (int) ind.Value - 1;
break;
}
default:
return null;
}
return _objects[i];
}
public ScriptType this[int index] => _objects[index];
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
{
throw new System.NotImplementedException();
}
public override TypeContainer Type => BaseTypes.Type.Table;
public override object ToCSharpObject()
{
return _objects;
}
public override System.Type GetCSharpType()
{
return _objects.GetType();
}
public override string ToString()
{
return $"{{{string.Join(", ", _objects.Select(x => x.ToString()))}}}";
}
}
}