public override object VisitFunctionDefinition([NotNull] functionalParser.FunctionDefinitionContext context)
        {
            var predicate = ((string, string[], Ty))Visit(context.predicate());

            FunctionType functionPredicate;

            if (predicate.Item3.Type.Is <FunctionType>())
            {
                functionPredicate = predicate.Item3.Type.As <FunctionType>();
            }
            else
            {
                // If the predicate is not a function type make it into one (e.g. main :: Int)
                functionPredicate = new FunctionType(new Ty[] { predicate.Item3 });
            }

            var overloads = context.stmt().Select(
                (obj) => ((string, Pattern[], Node, WhereClauseNode))Visit(obj)).ToArray();

            // check if all functions are of the same name
            if (overloads.Any((obj) => obj.Item1 != predicate.Item1))
            {
                ErrorReporter.Error("The function block `{0}` contains different functions", predicate.Item1);
            }

            return(new FunctionNode(
                       predicate.Item1,
                       overloads.Select((obj) => (obj.Item2, obj.Item3, obj.Item4)).ToArray(),
                       functionPredicate,
                       predicate.Item2,
                       FileAndLine(context)
                       ));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Visit a parse tree produced by the <c>FunctionDefinition</c>
 /// labeled alternative in <see cref="functionalParser.definition"/>.
 /// <para>
 /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
 /// on <paramref name="context"/>.
 /// </para>
 /// </summary>
 /// <param name="context">The parse tree.</param>
 /// <return>The visitor result.</return>
 public virtual Result VisitFunctionDefinition([NotNull] functionalParser.FunctionDefinitionContext context)
 {
     return(VisitChildren(context));
 }