forked from Maxoplata/StringToINTERCAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToINTERCAL.py
More file actions
73 lines (49 loc) · 1.51 KB
/
StringToINTERCAL.py
File metadata and controls
73 lines (49 loc) · 1.51 KB
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
#!/usr/bin/env python
"""
StringToINTERCAL.py
Converts a string to an INTERCAL script that will output said string.
usage: python StringToINTERCAL.py your string here
https://www.maxodev.org
https://github.com/Maxoplata/StringToINTERCAL
"""
import sys
__author__ = "Maxamilian Demian"
__email__ = "max@maxdemian.com"
# class definition
class StringToINTERCAL:
politeCount = 0
def politeLine(self, line):
if self.politeCount == 3:
self.politeCount = 0
return 'PLEASE {}\n'.format(line)
self.politeCount += 1
return 'DO {}\n'.format(line)
def leadingZeros(self, dec):
count = len(dec)
if count < 8:
dec = ('0' * (8 - count)) + dec
return dec
def convertToINTERCAL(self, string):
# reset politeCount
self.politeCount = 0
ret = self.politeLine(',1 <- #{}'.format(len(string)))
lastCharLoc = 256
for i in range(0, len(string)):
charLoc = int(self.leadingZeros('{0:b}'.format(ord(string[i])))[::-1], 2)
movePosition = 0
if charLoc < lastCharLoc:
movePosition = (lastCharLoc - charLoc)
elif charLoc > lastCharLoc:
movePosition = (256 - charLoc) + lastCharLoc
lastCharLoc -= movePosition
if lastCharLoc < 1:
lastCharLoc = 256 + lastCharLoc
ret += self.politeLine(',1 SUB #{} <- #{}'.format((i + 1), movePosition))
ret += self.politeLine('READ OUT ,1')
ret += self.politeLine('GIVE UP')
return ret
# 1337 codez
if len(sys.argv) > 1:
inputString = ' '.join(sys.argv[1:])
myINTERCAL = StringToINTERCAL()
print(myINTERCAL.convertToINTERCAL(inputString))