befunge98 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01e0f95f403d53d6741cd5490d880b94e64a5f92
4
- data.tar.gz: 43de87a5d275630c9f1616ce66e0ac336866660a
3
+ metadata.gz: 820a474f794f67a9cbbacea6b3142d41bdc86783
4
+ data.tar.gz: fcfbaaf5cc90ad71f8cd6284ef7664091b9e68da
5
5
  SHA512:
6
- metadata.gz: 2f889c5f8c5e79f34effd2b81ffe1a2ce6b25e8c6b7b520dbae380a08e7e5c01fdce6cff813fa1033d2263cfe390c9f717b0cb6ce000b370021d81813f97646b
7
- data.tar.gz: 0d5ddb8725d2203f18fafe2cc4f0fcfac044e681b0f69abc4871e8adb4b3f9332193620f03b037983681ab3281e3f84048008bb991d77ad82127d65dd529c496
6
+ metadata.gz: 751e2be52b36af30dcf99bb8c199f29c28150a9eff24d431aa19afc79f3dd01dfbdaa6464c03f179bffc20f5327c09400535d2c6c20ad77554665ce3175dd501
7
+ data.tar.gz: 4e9818eda68e4d969a6ac73d8d48207b48504f5c543e543acc67ac25806eed7a794dc187f9fa94bc344fe40ac90f3ab25d50569d7fb2dd842a799a90b58bd899
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "befunge98"
3
- spec.version = "0.0.1"
3
+ spec.version = "0.0.2"
4
4
  spec.summary = "[WIP] Probably the first Befunge-98 interpreter in Ruby"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
@@ -6,4 +6,4 @@ end
6
6
 
7
7
  require_relative "../lib/befunge98"
8
8
 
9
- exit Befunge98(File.read(ARGV.first), STDOUT).exitcode
9
+ exit Befunge98(ARGF.read, STDOUT).exitcode
@@ -18,24 +18,24 @@ def Befunge98 source, stdout = StringIO.new, stdin = STDIN
18
18
  move = lambda do
19
19
  y, x = y + dy, x + dx
20
20
  next if sy + oy + y >= 0 && code[sy + oy + y] && sx + ox + x >= 0 && code[sy + oy + y][sx + ox + x]
21
- top = code. index{ |l| !(l - [32, nil]).empty? }
22
- bottom = code.rindex{ |l| !(l - [32, nil]).empty? }
23
- left = code.map{ |l| l. index{ |c| c && c != 32 } }.compact.min
24
- right = code.map{ |l| l.rindex{ |c| c && c != 32 } }.compact.max
25
- STDERR.puts [code, top..bottom, left..right, [y, x], [dy, dx]].inspect if ENV["DEBUG"]
21
+ top = code. index{ |l| l.any?{ |i| i && i != 32 } }
22
+ bottom = code.rindex{ |l| l.any?{ |i| i && i != 32 } }
23
+ left = code.map{ |l| l && l. index{ |c| c && c != 32 } }.compact.min
24
+ right = code.map{ |l| l && l.rindex{ |c| c && c != 32 } }.compact.max
25
+ STDERR.puts [top..bottom, left..right, [y, x], [dy, dx], code].inspect if ENV["DEBUG"]
26
26
  ty, tx = y, x
27
27
  next if loop do
28
28
  zy, zx = sy + oy + ty, sx + ox + tx
29
- break unless (top..bottom).include?(zy) && (left..right).include?(zx)
29
+ break unless top <= zy && zy <= bottom && left <= zx && zx <= right
30
30
  break true if code[zy] && code[zy][zx] && code[zy][zx] != 32
31
31
  ty, tx = ty + dy, tx + dx
32
32
  end
33
33
  ty, tx = y - dy, x - dx
34
34
  loop do
35
- STDERR.puts [ty, tx].inspect if ENV["DEBUG"]
36
35
  zy, zx = sy + oy + ty, sx + ox + tx
37
- break unless (top..bottom).include?(zy) && (left..right).include?(zx)
38
- y, x = ty, tx if code[zy] && code[zy][zx] && code[zy][zx] != 32
36
+ break if dy > 0 && zy < top || dy < 0 && zy > bottom ||
37
+ dx > 0 && zx < left || dx < 0 && zx > right
38
+ y, x = ty, tx if code[zy] && code[zy][zx] && code[zy][zx] != 32 # TODO: should this execute if it's in a 'hole'?
39
39
  ty, tx = ty - dy, tx - dx
