diff --git a/Upsilon/BaseTypes/UserData/ListUserData.cs b/Upsilon/BaseTypes/UserData/ListUserData.cs index cafcc59..c063bcf 100644 --- a/Upsilon/BaseTypes/UserData/ListUserData.cs +++ b/Upsilon/BaseTypes/UserData/ListUserData.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.Evaluator; +using Upsilon.Exceptions; using Upsilon.Text; namespace Upsilon.BaseTypes.UserData @@ -37,9 +38,43 @@ namespace Upsilon.BaseTypes.UserData return List[i].ToScriptType(); } - public void Set(Diagnostics diagnostics, TextSpan span, ScriptType index, ScriptType value) + public void Set(Diagnostics diagnostics, TextSpan span, ScriptType scriptIndex, ScriptType value) { - throw new System.NotImplementedException(); + var index = -1; + if (scriptIndex.Type == Type.Number || scriptIndex.Type == Type.Unknown) + { + index = Convert.ToInt32(scriptIndex.ToCSharpObject()); + } + else if (scriptIndex.Type == Type.String) + { + index = int.Parse(((ScriptString) scriptIndex).Value); + } + else + { + var (i, pos) = diagnostics.ScriptString.GetLinePosition(span.Start); + var line = diagnostics.ScriptString.GetLine(i); + throw new ScriptRuntimeException( + $"Tried indexing a CSharp list with a value that's not an integer. Value: {scriptIndex.ToCSharpObject()}", + i, pos, line); + } + + index--; + if (index > List.Count) + { + if (index == List.Count) + { + List.Add(value.ToCSharpObject()); + } + else + { + throw new Exception( + $"Tried adding an item to a list at position {index}, but list is only of length {List.Count}"); + } + } + else + { + List[index] = value.ToCSharpObject(); + } } public override Type Type => Type.UserData;