Login
7 branches 0 tags
Ben (X13/Arch) Experiment with letting Claude write a Nujel reader in Nujel dfea60a 10 days ago 1257 Commits
nujel / binlib / tiny-repl.nuj
;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;; This project uses the MIT license, a copy should be included under /LICENSE
;;;
;;; A very simple REPL, only to be used in emergencies if for some reasons
;;; you can't use the full REPL module

(def tiny-repl (let*
                 (def ctx (environment*))

                 (defn exception-handler (error)
                       (print/error error))

                 (defn cmd/raw (line)
                       :export
                       (try (fn (err)
                                (if (= (car err) :unmatched-opening-bracket)
                                    (cmd/raw ctx (cat line (readline "... ")))
                                    (throw err)))
                            (def expr (read line))
                            (when (equal? '() expr)
                              (print "\r")
                              (return))
                            (try exception-handler
                                 (def result (eval-in ctx (cons do expr)))
                                 (display result)
                                 (newline))))

                 (defn cmd ()
                       (def buf "")
                       (def line "")
                       (while (not= (trim line) "[/cmd]")
                         (set! buf (cat buf line))
                         (set! line (readline)))
                       (try exception-handler
                            (def expr (cons do (read buf)))
                            (def result (eval-in ctx expr))
                            (display result)
                            (newline)))

                 (defn read-cmd ()
                       (def line (readline "> "))
                       (when (nil? line)
                         (println "Adios, cowboy...")
                         (exit 0))
                       (if (= (trim line) "[cmd]")
                           (cmd)
                           (cmd/raw line)))

                 (defn tiny-repl ()
                       (println "Nujel TinyREPL is ready for service!")
                       (while #t (read-cmd)))
))