brainfuck-extended 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/main.rb +253 -0
  3. metadata +76 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 427c06c5a17fb93038312e3fcf121d51ed87715f60cde6799d2e21c7a7ad9b97
4
+ data.tar.gz: a7d446fb9737d10e7a78cb68401e0a5d3b1e77fb8bef05363806611d71e7fd17
5
+ SHA512:
6
+ metadata.gz: a3c542e019d8e3b5cf6a3a146ace2bce80426b949444b8aa272ee5f86183785375d863dd56f47ce9c6312b0b53c79976adc655fe54554204a3d24fe234a7960e
7
+ data.tar.gz: 1720938c9047796826731110a7b84425df634e28549fd6629fddb8b376067571c0203d69645baa776072951ef531519bffa93aeeea727e5a4fb820d36f57391c
data/main.rb ADDED
@@ -0,0 +1,253 @@
1
+ require "io/console"
2
+ require 'io/console/size'
3
+ require "colorize"
4
+ require "tty-cursor"
5
+ require 'optparse'
6
+ opt = OptionParser.new
7
+ visible = false
8
+ debug = false
9
+ opt.on('-v', "--verbose", "Run with verbose mode") { |v|
10
+ visible = v
11
+ }
12
+ opt.on('-d', "--debug", "Run with debug mode") {|v|
13
+ debug = v
14
+ if v
15
+ visible = true
16
+ end
17
+ }
18
+ opt.banner += ' file'
19
+ opt.parse!(ARGV)
20
+ script = ""
21
+ if ARGV[0] == nil
22
+ script = "
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
+ elsif not File.exists?(ARGV[0])
50
+ script = "
51
+ ///++++++++[^^^++++++++///-]^^^:/
52
+ ;///++++++++[^^^++++///-]
53
+ /++++[^^^++++++++///-]^^^^^
54
+ >;++++++.
55
+ :>;<</:>;+++++++++.
56
+ +++.
57
+ ;+++++.
58
+ </.
59
+ ^^:>;///++[^^^+++++++///-]^^^.
60
+ /<:>;///+++[^^^+++++///-]^^^.
61
+ :>;<+++++.
62
+ </.
63
+ ^^>>.
64
+ /.
65
+ :>;++++++.
66
+ <-.
67
+ <<:>;++++.
68
+ </:>;///+++[^^^++++///-]^^^++.
69
+ "
70
+ else
71
+ File.open(ARGV[0], "r") do |f|
72
+ script = f.read
73
+ end
74
+ end
75
+ cursor = TTY::Cursor
76
+ index = 0
77
+ cursor_x = 0
78
+ cursor_y = 0
79
+ field = [[0]]
80
+ brackets = []
81
+ bracket_pairs = {}
82
+ clipboard = 0
83
+ output = ""
84
+ WIDTH = IO::console_size[1]
85
+ script = script.split("").filter{ |c| ".,/^<>[]?+-:;".include?(c)}.join("")
86
+ $finish_flag = false
87
+ if visible
88
+ print cursor.clear_screen
89
+ puts (" " * WIDTH).on_blue
90
+ print " ".on_blue
91
+ print "Brainfuck Extended".center(WIDTH - 4)
92
+ puts " ".on_blue
93
+ puts (" " * WIDTH).on_blue
94
+ puts ""
95
+ end
96
+ # Lint
97
+ lint_brackets = []
98
+ script.split("").each_with_index do |c, i|
99
+ case c
100
+ when "["
101
+ lint_brackets << i
102
+ when "]"
103
+ n = lint_brackets.delete_at(-1)
104
+ if n == nil
105
+ raise "SyntaxError: Unmatched brackets"
106
+ else
107
+ bracket_pairs[n] = i
108
+ end
109
+ end
110
+ end
111
+ # Execute
112
+ until index >= script.length
113
+ current_chr = script[index]
114
+ case current_chr
115
+ when "+"
116
+ field[cursor_y][cursor_x] += 1
117
+ when "-"
118
+ field[cursor_y][cursor_x] -= 1
119
+ when ">"
120
+ cursor_x += 1
121
+ if field[cursor_y].length <= cursor_x
122
+ field.each do |f|
123
+ f << 0
124
+ end
125
+ end
126
+ when "<"
127
+ cursor_x -= 1
128
+ when "."
129
+ if visible
130
+ output += field[cursor_y][cursor_x].chr
131
+ else
132
+ $stdout.print field[cursor_y][cursor_x].chr
133
+ end
134
+ when ","
135
+ field[cursor_y][cursor_x] = $stdin.getch
136
+ when "["
137
+ if field[cursor_y][cursor_x] != 0
138
+ brackets << index
139
+ else
140
+ until script[index] == "]"
141
+ index += 1
142
+ end
143
+ end
144
+ when "]"
145
+ if field[cursor_y][cursor_x] == 0
146
+ brackets.delete_at(0)
147
+ else
148
+ index = brackets[-1]
149
+ end
150
+ when "?"
151
+ field[cursor_y][cursor_x] = rand(0..255)
152
+ when ":"
153
+ clipboard = field[cursor_y][cursor_x]
154
+ when ";"
155
+ field[cursor_y][cursor_x] = clipboard
156
+ when "/"
157
+ cursor_y += 1
158
+ if field.length <= cursor_y
159
+ field << Array.new(field[0].length) { 0 }
160
+ end
161
+ when "^"
162
+ cursor_y -= 1
163
+ end
164
+ if cursor_x < 0
165
+ raise "Out of X range: #{cursor_x}"
166
+ end
167
+ if cursor_y < 0
168
+ raise "Out of Y range: #{cursor_y}"
169
+ end
170
+ unless (0..255).include? field[cursor_y][cursor_x]
171
+ raise "Overflow: #{cursor_x}, #{cursor_y}"
172
+ end
173
+ if visible
174
+ sleep(debug ? 0.1 : 0.01)
175
+ print cursor.move_to 0, 4
176
+ print " ".on_light_blue
177
+ print " Source "
178
+ puts (" " * (WIDTH - 8 - 2)).on_light_blue
179
+ print cursor.clear_line
180
+ running_bracket_ends = []
181
+ primary_bracket_end = -1
182
+ script.split("").each_with_index do |c, i|
183
+ if script.length <= WIDTH
184
+ do_print = true
185
+ elsif index < WIDTH / 2
186
+ do_print = i < WIDTH
187
+ elsif index >= script.length - WIDTH / 2
188
+ do_print = i > script.length - WIDTH
189
+ else
190
+ do_print = [(index - i).abs, 0].max < WIDTH / 2
191
+ end
192
+ if brackets[-1] == i
193
+ primary_bracket_end = bracket_pairs[i]
194
+ print c.light_green if do_print
195
+ elsif primary_bracket_end == i
196
+ print c.light_green if do_print
197
+ elsif brackets.include?(i)
198
+ running_bracket_ends << bracket_pairs[i]
199
+ print c.green if do_print
200
+ elsif index == i
201
+ print c.yellow if do_print
202
+ elsif running_bracket_ends.include?(i)
203
+ print c.green if do_print
204
+ else
205
+ print c if do_print
206
+ end
207
+ end
208
+ puts
209
+ print " ".on_light_blue
210
+ print " Output "
211
+ puts (" " * (WIDTH - 8 - 2)).on_light_blue
212
+ puts output
213
+ pos_txt = "#{clipboard} #{cursor_x}, #{cursor_y}"
214
+ print " ".on_light_blue
215
+ print " Field "
216
+ print (" " * (WIDTH - 7 - 2 - pos_txt.length - 2)).on_light_blue
217
+ print pos_txt.on_light_blue.light_white
218
+ puts " ".on_light_blue
219
+ max_length = field.flatten.map { |i| i.to_s.length }.max
220
+ print cursor.clear_line
221
+ print " "
222
+ field[0].length.times do |i|
223
+ print i.to_s.rjust(max_length).colorize(i == cursor_x ? :light_blue : :blue)
224
+ print " "
225
+ end
226
+ print "\n"
227
+ field.each_with_index do |f, j|
228
+ print cursor.clear_line
229
+ print j.to_s.colorize(j == cursor_y ? :light_blue : :blue)
230
+ print " "
231
+ f.each_with_index do |v, i|
232
+
233
+ res = v.to_s.rjust(max_length)
234
+ case [i,j]
235
+ in [^cursor_x, ^cursor_y]
236
+ print res.light_green
237
+ in [^cursor_x, _] | [_, ^cursor_y]
238
+ print res.green
239
+ else
240
+ print res
241
+ end
242
+ print " "
243
+ end
244
+
245
+ print "\n"
246
+ end
247
+ end
248
+ index += 1
249
+ end
250
+ if visible
251
+ print "Waiting for input..."
252
+ $stdin.getch
253
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainfuck-extended
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - sevenc-nanashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-cursor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2
42
+ An extended BrainFuck.
43
+ You can use random, you can use 2D data, you can use temp data.
44
+ email: sevenc-nanashi@sevenbot.jp
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - main.rb
50
+ homepage: https://github.com/sevenc-nanashi/brainfuck-extended
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ bug_tracker_uri: https://github.com/sevenc-nanashi/brainfuck-extended/issues
55
+ homepage_uri: https://github.com/sevenc-nanashi/brainfuck-extended
56
+ source_code_uri: https://github.com/sevenc-nanashi/brainfuck-extended
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.2.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: An extended BrainFuck.
76
+ test_files: []