コード例 #1
0
ファイル: PythonEnumsGenerator.cs プロジェクト: icedland/iced
        public PythonEnumsGenerator(GeneratorContext generatorContext)
            : base(generatorContext.Types)
        {
            pythonIdConverter   = PythonIdentifierConverter.Create();
            rustIdConverter     = RustIdentifierConverter.Create();
            rustDocWriter       = new Documentation.Rust.RustDocCommentWriter(rustIdConverter, ".", ".", ".", ".");
            exportedPythonTypes = genTypes.GetObject <ExportedPythonTypes>(TypeIds.ExportedPythonTypes);

            var dirs = generatorContext.Types.Dirs;

            toFullFileInfo = new();
            toFullFileInfo.Add(TypeIds.CC_a, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_a.py")));
            toFullFileInfo.Add(TypeIds.CC_ae, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_ae.py")));
            toFullFileInfo.Add(TypeIds.CC_b, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_b.py")));
            toFullFileInfo.Add(TypeIds.CC_be, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_be.py")));
            toFullFileInfo.Add(TypeIds.CC_e, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_e.py")));
            toFullFileInfo.Add(TypeIds.CC_g, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_g.py")));
            toFullFileInfo.Add(TypeIds.CC_ge, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_ge.py")));
            toFullFileInfo.Add(TypeIds.CC_l, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_l.py")));
            toFullFileInfo.Add(TypeIds.CC_le, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_le.py")));
            toFullFileInfo.Add(TypeIds.CC_ne, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_ne.py")));
            toFullFileInfo.Add(TypeIds.CC_np, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_np.py")));
            toFullFileInfo.Add(TypeIds.CC_p, new FullEnumFileInfo(dirs.GetPythonPyFilename("CC_p.py")));
            toFullFileInfo.Add(TypeIds.Code, new FullEnumFileInfo(dirs.GetPythonPyFilename("Code.py")));
            toFullFileInfo.Add(TypeIds.CodeSize, new FullEnumFileInfo(dirs.GetPythonPyFilename("CodeSize.py")));
            toFullFileInfo.Add(TypeIds.ConditionCode, new FullEnumFileInfo(dirs.GetPythonPyFilename("ConditionCode.py")));
            toFullFileInfo.Add(TypeIds.CpuidFeature, new FullEnumFileInfo(dirs.GetPythonPyFilename("CpuidFeature.py")));
            toFullFileInfo.Add(TypeIds.DecoderError, new FullEnumFileInfo(dirs.GetPythonPyFilename("DecoderError.py")));
            toFullFileInfo.Add(TypeIds.DecoderOptions, new FullEnumFileInfo(dirs.GetPythonPyFilename("DecoderOptions.py")));
            toFullFileInfo.Add(TypeIds.EncodingKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("EncodingKind.py")));
            toFullFileInfo.Add(TypeIds.FlowControl, new FullEnumFileInfo(dirs.GetPythonPyFilename("FlowControl.py")));
            toFullFileInfo.Add(TypeIds.FormatMnemonicOptions, new FullEnumFileInfo(dirs.GetPythonPyFilename("FormatMnemonicOptions.py")));
            toFullFileInfo.Add(TypeIds.MandatoryPrefix, new FullEnumFileInfo(dirs.GetPythonPyFilename("MandatoryPrefix.py")));
            toFullFileInfo.Add(TypeIds.MemorySize, new FullEnumFileInfo(dirs.GetPythonPyFilename("MemorySize.py")));
            toFullFileInfo.Add(TypeIds.MemorySizeOptions, new FullEnumFileInfo(dirs.GetPythonPyFilename("MemorySizeOptions.py")));
            toFullFileInfo.Add(TypeIds.Mnemonic, new FullEnumFileInfo(dirs.GetPythonPyFilename("Mnemonic.py")));
            toFullFileInfo.Add(TypeIds.OpAccess, new FullEnumFileInfo(dirs.GetPythonPyFilename("OpAccess.py")));
            toFullFileInfo.Add(TypeIds.OpCodeOperandKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("OpCodeOperandKind.py")));
            toFullFileInfo.Add(TypeIds.MvexEHBit, new FullEnumFileInfo(dirs.GetPythonPyFilename("MvexEHBit.py")));
            toFullFileInfo.Add(TypeIds.OpCodeTableKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("OpCodeTableKind.py")));
            toFullFileInfo.Add(TypeIds.OpKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("OpKind.py")));
            toFullFileInfo.Add(TypeIds.Register, new FullEnumFileInfo(dirs.GetPythonPyFilename("Register.py")));
            toFullFileInfo.Add(TypeIds.RepPrefixKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("RepPrefixKind.py")));
            toFullFileInfo.Add(TypeIds.RflagsBits, new FullEnumFileInfo(dirs.GetPythonPyFilename("RflagsBits.py")));
            toFullFileInfo.Add(TypeIds.RoundingControl, new FullEnumFileInfo(dirs.GetPythonPyFilename("RoundingControl.py")));
            toFullFileInfo.Add(TypeIds.TupleType, new FullEnumFileInfo(dirs.GetPythonPyFilename("TupleType.py")));
            toFullFileInfo.Add(TypeIds.FormatterSyntax, new FullEnumFileInfo(dirs.GetPythonPyFilename("FormatterSyntax.py")));
            toFullFileInfo.Add(TypeIds.MvexConvFn, new FullEnumFileInfo(dirs.GetPythonPyFilename("MvexConvFn.py")));
            toFullFileInfo.Add(TypeIds.MvexRegMemConv, new FullEnumFileInfo(dirs.GetPythonPyFilename("MvexRegMemConv.py")));
            toFullFileInfo.Add(TypeIds.MvexTupleTypeLutKind, new FullEnumFileInfo(dirs.GetPythonPyFilename("MvexTupleTypeLutKind.py")));

            toPartialFileInfo = new();
            toPartialFileInfo.Add(TypeIds.FormatterSyntax, new PartialEnumFileInfo("FormatterSyntax", TargetLanguage.Rust, dirs.GetPythonRustFilename("formatter.rs")));
        }
コード例 #2
0
 public PythonInstrCreateGen(GeneratorContext generatorContext)
     : base(generatorContext.Types)
 {
     this.generatorContext = generatorContext;
     idConverter           = PythonIdentifierConverter.Create();
     rustIdConverter       = RustIdentifierConverter.Create();
     docWriter             = new PythonDocCommentWriter(idConverter, TargetLanguage.Rust, isInRootModule: true);
     genNames = new Rust.GenCreateNameArgs {
         CreatePrefix = "create",
         Register     = "_reg",
         Memory       = "_mem",
         Int32        = "_i32",
         UInt32       = "_u32",
         Int64        = "_i64",
         UInt64       = "_u64",
     };
     sb = new StringBuilder();
 }
コード例 #3
0
        void WritePyi(List <PyClass> classes)
        {
            var reqEnumFields = GetRequiredEnumFields(classes);

            var filename = genTypes.Dirs.GetPythonPyFilename("_iced_x86_py.pyi");

            using (var writer = new FileWriter(TargetLanguage.Python, FileUtils.OpenWrite(filename))) {
                writer.WriteFileHeader();
                writer.WriteLine("from collections.abc import Iterator");
                writer.WriteLine("from enum import IntEnum, IntFlag");
                writer.WriteLine("from typing import Any, List, Optional, Union");
                writer.WriteLine();

                var idConverter  = PythonIdentifierConverter.Create();
                var allEnumTypes = exportedPythonTypes.Enums.Select(a => (enumType: a, pythonName: a.Name(idConverter)));
                var toEnumType   = allEnumTypes.ToDictionary(a => a.pythonName, a => a.enumType, StringComparer.Ordinal);
                foreach (var(enumType, pythonName) in allEnumTypes.OrderBy(a => a.pythonName, StringComparer.Ordinal))
                {
                    var baseClass = enumType.IsFlags ? "IntFlag" : "IntEnum";
                    if (reqEnumFields.TryGetValue(enumType, out var fields))
                    {
                        writer.WriteLine($"class {pythonName}({baseClass}):");
                        using (writer.Indent()) {
                            bool uppercaseRawName = PythonUtils.UppercaseEnum(enumType.TypeId.Id1);
                            foreach (var value in enumType.Values)
                            {
                                if (fields.Contains(value))
                                {
                                    fields.Remove(value);
                                    var(valueName, numStr) = PythonUtils.GetEnumNameValue(idConverter, value, uppercaseRawName);
                                    writer.WriteLine($"{valueName} = {numStr}");
                                }
                                if (fields.Count == 0)
                                {
                                    break;
                                }
                            }
                            if (fields.Count != 0)
                            {
                                throw new InvalidOperationException();
                            }
                            writer.WriteLine("...");
                        }
                    }
                    else
                    {
                        writer.WriteLine($"class {pythonName}({baseClass}): ...");
                    }
                }

                var docGen = new PyiDocGen();
                foreach (var pyClass in classes.OrderBy(a => a.Name, StringComparer.Ordinal))
                {
                    writer.WriteLine();
                    writer.WriteLine($"class {idConverter.Type(pyClass.Name)}:");
                    using (writer.Indent()) {
                        WriteDocs(writer, docGen.Convert(pyClass.DocComments));

                        int defCount = 0;
                        foreach (var member in GetMembers(pyClass))
                        {
                            switch (member)
                            {
                            case PyMethod method:
                                var docComments = method.Attributes.Any(AttributeKind.New) ?
                                                  pyClass.DocComments : method.DocComments;
                                Write(writer, docGen, idConverter, pyClass, method, docComments, toEnumType);
                                defCount++;
                                break;

                            case PyProperty property:
                                Write(writer, docGen, idConverter, pyClass, property.Getter, property.Getter.DocComments, toEnumType);
                                defCount++;
                                if (property.Setter is not null)
                                {
                                    Write(writer, docGen, idConverter, pyClass, property.Setter, property.Getter.DocComments, toEnumType);
                                    defCount++;
                                }
                                break;

                            default:
                                throw new InvalidOperationException();
                            }
                        }
                        if (defCount == 0)
                        {
                            throw new InvalidOperationException($"class {pyClass.Name}: No class members");
                        }
                    }
                }
            }
        }