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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
#!/usr/bin/env ruby
def getlist(stack, markermode, markerautoclear)
if markermode
h = []
wm = false
loop do
t = stack.pop
wm = t == :mark
break if wm
h << t
break if stack.empty?
end
stack.push(:mark) if wm && !markerautoclear
h.reverse
else
n = stack.pop
stack.pop(n)
end
end
def handle(str, stack, markermode, markerautoclear, destructiveprint)
exit if str.nil?
str.split.each do |token|
case token
when /\A[+-]?[0-9]+\Z/
stack.push(token.to_i)
when "+", "add", "plus"
a, b = stack.pop(2)
stack.push(a + b)
when ".+", ".add", ".plus", ".sum"
h = getlist(stack, markermode, markerautoclear)
stack.push(h.sum)
when "-", "sub", "subtract", "minus"
a, b = stack.pop(2)
stack.push(a - b)
when "*", "mul", "multiply", "times"
a, b = stack.pop(2)
stack.push(a * b)
when "/", "div", "divide"
a, b = stack.pop(2)
stack.push(a / b)
when "%", "mod", "modulate", "rem", "remainder"
a, b = stack.pop(2)
stack.push(a % b)
when "d", "roll"
n = stack.pop
d = stack.pop
n.times { stack.push(rand(d) + 1) }
when "c", "copy", "dup"
h = stack.pop
stack.push(h)
stack.push(h)
when ".c", ".copy", ".dup"
h = getlist(stack, markermode, markerautoclear)
stack.push(*h)
stack.push(:mark) if markermode && !markerautoclear
stack.push(*h)
when "s", "swap"
a, b = stack.pop(2)
stack.push(b, a)
when "r", "rot", "rotate"
a, b, c = stack.pop(3)
stack.push(b, c, a)
when ".r", ".rot", ".rotate"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(*h.rotate(c))
when "o", "over"
a, b = stack.pop(2)
stack.push(a, b, a)
when "p", "print"
t = stack.pop
puts t
stack.push(t) unless destructiveprint
when ".ex", ".explode"
x = stack.pop
h = getlist(stack, markermode, markerautoclear)
lm = 0
loop do
m = h.count(x)
break if m == lm
(m-lm).times { h << (rand(x) + 1) }
lm = m
end
stack.push(*h)
when ".k", ".kh", ".keep", ".keephigh", ".keephighest"
k = stack.pop
h = getlist(stack, markermode, markerautoclear)
h.sort!
stack.push(*h.pop(k))
when ".kl", ".keeplow", ".keeplowest"
k = stack.pop
h = getlist(stack, markermode, markerautoclear)
h.sort!
stack.push(*h.shift(k))
when ">", "gt", "greaterthan"
c = stack.pop
h = stack.pop
stack.push(c > h ? 1 : 0)
when ".>", ".gt", ".greaterthan"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(h.count{ |d| c > d })
when ">=", "gteq", "greaterthanorequalto"
c = stack.pop
h = stack.pop
stack.push(c >= h ? 1 : 0)
when ".>=", ".gteq", ".greaterthanorequalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(h.count{ |d| c >= d })
when "=", "eq", "equalto"
c = stack.pop
h = stack.pop
stack.push(c == h ? 1 : 0)
when ".=", ".eq", ".equalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(h.count{ |d| c == d })
when "<=", "lteq", "lessthanorequalto"
c = stack.pop
h = stack.pop
stack.push(c <= h ? 1 : 0)
when ".<=", ".lteq", ".lessthanorequalto"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(h.count{ |d| c <= d })
when "<", "lt", "lessthan"
c = stack.pop
h = stack.pop
stack.push(c < h ? 1 : 0)
when ".<", ".lt", ".lessthan"
c = stack.pop
h = getlist(stack, markermode, markerautoclear)
stack.push(h.count{ |d| c < d })
when "!", "drop"
stack.pop
when ".!", ".drop"
getlist(stack, markermode, markerautoclear)
when "m", "mark"
stack.push(:mark)
when /\A'(.*)/
file = File.readlines(File.join(ENV['HOME'], ".goforthanddie"))
cmd = file.map { |line| line.split(?:) }.to_h[$1]
if cmd.nil?
puts "You haven't told me how to #{$1}!"
else
markermode, markerautoclear, destructiveprint =
handle(cmd, stack, markermode, markerautoclear, destructiveprint)
end
when "~s", "~stack"
puts stack.join(" ")
when "~mm", "~markermode"
markermode = !markermode
when "~mac", "~markerautoclear"
markerautoclear = !markerautoclear
when "~dp", "~destructiveprint"
destructiveprint = !destructiveprint
when "~state"
print "Stack: "
pp stack
print "Marker Mode: #{markermode ? "ON " : "OFF"} "
puts "Marker Auto-Clear: #{markerautoclear ? "ON " : "OFF"}"
puts "Destructive Print: #{destructiveprint ? "ON " : "OFF"}"
when "~user"
puts "User definitions:"
file = File.
readlines(File.join(ENV['HOME'], ".goforthanddie")).
map { |line| line.split(?:) }
namemax = file.map {|defn| defn[0].length}.max + 1
file.each do |name, defn|
puts " %#{namemax}s: #{defn}" % "'#{name}"
end
when "~q", "~quit"
exit
else
puts "I don't know how to #{token}!"
end
end
[markermode, markerautoclear, destructiveprint]
end
stk = []
mm = true
mac = false
dp = true
if $stdin.tty?
loop do
print "> "
mm, mac, dp = handle(gets, stk, mm, mac, dp)
end
else
mm, mac, dp = handle($stdin.read, stk, mm, mac, dp)
end
|