(define steps-to-preview
  (fn
    (all-steps target)
    (if
      (or (empty? all-steps) (<= target 0))
      nil
      (let
        ((pos (dict "i" 0)) (max-i (min target (len all-steps))))
        (letrec
          ((bc-loop (fn (children) (if (>= (get pos "i") max-i) children (let ((step (nth all-steps (get pos "i"))) (stype (get step "type"))) (cond (= stype "open") (do (dict-set! pos "i" (+ (get pos "i") 1)) (let ((tag (get step "tag")) (attrs (or (get step "attrs") (list))) (spreads (or (get step "spreads") (list))) (inner (bc-loop (list)))) (append! children (concat (list (make-symbol tag)) spreads attrs inner))) (bc-loop children)) (= stype "close") (do (dict-set! pos "i" (+ (get pos "i") 1)) children) (= stype "leaf") (do (dict-set! pos "i" (+ (get pos "i") 1)) (append! children (get step "expr")) (bc-loop children)) (= stype "expr") (do (dict-set! pos "i" (+ (get pos "i") 1)) (append! children (get step "expr")) (bc-loop children)) :else (do (dict-set! pos "i" (+ (get pos "i") 1)) (bc-loop children))))))))
          (let
            ((root (bc-loop (list))))
            (cond
              (= (len root) 1)
              (first root)
              (empty? root)
              nil
              :else (concat (list (make-symbol "<>")) root))))))))
