tlang-interperter-cs/tlanglib/AddNode.cs

58 lines
1.5 KiB
C#

using System.IO;
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 TObject.Number(lNum.Value + rNum.Value);
if(lStr != null && rStr != null)
return TObject.String(lStr.Value + rStr.Value);
if(lStr != null && rNum != null)
return TObject.String(lStr.Value + rNum.Value.ToString());
if(lNum != null && rStr != null)
return TObject.String(lNum.Value.ToString() + rStr.Value);
return TObject.Null;
}
}
}