0
\$\begingroup\$

I wrote a tokenizer but it's not very readable. How can I make it more readable?

import std/os
import std/streams
import std/deques

var strm = newFileStream(paramStr(1), fmRead)
var stack = initDeque[string]()
var arr = newSeq[string]()

proc startEnclosure(token: string) {.discardable.} =
  echo token
  stack.addFirst(token)
  arr.insert(token, 0)

proc endEnclosure(token: string) {.discardable.} =
  echo token
  discard stack.popFirst
  arr.insert(token, 0)

if not isNil(strm):
  while true:
    var s = strm.readChar()
    
    if s == '(':
      startEnclosure("<LIST START>")

    elif s == '#':
      if strm.peekChar() == '(':
        startEnclosure("<LAMBDA START>")
      elif strm.peekChar() == '{':
        startEnclosure("<SET START>")

    elif s == ')':
      if stack.peekFirst == "<LIST START>":
        endEnclosure("<LIST END>")
      elif stack.peekFirst == "<LAMBDA START>":
        endEnclosure("<LAMBDA END>")

    elif s == '[':
      startEnclosure("<VECTOR START>")

    elif s == ']':
      endEnclosure("<VECTOR END>")

    elif s == '{':
      startEnclosure("<MAP START>")

    elif s == '}':
      if stack.peekFirst == "<SET START>":
        endEnclosure("<SET END>")
      elif stack.peekFirst == "<MAP START>":
        endEnclosure("<MAP END>")
      
    else:
      echo s
    
    if strm.peekChar() == '\x00':
      break

  strm.close()
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Do you have a sample input? \$\endgroup\$
    – ggorlen
    Commented Mar 1 at 23:14
  • \$\begingroup\$ @ggorlen (+ (* [7 3 5] 6 2) 9 2) \$\endgroup\$ Commented Mar 2 at 14:39
  • 1
    \$\begingroup\$ Thanks. Can you edit that into your post? I suggest changing the title to specify "lisp expression parser" or something like that so people understand the purpose of the code. \$\endgroup\$
    – ggorlen
    Commented Mar 2 at 18:13
  • \$\begingroup\$ @ggorlen Thank you for the advice \$\endgroup\$ Commented Mar 2 at 22:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.