示例#1
0
文件: Gen.cs 项目: zverev-iv/Telega
        static NestedText GenTypeTagBody(string typeName, string tagName, Signature tag)
        {
            var modifiedArgs = tag.Args
                               .Map(x => new Arg(
                                        name: x.Name == tagName ? $"{x.Name}Value" : x.Name, // TODO: move to normalizer
                                        type: x.Type,
                                        kind: x.Kind
                                        ))
                               .ToArr();
            var argsWithoutFlags = modifiedArgs
                                   .Filter(x => x.Kind.Match(_: () => true, flags: _ => false))
                                   .ToArr();
            var tagArgs = argsWithoutFlags
                          .Map(x => (
                                   name: x.Name,
                                   lowerName: Helpers.LowerFirst(x.Name),
                                   type: TgTypeConverter.ConvertArgType(x),
                                   isRef: TgTypeConverter.IsRefArgType(x)
                                   )).ToArray();

            return(Scope(
                       Scope(
                           Line($"internal const uint TypeNumber = {Helpers.TypeNumber(tag.TypeNumber)};"),
                           Line("[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]"),
                           Line("uint ITgTypeTag.TypeNumber => TypeNumber;"),
                           Line("")
                           ),
                       tagArgs.Map(arg => Line($"public readonly {arg.type} {arg.name};")).Scope(),
                       Line(""),
                       Scope(
                           Line($"public {tagName}("),
                           IndentedScope(1, $",{Environment.NewLine}",
                                         tagArgs.Map(arg => Line($"{arg.type} {arg.lowerName}"))
                                         ),
                           Line(") {"),
                           IndentedScope(1,
                                         tagArgs.Map(arg => Line(
                                                         $"{arg.name} = {arg.lowerName}" +
                                                         $"{(arg.isRef ? $" ?? throw new ArgumentNullException(nameof({arg.lowerName}))" : "")};"
                                                         ))
                                         ),
                           Line("}")
                           ),
                       Line(""),
                       WithGen.GenWith(argsWithoutFlags, tagName),
                       Line(""),
                       typeName != tagName ? Scope(
                           Line($"public static implicit operator {typeName}({tagName} tag) => new {typeName}(tag);"),
                           Line($"public static implicit operator Some<{typeName}>({tagName} tag) => new {typeName}(tag);"),
                           Line("")
                           ) : EmptyScope(),
                       RelationsGen.GenRelations(tagName, argsWithoutFlags),
                       Line(""),
                       Line(""),
                       SerializerGen.GenSerializer(modifiedArgs, typeNumber: None, "ITgTypeTag.SerializeTag"),
                       Line(""),
                       SerializerGen.GenTypeTagDeserialize(tagName, modifiedArgs)
                       ));
        }
示例#2
0
文件: Gen.cs 项目: zverev-iv/Telega
        static NestedText GenFunc(Signature func, string funcName)
        {
            var argsWithoutFlags = func.Args
                                   .Filter(x => x.Kind.Match(_: () => true, flags: _ => false))
                                   .ToArr();
            var funcArgs = argsWithoutFlags
                           .Map(x => (
                                    name: x.Name,
                                    lowerName: Helpers.LowerFirst(x.Name),
                                    type: TgTypeConverter.ConvertArgType(x),
                                    isRef: TgTypeConverter.IsRefArgType(x)
                                    )).ToArray();

            // usually it is a higher-order function, i am too lazy to modify the scheme just for this case
            var isWrapper        = func.Args.Exists(x => x.Type == TgType.OfTypeRef("X") || x.Type == TgType.OfTypeRef("!X"));
            var resType          = isWrapper ? "TFuncRes" : TgTypeConverter.ConvertType(func.ResultType);
            var classAccess      = isWrapper ? "" : "public ";
            var classTemplates   = isWrapper ? "<TFunc, TFuncRes>" : "";
            var classAnnotations = isWrapper
                ? $": ITgFunc<{resType}> where TFunc : class, ITgFunc<{resType}>"
                : $": ITgFunc<{resType}>, IEquatable<{funcName}>, IComparable<{funcName}>, IComparable";

            var resDes = isWrapper
                ? "Query.DeserializeResult(br);" // it is 'Query' all the time, i am too lazy
                : Concat("Read(br, ", SerializerGen.GenTypeDeserializer(func.ResultType), ");");
            var resultDeserializer = Scope(
                Line($"{resType} ITgFunc<{resType}>.DeserializeResult(BinaryReader br) =>"),
                Indent(1, Line(resDes))
                );

            return(Scope(
                       Line($"{classAccess}sealed class {funcName}{classTemplates} {classAnnotations}"),
                       Line("{"),
                       IndentedScope(1,
                                     funcArgs.Map(arg => Line($"public {arg.type} {arg.name} {{ get; }}")).Scope(),
                                     Scope(
                                         Line(""),
                                         Scope(
                                             Line($"public {funcName}("),
                                             IndentedScope(1, $",{Environment.NewLine}",
                                                           funcArgs.Map(arg => Line($"{arg.type} {arg.lowerName}"))
                                                           ),
                                             Line(") {"),
                                             IndentedScope(1,
                                                           funcArgs.Map(arg => Line(
                                                                            $"{arg.name} = {arg.lowerName}" +
                                                                            $"{(arg.isRef ? $" ?? throw new ArgumentNullException(nameof({arg.lowerName}))" : "")};"
                                                                            ))
                                                           ),
                                             Line("}")
                                             ),
                                         Line(""),
                                         Line(""),
                                         isWrapper ? Scope(new NestedText[0]) : Scope(
                                             WithGen.GenWith(argsWithoutFlags, funcName),
                                             Line(""),
                                             RelationsGen.GenRelations(funcName, argsWithoutFlags),
                                             Line("")
                                             ),
                                         SerializerGen.GenSerializer(func.Args, typeNumber: func.TypeNumber, "ITgSerializable.Serialize"),
                                         Line(""),
                                         resultDeserializer
                                         )
                                     ),
                       Line("}")
                       ));
        }