tlang-interperter-cs/tlanglib/MemberGetValueNode.cs

139 lines
4.3 KiB
C#

using System;
namespace tlang
{
public class MemberGetVariableValue : GetVariableValue
{
public Node Parent {get;set;}
public MemberGetVariableValue(Node parent,string text) : base(text)
{
Parent = parent;
}
public override void SetValue(IScopeEnvironment nodeEnv, TObject value)
{
var res=Parent.Execute(nodeEnv);
var dict = res as TDictionary;
if(dict != null)
{
if(dict.MemberExists($"set{Name}"))
{
var mbm= dict[$"set{Name}"] as ICallable;
if(mbm != null)
{
mbm.Call(value);
}
}else{
dict[Name] = value;
}
}
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var res=Parent.Execute(nodeEnv);
var dict = res as TDictionary;
var integer = res as TNumber;
var array = res as TArray;
var str = res as TString;
if(array != null)
{
if(Name == "length" || Name == "count")
{
return TObject.Number(array.Items.Count);
}
if(Name == "ittr")
{
var ittr = TObject.Dictionary();
var enumerator=array.Items.GetEnumerator();
ittr["getcurrent"] = new TExternalMethod(()=>{
return enumerator.Current;
});
ittr["movenext"] = new TExternalMethod(()=>{
return TObject.Boolean(enumerator.MoveNext());
});
ittr["reset"] = new TExternalMethod(()=>{
});
ittr["dispose"] = new TExternalMethod(()=>{
enumerator.Dispose();
});
return ittr;
}
}
if(dict != null)
{
if(dict.MemberExists($"get{Name}"))
{
var mbm= dict[$"get{Name}"] as ICallable;
if(mbm != null)
return mbm.Call();
}
if(dict.MemberExists(Name))
{
return dict[Name];
}
}
if(integer != null)
{
if(Name == "abs")
{
return TObject.Number(Math.Abs(integer.Value));
}
if(Name == "int")
{
return TObject.Number(Math.Round(integer.Value));
}
}
if(str != null)
{
if(Name == "number")
{
double value;
if(double.TryParse(str.Value,out value))
{
return TObject.Number(value);
}
return TObject.Null;
}
if(Name == "ittr")
{
var ittr = TObject.Dictionary();
var enumerator=str.Value.GetEnumerator();
ittr["getcurrent"] = new TExternalMethod(()=>{
return TObject.Char(enumerator.Current);
});
ittr["movenext"] = new TExternalMethod(()=>{
return TObject.Boolean(enumerator.MoveNext());
});
ittr["reset"] = new TExternalMethod(()=>{
enumerator.Reset();
});
ittr["dispose"] = new TExternalMethod(()=>{
enumerator.Dispose();
});
return ittr;
}
if(Name == "boolean")
{
return TObject.Boolean(str.Value.ToLower() == "true");
}
if(Name == "length" || Name == "count")
{
return TObject.Number(str.Value.Length);
}
}
return TObject.Uninit;
}
}
}