fish.rb 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjA2MGU2ZDJlODdkMmMyMjY4NzUxMTQ0ZGU2YzIwYTY1ODNiNzdmNg==
4
+ MjhkODdkZWVjMjFlODExNGZiNmQ4Yjk4NzFkNmQ1N2I0NmNhZWI5Mw==
5
5
  data.tar.gz: !binary |-
6
- M2M3MTYyYWY0MDkyZjA4YzU1MWYwNzM3NzJiY2NmYjRlZTk2ZmQ4Nw==
6
+ MzNlMWE0MGQzZGQzZWQ1OTg3MWRkODczYjM1MzM0MGViNTg4ZmVjMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjFiMjE3ZGNlMGViMzRiNGZmNGY1ZWIwYmI0M2ViNjg2NGZmZjE1OWI2N2Zm
10
- Y2Q3NjA2N2UxYmU1YTdmMmIwZDEyYzQyMTFhZGM0NjY5YTk3ZjVlYjg2MTk4
11
- OTMzZjA0NmExY2ZjYTY4YTNlNjRiNjljZTY0OTkwMzlhNDM0Nzg=
9
+ ZmZiYWZlMGEzNDNhNWQxM2QxMmEzMmZkMDIxZTQyYTEzZDE4MzVhNmYzMWYw
10
+ ZTQyZTM4YzZlMjY2YjNlZDNlYjk1M2MzNzA4MDkwMzY2M2U3M2ZjYmNmZjMw
11
+ N2NkOTY1MDBhM2JmZTAxMjU3NGU3NWNjZjc0NTMzZGFjZTdhMDc=
12
12
  data.tar.gz: !binary |-
13
- YWI3NzgxYzI1Mzk4ZDhiOTBhZTI5ODFkMjNmY2Y4MThlYWQ3NDlhMGQ5NDIw
14
- NTIzZDk2MmU1NmU4NTJlY2I5OGE1NDk1M2NlMjU3Zjc3MTdlYWM2MjQxMWI4
15
- YzI3NDcyODNkNjcwY2VjNmMzOTA3N2M2ODg0ZGU5OGMwMjA2YmY=
13
+ MWRhZGVhOTJhOGM5YTJkZGE2NWZkZDI3ODBkZDdkNjc3ZWU1MDdhZjhjOGE0
14
+ Yzg4MjdjOGZjNzJlYTRmODgzZjg5YzQzZTE3NWNiMzg0ZjYzYTAwNzA3NWM2
15
+ YWMzZDAxYmI2ZTcxMzcxNDE2MDg2MmE0MmI4MGE4ZTJiMTdiNTk=
data/README.md CHANGED
@@ -3,8 +3,14 @@ fish.rb
3
3
 
4
4
  A [fish](http://esolangs.org/wiki/Fish) interpreter in ruby.
5
5
 
6
+ gem install fish.rb
7
+
6
8
  There is a slew of examples in the examples directory.
7
9
 
10
+ Usage
11
+ -----
12
+
13
+
8
14
  Todo
9
15
  ----
10
16
 
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |gem|
13
13
  gem.summary = "A fish interpreter"
14
14
  gem.description = "Ruby fish interpreter for the newer multi-stack version of fish"
15
15
 
16
- gem.files = `git ls-files`.split($\)
16
+ gem.files = `git ls-files | grep -v examples`.split($\)
17
17
 
18
18
  gem.bindir = "./"
19
19
  gem.executables = ["fish.rb"]
data/fish.rb CHANGED
@@ -55,7 +55,7 @@ class Stack
55
55
  @data = data
56
56
 
57
57
  def self.pop cnt
58
- if cnt > @data.length; abort 'something smells fishy... (stack error)' end
58
+ if cnt > @data.length; abort "\nsomething smells fishy... (stack error)" end
59
59
  @data.pop cnt
60
60
  end
61
61
 
@@ -64,18 +64,19 @@ class Stack
64
64
 
65
65
  @reg = nil
66
66
  def self.reg
67
- if @reg.is_nil?
68
- @reg = self.pop 1
67
+ if @reg.nil?
68
+ @reg = (self.pop 1)[0]
69
69
  else
70
70
  self.push @reg
71
71
  @reg = nil
72
- ret
73
72
  end
74
73
  end
75
74
  end
76
75
  end # class Stack
77
76
 
78
77
  class Interpreter
78
+ def self.set_stdout file; @@stdout = file end
79
+
79
80
  @@prng = Random.new
80
81
  @@stdout = $stdout
81
82
 
@@ -88,7 +89,7 @@ class Interpreter
88
89
  }
89
90
 
90
91
  @@div_chk = lambda do |d|
91
- abort 'something smells fishy... (division by zero)' unless d != 0
92
+ abort "\nsomething smells fishy... (division by zero)" unless d != 0
92
93
  end
93
94
 
94
95
  @@ops = {
@@ -118,16 +119,16 @@ class Interpreter
118
119
  '~' => lambda { |pt, dir, stks, box, cntl| stks[-1].pop 1 },
119
120
  '$' => lambda { |pt, dir, stks, box, cntl| x, y = stks[-1].pop 2; stks[-1].push y; stks[-1].push x },
120
121
  '@' => lambda { |pt, dir, stks, box, cntl| x, y, z = stks[-1].pop 3; stks[-1].push z; stks[-1].push x; stks[-1].push y },
121
- '}' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.rotate! 1 },
122
- '{' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.rotate! -1 },
122
+ '{' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.rotate! 1 },
123
+ '}' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.rotate! -1 },
123
124
  'r' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.reverse! },
124
125
  'l' => lambda { |pt, dir, stks, box, cntl| stks[-1].push stks[-1].data.length },
