lib: Update libs to use correctly ascii and code_point
[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[Byte]
22 # Data pointer
23 var dp = 0
24 # Instruction pointer
25 var ip = 0
26
27 # The program being interpreted
28 var program: Bytes
29
30 # Create an interpreter for `program`.
31 init do
32 dr.add 0u8
33 end
34
35 # Create an interpreter for the file located at `path`.
36 init from_file(path: String) do
37 var ifs = new FileReader.open(path)
38 init(ifs.read_all_bytes)
39 end
40
41 # Starts the interpretation of the loaded program
42 fun start do
43 loop
44 if ip >= program.length then break
45 eval
46 ip += 1
47 end
48 end
49
50
51 # Evaluates the current instruction
52 fun eval do
53 var instr = program[ip]
54 if instr == '.'.ascii then printn dr[dp].ascii
55 if instr == '['.ascii then
56 if dr[dp] == 0u8 then
57 ip = find_matching_rbra
58 return
59 end
60 end
61 if instr == ']'.ascii then
62 if dr[dp] != 0u8 then
63 ip = find_matching_lbra
64 return
65 end
66 end
67 if instr == '>'.ascii then
68 dp += 1
69 if dp >= dr.length then dr.add(0u8)
70 end
71 if instr == '<'.ascii then
72 dp -= 1
73 if dp < 0 then abort
74 end
75 if instr == '+'.ascii then
76 dr[dp] = dr[dp] + 1u8
77 end
78 if instr == '-'.ascii then
79 dr[dp] = dr[dp] - 1u8
80 end
81 if instr == ','.ascii then
82 dr[dp] = getc.ascii
83 end
84 end
85
86 # Seeks for the position of the matching `]` for the `[` located at `ip`
87 fun find_matching_rbra: Int do
88 var pos = ip + 1
89 var lbracnt = 0
90 loop
91 if pos > program.length then abort
92 if program[pos] == ']'.ascii then
93 if lbracnt > 0 then
94 lbracnt -= 1
95 else
96 break
97 end
98 end
99 if program[pos] == '['.ascii then lbracnt += 1
100 pos += 1
101 end
102 return pos
103 end
104
105 # Seeks for the position of the matching `[` for the `]` located at `ip`
106 fun find_matching_lbra: Int do
107 var pos = ip - 1
108 var rbracnt = 0
109 loop
110 if pos < 0 then abort
111 if program[pos] == '['.ascii then
112 if rbracnt > 0 then
113 rbracnt -= 1
114 else
115 break
116 end
117 end
118 if program[pos] == ']'.ascii then rbracnt += 1
119 pos -= 1
120 end
121 return pos
122 end
123 end
124
125 new BFInterpreter.from_file(args[0]).start