Beispiel #1
0
        private void DefineOperator(LeafParser.Def_operatorContext def, LeafParser.Attribute_addContext[]?attribs)
        {
            var impl = def.function_impl();
            var name = def.operator_id().GetText();

            try
            {
                var func = DefineFunction(name, null, impl, def.generic_def_list(), attribs);

                if ((func.Flags & FunctionFlags.MemberFunc) == 0)
                {
                    throw new CompilationException("Operator is not bound to a type. Consider using 'this' as the first parameter.",
                                                   this, def.Start.Line);
                }

                var requiredParameters = name switch
                {
                    "as" => 1,
                    "+" or "-" or "*" or "/" or "%" => 2,
                    _ => throw new NotImplementedException($"Operator {name} is not supported.")
                };

                if (func.Parameters.Count != requiredParameters)
                {
                    throw new CompilationException($"Invalid number of parameters specified. Required: {requiredParameters}, found: {func.Parameters.Count}.",
                                                   this, def.Start.Line);
                }
            }
            catch (CompilationException e)
            { throw new CompilationException($"Could not define operator '{name}'.", this, def.Start.Line, e); }
        }
 /// <summary>
 /// Visit a parse tree produced by <see cref="LeafParser.def_operator"/>.
 /// <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 VisitDef_operator([NotNull] LeafParser.Def_operatorContext context)
 {
     return(VisitChildren(context));
 }