nim: style: don't indent of
in case statements
This commit is contained in:
parent
78af780177
commit
e483d930d6
1 changed files with 31 additions and 31 deletions
62
nim/bf.nim
62
nim/bf.nim
|
@ -47,15 +47,15 @@ proc tokenize(data: string): seq[Token] =
|
||||||
var tokens: seq[Token]
|
var tokens: seq[Token]
|
||||||
for c in data:
|
for c in data:
|
||||||
case c:
|
case c:
|
||||||
of '>': tokens.add(IncrementHead)
|
of '>': tokens.add(IncrementHead)
|
||||||
of '<': tokens.add(DecrementHead)
|
of '<': tokens.add(DecrementHead)
|
||||||
of '+': tokens.add(IncrementCell)
|
of '+': tokens.add(IncrementCell)
|
||||||
of '-': tokens.add(DecrementCell)
|
of '-': tokens.add(DecrementCell)
|
||||||
of '.': tokens.add(Print)
|
of '.': tokens.add(Print)
|
||||||
of ',': tokens.add(Read)
|
of ',': tokens.add(Read)
|
||||||
of '[': tokens.add(LoopStart)
|
of '[': tokens.add(LoopStart)
|
||||||
of ']': tokens.add(LoopEnd)
|
of ']': tokens.add(LoopEnd)
|
||||||
else: continue
|
else: continue
|
||||||
return tokens
|
return tokens
|
||||||
|
|
||||||
proc parse(tokens: seq[Token], index: int): (seq[Command], int) =
|
proc parse(tokens: seq[Token], index: int): (seq[Command], int) =
|
||||||
|
@ -63,34 +63,34 @@ proc parse(tokens: seq[Token], index: int): (seq[Command], int) =
|
||||||
var index = index
|
var index = index
|
||||||
while index < tokens.len:
|
while index < tokens.len:
|
||||||
case tokens[index]:
|
case tokens[index]:
|
||||||
of LoopStart:
|
of LoopStart:
|
||||||
let res = parse(tokens, index+1)
|
let res = parse(tokens, index+1)
|
||||||
index = res[1]
|
index = res[1]
|
||||||
let loop = Command(kind: Loop, cs: res[0])
|
let loop = Command(kind: Loop, cs: res[0])
|
||||||
commands.add(loop)
|
commands.add(loop)
|
||||||
of LoopEnd:
|
of LoopEnd:
|
||||||
return (commands, index)
|
return (commands, index)
|
||||||
else:
|
else:
|
||||||
let command = Command(kind: Do, tk: tokens[index])
|
let command = Command(kind: Do, tk: tokens[index])
|
||||||
commands.add(command)
|
commands.add(command)
|
||||||
index.inc
|
index.inc
|
||||||
return (commands, index)
|
return (commands, index)
|
||||||
|
|
||||||
proc run(program: seq[Command], tape: Tape) =
|
proc run(program: seq[Command], tape: Tape) =
|
||||||
for command in program:
|
for command in program:
|
||||||
case command.kind:
|
case command.kind:
|
||||||
of Do:
|
of Do:
|
||||||
case command.tk:
|
case command.tk:
|
||||||
of IncrementHead: tape.incrementHead()
|
of IncrementHead: tape.incrementHead()
|
||||||
of DecrementHead: tape.decrementHead()
|
of DecrementHead: tape.decrementHead()
|
||||||
of IncrementCell: tape.incrementCell()
|
of IncrementCell: tape.incrementCell()
|
||||||
of DecrementCell: tape.decrementCell()
|
of DecrementCell: tape.decrementCell()
|
||||||
of Print: tape.print()
|
of Print: tape.print()
|
||||||
of Read: tape.read()
|
of Read: tape.read()
|
||||||
else: discard
|
else: discard
|
||||||
of Loop:
|
of Loop:
|
||||||
while tape.getData() != 0:
|
while tape.getData() != 0:
|
||||||
run(command.cs, tape)
|
run(command.cs, tape)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let brainfuck = paramStr(1).readFile()
|
let brainfuck = paramStr(1).readFile()
|
||||||
|
|
Loading…
Add table
Reference in a new issue