tlang-interperter-cs/tlanglib/MemberFunctionCallNode.cs

194 lines
6.3 KiB
C#

namespace tlang
{
internal class MemberFunctionCallNode : FunctionCallNode
{
public Node Parent {get;set;}
public MemberFunctionCallNode(Node parent, string name, List<Node> args) : base(name,args)
{
Parent = parent;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var res=Parent.Execute(nodeEnv);
var dict = res as TDictionary;
var integer = res as TNumber;
var str = res as TString;
var chr = res as TChar;
var array = res as TArray;
if(dict != null)
{
var n = dict[Text] as ICallable;
if(n != null)
{
return n.Call(Args.Select<Node,TObject>(e=>e.Execute(nodeEnv)).ToArray());
}
}else if(chr != null)
{
if(Text == "toUpper")
{
return new TChar(char.ToUpper(chr.Value));
}
if(Text == "toLower")
{
return new TChar(char.ToLower(chr.Value));
}
if(Text == "toString")
{
return new TString(chr.Value.ToString());
}
if(Text == "toNumber")
{
return new TNumber(chr.Value);
}
}
else if(array != null)
{
if(Text == "add")
{
if(Args.Count == 1)
{
TObject obj = Args[0].Execute(nodeEnv);
array.Items.Add(obj);
return obj;
}
}
if(Text == "at")
{
//at(index)
if(Args.Count == 1)
{
var index=Args[0].Execute(nodeEnv) as TNumber;
if(index != null)
{
if(index.Value < array.Items.Count)
return array.Items[(int)index.Value];
}
}
}
if(Text == "setAt")
{
TObject value = TObject.Null;
//setAt(index,value)
//setAt(index) //null
if(Args.Count == 2)
{
value=Args[1].Execute(nodeEnv);
}
if(Args.Count > 0)
{
var index=Args[0].Execute(nodeEnv) as TNumber;
if(index != null)
{
if(index.Value < array.Items.Count)
array.Items[(int)index.Value]=value;
}
}
return value;
}
if(Text == "length" || Text == "count")
{
return new TNumber(array.Items.Count);
}
}
else if(integer != null)
{
if(Text == "toString")
{
return new TString(integer.Value.ToString());
}
if(Text == "abs")
{
return new TNumber(Math.Abs(integer.Value));
}
if(Text == "toChar")
{
return new TChar((char)integer.Value);
}
}else if(str != null)
{
if(Text == "replace")
{
//todo fill this method out
}
if(Text == "split")
{
//todo fill this method out
//str.Value.Split(,,StringSplitOptions.TrimEntries);
}
if(Text == "startsWith")
{
string val = " ";
if(Args.Count > 0){
var firstArg = Args[0].Execute(nodeEnv);
var c = firstArg as TChar;
var mystr = firstArg as TString;
var mynum = firstArg as TNumber;
if(c!=null) val = c.Value.ToString();
if(mystr != null) val = mystr.Value.ToString();
if(mynum != null) val = mynum.Value.ToString();
}
return new TNumber(str.Value.StartsWith(val) ? 1 : 0);
}
if(Text == "trimStart")
{
if(Args.Count == 0)
{
return new TString(str.Value.TrimStart());
}else{
return new TString(str.Value.TrimStart(Args.Select<Node,char>(n=>{
var nV=n.Execute(nodeEnv) as TChar;
if(nV != null)
{
return nV.Value;
}
return ' ';
}).ToArray()));
}
}
if(Text == "trimEnd")
{
//trimend()
if(Args.Count == 0)
{
return new TString(str.Value.TrimEnd());
}else{
return new TString(str.Value.TrimEnd(Args.Select<Node,char>(n=>{
var nV=n.Execute(nodeEnv) as TChar;
if(nV != null)
{
return nV.Value;
}
return ' ';
}).ToArray()));
}
}
if(Text == "toString")
{
return str;
}
}
if(Text == "toString") return new TString("");
return new TUninit();
}
}
}