I don't know if this is Nim's style or not, but your version somewhat more
efficient
type
TokenKind = enum tokenNested, tokenSingle
Token = ref object
case kind: TokenKind
of tokenNested:
tokens: seq[Token]
of tokenSingle:
content: char
else: nil
proc tokenize(text: string): seq[Token] =
result = @[]
var current, latest: ptr seq[Token]
current = addr result
for it in text:
case it
of '[':
latest = current
let t = Token(kind: tokenNested, tokens: @[])
current = addr t.tokens
latest[].add(t)
of ']':
current = latest
of '>', '<', '+', '-', '.', ',':
current[].add(Token(kind: tokenSingle, content: it))
else:
continue
import strutils
proc `$`(t: Token): string =
result = ""
case t.kind
of tokenNested:
result &= "tokens{" & t.tokens.join(" ") & "}"
of tokenSingle:
result &= $t.content
else:
discard
proc tokenize2(text: string): seq[Token] =
proc rec(txt: string): tuple[res: seq[Token], length: int] =
var walk = 0
var bufr = newSeq[Token]()
var path = txt.len
while walk < path:
var t = txt[walk]
inc walk
case t
of ']':
return (bufr, walk)
of '[':
var (nest, leap) = rec(txt[walk .. ^1])
walk += leap
bufr.add Token(kind: tokenNested, tokens: nest)
of '>', '<', '+', '-', '.', ',':
bufr.add Token(kind: tokenSingle, content: t)
else:
discard
(bufr, walk)
var (res, length) = rec(text)
result = res
echo tokenize("+-<>[++--].,.")
echo tokenize2("+-<>[++--].,.")