~bouncepaw/agidel-stdlib

e7cc47f50086ea27a9b0c264a261db929112af26 — Timur Ismagilov 5 years ago af2a778
Reimplement existing functions in Agidel/sh using functions
1 files changed, 25 insertions(+), 28 deletions(-)

M sh.scm
M sh.scm => sh.scm +25 -28
@@ 1,38 1,35 @@
(module
 agidel-plugin.sh
 (shebang! str set for-each-cli-arg _bracket)
 (import (prefix scheme -)
 (import (rename (prefix scheme -) (-define define)
                 (-define-syntax define-syntax))
         (prefix chicken.base -)
         (prefix srfi-13 -)
         matchable
         format)

 (-define (str lst)
          (-if (-list? lst)
               (-map (-lambda (x) (-if (-string? x)
                                       (-string-append "\"" x "\"")
                                       (-symbol->string x)))
                     lst)
               (-car (str (-list lst)))))
 (define (str lst)
   (-if (-list? lst)
        (-map (-lambda (x) (-if (-string? x)
                                (-string-append "\"" x "\"")
                                (-symbol->string x)))
              lst)
        (-car (str (-list lst)))))

 (-define-syntax
  shebang!
  (syntax-rules ()
    ((_) "#!/bin/sh\n")
    ((_ path) (format "#!~A\n" path))))
 (define shebang!
   (match-lambda*
    (() "#!/bin/sh\n")
    ((path (format "#!~A\n" path)))))

 (-define-syntax
  set
  (syntax-rules ()
    ((_ var val) (format "~A=~A\n" (str 'var) (str 'val)))
    ((_ var oper val) (format "~A~A=~A\n" (str 'var) (str 'oper) (str 'val)))))
 (define set
   (match-lambda*
    ((var val) (format "~A=~A\n" (str var) (str val)))
    ((var oper val) (format "~A~A=~A\n" (str var) (str oper) (str val)))))

 (-define-syntax
  for-each-cli-arg
  (syntax-rules ()
    ((_ var expr* ...) (format "for ~A in \"$@\"; do\n~A\ndone\n"
                             'var
                             (-apply -string-append (-list expr* ...))))))
 (-define-syntax
  _bracket
  (syntax-rules ()
    ((_ expr* ...) (format "~A\n" (-string-join (str (-list 'expr* ...)) " "))))))
 (define (for-each-cli-arg var . exprs)
   (format "for ~A in \"$@\"; do\n~A\ndone\n"
           var
           (-apply -string-append exprs)))

 (define (_bracket . exprs)
   (format "~A\n" (-string-join (str exprs)))))