File: icebox_hlcsort.py

package info (click to toggle)
fpga-icestorm 0~20250207git7fbf8c0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,440 kB
  • sloc: python: 28,401; cpp: 4,970; sh: 2,594; ansic: 1,206; makefile: 697; xml: 16
file content (41 lines) | stat: -rwxr-xr-x 1,129 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
import sys

top_levels = []
f = open(sys.argv[1])

current_block_stack = [top_levels]
while len(current_block_stack) > 0:
    line = f.readline()
    if not line:
        break

    if '#' in line:
        continue

    if '{' in line:
        new_block = []
        current_block_stack[-1].append(new_block)
        current_block_stack.append(new_block)

    if line.strip():
        current_block_stack[-1].append(line)

    if '}' in line or not line:
        assert len(current_block_stack) > 1 or not line, current_block_stack
        old_block = current_block_stack.pop(-1)
        sorted_block = [old_block[0],] + sorted(old_block[1:-1], key=lambda x: repr(x)) + [old_block[-1],]
        old_block.clear()
        old_block.extend(sorted_block)

top_levels = list(sorted(top_levels, key=lambda x: repr(x)))

output_stack = [top_levels]
while len(output_stack) > 0:
    if len(output_stack[0]) == 0:
        output_stack.pop(0)
        continue
    if type(output_stack[0][0]) == list:
        output_stack.insert(0, output_stack[0].pop(0))
        continue
    print(output_stack[0].pop(0), end='')