-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathc_white_reducer.py
129 lines (109 loc) · 2.39 KB
/
c_white_reducer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
from __future__ import print_function
import sys
import re
def strip_comments(instr):
outStr = ""
for line in instr.split('\n'):
outStr+= line.split('//')[0] + '\n'
return outStr
def remove_indentation(instr):
outStr = '\n'.join([ line.strip() for line in instr.split('\n') ])
return outStr
def remove_newlines(instr):
outStr = ""
for x in instr.split('\n'):
if x and x[0] == '#':
outStr += x+'\n'
else:
outStr += x
return outStr
def remove_inner_spaces(instr):
opList = [
';',
',',
'for',
'=',
'-',
'<',
'>',
'if',
'&',
'+',
'*',
'/',
'%',
'?',
':'
]
for op in opList:
p = re.escape(op)
find = r'(\s{}\s|{}\s|\s{})'.format(p,p,p)
r = re.compile(find)
instr = r.sub(op,instr)
return instr
def printf_replace(instr):
instr = instr.replace('printf','pc')
# instr = instr.replace('node_num','nn')
instr = instr.replace('"',"'")
return instr
def fname_replace(instr):
repMap = [
('int', 'a'),
('repeat_char', 'b'),
('item', 'c'),
('len', 'd'),
('output_row', 'e'),
('padding', 'f'),
('height', 'f'),
('symbol', 'g'),
('width', 'h'),
('count', 'i'),
('character', 'i'),
('output_tree', 'j'),
('char_arr', 'k'),
('item', 'l'),
('argc', 'm'),
('argv', 'n'),
('char_buf', 'o'),
("'|'", '124'),
("'O'", '79'),
("'='", '61'),
("' '", '32'),
("'\\n'", '10')
]
for big, small in repMap:
instr = instr.replace(big, small)
return instr
def preserve_meta(instr):
outStr = ''
pres = ''
for line in instr.split('\n'):
if '#' not in line:
outStr+= line+'\n'
else:
pres += line+'\n'
outStr = fname_replace(outStr)
outStr = pres+outStr
return outStr
def strip_defines(instr):
outStr = ""
for line in instr.split('\n'):
if not line.startswith('#define'):
outStr+= line.split('//')[0] + '\n'
return outStr
def preserve_meta(instr):
lines = iter(instr.split('\n'))
pres = next(lines)
outStr = ''.join(lines)
outStr = fname_replace(outStr)
return pres + outStr
with open('tree_print.c','r') as f:
contents = f.read()
contents = strip_comments(contents)
contents = strip_defines(contents)
contents = remove_indentation(contents)
contents = remove_inner_spaces(contents)
contents = preserve_meta(contents)
contents = contents.strip()
print(contents, end="")