forked from Maxoplata/StringToINTERCAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToINTERCAL.rb
More file actions
79 lines (59 loc) · 1.49 KB
/
StringToINTERCAL.rb
File metadata and controls
79 lines (59 loc) · 1.49 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
#!/usr/bin/ruby
# StringToINTERCAL.rb
#
# Converts a string to an INTERCAL script that will output said string.
# usage: ruby StringToINTERCAL.rb your string here
#
# @author Maxamilian Demian <max@maxdemian.com>
# @link https://www.maxodev.org
# @link https://github.com/Maxoplata/StringToINTERCAL
# class definition
class StringToINTERCAL
def initialize()
@politeCount = 0
end
def politeLine(line)
if @politeCount === 3
@politeCount = 0
return "PLEASE #{line}\n"
end
@politeCount += 1
return "DO #{line}\n"
end
def leadingZeros(dec)
count = dec.length
if count < 8
dec = ('0' * (8 - count)) + dec
end
return dec
end
def convertToINTERCAL(string)
# reset politeCount
@politeCount = 0
chars = string.split('')
ret = self.politeLine(',1 <- #' + chars.count.to_s)
lastCharLoc = 256
chars.each_with_index {|char, i|
charLoc = self.leadingZeros(char.ord.to_s(2)).reverse.to_i(2)
movePosition = 0
if charLoc < lastCharLoc
movePosition = (lastCharLoc - charLoc)
elsif charLoc > lastCharLoc
movePosition = (256 - charLoc) + lastCharLoc
end
lastCharLoc -= movePosition
if lastCharLoc < 1
lastCharLoc = 256 + lastCharLoc
end
ret += self.politeLine(',1 SUB #' + (i + 1).to_s + " <- ##{movePosition}")
}
ret += self.politeLine('READ OUT ,1')
ret += self.politeLine('GIVE UP')
return ret
end
end
# 1337 codez
if ARGV.count > 0
inputString = ARGV.join(' ')
puts StringToINTERCAL.new.convertToINTERCAL(inputString)
end