Upsilon/Upsilon/BaseTypes/ScriptTable/ScriptTable.cs

66 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2018-11-17 18:13:05 +00:00
using System.Linq;
2018-11-24 14:11:33 +00:00
using Upsilon.BaseTypes.Number;
2018-11-23 17:18:07 +00:00
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Binder.VariableSymbols;
2018-11-19 11:17:21 +00:00
using Upsilon.Evaluator;
using Upsilon.Text;
2018-11-17 18:13:05 +00:00
namespace Upsilon.BaseTypes.ScriptTable
2018-11-17 18:13:05 +00:00
{
public abstract class ScriptTable : ScriptType, IIndexable, IScopeOwner, IIterable, ILengthType
2018-11-17 18:13:05 +00:00
{
2018-11-19 11:17:21 +00:00
public EvaluationScope EvaluationScope { get; }
2018-11-17 18:13:05 +00:00
2018-11-23 17:18:07 +00:00
public ScriptTable(EvaluationScope scope)
2018-11-17 18:13:05 +00:00
{
2018-11-19 11:17:21 +00:00
EvaluationScope = scope;
2018-11-17 18:13:05 +00:00
}
public override TypeContainer Type => BaseTypes.Type.Table;
2018-11-23 17:18:07 +00:00
public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope)
2018-11-18 13:18:24 +00:00
{
var s = index.ToString();
2018-11-19 11:49:48 +00:00
if (EvaluationScope.TryGet(s, out var o))
2018-11-19 11:17:21 +00:00
{
return o;
}
2018-11-23 17:18:07 +00:00
return new ScriptNull();
2018-11-18 13:18:24 +00:00
}
2018-11-19 15:22:13 +00:00
2018-11-23 17:18:07 +00:00
public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value)
2018-11-19 15:22:13 +00:00
{
var s = index.ToString();
if (value.Type == BaseTypes.Type.Nil)
{
EvaluationScope.Delete(s);
return;
}
2018-11-24 11:42:54 +00:00
EvaluationScope.CreateLocal(new VariableSymbol(s, value.Type, false), value);
2018-11-19 15:22:13 +00:00
}
2019-02-25 12:46:06 +00:00
public ScriptType GetValueFromIndex(ScriptType index)
{
if (EvaluationScope.TryGet(index.ToString(), out var result))
{
return result;
}
return new ScriptNull();
}
public abstract IEnumerator<ScriptType> GetScriptEnumerator();
2019-02-25 12:39:45 +00:00
public abstract bool Contains(ScriptType obj);
2019-02-25 12:46:06 +00:00
public void Delete(ScriptType key)
{
EvaluationScope.Delete(key.ToString());
}
2018-11-24 14:11:33 +00:00
public ScriptNumberLong Length()
{
return new ScriptNumberLong(EvaluationScope.Variables.Count);
}
2018-11-17 18:13:05 +00:00
}
}