Skip to content

Commit 1940188

Browse files
committed
init
1 parent 07f3dd8 commit 1940188

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

.github/workflows/pretty.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Pretty
2+
3+
on:
4+
push:
5+
6+
pull_request:
7+
8+
workflow_dispatch:
9+
10+
jobs:
11+
docs:
12+
name: Code formatting
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
18+
- name: Count
19+
run: ./mfc.sh format diff

toolchain/bootstrap/format.sh

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
#!/bin/bash
22

3-
log "Formatting MFC with fprettify..."
3+
log "MFC format..."
44

5-
fprettify ${@:-src} --exclude "src/*/autogen" --recursive --silent \
6-
--indent 4 --c-relations --enable-replacements --enable-decl \
7-
--whitespace-comma 1 --whitespace-multdiv 1 --whitespace-plusminus 1 \
8-
--case 1 1 1 1 --strict-indent
5+
opts="--recursive --silent --indent 4 --c-relations --enable-replacements --enable-decl --whitespace-comma 0 --whitespace-multdiv 0 --whitespace-plusminus 1 --case 1 1 1 1 --strict-indent --line-length 1000"
6+
7+
if [ "$1" == "diff" ]; then
8+
log "Checking format diff..."
9+
shift
10+
11+
# Indent acc directives
12+
out_indent=""
13+
fn=""
14+
for filename in src/*/*.f*; do
15+
out_indent+=$(python3 toolchain/indenter.py "$filename" "diff")
16+
done
17+
echo out_indent
18+
echo ${out_indent}
19+
exit 0
20+
21+
# Check prettify out
22+
out_prettify=$(fprettify ${@:-src} --exclude "src/*/autogen" $opts -s -d)
23+
if [ -z "${out_prettify}" ] && [ -z "${out_indent}" ]; then
24+
echo "Already pretty!"
25+
exit 0
26+
else
27+
error "Not pretty, run ./mfc.sh format"
28+
exit 1
29+
fi
30+
fi
31+
32+
# Indent acc directives
33+
for filename in src/*/*.f*; do
34+
python3 toolchain/indenter.py "$filename"
35+
done
36+
37+
fprettify ${@:-src} --exclude "src/*/autogen" $opts
938
ret="$?"
1039

1140
if [ "$ret" != '0' ]; then
@@ -15,4 +44,4 @@ if [ "$ret" != '0' ]; then
1544
exit 1
1645
fi
1746

18-
ok "MFC has been fprettify'ied."
47+
ok "MFC format complete."

toolchain/indenter.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import os
4+
import filecmp
5+
6+
def main():
7+
num_args = len(sys.argv)
8+
if num_args != 2 or num_args !=3:
9+
AssertionError('Invalid number of arguments, found ', num_args)
10+
if num_args == 3 and str(sys.argv[2]) != "diff":
11+
AssertionError('Invalid third argument, ', sys.argv[2])
12+
13+
infile = str(sys.argv[1])
14+
outfile = infile+".new"
15+
16+
adjust_indentation(infile, outfile)
17+
if num_args == 2:
18+
print('replacing',infile,'with',outfile)
19+
os.replace(outfile,infile)
20+
if num_args == 3:
21+
same = filecmp.cmp(outfile,infile)
22+
if not same:
23+
print(infile)
24+
print('different!')
25+
# os.remove(outfile)
26+
27+
def adjust_indentation(input_file, output_file):
28+
startingchar='!$acc'
29+
startingloop1='!$acc parallel loop'
30+
startingloop2='!$acc loop'
31+
max_empty_lines=4
32+
33+
with open(input_file, 'r') as file_in, open(output_file, 'w') as file_out:
34+
lines = file_in.readlines()
35+
36+
# this makes sure !$acc lines that have line continuations get indented at proper level
37+
for kk in range(10):
38+
# loop through file
39+
for i in range(len(lines)):
40+
if lines[i].lstrip().startswith(startingchar) and i + 1 < len(lines):
41+
j = i + 1
42+
empty_lines = 0
43+
# look down to see how to indent a line
44+
while j < len(lines) and empty_lines < max_empty_lines:
45+
# if the following line starts with [end, else, contains], skip to looking up
46+
if lines[j].lstrip().startswith('end') or \
47+
lines[j].lstrip().startswith('contains') or \
48+
lines[j].lstrip().startswith('else'):
49+
empty_lines = max_empty_lines
50+
# skip empty lines
51+
elif lines[j].strip() == '':
52+
empty_lines += 1
53+
# indent acc lines
54+
elif not lines[j].lstrip().startswith(startingchar):
55+
indent = len(lines[j]) - len(lines[j].lstrip())
56+
lines[i] = ' ' * indent + lines[i].lstrip()
57+
break
58+
j += 1
59+
# if looking down just finds empty lines, start looking up for indendation level
60+
if empty_lines == max_empty_lines:
61+
k = i - 1
62+
while k >= 0:
63+
# if line above is not empty
64+
if lines[k].strip() != '':
65+
# if line 2 above ends with line continuation, indent at that level
66+
if lines[k-1].strip().endswith('&'):
67+
indent = len(lines[k-1]) - len(lines[k-1].lstrip())
68+
# else indent at level of line above
69+
else:
70+
indent = len(lines[k]) - len(lines[k].lstrip())
71+
lines[i] = ' ' * indent + lines[i].lstrip()
72+
break
73+
k -= 1
74+
75+
# remove empty lines following an acc loop directive
76+
i = 0
77+
while i < len(lines):
78+
if (lines[i].lstrip().startswith(startingloop1) or \
79+
lines[i].lstrip().startswith(startingloop2)) and \
80+
i+1 < len(lines) and lines[i+1].strip() == '':
81+
file_out.write(lines[i])
82+
i +=2
83+
else:
84+
file_out.write(lines[i])
85+
i += 1
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)