-
Notifications
You must be signed in to change notification settings - Fork 8
/
lispfmt.py
50 lines (42 loc) · 802 Bytes
/
lispfmt.py
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
42
43
44
45
46
47
48
49
50
#!/usr/bin/python
# formatting for lisp code
# 2 spaces per paren depth per line
import sys
def reformat(s):
depth = 0
# formatted string
fmt = ""
# remove leading whitespaces
unfmt = ""
for c in s:
if c == '\t' or c == ' ' and hit == False:
continue
else:
unfmt += c
if c == '\n':
hit = False
else:
hit = True
# add in proper indentation
for c in unfmt:
fmt += c
if c == '(':
depth += 1
elif c == ')':
depth -= 1
elif c == '\n':
for x in range(0, depth):
fmt += " "
return fmt
def format_file(filename):
with open(filename) as f:
return reformat(f.read())
if __name__ == "__main__":
if len(sys.argv) > 1:
for f in sys.argv[1:]:
print(format_file(f))
else:
s = ""
for line in sys.stdin:
s += line
print(reformat(s))