using System; using System.Collections.Generic; using System.Linq; using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.Binder.VariableSymbols; using Upsilon.Evaluator; using Upsilon.Text; namespace Upsilon.BaseTypes.ScriptTable { public abstract class ScriptTable : ScriptType, IIndexable, IScopeOwner, IIterable, ILengthType { public EvaluationScope EvaluationScope { get; } public ScriptTable(EvaluationScope scope) { EvaluationScope = scope; } public override TypeContainer Type => BaseTypes.Type.Table; public ScriptType Get(Diagnostics diagnostics, TextSpan span, ScriptType index, EvaluationScope scope) { var s = index.ToString(); if (EvaluationScope.TryGet(s, out var o)) { return o; } return new ScriptNull(); } public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value) { var s = index.ToString(); if (value.Type == BaseTypes.Type.Nil) { EvaluationScope.Delete(s); return; } EvaluationScope.CreateLocal(new VariableSymbol(s, value.Type, false), value); } public ScriptType GetValueFromIndex(ScriptType index) { if (EvaluationScope.TryGet(index.ToString(), out var result)) { return result; } return new ScriptNull(); } public abstract IEnumerator GetScriptEnumerator(); public abstract bool Contains(ScriptType obj); public void Delete(ScriptType key) { EvaluationScope.Delete(key.ToString()); } public ScriptNumberLong Length() { return new ScriptNumberLong(EvaluationScope.Variables.Count); } } }