diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-03 12:36:35 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-07-07 21:31:32 +0200 |
commit | cba504b509cd59f376063f6e590362b197147a2c (patch) | |
tree | 954e90b0053ab4c0247ef242607654c862d02e48 /module/c/line-fold.scm | |
parent | Merge branch 'new-object-system' into c-parser (diff) | |
download | calp-cba504b509cd59f376063f6e590362b197147a2c.tar.gz calp-cba504b509cd59f376063f6e590362b197147a2c.tar.xz |
Major work.
Diffstat (limited to '')
-rw-r--r-- | module/c/line-fold.scm | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/module/c/line-fold.scm b/module/c/line-fold.scm new file mode 100644 index 00000000..c61c2c70 --- /dev/null +++ b/module/c/line-fold.scm @@ -0,0 +1,29 @@ +(define-module (c line-fold) + :use-module (srfi srfi-1) + :use-module (srfi srfi-71) + :export (fold-lines)) + +(define (line-continued? line) + (and (not (string-null? line)) + (char=? #\\ (string-ref line (1- (string-length line)))))) + +(define (strip-backslash line) + (string-drop-right line 1)) + +(define (fold-lines string) + (with-output-to-string + (lambda () + (let loop ((lines (string-split string #\newline))) + (cond ((null? lines) 'NOOP) + ((null? (cdr lines)) + ;; TODO error message if last character is a backslash + (display (car lines)) + (newline)) + (else + (let ((to-merge remaining (span line-continued? lines))) + (for-each display (map strip-backslash to-merge)) + (display (car remaining)) + (newline) + (for-each (lambda _ (newline)) + (iota (length to-merge))) + (loop (cdr remaining))))))))) |