tlang-interperter-cs/tlanglib/AddNode.cs

52 lines
1.4 KiB
C#
Raw Normal View History

2023-03-09 19:40:14 +00:00
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;
2023-03-09 23:57:16 +00:00
var dleft = l as TDictionary;
2023-03-09 19:40:14 +00:00
2023-03-09 23:57:16 +00:00
if(dleft != null)
{
if(dleft.MemberExists("add"))
{
var mbm = dleft["add"] as ICallable;
if(mbm != null)
{
return mbm.Call(r);
}
}
}
2023-03-09 19:40:14 +00:00
2023-03-09 23:57:16 +00:00
2023-03-09 19:40:14 +00:00
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);
2023-03-09 23:57:16 +00:00
2023-03-09 19:40:14 +00:00
return new TNull();
}
}
}