Login
7 branches 0 tags
Ben (X220/Parabola) Keywords are now a separate type aa362f9 3 years ago 453 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"]

[defun help [i]
       "Describe 10 functions at offset 1"
       [when-not [int? i] [set! i [int i]]]
       [def off [* i 10]]
       [for-each [map [symbol-table off 10] string]
                 [\ [l] [def desc [describe l]]
                        [pfmtln "{l} {desc}"]]]
       [def sc [/ [symbol-count] 10]]
       [pfmtln "Help page {i} of {sc}"]]

[defun file/compile [path no-write environment]
        "Compile a Nujel source file into optimized object code"
        [def source [cons 'do [read [file/read path]]]]
        [def object-code [compile/forms source environment]]
        [when-not no-write [file/write [cat [path/without-extension path] ".no"]
                                       [if object-code [str/write object-code] ""]]]
        object-code]

[defun file/eval [path environment]
       "Evaluate a Nujel source file in the current context"
       [when-not environmnet [set! environment root-closure]]
       [def source [cons 'do [read [file/read path]]]]
       [compile/forms [cons 'do source] environment]]

[defun eval/forked [nujel-binary raw-expr]
        "Evaluate @EXPR in a separate process running NUJEL-BINARY"
        [def expr [cat "[print [str/write " [str/write raw-expr]] "]] "]
        [def tmp-path [file/temp expr]]

        [def command [cat nujel-binary " " tmp-path]]
        [def res [popen command]]
        [file/remove tmp-path]
        [cons [car res] [cdr res]]]

[defun file/file? [filename]
       [tree/get [file/stat filename] :regular-file?]]

[defun file/dir? [filename]
       [tree/get [file/stat filename] :directory?]]