tlang-interperter-cs/AddNode.cs

52 lines
1.4 KiB
C#

namespace tlang
{
internal class AddNode : Node
{
public Node Left {get;set;}
public Node Right {get;set;}
public AddNode(Node left, Node right)
{
this.Left = left;
this.Right = right;
}
public override TObject Execute(IScopeEnvironment nodeEnv)
{
var l=Left.Execute(nodeEnv);
var r = Right.Execute(nodeEnv);
var lNum = l as TNumber;
var rNum = r as TNumber;
var lStr = l as TString;
var rStr = r as TString;
var dleft = l as TDictionary;
if(dleft != null)
{
if(dleft.MemberExists("add"))
{
var mbm = dleft["add"] as ICallable;
if(mbm != null)
{
return mbm.Call(r);
}
}
}
if(lNum != null && rNum != null)
return new TNumber(lNum.Value + rNum.Value);
if(lStr != null && rStr != null)
return new TString(lStr.Value + rStr.Value);
if(lStr != null && rNum != null)
return new TString(lStr.Value + rNum.Value.ToString());
if(lNum != null && rStr != null)
return new TString(lNum.Value.ToString() + rStr.Value);
return new TNull();
}
}
}