Skip to content

Instantly share code, notes, and snippets.

@athos
Created May 30, 2009 18:32
Show Gist options
  • Save athos/120598 to your computer and use it in GitHub Desktop.
Save athos/120598 to your computer and use it in GitHub Desktop.
this script enables gosh to call the external commands as a function without any definitions.
#!/usr/bin/env gosh
(use gauche.process)
(use srfi-1)
(use srfi-13)
(define (tree-filter f tree)
(define (rec t s)
(cond [(null? t) s]
[(pair? t) (rec (car t) (rec (cdr t) s))]
[(f t) (cons t s)]
[else s]))
(rec tree '()))
(define (command? sym)
(and (symbol? sym)
(string=? (string-take (symbol->string sym) 1) "!")))
(define (command-name com)
(string->symbol (string-drop (symbol->string com) 1)))
(define (exec-command command . args)
(process-output->string-list (cons command args)))
(define-macro (with-command . body)
(let1 commands (delete-duplicates (tree-filter command? body) eq?)
`(let ,(map (lambda (command)
`(,command
(cut exec-command ',(command-name command) <...>)))
commands)
,@body)))
(define (main args)
(define (reader)
(let1 exp (read)
(if (eof-object? exp)
exp
`(with-command ,exp))))
(read-eval-print-loop reader)
0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment