2018-11-24 13:35:23 +00:00
|
|
|
using System;
|
2018-11-21 12:08:41 +00:00
|
|
|
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;
|
2018-11-30 14:28:36 +00:00
|
|
|
using Upsilon.Binder.VariableSymbols;
|
2018-11-19 11:17:21 +00:00
|
|
|
using Upsilon.Evaluator;
|
2018-11-20 16:33:51 +00:00
|
|
|
using Upsilon.Text;
|
2018-11-17 18:13:05 +00:00
|
|
|
|
2018-12-09 12:23:09 +00:00
|
|
|
namespace Upsilon.BaseTypes.ScriptTable
|
2018-11-17 18:13:05 +00:00
|
|
|
{
|
2018-12-09 12:23:09 +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
|
|
|
}
|
|
|
|
|
2019-01-19 16:50:54 +00:00
|
|
|
public override TypeContainer Type => BaseTypes.Type.Table;
|
2018-11-21 12:08:41 +00:00
|
|
|
|
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
|
|
|
{
|
2018-11-21 13:49:59 +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
|
|
|
{
|
2018-11-21 13:49:59 +00:00
|
|
|
var s = index.ToString();
|
2019-01-19 16:50:54 +00:00
|
|
|
if (value.Type == BaseTypes.Type.Nil)
|
2018-12-09 18:02:35 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2018-11-21 19:59:27 +00:00
|
|
|
|
2019-02-25 12:46:06 +00:00
|
|
|
|
2018-11-24 13:35:23 +00:00
|
|
|
public ScriptType GetValueFromIndex(ScriptType index)
|
|
|
|
{
|
|
|
|
if (EvaluationScope.TryGet(index.ToString(), out var result))
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2018-12-10 16:15:25 +00:00
|
|
|
return new ScriptNull();
|
2018-11-24 13:35:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 17:23:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|