b614776ab72782170ee6930edb02d7b214ed5556
[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 BFInterpret
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]
32
33 # Starts interpretation of file `filename`
34 init(filename: String) do
35 var ifs = new IFStream.open(filename.simplify_path)
36 valid_instr = new HashSet[Char]
37 valid_instr.add_all "><[].,+-".chars
38 dr.add 0.ascii
39 program = ifs.read_all
40 start
41 end
42
43 # Starts the interpretation of the loaded program
44 fun start do
45 loop
46 if ip >= program.length then break
47 eval
48 next
49 end
50 end
51
52 # Go to the next executable instruction
53 fun next do
54 ip += 1
55 while ip < program.length and not valid_instr.has(program[ip]) do
56 ip += 1
57 end
58 end
59
60 # Evaluates the current instruction
61 fun eval do
62 var instr = program[ip]
63 if instr == '.' then printn dr[dp]
64 if instr == '[' then
65 if dr[dp] == 0.ascii then
66 ip = find_matching_rbra
67 return
68 end
69 end
70 if instr == ']' then
71 if dr[dp] != 0.ascii then
72 ip = find_matching_lbra
73 return
74 end
75 end
76 if instr == '>' then
77 dp += 1
78 if dp >= dr.length then dr.add(0.ascii)
79 end
80 if instr == '<' then
81 dp -= 1
82 if dp < 0 then abort
83 end
84 if instr == '+' then
85 dr[dp] = (dr[dp].ascii + 1).ascii
86 end
87 if instr == '-' then
88 dr[dp] = (dr[dp].ascii - 1).ascii
89 end
90 if instr == ',' then
91 dr[dp] = getc
92 end
93 end
94
95 # Seeks for the position of the matching `]` for the `[` located at `ip`
96 fun find_matching_rbra: Int do
97 var pos = ip + 1
98 var lbracnt = 0
99 loop
100 if pos > program.length then abort
101 if program[pos] == ']' then
102 if lbracnt > 0 then
103 lbracnt -= 1
104 else
105 break
106 end
107 end
108 if program[pos] == '[' then lbracnt += 1
109 pos += 1
110 end
111 return pos
112 end
113
114 # Seeks for the position of the matching `[` for the `]` located at `ip`
115 fun find_matching_lbra: Int do
116 var pos = ip - 1
117 var rbracnt = 0
118 loop
119 if pos < 0 then abort
120 if program[pos] == '[' then
121 if rbracnt > 0 then
122 rbracnt -= 1
123 else
124 break
125 end
126 end
127 if program[pos] == ']' then rbracnt += 1
128 pos -= 1
129 end
130 return pos
131 end
132 end
133
134 var i = new BFInterpret(args[0])