Verilator

Форк
0
/
dot_importer 
110 строк · 3.7 Кб
1
#!/usr/bin/env python3
2
# pylint: disable=C0103,C0114,C0115,C0116,C0209,C0301
3
######################################################################
4

5
import argparse
6
import re
7

8
######################################################################
9

10
Header = []
11
Vertexes = []
12
Edges = []
13

14
#######################################################################
15

16

17
def dotread(filename):
18
    with open(filename, "r", encoding="utf8") as fh:
19
        header = True
20
        vnum = 0
21

22
        vertex_re = re.compile(r'^\t([a-zA-Z0-9_]+)\t(.*)$')
23
        edge_re = re.compile(
24
            r'^\t([a-zA-Z0-9_]+)\s+->\s+([a-zA-Z0-9_]+)\s*(.*)$')
25

26
        for line in fh:
27
            vertex_match = re.search(vertex_re, line)
28
            edge_match = re.search(edge_re, line)
29
            if vertex_match:
30
                if vertex_match.group(1) != 'nTITLE':
31
                    header = False
32
                    Vertexes.append({
33
                        'num': vnum,
34
                        'line': line,
35
                        'name': vertex_match.group(1)
36
                    })
37
                    vnum += 1
38
            elif edge_match:
39
                fromv = edge_match.group(1)
40
                tov = edge_match.group(2)
41
                w = re.match(r'weight=(\d+)', line)
42
                weight = w.group(1) if w else 1
43
                w = re.match(r'style=(\S+)', line)
44
                cutable = w.group(1) if w else None
45
                edge = {
46
                    'num': vnum,
47
                    'line': line,
48
                    'weight': weight,
49
                    'cutable': cutable,
50
                    'from': fromv,
51
                    'to': tov
52
                }
53
                vnum += 1
54
                Edges.append(edge)
55
            elif header:
56
                Header.append(line)
57
                print("IGNORE: " + line)
58

59

60
#######################################################################
61

62

63
def cwrite(filename):
64
    with open(filename, "w", encoding="utf8") as fh:
65
        fh.write("void V3GraphTestImport::dotImport() {\n")
66
        fh.write("    auto* gp = &m_graph;\n")
67
        for ver in sorted(Vertexes, key=lambda ver: ver['num']):
68
            fh.write(
69
                "    auto* %s = new V3GraphTestVertex{gp, \"%s\"};  if (%s) {}\n"
70
                % (ver['name'], ver['name'], ver['name']))
71
        fh.write("\n")
72
        for edge in Edges:
73
            fh.write("    new V3GraphEdge{gp, %s, %s, %s, %s};\n" %
74
                     (edge['from'], edge['to'], edge['weight'],
75
                      "true" if edge['cutable'] else "false"))
76
        fh.write("}\n")
77

78

79
######################################################################
80
# main
81

82
parser = argparse.ArgumentParser(
83
    allow_abbrev=False,
84
    formatter_class=argparse.RawDescriptionHelpFormatter,
85
    description=
86
    """dot_importer takes a graphvis .dot file and converts into .cpp file.
87
This x.cpp file is then manually included in V3GraphTest.cpp to verify
88
various xsub-algorithms.""",
89
    epilog=
90
    """Copyright 2005-2024 by Wilson Snyder. This program is free software; you
91
can redistribute it and/or modify it under the terms of either the GNU
92
Lesser General Public License Version 3 or the Perl Artistic License
93
Version 2.0.
94

95
SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0""")
96

97
parser.add_argument('--debug',
98
                    action='store_const',
99
                    const=9,
100
                    help='enable debug')
101
parser.add_argument('filename', help='input .dot filename to process')
102

103
Args = parser.parse_args()
104
dotread(Args.filename)
105
cwrite("graph_export.cpp")
106

107
######################################################################
108
# Local Variables:
109
# compile-command: "./dot_importer ../test_regress/obj_vlt/t_EXAMPLE/*orderg_o*.dot && cat graph_export.cpp"
110
# End:
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.