Login
7 branches 0 tags
Ben (X13/Arch) Added [dotimes] from CL/Clojure e4c7e8a 3 years ago 709 Commits
nujel / binlib / io.nuj
; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
; This project uses the MIT license, a copy should be included under /LICENSE

[def test-context "Nujel Standalone"]

[defn file/eval [path environment]
       "Evaluate a Nujel source file in the current context"
       [when-not environment [set! environment root-closure]]
       [doseq [form [read [file/read path]]]
              [eval-in environment form]]]

[defn file/compile [path environment]
      "Compile a Nujel source file into optimized object code"
      [when-not environment [set! environment [environment*]]]
      [def source [cons 'do [read [file/read path]]]]
      [def object-code [compile* source environment]]
      [file/write [if object-code [string/write object-code] ""]
                  [cat [path/without-extension path] ".no"]]
      [return object-code]]

[defn file/compile/argv []
      [file/compile [car [last-pair repl/args]]]]

[defn load [filename]
  [try [fn [err]
         [display/error err]
         #f]
       [file/eval filename]
       [pfmtln "Loaded {filename}"]
       #t]]

[defn file/file? [filename]
       [ref [file/stat filename] :regular-file?]]

[defn file/dir? [filename]
       [ref [file/stat filename] :directory?]]

[defn directory/read-relative [path]
      [map [directory/read path]
           [fn [a]
               [cat path "/" a]]]]

[defn directory/read-recursive [path]
       [flatten [map [directory/read-relative path]
                     [fn [A] [if [file/dir? A]
                                 [directory/read-recursive A]
                                 A]]]]]

[defn popen/trim [cmd]
  "Run CMD using popen and return the trimmed stdout"
  [trim [cdr [popen cmd]]]]

[def +root-working-dir+ [path/working-directory]]

[defn file/module-loader [name]
      [def module-path [fmt "{+root-working-dir+}/modules/{}.nuj" [keyword->string name]]]
      [def source [file/read module-path]]
      [when-not source [return #nil]]
      [def expr [list 'module [cons do [read source]]]]
      [def mod [eval expr]]
      [return mod]]

[module/add-loader file/module-loader]