tlang-interperter-cs/ArraySubscriptNode.cs

86 lines
2.4 KiB
C#
Raw Normal View History

2023-03-09 23:57:16 +00:00
namespace tlang
{
internal class ArraySubscriptNode : MemberGetVariableValue
{
public Node Argument {get;set;}
public ArraySubscriptNode(Node parent, string text, Node arg) : base(parent,text)
{
Argument = arg;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var arg = Argument.Execute(nodeEnv);
var p=Parent.Execute(nodeEnv);
var dict = p as TDictionary;
var ls = p as TArray;
var str = p as TString;
if(dict != null)
{
var sArg = arg as TString;
var at = dict["at"] as ICallable;
if(at != null)
{
return at.Call(arg);
}
if(sArg != null)
{
return dict[sArg.Value];
}
}
if(ls != null)
{
var nArg = arg as TNumber;
if(nArg != null && nArg.Value < ls.Items.Count)
{
return ls.Items[(int)nArg.Value];
}
}
if(str != null)
{
var nArg = arg as TNumber;
if(nArg != null && nArg.Value < str.Value.Length)
{
return new TChar(str.Value[(int)nArg.Value]);
}
}
return TObject.Uninit;
}
public override void SetValue(IScopeEnvironment nodeEnv, TObject value)
{
var arg = Argument.Execute(nodeEnv);
var p=Parent.Execute(nodeEnv);
var dict = p as TDictionary;
var ls = p as TArray;
var str = p as TString;
if(dict != null)
{
var sArg = arg as TString;
var at = dict["setAt"] as ICallable;
if(at != null)
{
at.Call(arg,value);
}
else
if(sArg != null)
{
dict[sArg.Value] = value;
}
}
if(ls != null)
{
var nArg = arg as TNumber;
if(nArg != null && nArg.Value < ls.Items.Count)
{
ls.Items[(int)nArg.Value] = value;
}
}
}
}
}