Login
7 branches 0 tags
Ben (Win10) [str/length] -> [string/length], and pad-start, pad-end 6dc9f24 4 years ago 195 Commits
nujel / bootstrap / binlib.no
[do [def help [let* [do [def iter [λ* [l]
           ""
           [cond [[nil? l] #t]
                 [#t [do [display [describe [car l]]]
                    [newline]
                    [iter [cdr l]]]]]]]
       [λ* [i]
           "Describe 10 functions at offset 1"
           [do [def off [* [int i] 10]]
              [iter [map cat [symbol-table off 10]]]
              [display [cat "Help page " [int i] " of " [/ [symbol-count] 10]]]
              [newline]
              #t]]]]]
    [def file/compile [λ* [path no-write]
        "Compile a Nujel source file into optimized object code"
        [do [def source [read [file/read path]]]
           [def object-code [compile [cons 'do source] source]]
           [if no-write
              #nil
              [file/write [cat [path/without-extension path] ".no"] [str/write object-code]]]
           object-code]]]
    [def file/eval [λ* [path]
        "Evaluate a Nujel source file in the current context"
        [do [def source [cons 'do [read [file/read path]]]]
           [eval* [compile source]]]]]
    [def eval/forked [λ* [nujel-binary raw-expr]
        "Evaluate @EXPR in a separate process running NUJEL-BINARY"
        [do [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]]]]]
    [def test-context "Nujel Bootstrap"]][do [def ctx [current-closure]]
    [def display/error [let* [do [def wrap [λ* [i text]
           ""
           [cond [[eq? i 0] [ansi-red text]]
                 [[eq? i 1] [string text]]
                 [[eq? i 2] [ansi-yellow [str/write text]]]
                 [#t text]]]]
       [def iter [λ* [error i]
           ""
           [if error
              [cons [wrap i [car error]] [iter [cdr error] [++ i]]]
              [cons "" #nil]]]]
       [λ* [error]
           "Display ERROR in a nice, human readable way"
           [display [join [iter error 0] "\r\n"]]]]]]
    [def repl/exception-handler [λ* [error]
        ""
        [display/error error]]]
    [def repl/history #nil]
    [def repl/prompt [λ* []
        "> "
        "> "]]
    [def repl/wasm [let* [λ* [line]
        "Evaluate LINE in the wasm context"
        [try repl/exception-handler [do [def raw [read line]]
           [def expr [cons 'do raw]]
           [def cexpr [cons [compile expr]]]
           [apply ctx cexpr]]]]]]
    [def repl/readline [λ* [ctx]
        ""
        [do [def line [readline [repl/prompt]]]
           [if [nil? line]
              [do [display "Bye!\r\n"]
                 [exit 0]]
              #nil]
           [readline/history/add line]
           [println line]
           [def raw [read line]]
           [def expr [cons 'do raw]]
           [def cexpr [cons [compile expr]]]
           [def result [apply ctx cexpr]]
           [set! repl/history [cons line repl/history]]
           [display [cat [if [nil? result]
              ""
              [str/write result]] "\n"]]]]]
    [def repl [λ* []
        ""
        [do [readline/history/load [readline/history/path]]
           [while #t [do [try repl/exception-handler [repl/readline ctx]]
              [readline/history/save [readline/history/path]]]]]]]][if [eq? OS "Emscripten"]
    #nil
    [do [test/add* '[0 . ""] '[do [popen "true"]]]
       [test/add* #f '[do [zero? [car [popen "false"]]]]]
       [test/add* '[0 . "testerle\n"] '[do [popen "echo testerle"]]]]]