Skip to content

Commit

Permalink
Prefer Array [index] instead of first/last
Browse files Browse the repository at this point in the history
  • Loading branch information
Maumagnaguagno committed Dec 26, 2023
1 parent 9e4aeaf commit daa499f
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions MindFreak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,16 @@ def bytecode(program)
program.each_byte {|c|
# Repeated instruction
if c == last
if (bytecode.last[1] += 1) == 0
if (bytecode[-1][1] += 1) == 0
bytecode.pop
last = bytecode.last && bytecode.last.first < JUMP ? bytecode.last.first : 0
last = bytecode[-1] && bytecode[-1][0] < JUMP ? bytecode[-1][0] : 0
index -= 1
end
# Disguised repeated instruction
elsif (c == DECREMENT and last == INCREMENT) or (c == BACKWARD and last == FORWARD)
if (bytecode.last[1] -= 1) == 0
if (bytecode[-1][1] -= 1) == 0
bytecode.pop
last = bytecode.last && bytecode.last.first < JUMP ? bytecode.last.first : 0
last = bytecode[-1] && bytecode[-1][0] < JUMP ? bytecode[-1][0] : 0
index -= 1
end
else
Expand All @@ -285,7 +285,7 @@ def bytecode(program)
bytecode << [JUMP]
jump_stack << index
# Dead jump
elsif (last = jump_stack.pop) > 0 and bytecode[last-1].first == JUMPBACK
elsif (last = jump_stack.pop) > 0 and bytecode[last-1][0] == JUMPBACK
bytecode.pop(index - last)
index = last - 1
# Jump program counter to index
Expand All @@ -308,17 +308,17 @@ def bytecode(program)

def optimize(bytecode, blank_tape = false)
# Dead code elimination
bytecode.shift(bytecode[0].last+1) if blank_tape and bytecode[0].first == JUMP and not bytecode[1].first == INCREMENT && bytecode[2].first == JUMPBACK
bytecode.shift(bytecode[0][-1]+1) if blank_tape and bytecode[0][0] == JUMP and not bytecode[1][0] == INCREMENT && bytecode[2][0] == JUMPBACK
clear = [INCREMENT, 0, nil, true]
memory = Hash.new(0)
i = -1
while (i += 1) < bytecode.size
case bytecode[i].first
case bytecode[i][0]
# Clear [-] [+] or Assign [-]+ [+]-
when JUMP
if bytecode[i+1].first == INCREMENT and bytecode[i+2].first == JUMPBACK
if bytecode[i+1][0] == INCREMENT and bytecode[i+2][0] == JUMPBACK
# Assign
if bytecode[i+3] and bytecode[i+3].first == INCREMENT
if bytecode[i+3] and bytecode[i+3][0] == INCREMENT
bytecode.slice!(i,3)
bytecode[i].push(nil, true)
# Clear
Expand All @@ -327,7 +327,7 @@ def optimize(bytecode, blank_tape = false)
bytecode[i] = clear
end
# Previous increment operation is redundant
bytecode.delete_at(i -= 1) if i != 0 and bytecode[i-1].first == INCREMENT
bytecode.delete_at(i -= 1) if i != 0 and bytecode[i-1][0] == INCREMENT
else start = i
end
# Multiplication [->+<]
Expand All @@ -336,9 +336,9 @@ def optimize(bytecode, blank_tape = false)
# Extract data
pointer = 0
(start+1).upto(i-1) {|k|
if (k = bytecode[k]).first == FORWARD
if (k = bytecode[k])[0] == FORWARD
pointer += k[1]
elsif k.first == INCREMENT and not k[3]
elsif k[0] == INCREMENT and not k[3]
memory[pointer] += k[1]
else
pointer = start = nil
Expand All @@ -349,7 +349,7 @@ def optimize(bytecode, blank_tape = false)
if pointer == 0 and memory.delete(0) == -1
bytecode[start..i] = memory.map {|key,value| [MULTIPLY, key, nil, nil, value]}
i = start + memory.size
if k = bytecode[i] and k.first == INCREMENT
if k = bytecode[i] and k[0] == INCREMENT
k[3] = true
i += 1
else bytecode.insert(i, clear)
Expand All @@ -361,22 +361,22 @@ def optimize(bytecode, blank_tape = false)
jump_stack = []
i = -1
while (i += 1) < bytecode.size
case bytecode[i].first
case bytecode[i][0]
# Offset >+< <.>
when FORWARD
if next_inst = bytecode[i+1]
if next_inst.first < JUMP
if next_inst[0] < JUMP
# Original instruction uses offset
offset = bytecode[i]
(bytecode[i] = next_inst.equal?(clear) ? clear.dup : next_inst)[2] = offset[1]
# Push offset to next forward if they do not nullify
if bytecode[i+2] and bytecode[i+2].first == FORWARD
if bytecode[i+2] and bytecode[i+2][0] == FORWARD
bytecode.delete_at(i+2) if (bytecode[i+2][1] += offset[1]) == 0
bytecode.delete_at(i+1)
# Swap forward with original instruction
else bytecode[i+1] = offset
end
i -= 1 if next_inst.first == MULTIPLY
i -= 1 if next_inst[0] == MULTIPLY
end
# Remove last forward
else bytecode.pop
Expand All @@ -386,7 +386,7 @@ def optimize(bytecode, blank_tape = false)
when JUMPBACK then bytecode[bytecode[i][1] = jump_stack.pop][1] = i
# Multiplication assignment
when MULTIPLY
if i > 0 and (a = bytecode[i-1]).first == INCREMENT and a[1] == 0 and a[3] and a[2] == (b = bytecode[i])[1] + b[2].to_i
if i > 0 and (a = bytecode[i-1])[0] == INCREMENT and a[1] == 0 and a[3] and a[2] == (b = bytecode[i])[1] + b[2].to_i
b[3] = true
bytecode.delete_at(i -= 1)
end
Expand All @@ -403,7 +403,7 @@ def optimize(bytecode, blank_tape = false)
if $0 == __FILE__
begin
# Help
if ARGV.empty? or ARGV.first == '-h'
if ARGV.empty? or ARGV[0] == '-h'
puts MindFreak::HELP
else
# Options
Expand Down

0 comments on commit daa499f

Please sign in to comment.