tlang-interperter-cs/tlanglib/MemberFunctionCallNode.cs

198 lines
6.3 KiB
C#
Raw Normal View History

2023-03-10 10:12:36 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2023-03-09 19:40:14 +00:00
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)
{
2023-03-10 04:33:58 +00:00
2023-03-09 19:40:14 +00:00
var res=Parent.Execute(nodeEnv);
var dict = res as TDictionary;
var integer = res as TNumber;
2023-03-09 23:57:16 +00:00
var str = res as TString;
var chr = res as TChar;
var array = res as TArray;
2023-03-09 19:40:14 +00:00
if(dict != null)
{
var n = dict[Text] as ICallable;
if(n != null)
{
return n.Call(Args.Select<Node,TObject>(e=>e.Execute(nodeEnv)).ToArray());
}
2023-03-09 23:57:16 +00:00
}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)
{
2023-03-10 04:33:58 +00:00
2023-03-09 23:57:16 +00:00
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);
}
2023-03-09 19:40:14 +00:00
}
else if(integer != null)
{
if(Text == "toString")
{
return new TString(integer.Value.ToString());
}
if(Text == "abs")
{
return new TNumber(Math.Abs(integer.Value));
}
2023-03-09 23:57:16 +00:00
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;
}
2023-03-09 19:40:14 +00:00
}
if(Text == "toString") return new TString("");
return new TUninit();
}
}
2023-03-09 23:57:16 +00:00
2023-03-09 19:40:14 +00:00
}