brainfuck_converter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/brainfuck_converter.rb +117 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c08e5e4a5d77feebda6066f7179d82c793261df3068262fdfb3f9e14a36d72e2
4
+ data.tar.gz: a39d32789909b1db55415a73e4fb30877258deefda3a45e9835c0c39e5eaa0f7
5
+ SHA512:
6
+ metadata.gz: afa32bc2d86c91882080a7c5c987860251ce7df3f73d2d05166e38ad9e474cebc9d1e5d8e41841a5af7817260cfbac1e9cb8cc22746043673c7f2b4b03474683
7
+ data.tar.gz: 6fd090138d52569de4f92d1db8bf30e3850ea520db2a3dd5de6e5745dbec7b60a283b65ee466b9880b9c3601547cf7c14873ffd4873f580f5bc7c0ef96396314
@@ -0,0 +1,117 @@
1
+
2
+ class BrainfuckConverter
3
+
4
+ DEFAULT_COMMANDS = {
5
+ inc_val: "+",
6
+ dec_val: "-",
7
+ inc_ptr: ">",
8
+ dec_ptr: "<",
9
+ output: ".",
10
+ loop_begin: "[",
11
+ loop_end: "]"
12
+ }
13
+
14
+ attr_reader :code
15
+ attr_accessor :cmds
16
+
17
+ # Initializes a new Brainfuck Converter
18
+ #
19
+ # @param bf_cmds [Hash] Command sign. If not specified, the default command
20
+ # set under BrainfuckConverter::DEFAULT_COMMANDS will be used.
21
+ def initialize bf_cmds = DEFAULT_COMMANDS
22
+ @cmds = bf_cmds
23
+ end
24
+
25
+ # Converts an ASCII string to Brainfuck code.
26
+ #
27
+ # @param text [String] The text to be converted into Brainfuck code.
28
+ # @param cells_num [Integer] Number of cells to be used. All values from 0
29
+ # can be used. If not specified, 8 cells are used. Regardless of the number
30
+ # specified here, an additional cell is used.
31
+ # @return [String]
32
+ # @example
33
+ # require "brainfuck_converter"
34
+ #
35
+ # con = BrainfuckConverter.new
36
+ #
37
+ # con.convert "Hello World!" # => "Hello World!"
38
+ # con.code # => "++++++++++++++[>+>++>+++>++++>+++++>++++++>++++++... too long"
39
+ #
40
+ # con.convert "Hallo Welt!", 15 # => "Hallo Welt!"
41
+ # con.code # => "+++++++[>+>++>+++>++++>+++++>++++++>+++++++>+++++... too long"
42
+ def convert text, cells_num = 8
43
+ # To output a string in Brainfuck, "auxiliary numbers" are written into
44
+ # certain cells. If a letter is to be output, the letter is converted into
45
+ # an ASCII value and the auxiliary number that is closest to it is searched for.
46
+ # This is then adjusted so that it corresponds to the ASCII value of the letter.
47
+ # The auxiliary number or the letter is then output.
48
+
49
+ if cells_num < 1
50
+ return false
51
+ end
52
+
53
+ # Code is cleared. A new Brainfuck program is started.
54
+ @code = ""
55
+
56
+ # Calculating the auxiliary numbers
57
+ space_cells = 127 / (cells_num + 1)
58
+ @cell_values = []
59
+ @cell_values[0] = space_cells
60
+ for i in 1...cells_num
61
+ @cell_values[i] = @cell_values[i - 1] + space_cells
62
+ end
63
+
64
+ # The code to create the auxiliary numbers in the cells is created.
65
+ # The auxiliary numbers are created by multiplication.
66
+ # This also has the advantage that you can use a loop.
67
+ @code += @cmds[:inc_val] * space_cells
68
+ @code += @cmds[:loop_begin]
69
+ for i in 1..cells_num
70
+ @code += @cmds[:inc_ptr]
71
+ @code += @cmds[:inc_val] * i
72
+ end
73
+ @code += @cmds[:dec_ptr] * cells_num
74
+ @code += @cmds[:dec_val]
75
+ @code += @cmds[:loop_end]
76
+ @code += @cmds[:inc_ptr]
77
+
78
+ # A pointer to get to the corresponding cells later.
79
+ # To be exact, you would be at cell 1, but you can ignore the first cell 0,
80
+ # because it was only used as a counter for the loop.
81
+ @pointer = 0
82
+
83
+ text.each_byte { |search|
84
+ # Search for the next auxiliary number
85
+ diffs = @cell_values.map { |val|
86
+ (search - val).abs
87
+ }
88
+ nearest = diffs.index(diffs.min)
89
+ diff = search - @cell_values[nearest]
90
+
91
+ # It goes to the auxiliary number. This is changed accordingly and the
92
+ # corresponding ASCII_character is output.
93
+ move_pointer @pointer, nearest
94
+ change_value search
95
+ output_cell
96
+ }
97
+ end
98
+
99
+ protected
100
+
101
+ def move_pointer cur_pos, target_pos
102
+ move = target_pos - cur_pos
103
+ @code += (move < 0 ? @cmds[:dec_ptr] : @cmds[:inc_ptr]) * move.abs
104
+ @pointer = target_pos
105
+ end
106
+
107
+ def change_value target_value
108
+ change = target_value - @cell_values[@pointer]
109
+ @code += (change < 0 ? @cmds[:dec_val] : @cmds[:inc_val]) * change.abs
110
+ @cell_values[@pointer] = target_value
111
+ end
112
+
113
+ def output_cell
114
+ @code += @cmds[:output]
115
+ end
116
+
117
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainfuck_converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marek Kuethe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem, which makes it possible to convert any ASCII characters into brainfuck
14
+ code
15
+ email: m.k@mk16.de
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/brainfuck_converter.rb
21
+ homepage: https://github.com/marek22k/BrainfuckConverter
22
+ licenses:
23
+ - WTFPL
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.3.4
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Gem, which makes it possible to convert any ASCII characters into brainfuck
44
+ code
45
+ test_files: []