diff --git a/gash/builtins.scm b/gash/builtins.scm index a57e060..851dd14 100644 --- a/gash/builtins.scm +++ b/gash/builtins.scm @@ -46,10 +46,12 @@ glob singlequotes doublequotes - expression + sequence + splice for + split substitution - sh-exec + script if-clause bg-command @@ -475,16 +477,23 @@ Options: (define (doublequotes . o) (string-join (append-map glob o) "")) -(define (expression . args) - (append-map glob args)) +(define (sequence . args) + (apply append args)) -(define (for name expr body) +(define (script . o) + o) + +(define (for name sequence body) (for-each (lambda (value) (assignment name value) - (body)) (expr))) + (body)) + (sequence))) -(define (substitution . commands) - (apply (@ (gash pipe) pipeline->string) (map cdr commands))) ;;HACK +(define (split o) + ((compose string-tokenize string-trim-right) o)) + +(define-syntax-rule (substitution commands) + (split (with-output-to-string (lambda _ commands)))) (define-syntax if-clause (lambda (x) diff --git a/gash/peg.scm b/gash/peg.scm index e3839c5..9cbbb3d 100644 --- a/gash/peg.scm +++ b/gash/peg.scm @@ -117,7 +117,8 @@ for-keyword < 'for' in-keyword < 'in' - for-clause <-- for-keyword sp+ name (ws+ in-keyword pipeline)? sp* sequential-sep do-group + for-clause <-- for-keyword sp+ name (ws+ in-keyword sequence)? sp* sequential-sep do-group + sequence <-- (sp+ word)+ do-keyword < 'do' done-keyword < 'done' do-group <- do-keyword ws* compound-list separator done-keyword @@ -202,11 +203,10 @@ (eq? o *unspecified*)) (define (transform ast) - (when (> %debug-level 1) + (when (> %debug-level -1) (format (current-error-port) "transform ast=~s\n" ast)) (match ast - (('script o ...) (map transform o)) - (('substitution o) `(substitution ,@(transform o))) + (('script o ...) `(script ,@(map transform o))) (('pipeline o) (pk `(pipeline ,(transform o)))) (('pipeline h t) (pk `(pipeline ,(transform h) ,@(map transform t)))) (('command o ...) `(command ,@(map transform o))) @@ -214,7 +214,17 @@ (('name o) o) (('number o) o) (('assignment a b) `(lambda _ (assignment ,(transform a) ,(transform b)))) - (('for-clause name expr do) `(for ,(transform name) (lambda _ ,(transform expr)) (lambda _ ,(transform do)))) + (('for-clause name expr do) + `(for ,(transform name) (lambda _ ,(transform expr)) (lambda _ ,(transform do)))) + (('sequence o ...) + `(sequence ,@(fold-right (lambda (o r) + (cons + (match o + (('substitution x) (transform o)) + (_ `(list ,(transform o)))) + r)) + '() o))) + (('substitution o) `(substitution ,(transform o))) (('if-clause expr then) `(if-clause ,(transform expr) ,(transform then))) (('if-clause expr then else) `(if-clause ,(transform expr) ,(transform then) ,(transform else))) (('then-part o ...) `(begin ,@(map transform o)))