tlang-interperter-cs/tlanglib/MemberFunctionCallNode.cs

271 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
var myBool = res as TBoolean;
if(Text == "toChar2")
{
throw new Exception(res.GetType().Name);
}
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(myBool != null)
{
//branch
if(Text == "toChar")
{
return TObject.Char(myBool.Value ? 'y' : 'n');
}
if(Text == "toNumber")
{
return TObject.Number(myBool.Value ? 1 : 0);
}
}
else if(chr != null)
{
if(Text == "toUpper")
{
return TObject.Char(char.ToUpper(chr.Value));
}
if(Text == "toLower")
{
return TObject.Char(char.ToLower(chr.Value));
}
if(Text == "toNumber")
{
return TObject.Number(chr.Value);
}
}
else if(array != null)
{
if(Text == "add")
{
if(Args.Count == 1)
{
TObject obj = Args[0].Execute(nodeEnv);
array.Items.Add(obj);
return res;
}
}
if(Text == "remove")
{
if(Args.Count == 1)
{
TObject obj = Args[0].Execute(nodeEnv);
array.Items.Remove(obj);
return res;
}
}
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;
}
}
else if(integer != null)
{
if(Text == "int")
{
return TObject.Number(Math.Round(integer.Value));
}
if(Text == "abs")
{
return TObject.Number(Math.Abs(integer.Value));
}
if(Text == "toChar")
{
return TObject.Char((char)integer.Value);
}
}else if(str != null)
{
if(Text == "toArray")
{
var _array=TObject.Array();
foreach(var c in str.Value)
{
_array.Items.Add(TObject.Char(c));
}
}
if(Text == "replace")
{
//todo fill this method out
if(Args.Count >= 2)
return TObject.String(str.Value.Replace(Args[0].Execute(nodeEnv).ToString(),Args[1].Execute(nodeEnv).ToString()));
}
if(Text == "split")
{
if(Args.Count >= 1)
{
var text= str.Value;
var splitOn = Args[0].Execute(nodeEnv).ToString();
int count = int.MaxValue;
bool splitOp = false;
if(Args.Count >= 2)
{
splitOp = Args[1].Execute(nodeEnv).AsBoolean;
}
if(Args.Count >= 3)
{
var n = Args[2].Execute(nodeEnv) as TNumber;
if(n != null) count = (int)n.Value;
}
var array1=TObject.Array();
array1.Items.AddRange(text.Split(new string[]{splitOn},count,splitOp ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None).Select<string,TString>(e=>TObject.String(e)));
return array1;
}
//todo fill this method out
//str.Value.Split(,,StringSplitOptions.TrimEntries);
}
if(Text == "startsWith")
{
string val = " ";
if(Args.Count > 0){
val = Args[0].Execute(nodeEnv).ToString();
}
return TObject.Boolean(str.Value.StartsWith(val));
}
if(Text == "endsWith")
{
string val = " ";
if(Args.Count > 0){
val = Args[0].Execute(nodeEnv).ToString();
}
return TObject.Boolean(str.Value.EndsWith(val));
}
if(Text == "trimStart")
{
if(Args.Count == 0)
{
return TObject.String(str.Value.TrimStart());
}else{
return TObject.String(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 TObject.String(str.Value.TrimEnd());
}else{
return TObject.String(str.Value.TrimEnd(Args.Select<Node,char>(n=>{
var nV=n.Execute(nodeEnv) as TChar;
if(nV != null)
{
return nV.Value;
}
return ' ';
}).ToArray()));
}
}
}
if(dict == null)
{
if(Text == "toString")
{
return TObject.String(res.ToString());
}
if(Text == "toBool")
{
return TObject.Boolean(res.AsBoolean);
}
}
if(Text == "toString") return TObject.String("");
return TObject.Uninit;
}
}
}