125
- '[' => lambda { |pt, dir, stks, box, cntl| stks << (Stack.new stks[-1].pop stks[-1].pop 1) },
126
- ']' => lambda { |pt, dir, stks, box, cntl| stks[-1].data.concat stks.pop.data },
126
+ '[' => lambda { |pt, dir, stks, box, cntl| stks << (Stack.new stks[-1].pop (stks[-1].pop 1)[0]) },
127
+ ']' => lambda { |pt, dir, stks, box, cntl| stks[-2].data.concat stks.pop.data },
127
128
  # io
128
129
  'o' => lambda { |pt, dir, stks, box, cntl| @@stdout.write (stks[-1].pop 1)[0].round.chr },
129
130
  'n' => lambda { |pt, dir, stks, box, cntl| @@stdout.write (stks[-1].pop 1)[0].to_s },
130
- 'i' => lambda { |pt, dir, stks, box, cntl| stks[-1].push (cntl[:ibuf].empty? ? -1 : cntl[:ibuf][0].ord); cntl[:ibuf] = cntl[:ibuf][1..-1] unless cntl[:ibuf].length < 2 },
131
+ 'i' => lambda { |pt, dir, stks, box, cntl| stks[-1].push (cntl[:ibuf].empty? ? -1 : cntl[:ibuf][0].ord); cntl[:ibuf] = cntl[:ibuf][1..-1] unless cntl[:ibuf].length < 1 },
131
132
  # reflection
132
133
  'g' => lambda { |pt, dir, stks, box, cntl| stks[-1].push (box.at stks[-1].pop 2).ord },
133
134
  'p' => lambda { |pt, dir, stks, box, cntl| box.set (stks[-1].pop 2), (stks[-1].pop 1) },
@@ -155,7 +156,7 @@ class Interpreter
155
156
  @pt = [0, 0]
156
157
  @dir = 'r'
157
158
  @stks = []
158
- @stks << (Stack.new [])
159
+ @stks << (Stack.new options[:stack])
159
160
  @cntl = {
160
161
  :ibuf => options[:stdin],
161
162
  :done => false,
@@ -167,28 +168,36 @@ class Interpreter
167
168
  op = ' ' if op.nil?
168
169
  if options[:debug] >= 3; puts op end
169
170
  func = @@ops[op]
170
- abort 'something smells fishy... (invalid instruction)' if func.nil?
171
+ abort "\nsomething smells fishy... (invalid instruction)" if func.nil?
171
172
  func.call @pt, @dir, @stks, @box, @cntl
172
173
  op = @box.send @dir, @pt
173
174
  end
175
+ @@stdout.flush
174
176
  end
175
177
  end # class Interpreter
176
178
 
179
+ banner = "usage: fish <options> <files>"
177
180
 
178
181
  options = {}
179
182
  optparse = OptionParser.new do |opts|
180
- opts.banner = "fish.rb <options> <files>"
183
+ opts.banner = banner
181
184
 
182
185
  opts.on '-h', '--help', 'Display help' do; puts opts; exit end
183
186
  options[:debug] = 0
184
- opts.on '-d [level]', '--debug [level]', 'Print debug messages (max of 3, defaults to 1)' do |opt|
187
+ opts.on '-d <level>', '--debug <level>', 'Print debug messages (max of 3, defaults to 1)' do |opt|
185
188
  options[:debug] = opt.to_i || options[:debug] = 1
186
189
  end
187
190
  options[:stdin] = ""
188
- opts.on '-s str', '--stdin str', 'Stdin for fish script' do |opt|; options[:stdin] = opt end
189
- opts.on '-r file', '--redir-stdout file', 'Redirect fish stdout to file' do |opt|
190
- options[:stdout] = File.open opt, 'w'
191
+ opts.on '-s <input>', '--stdin <input>', 'Stdin for fish script' do |opt|; options[:stdin] = opt end
192
+ opts.on '-r <file>', '--redir-stdout <file>', 'Redirect fish stdout to file (useful when debuging)' do |opt|
193
+ Interpreter.set_stdout (File.open opt, 'w')
194
+ end
195
+ options[:stack] = []
196
+ opts.on '-v <vector>', '--vector <vector>', Array, 'Inital stack vector' do |opt|
197
+ options[:stack] = opt.collect { |x| x.to_i }
191
198
  end
192
199
  end.parse!
193
200
 
194
- Interpreter.new (IO.readlines ARGV.shift), options
201
+ puts banner if ARGV.empty?
202
+
203
+ ARGV.each { |fish| Interpreter.new (IO.readlines fish), options }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fish.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ! 'floomby
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: ./
12
12
  cert_chain: []
13
- date: 2014-07-09 00:00:00.000000000 Z
13
+ date: 2014-07-10 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Ruby fish interpreter for the newer multi-stack version of fish
16
16
  email: ! 'floomby@nmt.edu
@@ -25,9 +25,6 @@ files:
25
25
  - .gitignore
26
26
  - README.md
27
27
  - Rakefile
28
- - examples/fibonacci.fsh
29
- - examples/hello.fsh
30
- - examples/quine.fsh
31
28
  - fish.rb
32
29
  homepage: https://github.com/zerocool/fish.rb
33
30
  licenses: []
@@ -1,2 +0,0 @@
1
- 0:n84*o1:nv
2
- n:+@:o*48<
data/examples/hello.fsh DELETED
@@ -1,2 +0,0 @@
1
- !v"hello, world"r!
2
- >l?!;o
data/examples/quine.fsh DELETED
@@ -1 +0,0 @@
1
- "ar00g!;oooooooooo|