tlang-interperter-cs/tlanglib/MemberGetValueNode.cs

139 lines
4.3 KiB
C#
Raw Normal View History

2023-03-10 10:12:36 +00:00
using System;
2023-03-09 19:40:14 +00:00
namespace tlang
{
public class MemberGetVariableValue : GetVariableValue
{
public Node Parent {get;set;}
public MemberGetVariableValue(Node parent,string text) : base(text)
{
Parent = parent;
}
2023-03-09 23:57:16 +00:00
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;
}
2023-03-09 19:40:14 +00:00
2023-03-09 23:57:16 +00:00
}
}
2023-03-09 19:40:14 +00:00
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;
2023-03-09 23:57:16 +00:00
var str = res as TString;
2023-03-09 19:40:14 +00:00
if(array != null)
{
if(Name == "length" || Name == "count")
{
2023-06-08 18:14:35 +00:00
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;
2023-03-09 19:40:14 +00:00
}
}
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)
2023-03-09 23:57:16 +00:00
{
if(Name == "abs")
{
2023-06-08 18:14:35 +00:00
return TObject.Number(Math.Abs(integer.Value));
2023-03-09 23:57:16 +00:00
}
if(Name == "int")
{
2023-06-08 18:14:35 +00:00
return TObject.Number(Math.Round(integer.Value));
2023-03-09 23:57:16 +00:00
}
}
if(str != null)
2023-03-09 19:40:14 +00:00
{
2023-03-09 23:57:16 +00:00
if(Name == "number")
{
2023-06-08 18:14:35 +00:00
double value;
if(double.TryParse(str.Value,out value))
{
return TObject.Number(value);
}
return TObject.Null;
2023-03-09 23:57:16 +00:00
}
if(Name == "ittr")
{
2023-06-08 18:14:35 +00:00
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;
2023-03-09 23:57:16 +00:00
}
2023-06-08 18:14:35 +00:00
if(Name == "boolean")
2023-03-09 23:57:16 +00:00
{
2023-06-08 18:14:35 +00:00
return TObject.Boolean(str.Value.ToLower() == "true");
2023-03-09 23:57:16 +00:00
}
2023-06-08 18:14:35 +00:00
if(Name == "length" || Name == "count")
2023-03-09 23:57:16 +00:00
{
2023-06-08 18:14:35 +00:00
return TObject.Number(str.Value.Length);
2023-03-09 23:57:16 +00:00
}
2023-06-08 18:14:35 +00:00
2023-03-09 19:40:14 +00:00
}
2023-03-09 23:57:16 +00:00
2023-06-08 18:14:35 +00:00
return TObject.Uninit;
2023-03-09 19:40:14 +00:00
}
}
2023-06-08 18:14:35 +00:00
}