I was wondering if it's possible to load the code contained in a Clojure .clj source file as a list, without compiling it.
If I can load a .clj file as a list, I can modify that list and pretty print it back into the same file which can then be loaded again.
(Maybe this is a bad idea.) Does anyone know if this is possible?
It is not a bad idea, it is one of the major properties of lisp, code is data. you can read the clj file as a list using read-string modify it and write it back.
(ns tmp
  (:require [clojure.zip :as zip])
  (:use clojure.contrib.pprint))
(def some-var true)
;;stolen from http://nakkaya.com/2011/06/29/ferret-an-experimental-clojure-compiler/
(defn morph-form [tree pred f]
  (loop [loc (zip/seq-zip tree)]
    (if (zip/end? loc)
      (zip/root loc)
      (recur
       (zip/next
        (if (pred (zip/node loc))
          (zip/replace loc (f (zip/node loc)))
          loc))))))
(let [morphed (morph-form (read-string (str \( (slurp "test.clj")\)))
                          #(or (= 'true %)
                               (= 'false %))
                          (fn [v] (if (= 'true v)
                                   'false
                                   'true)))]
  (spit "test.clj"
        (with-out-str
          (doseq [f morphed]
            (pprint f)))))
This reads itself and toggles boolean values and writes it back.
A slightly simpler example:
user=> (def a '(println (+ 1 1))) ; "'" escapes the form to prevent immediate evaluation
#'user/a
user=> (spit "test.code" a) ; write it to a file
nil
user=> (def from-file (read-string (slurp "test.code"))) ; read it from a file
#'user/from-file
user=> (def modified (clojure.walk/postwalk-replace {1 2} from-file)) ; modify the code
#'user/modified
user=> (spit "new.code" modified) ; write it back
nil
user=> (load-string (slurp "new.code")) ; check it worked!
4
nil
Where slurp gives you a string, read-string gives you an un-evaluated form, and load-string gives you the result of evaluating the form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With