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

View File

@ -40,9 +40,7 @@
"script <-- ws* (term (separator term)* separator?)? eof
eof < !. / error
error <-- .*
term <-- pipeline (sp* (and / or) ws* pipeline)*
and <-- '&&'
or <-- '||'
term <-- pipeline (sp* ('&&' / '||') ws* pipeline)*
pipeline <-- '!'? sp* command (sp* pipe ws* command)*
pipe <-- '|'
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-sep < ';;'
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*
do-group <-- 'do' ws* (ne-compound-list 'done' / error)
if-clause <-- 'if' (ne-compound-list 'then' ne-compound-list else-part? 'fi' / error)