AspectedRouting/AspectedRouting/Language/Expression/FunctionCall.cs

95 lines
No EOL
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AspectedRouting.Language.Typ;
using Type = AspectedRouting.Language.Typ.Type;
namespace AspectedRouting.Language.Expression
{
public class FunctionCall : IExpression
{
private readonly string _name;
public string CalledFunctionName
{
get
{
if (_name.StartsWith("$"))
{
return _name.Substring(1);
}
else
{
return _name;
}
}
}
public IEnumerable<Type> Types { get; }
public FunctionCall(string name, IEnumerable<Type> types)
{
_name = name;
Types = types;
}
public object Evaluate(Context c, params IExpression[] arguments)
{
var func = c.GetFunction(_name);
c = c.WithAspectName(_name);
return func.Evaluate(c, arguments);
}
public IExpression Specialize(IEnumerable<Type> allowedTypes)
{
var unified = Types.SpecializeTo(allowedTypes);
if (unified == null)
{
return null;
}
return new FunctionCall(_name, unified);
}
public IExpression PruneTypes(Func<Type, bool> allowedTypes)
{
var passedTypes = this.Types.Where(allowedTypes);
if (!passedTypes.Any()) {
return null;
}
return new FunctionCall(this._name, passedTypes);
}
public IExpression Optimize()
{
return this;
}
public IExpression OptimizeWithArgument(IExpression argument)
{
if (_name.Equals(Funcs.Id.Name))
{
return argument;
}
return this.Apply(argument);
}
public void Visit(Func<IExpression, bool> f)
{
f(this);
}
public override string ToString()
{
return $"${_name}";
}
}
}