Demo entry 6358207

pthy

   

Submitted by Eduardo Padoan on Apr 24, 2017 at 23:15
Language: Hy. Code size: 1.2 kB.

(import [traceback [format-exc]])

(import [prompt-toolkit.shortcuts [prompt print-tokens]])
(import [prompt-toolkit.history [InMemoryHistory]])
(import [prompt-toolkit.styles [style-from-pygments]])

(import [pygments.lexers.lisp [HyLexer]])
(import [pygments.lexers.python [Python3TracebackLexer]])
(import [pygments.styles.emacs [EmacsStyle]])


(defn hy-prompt [history]
  "Display the prompt and wait user input"
  ;; TODO: prompt customization, completion
  (prompt "> "
          :history history
          :lexer HyLexer
          :style (style-from-pygments EmacsStyle)))

(defn print-exception [lexer]
  "Display the exception in pretty colors"
  (-> (format-exc) (lexer.get-tokens) (print-tokens)))

(defmain [&rest args]
  (setv history (InMemoryHistory))
  (setv exception-lexer (Python3TracebackLexer))

  ; read-eval-print loop
  (while True
    (try
     (-> (hy-prompt history)
         (read-str)
         (eval)
         (print))
     ; Ctrl-C
     (except [err KeyboardInterrupt] (print "KeyboardInterrupt"))
     ; Ctrl-D
     (except [err EOFError] (break))
     ; Report errors
     (except [err Exception] (print-exception exception-lexer))))

  (print "Bye!"))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).