-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpreprocess.py
138 lines (108 loc) · 3.29 KB
/
preprocess.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
130
131
132
133
134
135
136
137
138
#The Keg Preprocessor
STRING_CHARS = "`¶“‘«„"
def process(string):
final = ""
string_mode = [False, ""]
escaped = False
comment = False
ssl_reference = [False, ""]
for char in string:
if string_mode[0]:
if escaped:
final += char
escaped = False
continue
elif char == "\\":
escaped = True
final += char
continue
else:
if char == string_mode[1]:
string_mode = [False, ""]
final += char
continue
if char in STRING_CHARS:
string_mode = [True, char]
final += char
continue
if escaped:
final += char
escaped = False
continue
if char == "\\":
escaped = True
final += char
continue
if char == "₳":
ssl_reference = [True, ""]
continue
if char == "\n" and comment:
comment = False
if ssl_reference[0] and len(ssl_reference[1]) != 2:
ssl_reference[1] += char
if len(ssl_reference[1]) == 2:
ssl_reference[0] = False
if ssl_reference[1] in ssls:
final += ssls[ssl_reference[1]]
else:
final += f"<SSL:{ssl_reference[1]}>"
ssl_reference[1] = ""
continue
if char == "∑":
final += "(!;|"
continue
if char == "⑳":
final += "{!|"
continue
if char == "#":
comment = True
final += char
return final
def balance_strings(source):
symbol = ""
string_mode = False
escaped = False
alt_strings = {"`" : "834**", "¶" : "882**",
"‘" : "27*", "“" : "35*",
"«" : "25*7+", "„" : "82*"}
result = ""
for char in source:
if escaped:
if char in alt_strings:
if string_mode:
result += "\\" + char
else:
result += alt_strings[char]
else:
result += "\\" + char
escaped = False
continue
elif char == "\\":
escaped = True
continue
elif string_mode:
if char == symbol:
symbol = None
string_mode = False
result += char
continue
elif char in alt_strings:
string_mode = True
symbol = char
result += char
else:
result += char
if symbol:
result += symbol
return result
ssls = {
"0a" : "`0123456789abcdefghijklmnopqrstuvwxyz`",
"0b" : "`0123456789`",
"0c" : "`abcdefghijklmnopqrstuvwxyz`"
}
if __name__ == "__main__":
assert process("₳0a") == "<SSL:0a>"
assert process("₳0a₳0b") == "<SSL:0a><SSL:0b>"
assert process("a₳0b") == "a<SSL:0b>"
assert process("a₳0bc") == "a<SSL:0b>c"
assert process("m₳0em₳0E") == "m<SSL:0e>m<SSL:0E>"