nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
gamnit: cache sprite GPU index, as index_of is very costly
[nit.git]
/
contrib
/
brainfuck
/
brainfuck.nit
diff --git
a/contrib/brainfuck/brainfuck.nit
b/contrib/brainfuck/brainfuck.nit
index
c35b08a
..
4216952
100644
(file)
--- a/
contrib/brainfuck/brainfuck.nit
+++ b/
contrib/brainfuck/brainfuck.nit
@@
-18,29
+18,24
@@
module brainfuck
# Interpreter for Brainfuck source code.
class BFInterpreter
# Data cells
# Interpreter for Brainfuck source code.
class BFInterpreter
# Data cells
- var dr = new Array[Char]
+ var dr = new Array[Byte]
# Data pointer
var dp = 0
# Instruction pointer
var ip = 0
# The program being interpreted
# Data pointer
var dp = 0
# Instruction pointer
var ip = 0
# The program being interpreted
- var program: String
-
- # Contains the set of valid instructions, used in next
- var valid_instr: Set[Char] is noinit
+ var program: Bytes
# Create an interpreter for `program`.
init do
# Create an interpreter for `program`.
init do
- valid_instr = new HashSet[Char]
- valid_instr.add_all "><[].,+-".chars
- dr.add 0.ascii
+ dr.add 0u8
end
# Create an interpreter for the file located at `path`.
init from_file(path: String) do
end
# Create an interpreter for the file located at `path`.
init from_file(path: String) do
- var ifs = new IFStream.open(path)
- init(ifs.read_all)
+ var ifs = new FileReader.open(path)
+ init(ifs.read_all_bytes)
end
# Starts the interpretation of the loaded program
end
# Starts the interpretation of the loaded program
@@
-48,50
+43,43
@@
class BFInterpreter
loop
if ip >= program.length then break
eval
loop
if ip >= program.length then break
eval
- next
- end
- end
-
- # Go to the next executable instruction
- fun next do
- ip += 1
- while ip < program.length and not valid_instr.has(program[ip]) do
ip += 1
end
end
ip += 1
end
end
+
# Evaluates the current instruction
fun eval do
var instr = program[ip]
# Evaluates the current instruction
fun eval do
var instr = program[ip]
- if instr == '.' then printn dr[dp]
- if instr == '[' then
- if dr[dp] == 0.ascii then
+ if instr == '.'.ascii then printn dr[dp].ascii
+ if instr == '['.ascii then
+ if dr[dp] == 0u8 then
ip = find_matching_rbra
return
end
end
ip = find_matching_rbra
return
end
end
- if instr == ']' then
- if dr[dp] != 0.ascii then
+ if instr == ']'.ascii then
+ if dr[dp] != 0u8 then
ip = find_matching_lbra
return
end
end
ip = find_matching_lbra
return
end
end
- if instr == '>' then
+ if instr == '>'.ascii then
dp += 1
dp += 1
- if dp >= dr.length then dr.add(0.ascii)
+ if dp >= dr.length then dr.add(0u8)
end
end
- if instr == '<' then
+ if instr == '<'.ascii then
dp -= 1
if dp < 0 then abort
end
dp -= 1
if dp < 0 then abort
end
- if instr == '+' then
- dr[dp] = (dr[dp].ascii + 1).ascii
+ if instr == '+'.ascii then
+ dr[dp] = dr[dp] + 1u8
end
end
- if instr == '-' then
- dr[dp] = (dr[dp].ascii - 1).ascii
+ if instr == '-'.ascii then
+ dr[dp] = dr[dp] - 1u8
end
end
- if instr == ',' then
- dr[dp] = getc
+ if instr == ','.ascii then
+ dr[dp] = getc.ascii
end
end
end
end
@@
-101,14
+89,14
@@
class BFInterpreter
var lbracnt = 0
loop
if pos > program.length then abort
var lbracnt = 0
loop
if pos > program.length then abort
- if program[pos] == ']' then
+ if program[pos] == ']'.ascii then
if lbracnt > 0 then
lbracnt -= 1
else
break
end
end
if lbracnt > 0 then
lbracnt -= 1
else
break
end
end
- if program[pos] == '[' then lbracnt += 1
+ if program[pos] == '['.ascii then lbracnt += 1
pos += 1
end
return pos
pos += 1
end
return pos
@@
-120,14
+108,14
@@
class BFInterpreter
var rbracnt = 0
loop
if pos < 0 then abort
var rbracnt = 0
loop
if pos < 0 then abort
- if program[pos] == '[' then
+ if program[pos] == '['.ascii then
if rbracnt > 0 then
rbracnt -= 1
else
break
end
end
if rbracnt > 0 then
rbracnt -= 1
else
break
end
end
- if program[pos] == ']' then rbracnt += 1
+ if program[pos] == ']'.ascii then rbracnt += 1
pos -= 1
end
return pos
pos -= 1
end
return pos