尼姆:'readChar deprecated'。用什么代替?
Nim: 'readChar deprecated'. What to use instead?
我正在按照本指南通过编写一个 brainfuck 解释器开始使用 Nim:[http://howistart.org/posts/nim/1/][1]
一切正常,但编译包含警告:
error: readChar is deprecated
谷歌搜索该消息(在引号中)没有找到任何结果。 readChar 的替代品是否有所下降?
如果不是,相同功能的最简单替代品是什么? (最好来自标准库)。
使用的代码是:
proc run(skip = false): bool =
while tapePos >= 0 and codePos < code.len:
if tapePos >= tape.len:
tape.add '[=11=]'
if code[codePos] == '[':
inc codePos
let oldPos = codePos
while run(tape[tapePos] == '[=11=]'):
codePos = oldPos
elif code[codePos] == ']':
return tape[tapePos] != '[=11=]'
elif not skip:
case code[codePos]
of '+': inc tape[tapePos]
of '-': dec tape[tapePos]
of '>': inc tapePos
of '<': dec tapePos
of '.': stdout.write tape[tapePos]
of ',': tape[tapePos] = stdin.readChar
else: discard
inc codePos
Nim 文档建议使用 readBuffer。
proc readBuffer(f: File; buffer: pointer; len: Natural): int {..}
reads len bytes into the buffer pointed to by buffer. Returns the
actual number of bytes that have been read which may be less than len
(if not as many bytes are remaining), but not greater.
以下是如何使用 readChars
实现相同的语义(未经测试,但应该有效):
proc run(skip = false): bool =
while tapePos >= 0 and codePos < code.len:
if tapePos >= tape.len:
tape.add '[=10=]'
if code[codePos] == '[':
inc codePos
let oldPos = codePos
while run(tape[tapePos] == '[=10=]'):
codePos = oldPos
elif code[codePos] == ']':
return tape[tapePos] != '[=10=]'
elif not skip:
case code[codePos]
of '+': inc tape[tapePos]
of '-': dec tape[tapePos]
of '>': inc tapePos
of '<': dec tapePos
of '.': stdout.write tape[tapePos]
of ',': discard stdin.readChars(tape, tapePos, 1)
else: discard
inc codePos
有关弃用原因的上下文,请参阅我的评论
我正在按照本指南通过编写一个 brainfuck 解释器开始使用 Nim:[http://howistart.org/posts/nim/1/][1]
一切正常,但编译包含警告:
error: readChar is deprecated
谷歌搜索该消息(在引号中)没有找到任何结果。 readChar 的替代品是否有所下降? 如果不是,相同功能的最简单替代品是什么? (最好来自标准库)。
使用的代码是:
proc run(skip = false): bool =
while tapePos >= 0 and codePos < code.len:
if tapePos >= tape.len:
tape.add '[=11=]'
if code[codePos] == '[':
inc codePos
let oldPos = codePos
while run(tape[tapePos] == '[=11=]'):
codePos = oldPos
elif code[codePos] == ']':
return tape[tapePos] != '[=11=]'
elif not skip:
case code[codePos]
of '+': inc tape[tapePos]
of '-': dec tape[tapePos]
of '>': inc tapePos
of '<': dec tapePos
of '.': stdout.write tape[tapePos]
of ',': tape[tapePos] = stdin.readChar
else: discard
inc codePos
Nim 文档建议使用 readBuffer。
proc readBuffer(f: File; buffer: pointer; len: Natural): int {..}
reads len bytes into the buffer pointed to by buffer. Returns the actual number of bytes that have been read which may be less than len (if not as many bytes are remaining), but not greater.
以下是如何使用 readChars
实现相同的语义(未经测试,但应该有效):
proc run(skip = false): bool =
while tapePos >= 0 and codePos < code.len:
if tapePos >= tape.len:
tape.add '[=10=]'
if code[codePos] == '[':
inc codePos
let oldPos = codePos
while run(tape[tapePos] == '[=10=]'):
codePos = oldPos
elif code[codePos] == ']':
return tape[tapePos] != '[=10=]'
elif not skip:
case code[codePos]
of '+': inc tape[tapePos]
of '-': dec tape[tapePos]
of '>': inc tapePos
of '<': dec tapePos
of '.': stdout.write tape[tapePos]
of ',': discard stdin.readChars(tape, tapePos, 1)
else: discard
inc codePos
有关弃用原因的上下文,请参阅我的评论