-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintercal.py
More file actions
86 lines (77 loc) · 2.28 KB
/
intercal.py
File metadata and controls
86 lines (77 loc) · 2.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
#INTERCAL code creator 9000!
def binFlip(num): #Function converts to backwards binary and back to decimal
b1 = bin(num)[2:]
while len(b1) < 8:
b1 = '0' + b1
b2 = ''
i = 7
while len(b2) < 8:
b2 = b2 + b1[i]
i -= 1
return int(b2, base = 2)
def generate(text):
# Create INTERCAL code that prints out text
f = open("output.i","w")
text = text + '\n'
f.write("DO ,1 <- #%d \n" %len(text))
s_index = 0 #This is the position in the string called text
OutPos = [0] #This is a list of the positions of the output head
line = 2 #This is the current line being written
while s_index < len(text):
char = d[text[s_index]]
OutPos.append(binFlip(char))
if line % 3 == 0:
p = 'PLEASE '
else:
p = ''
move = OutPos[s_index] - OutPos[-1]
if move < 0:
move += 256
f.write(p + 'DO ,1SUB#%d <- #%d \n' %(s_index + 1,move))
line += 1
s_index += 1
f.write('DO READ OUT ,1 \n')
f.write('DO GIVE UP')
f.close()
def generate_from_file(fname):
fin = open(fname, 'r')
text = fin.read()
fin.close()
text = text + '\n'
f = open("output.i","w")
f.write("DO ,1 <- #%d \n" %len(text))
s_index = 0 #This is the position in the string called text
OutPos = [0] #This is a list of the positions of the output head
line = 2 #This is the current line being written
while s_index < len(text):
char = d[text[s_index]]
OutPos.append(binFlip(char))
if line % 3 == 0:
p = 'PLEASE '
else:
p = ''
move = OutPos[s_index] - OutPos[-1]
if move < 0:
move += 256
f.write(p + 'DO ,1SUB#%d <- #%d \n' %(s_index + 1,move))
line += 1
s_index += 1
f.write('DO READ OUT ,1 \n')
f.write('DO GIVE UP')
f.close()
d = dict()
lowers = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
uppers = [0]*26
for k in range(26):
uppers[k] = lowers[k].upper()
d[uppers[k]] = 65 + k
d[lowers[k]] = 97 + k
d[' '] = 32
d[':'] = 58
d['\n'] = 10
d['?'] = 63
d["."] = 46
d["'"] = 39
d[","] = 44
for k in range(10):
d[str(k)] = 48 + k