Upsilon/Upsilon/Binder/VariableSymbols/FunctionVariableSymbol.cs

51 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Upsilon.BaseTypes;
namespace Upsilon.Binder.VariableSymbols
{
public abstract class FunctionVariableSymbol : VariableSymbol
{
public List<FunctionVariableSymbolOption> FunctionOption { get; protected set; } = new List<FunctionVariableSymbolOption>();
public bool IsCoroutine { get; }
public FunctionVariableSymbol(string name, bool local, bool isCoroutine)
: base(name, Type.Function, local)
{
IsCoroutine = isCoroutine;
}
public abstract bool ValidateParameters(
ImmutableArray<BoundExpression> callingParameters);
public FunctionVariableSymbolOption GetFirstValid(TypeContainer[] types)
{
return FunctionOption.FirstOrDefault(x =>
{
var parTypes = x.GetParameterTypes();
for (var i = 0; i < parTypes.Length; i++)
{
var parType = parTypes[i];
var givenType = types[i];
if (parType == Type.Unknown || givenType == Type.Unknown)
continue;
if (!parType.Type.HasFlag(givenType))
return false;
}
return true;
});
}
}
public abstract class FunctionVariableSymbolOption
{
public FunctionVariableSymbolOption(TypeContainer resultType)
{
ResultType = resultType;
}
public abstract TypeContainer[] GetParameterTypes();
public TypeContainer ResultType { get; internal set; }
}
}