brainfuck: Remove warnings.
[nit.git] / contrib / brainfuck / brainfuck.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Simple brainfuck interpreter
16 module brainfuck
17
18 # Interpreter for Brainfuck source code.
19 class BFInterpreter
20 # Data cells
21 var dr = new Array[Char]
22 # Data pointer
23 var dp = 0
24 # Instruction pointer
25 var ip = 0
26
27 # The program being interpreted
28 var program: String
29
30 # Contains the set of valid instructions, used in next
31 var valid_instr: Set[Char] is noinit
32
33 # Create an interpreter for `program`.
34 init do
35 valid_instr = new HashSet[Char]
36 valid_instr.add_all "><[].,+-".chars
37 dr.add 0.ascii
38 end
39
40 # Create an interpreter for the file located at `path`.
41 init from_file(path: String) do
42 var ifs = new IFStream.open(path)
43 init(ifs.read_all)
44 end
45
46 # Starts the interpretation of the loaded program
47 fun start do
48 loop
49 if ip >= program.length then break
50 eval
51 next
52 end
53 end
54
55 # Go to the next executable instruction
56 fun next do
57 ip += 1
58 while ip < program.length and not valid_instr.has(program[ip]) do
59 ip += 1
60 end
61 end
62
63 # Evaluates the current instruction
64 fun eval do
65 var instr = program[ip]
66 if instr == '.' then printn dr[dp]
67 if instr == '[' then
68 if dr[dp] == 0.ascii then
69 ip = find_matching_rbra
70 return
71 end
72 end
73 if instr == ']' then
74 if dr[dp] != 0.ascii then
75 ip = find_matching_lbra
76 return
77 end
78 end
79 if instr == '>' then
80 dp += 1
81 if dp >= dr.length then dr.add(0.ascii)
82 end
83 if instr == '<' then
84 dp -= 1
85 if dp < 0 then abort
86 end
87 if instr == '+' then
88 dr[dp] = (dr[dp].ascii + 1).ascii
89 end
90 if instr == '-' then
91 dr[dp] = (dr[dp].ascii - 1).ascii
92 end
93 if instr == ',' then
94 dr[dp] = getc
95 end
96 end
97
98 # Seeks for the position of the matching `]` for the `[` located at `ip`
99 fun find_matching_rbra: Int do
100 var pos = ip + 1
101 var lbracnt = 0
102 loop
103 if pos > program.length then abort
104 if program[pos] == ']' then
105 if lbracnt > 0 then
106 lbracnt -= 1
107 else
108 break
109 end
110 end
111 if program[pos] == '[' then lbracnt += 1
112 pos += 1
113 end
114 return pos
115 end
116
117 # Seeks for the position of the matching `[` for the `]` located at `ip`
118 fun find_matching_lbra: Int do
119 var pos = ip - 1
120 var rbracnt = 0
121 loop
122 if pos < 0 then abort
123 if program[pos] == '[' then
124 if rbracnt > 0 then
125 rbracnt -= 1
126 else
127 break
128 end
129 end
130 if program[pos] == ']' then rbracnt += 1
131 pos -= 1
132 end
133 return pos
134 end
135 end
136
137 new BFInterpreter.from_file(args[0]).start