This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
143 lines (118 loc) · 3.13 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env ruby
# Take a file path from a cmd line argument
if ARGV.length != 1
puts "Please provide the path to brainfuck program"
exit
end
bf_filename = ARGV[0]
unless File.exist?(bf_filename)
puts "File %s doesnt exist" % [bf_filename]
exit
end
# Open the file
FILE = File.read(bf_filename)
size = FILE.size
# bf_chars = ['>','<','+','-','.',',','[',']']
bf_pointer = 0
bf_tape = [0]
# print "Brainfuck characters: ", bf_chars
# puts
# puts
# puts "File content: ", FILE
# test = $stdin.gets.chomp
# puts "test: !%s! " % test[-1]
puts
i = 0
while i < FILE.size do
case FILE[i]
when '>'
bf_pointer+=1
if bf_pointer >= bf_tape.size-1
bf_tape << 0
end
puts FILE[i],'{incrementing pointer ptr=%d}'% bf_pointer
i+=1
when '<'
bf_pointer-=1
puts FILE[i],'{decrementing pointer ptr=%d}'% bf_pointer
if bf_pointer < 0
puts "ERROR: Using negative tape pointer."
exit 1
end
i+=1
when '+'
bf_tape[bf_pointer]+=1
puts FILE[i],'{incrementing contents cont=%d ptr=%d}' % [ bf_tape[bf_pointer], bf_pointer ]
i+=1
when '-'
bf_tape[bf_pointer]-=1
puts FILE[i],'{decrementing contents cont=%d ptr=%d}' % [ bf_tape[bf_pointer], bf_pointer ]
i+=1
when '.'
print bf_tape[bf_pointer].chr
puts FILE[i],'{printing %s }' % bf_tape[bf_pointer].chr
i+=1
when ','
ch = $stdin.gets.chomp
unless ch.length == 0
bf_tape[bf_pointer] = ch[-1].ord
end
puts FILE[i],'{saving user input character %s}' % ch[-1]
i+=1
when '['
puts FILE[i],'{handling [ }'
nested_cnt=0
if bf_tape[bf_pointer] == 0
puts "SKIPPING THE BRACKET"
# set pointer to the maching closing bracket
while true do
i+=1
if FILE[i] == ']' && nested_cnt == 0
break
end
if FILE[i] == '['
nested_cnt+=1
puts "nested_cnt: " + nested_cnt.to_s
end
if nested_cnt >0 && FILE[i] == ']'
nested_cnt-=1
puts "nested_cnt: " + nested_cnt.to_s
end
end
else
puts "ENTERING THE LOOP"
end
i+=1
when ']'
puts FILE[i],'{handling ] }'
nested_cnt=0
while true do
i-=1
if FILE[i] == '[' && nested_cnt==0
break
end
if FILE[i] == ']'
nested_cnt+=1
end
if nested_cnt >0 && FILE[i] == '['
nested_cnt-=1
end
puts "nested_cnt: " + nested_cnt.to_s
end
else
print FILE[i]
i+=1
end
puts "index: "+i.to_s
puts "pointer: "+bf_pointer.to_s
print "tape: ", bf_tape, "\ntape size: ", bf_tape.size
puts
puts
sleep 1
end
# def FILE.remove_nonbf_chars
# puts "HELLO from inside method"
# puts FILE
# end
# FILE.remove_nonbf_chars
# puts "File content: ", FILE.remove_nonbf_chars