aboutsummaryrefslogtreecommitdiff
path: root/goforthanddie.rb
blob: cf24acd3369576f35dfb490f0a2101a606d07ab2 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env ruby

trap("INT") do
  quit
end

def quit
  hfile = File.join(ENV['HOME'], ".goforthanddie_history")
  File.open(hfile, ?w) do |f|
    f.puts(Readline::HISTORY.to_a)
    f.flush
  end
  exit
end

def getlist(stack, markermode, markerautoclear)
  if markermode
    h = []
    wm = false
    loop do
      break if stack.empty?
      t = stack.pop
      wm = t == :mark
      break if wm
      h << t
    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)
  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 = getlist(stack, markermode, markerautoclear)
      stack.push(*h.select{ |d| d > c })
    when ".>=", ".gteq", ".greaterthanorequalto"
      c = stack.pop
      h = getlist(stack, markermode, markerautoclear)
      stack.push(*h.select{ |d| d >= c })
    when ".=", ".eq", ".equalto"
      c = stack.pop
      h = getlist(stack, markermode, markerautoclear)
      stack.push(*h.select{ |d| d == c })
    when ".<=", ".lteq", ".lessthanorequalto"
      c = stack.pop
      h = getlist(stack, markermode, markerautoclear)
      stack.push(*h.select{ |d| d <= c })
    when ".<", ".lt", ".lessthan"
      c = stack.pop
      h = getlist(stack, markermode, markerautoclear)
      stack.push(*h.select{ |d| d < c })
    when ".count"
      h = getlist(stack, markermode, markerautoclear)
      stack.push(h.count)
    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"
      quit
    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?
  begin
    require "readline"
  rescue LoadError
    Gem.install "readline"
    Gem.install "readline-ext"
    retry
  end

  hfile = File.join(ENV['HOME'], ".goforthanddie_history")
  if File.exist?(hfile)
    File.readlines(hfile).each do |line|
      Readline::HISTORY << line.strip
    end
  end

  while buf = Readline.readline("> ", true)
    mm, mac, dp = handle(buf, stk, mm, mac, dp)
  end

  quit
else
  mm, mac, dp = handle($stdin.read, stk, mm, mac, dp)
end