ShintenScript/ASTNodeBinaryNumericOperato...

54 lines
1.5 KiB
C#
Raw Permalink Normal View History

2023-02-02 14:05:53 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShintenScript
{
public abstract class ASTNodeBinaryNumericOperator : IASTNode
{
public IASTNode lhs, rhs;
protected ASTNodeBinaryNumericOperator(IASTNode lhs, IASTNode rhs)
{
this.lhs = lhs;
this.rhs = rhs;
}
public bool Const()
{
return lhs.Const() && rhs.Const();
}
public object CreateFunction()
{
SSType lhst = lhs.Type();
SSType rhst = rhs.Type();
if (lhst == SSType.real && rhst == SSType.real)
{
Func<ExecutionContext, float> left = (Func<ExecutionContext, float>)lhs.CreateFunction();
Func<ExecutionContext, float> right = (Func<ExecutionContext, float>)rhs.CreateFunction();
return CreateFunction11(left, right);
}
throw new Exception("This should be unreachable");
}
public SSType Type()
{
SSType lhst = lhs.Type();
SSType rhst = rhs.Type();
if (lhst == SSType.real && rhst == SSType.real)
return SSType.real;
throw new TypeException($"Unsupported types for operator '{Op()}': {lhst} and {rhst}");
}
public abstract string Op();
public abstract object CreateFunction11(Func<ExecutionContext, float> left, Func<ExecutionContext, float> right);
}
}