substitution

This commit is contained in:
Rutger van Beusekom 2017-05-15 23:14:54 +02:00
parent 2446a63281
commit f171f79ec9
4 changed files with 22 additions and 18 deletions

View File

@ -48,6 +48,7 @@
(define (display-help)
(display "\
gash [options]
-d, --debug Enable PEG tracing
-h, --help Display this help
-p, --parse Parse the shell script and print the parse tree
-v, --version Display the version
@ -195,7 +196,8 @@ the GNU Public License, see COPYING for the copyleft.
(('script term "&") (list (background (transform term))))
(('script term) `(,(transform term)))
(('script terms ...) (transform terms))
(('substitution "$(" (script ")")) (local-eval (cons 'substitute (cddr (car (transform script)))) (the-environment)))
(('substitution "$(" script ")") (local-eval (cons 'substitute (cddr (car (transform script)))) (the-environment)))
(('substitution "`" script "`") (local-eval (cons 'substitute (cddr (car (transform script)))) (the-environment)))
((('term command)) `(,(transform command)))
((('term command) ...) (map transform command))
((('term command) (('term commands) ...)) (map transform (cons command commands)))

View File

@ -2,7 +2,7 @@
:use-module (ice-9 peg)
:use-module (ice-9 peg codegen)
:export (parse))
:export (parse peg-trace?))
(define (error? x)
(let loop ((x x))
@ -62,7 +62,7 @@
function-body <-- compound-command io-redirect*
brace-group <-- '{' (sp* (compound-list / error) sp* '}' / error)
simple-command <-- (io-redirect sp+)* nonreserved (sp+ (io-redirect / nonreserved))*
reserved < ('case' / 'esac' / 'if' / 'fi' / 'then' / 'else' / 'elif' / 'for' / 'done' / 'do' / 'until' / 'while')
reserved < 'case' / 'esac' / 'if' / 'fi' / 'then' / 'else' / 'elif' / 'for' / 'done' / 'do' / 'until' / 'while'
nonreserved <- &(reserved word) word / !reserved word
io-redirect <-- [0-9]* sp* (io-here / io-file)
io-file <-- ('<&' / '>&' / '>>' / '>' / '<>'/ '<' / '>|') sp* ([0-9]+ / filename)
@ -77,18 +77,17 @@
test <-- ltest (!rtest .)* rtest
ltest < '[ '
rtest < ' ]'
substitution <-- ('$(' (script ')' / error)) / ('`' (script '`' / error))
substitution <-- ('$(' script ')') / ('`' script '`')
assignment <-- name assign (substitution / word)?
assign < '='
literal <-- (variable / delim / (![0-9] (![()] !io-op !sp !nl !break !pipe !assign .)+) / ([0-9]+ &separator)) literal*
literal <-- (variable / delim / (![0-9] (![()] !io-op !sp !nl !break !pipe !assign !bt !sq !dq .)+) / ([0-9]+ &separator)) literal*
variable <-- '$' ('$' / '*' / '@' / [0-9] / identifier / ([{] (![}] .)+ [}]))
delim <-- singlequotes / doublequotes / backticks
delim <-- singlequotes / doublequotes / substitution
sq < [']
dq < [\"]
bt < [`]
singlequotes <-- sq ((doublequotes / backticks / (!sq .))* sq)
doublequotes <-- dq ((singlequotes / backticks / (!dq .))* dq)
backticks <-- bt ((singlequotes / doublequotes / (!bt .))* bt)
singlequotes <-- sq (doublequotes / substitution / (!sq .))* sq
doublequotes <-- dq (singlequotes / substitution / (!dq .))* dq
separator <- (sp* break ws*) / ws+
break <- amp / semi !semi
sequential-sep <-- (semi !semi ws*) / ws+

View File

@ -83,16 +83,10 @@
;;(pipeline #f (list "ls" "/"))
;;(pipeline #f (list "ls" "/") (list "grep" "o") (list "tr" "o" "e"))
(define (read-n-format r)
(string-trim (string-map (lambda (c)
(if (eq? #\newline c) #\space c))
(read-string r))
#\space))
(define (substitute . commands)
(define (pipeline->string . commands)
(let* ((fg? #f)
(job (new-job))
(output (read-n-format
(output (read-string
(if (> (length commands) 1)
(let loop ((src (spawn-source fg? job (car commands)))
(commands (cdr commands)))
@ -104,4 +98,12 @@
(wait job)
output))
;;(display (substitute '("ls") '("cat"))) (newline)
(define (substitute . commands)
(string-trim-right
(string-map (lambda (c)
(if (eq? #\newline c) #\space c))
(apply pipeline->string commands))
#\space))
;; (display (pipeline->string '("ls") '("cat"))) (newline)
;; (display (substitute '("ls") '("cat"))) (newline)

View File

@ -1 +1,2 @@
echo $(find test -type f)
echo `find test -type f`