40
40
  end
41
41
  end
@@ -47,13 +47,13 @@ def Befunge98 source, stdout = StringIO.new, stdin = STDIN
47
47
  loop do
48
48
  if iterate.zero?
49
49
  move[]
50
- char = get[]
51
50
  else
52
51
  iterate -= 1
53
52
  end
53
+ char = get[]
54
54
  STDERR.puts [[y, x], char.chr, stack].inspect if ENV["DEBUG"]
55
55
 
56
- next stack << char if stringmode && char.chr != ?"
56
+ next stack << char if stringmode && char.chr != ?" # TODO: "SGML-style"
57
57
  next unless (33..126).include? char
58
58
  next if jump_over && char.chr != ?;
59
59
  case char.chr
@@ -123,7 +123,7 @@ def Befunge98 source, stdout = StringIO.new, stdin = STDIN
123
123
  code[sy + oy + y][sx + ox + x] = pop[]
124
124
  when ?[ ; dy, dx = ds[(ds.index([dy, dx]) - 1) % 4]
125
125
  when ?] ; dy, dx = ds[(ds.index([dy, dx]) + 1) % 4]
126
- when ?w ; dy, dx = ds[(ds.index([dy, dx]) + (pop[] > pop[] ? -1 : 1)) % 4]
126
+ when ?w ; dy, dx = ds[(ds.index([dy, dx]) - (pop[] <=> pop[])) % 4]
127
127
  when ?r ; reflect[]
128
128
  when ?x ; dy, dx = [pop[], pop[]]
129
129
  when ?j
@@ -131,14 +131,14 @@ def Befunge98 source, stdout = StringIO.new, stdin = STDIN
131
131
  t.times{ move[] }
132
132
  else
133
133
  reflect[]
134
- t.times{ move[] }
134
+ (-t).times{ move[] }
135
135
  reflect[]
136
136
  end
137
137
  when ?k
138
138
  iterate = pop[]
139
- fail if char != 32 || char.chr != ?;
140
139
  move[]
141
140
  char = get[]
141
+ fail if char == 32 || char == ?;.ord
142
142
  when ?{
143
143
  toss = if 0 > n = pop[]
144
144
  stack.concat [0] * -n
@@ -150,7 +150,7 @@ def Befunge98 source, stdout = StringIO.new, stdin = STDIN
150
150
  ox += x + dx
151
151
  oy += y + dy
152
152
  stacks << stack = toss
153
- when ?{
153
+ when ?}
154
154
  if 1 == stacks.size
155
155
  reflect[]
156
156
  elsif 0 > n = pop[]
data/test.rb CHANGED
@@ -121,6 +121,33 @@ describe "lib" do
121
121
  assert_equal 0, Befunge98(?@).exitcode
122
122
  end
123
123
 
124
+ it "n" do
125
+ assert_equal [], Befunge98("1n@", StringIO.new, StringIO.new).stack
126
+ end
127
+ it "'" do
128
+ assert_equal "'@".bytes, Befunge98("'''@@").stack
129
+ end
130
+ it "s with 1 and \"" do
131
+ assert_equal "@1s\1s\0".bytes, Befunge98("1ssss\"@").stack
132
+ end
133
+ it "j" do
134
+ assert_equal [9], Befunge98("2j789@").stack
135
+ end
136
+ it "j with -" do
137
+ assert_equal [2, 3], Befunge98("6-j123@").stack
138
+ end
139
+ it "w with 123, # and ." do
140
+ assert_equal "1 2 3 ", Befunge98( (
141
+ <<~HEREDOC
142
+ \#@w1.21\#@w3.@
143
+ @ 2
144
+ .
145
+ 1
146
+ #
147
+ @
148
+ HEREDOC
149
+ ), StringIO.new).stdout.string
150
+ end
124
151
  it "~" do
125
152
  assert_equal [2], Befunge98("~1@2", StringIO.new, StringIO.new).stack
126
153
  assert_equal [0, 10, 255, 0], Befunge98("~~~~@", StringIO.new,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: befunge98
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-18 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest