Implements setting to CSHarp lists

This commit is contained in:
Deukhoofd 2018-12-09 14:05:17 +01:00
parent b63e8d37b5
commit 2e0ac29878
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
1 changed files with 37 additions and 2 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using Upsilon.BaseTypes.Number; using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces; using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Evaluator; using Upsilon.Evaluator;
using Upsilon.Exceptions;
using Upsilon.Text; using Upsilon.Text;
namespace Upsilon.BaseTypes.UserData namespace Upsilon.BaseTypes.UserData
@ -37,9 +38,43 @@ namespace Upsilon.BaseTypes.UserData
return List[i].ToScriptType(); 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; public override Type Type => Type.UserData;