for-clause idiosynchrasies

This commit is contained in:
Rutger van Beusekom 2017-02-12 13:10:23 +01:00
parent b35876f4d9
commit a0b61a24df
2 changed files with 20 additions and 16 deletions

View File

@ -1,7 +1,4 @@
(define-module (sh anguish) (define-module (sh anguish)
;;:use-module (statprof)
:use-module (srfi srfi-1) :use-module (srfi srfi-1)
:use-module (srfi srfi-26) :use-module (srfi srfi-26)
@ -101,7 +98,6 @@ copyleft.
(with-readline-completion-function completion thunk) (with-readline-completion-function completion thunk)
(write-history HOME)) (write-history HOME))
(newline))))))) (newline)))))))
;;(statprof thunk #:hz 100 #:count-calls? #t)
(thunk))) (thunk)))
(define (remove-shell-comments s) (define (remove-shell-comments s)
@ -158,6 +154,11 @@ copyleft.
(list pattern))) (list pattern)))
(define (background ast)
(match ast
(('pipeline fg rest ...) `(pipeline #f ,@rest))
(_ ast)))
(define (builtin ast) (define (builtin ast)
(match ast (match ast
(('append ('glob "cd") arg) `(apply chdir ,arg)) (('append ('glob "cd") arg) `(apply chdir ,arg))
@ -170,13 +171,9 @@ copyleft.
(('glob "jobs") `(jobs)) (('glob "jobs") `(jobs))
(('for-each rest ...) ast) (('for-each rest ...) ast)
(('if rest ...) ast) (('if rest ...) ast)
(#t #t)
(_ #f))) (_ #f)))
(define (background ast)
(match ast
(('pipeline fg rest ...) `(pipeline #f ,@rest))
(_ ast)))
;; transform ast -> list of expr ;; transform ast -> list of expr
;; such that (map eval expr) ;; such that (map eval expr)
@ -189,9 +186,18 @@ copyleft.
((('term command) ...) (map transform command)) ((('term command) ...) (map transform command))
((('term command) (('term commands) ...)) (map transform (cons command commands))) ((('term command) (('term commands) ...)) (map transform (cons command commands)))
(('compound-list terms ...) (transform terms)) (('compound-list terms ...) (transform terms))
(('if-clause "if" (expression "then" consequent "fi")) `(if (equal? 0 (status:exit-val (begin ,@(transform expression)))) (begin ,@(transform consequent)))) (('if-clause "if" (expression "then" consequent "fi"))
(('if-clause "if" (expression "then" consequent ('else-part "else" alternative) "fi")) `(if (equal? 0 (status:exit-val ,@(transform expression))) (begin ,@(transform consequent)) (begin ,@(transform alternative)))) `(if (equal? 0 (status:exit-val ,@(transform expression)))
(('for-clause "for" ((identifier "in" lst sep) do-group)) `(for-each (lambda (,(string->symbol identifier)) (begin ,@(expand identifier (transform do-group)))) (glob ,(transform lst)))) (begin ,@(transform consequent))))
(('if-clause "if" (expression "then" consequent ('else-part "else" alternative) "fi"))
`(if (equal? 0 (status:exit-val ,@(transform expression)))
(begin ,@(transform consequent))
(begin ,@(transform alternative))))
(('for-clause ("for" identifier sep do-group)) #t)
(('for-clause "for" ((identifier "in" lst sep) do-group))
`(for-each (lambda (,(string->symbol identifier))
(begin ,@(expand identifier (transform do-group))))
(glob ,(transform lst))))
(('do-group "do" (command "done")) (transform command)) (('do-group "do" (command "done")) (transform command))
(('pipeline command) (let* ((command (transform command))) (or (builtin command) `(pipeline #t ,command)))) (('pipeline command) (let* ((command (transform command))) (or (builtin command) `(pipeline #t ,command))))
(('pipeline command piped-commands) `(pipeline #t ,(transform command) ,@(transform piped-commands))) (('pipeline command piped-commands) `(pipeline #t ,(transform command) ,@(transform piped-commands)))

View File

@ -40,9 +40,7 @@
"script <-- ws* (term (separator term)* separator?)? eof "script <-- ws* (term (separator term)* separator?)? eof
eof < !. / error eof < !. / error
error <-- .* error <-- .*
term <-- pipeline (sp* (and / or) ws* pipeline)* term <-- pipeline (sp* ('&&' / '||') ws* pipeline)*
and <-- '&&'
or <-- '||'
pipeline <-- '!'? sp* command (sp* pipe ws* command)* pipeline <-- '!'? sp* command (sp* pipe ws* command)*
pipe <-- '|' pipe <-- '|'
command <-- simple-command / (compound-command (sp+ io-redirect)*) / function-def command <-- simple-command / (compound-command (sp+ io-redirect)*) / function-def
@ -54,7 +52,7 @@
case-item <-- pattern (ne-compound-list? case-sep ws* / error) case-item <-- pattern (ne-compound-list? case-sep ws* / error)
case-sep < ';;' case-sep < ';;'
pattern <-- sp* word (sp* '|' sp* word)* sp* ')' sp* pattern <-- sp* word (sp* '|' sp* word)* sp* ')' sp*
for-clause <-- 'for' (sp+ identifier ws+ ('in' expression sequential-sep)? do-group / error) for-clause <-- 'for' (sp+ identifier (ws+ 'in' expression sequential-sep / sp* sequential-sep) do-group / error)
expression <-- sp+ substitution sp* / (sp+ word)* sp* expression <-- sp+ substitution sp* / (sp+ word)* sp*
do-group <-- 'do' ws* (ne-compound-list 'done' / error) do-group <-- 'do' ws* (ne-compound-list 'done' / error)
if-clause <-- 'if' (ne-compound-list 'then' ne-compound-list else-part? 'fi' / error) if-clause <-- 'if' (ne-compound-list 'then' ne-compound-list else-part? 'fi' / error)