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
|
#!/usr/bin/env ruby
def getmarkedlist(stack, markerautoclear)
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
end
def handle(str, stack, markermode, markerautoclear, destructiveprint)
exit if str.nil?
str.split.each do |token|
case token
when /[0-9]+/
stack.push(token.to_i)
when "+"
a, b = stack.pop(2)
stack.push(a + b)
when ".+"
if markermode
h = getmarkedlist(stack, markerautoclear)
stack.push(h.sum)
else
n = stack.pop
h = stack.pop(n)
stack.push(h.sum)
end
when "-"
a, b = stack.pop(2)
stack.push(a - b)
when "*"
a, b = stack.pop(2)
stack.push(a * b)
when "/"
a, b = stack.pop(2)
stack.push(a / b)
when "%"
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 "p"
t = stack.pop
puts t
stack.push(t) unless destructiveprint
when ".ex", ".explode"
if markermode
x = stack.pop
h = getmarkedlist(stack, markerautoclear)
h.reverse!
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)
else
x = stack.pop
n = stack.pop
h = stack.pop(n)
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)
end
when ".k", ".kh", ".keep", ".keephigh", ".keephighest"
if markermode
k = stack.pop
h = getmarkedlist(stack, markerautoclear)
h.sort!
stack.push(*h.pop(k))
else
k = stack.pop
n = stack.pop
h = stack.pop(n).sort
stack.push(*h.pop(k))
end
when ".kl", ".keeplow", ".keeplowest"
if markermode
k = stack.pop
h = getmarkedlist(stack, markerautoclear)
h.sort!
stack.push(*h.shift(k))
else
k = stack.pop
n = stack.pop
h = stack.pop(n).sort
stack.push(*h.shift(k))
end
when ">"
c = stack.pop
h = stack.pop
stack.push(c > h ? 1 : 0)
when ".>"
if markermode
c = stack.pop
h = getmarkedlist(stack, markerautoclear)
stack.push(h.count{ |d| c > d })
else
c = stack.pop
n = stack.pop
h = stack.pop(n)
stack.push(h.count{ |d| c > d })
end
when "<"
c = stack.pop
h = stack.pop
stack.push(c < h ? 1 : 0)
when ".<"
if markermode
c = stack.pop
h = getmarkedlist(stack, markerautoclear)
stack.push(h.count{ |d| c < d })
else
c = stack.pop
n = stack.pop
h = stack.pop(n)
stack.push(h.count{ |d| c < d })
end
when "!"
stack.pop
when ".!"
if markermode
getmarkedlist(stack, markerautoclear)
else
n = stack.pop
stack.pop(n)
end
when "m", "mark"
stack.push(:mark)
when "~s", "~stack"
puts stack.join(" ")
when "~mm"
markermode = !markermode
when "~mac"
markerautoclear = !markerautoclear
when "~dp"
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 "~q"
exit
else
puts "I don't know how to #{token}!"
end
end
end
stack = []
markermode = true
markerautoclear = false
destructiveprint = true
if $stdin.tty?
loop do
print "> "
handle(gets, stack, markermode, markerautoclear, destructiveprint)
end
else
handle($stdin.read, stack, markermode, markerautoclear, destructiveprint)
end
|