Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
72
Task/Execute-Brain-/Nanoquery/execute-brain-.nanoquery
Normal file
72
Task/Execute-Brain-/Nanoquery/execute-brain-.nanoquery
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// nanoquery has no function to get just a character
|
||||
// so we have to implement our own
|
||||
def get_char()
|
||||
c = ""
|
||||
while len(c)=0
|
||||
c = input()
|
||||
end
|
||||
return c[0]
|
||||
end
|
||||
|
||||
// a function to handle fatal errors
|
||||
def fatal_error(errtext)
|
||||
println "%" + errtext
|
||||
println "usage: " + args[1] + " [filename.bf]"
|
||||
exit
|
||||
end
|
||||
|
||||
// get a filename from the command line and read the file in
|
||||
fname = null
|
||||
source = null
|
||||
try
|
||||
fname = args[2]
|
||||
source = new(Nanoquery.IO.File, fname).readAll()
|
||||
catch
|
||||
fatal_error("error while trying to read from specified file")
|
||||
end
|
||||
|
||||
// start with one hundred cells and the pointer at 0
|
||||
cells = {0} * 100
|
||||
ptr = 0
|
||||
|
||||
// loop through the instructions
|
||||
loc = 0
|
||||
while loc < len(source)
|
||||
instr = source[loc]
|
||||
|
||||
if instr = ">"
|
||||
ptr += 1
|
||||
if ptr = len(cells)
|
||||
cells.append(0)
|
||||
end
|
||||
else if instr = "<"
|
||||
ptr -= 1
|
||||
if ptr < 0
|
||||
ptr = 0
|
||||
end
|
||||
else if instr = "+"
|
||||
cells[ptr] += 1
|
||||
else if instr = "-"
|
||||
cells[ptr] -= 1
|
||||
else if instr = "."
|
||||
print chr(cells[ptr])
|
||||
else if instr = ","
|
||||
cells[ptr] = ord(get_char())
|
||||
else if instr = "["
|
||||
if cells[ptr] = 0
|
||||
while source[loc] != "]"
|
||||
loc += 1
|
||||
end
|
||||
end
|
||||
else if instr = "]"
|
||||
if cells[ptr] != 0
|
||||
while source[loc] != "["
|
||||
loc -= 1
|
||||
end
|
||||
end
|
||||
else
|
||||
// do nothing
|
||||
end
|
||||
|
||||
loc += 1
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue