anchor an if's arms only where the if is a statement
Every if had its arms anchored, including one inside an argument that the writer renders as ?:, so a #line landed between its parts and the C did not compile. -C output, with no directives, hid it. Fixes #54.
This commit is contained in:
@@ -46,10 +46,10 @@ source location. Only statement positions may be walked this way: a
|
|||||||
(append-map (lambda (s)
|
(append-map (lambda (s)
|
||||||
(let ((src (form-source s)))
|
(let ((src (form-source s)))
|
||||||
(if src
|
(if src
|
||||||
(list (line-directive src) (walk-expr s))
|
(list (line-directive src) (walk-statement s))
|
||||||
(list (walk-expr s)))))
|
(list (walk-statement s)))))
|
||||||
(pack-comments stmts))
|
(pack-comments stmts))
|
||||||
(map walk-expr (pack-comments stmts))))
|
(map walk-statement (pack-comments stmts))))
|
||||||
|
|
||||||
(define (walk-stmt s)
|
(define (walk-stmt s)
|
||||||
"A statement in a slot that holds exactly one form -- an `if' arm.
|
"A statement in a slot that holds exactly one form -- an `if' arm.
|
||||||
@@ -59,8 +59,18 @@ as an `else if' chain, so the anchor and the statement are wrapped in
|
|||||||
surrounding c-block supplies them)."
|
surrounding c-block supplies them)."
|
||||||
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
||||||
(if src
|
(if src
|
||||||
`(%begin ,(line-directive src) ,(walk-expr s))
|
`(%begin ,(line-directive src) ,(walk-statement s))
|
||||||
(walk-expr s))))
|
(walk-statement s))))
|
||||||
|
|
||||||
|
;;; Only here is an `if' a statement, with statements for arms that can
|
||||||
|
;;; each be anchored. Anywhere else it is `?:', and a #line between its
|
||||||
|
;;; parts does not compile
|
||||||
|
(define (walk-statement s)
|
||||||
|
(match s
|
||||||
|
(('if . clauses)
|
||||||
|
(with-comments (filter comment-form? clauses)
|
||||||
|
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
||||||
|
(_ (walk-expr s))))
|
||||||
|
|
||||||
;;; A `;' comment reads as a form, so one written inside a construct
|
;;; A `;' comment reads as a form, so one written inside a construct
|
||||||
;;; with positional slots lands in a slot and shifts everything after
|
;;; with positional slots lands in a slot and shifts everything after
|
||||||
@@ -229,9 +239,7 @@ forms, and what remains."
|
|||||||
;; and each is spliced or wrapped according to what the
|
;; and each is spliced or wrapped according to what the
|
||||||
;; corresponding fmt-c procedure accepts.
|
;; corresponding fmt-c procedure accepts.
|
||||||
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
||||||
(('if . clauses)
|
(('if . clauses) (cons 'if (map walk-expr (remove comment-form? clauses))))
|
||||||
(with-comments (filter comment-form? clauses)
|
|
||||||
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
|
||||||
(('while . rest)
|
(('while . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
|
|||||||
@@ -86,7 +86,10 @@
|
|||||||
" (: done)" ; 52
|
" (: done)" ; 52
|
||||||
" (var svar u64 (sizeof (struct pt)))" ; 53
|
" (var svar u64 (sizeof (struct pt)))" ; 53
|
||||||
" (var cvar int (cast mvar int))" ; 54
|
" (var cvar int (cast mvar int))" ; 54
|
||||||
" (return 122))" ; 55
|
" (putchar (if (< mvar 123)" ; 55 a conditional as an
|
||||||
|
" (putchar 124)" ; 56 argument: an expression,
|
||||||
|
" 125))" ; 57 so no anchor inside it
|
||||||
|
" (return 122))" ; 58
|
||||||
))
|
))
|
||||||
|
|
||||||
(define fixture (string-intersperse fixture-lines "\n"))
|
(define fixture (string-intersperse fixture-lines "\n"))
|
||||||
@@ -231,7 +234,14 @@ after that is one more. Lines before the first directive get #f."
|
|||||||
;; Expressions that are their own statement
|
;; Expressions that are their own statement
|
||||||
(test-group "expressions"
|
(test-group "expressions"
|
||||||
(maps/tokens "sizeof" "(var svar" "sizeof")
|
(maps/tokens "sizeof" "(var svar" "sizeof")
|
||||||
(maps/tokens "cast" "(var cvar" "(int)mvar"))
|
(maps/tokens "cast" "(var cvar" "(int)mvar")
|
||||||
|
;; an `if' arm is a statement only where the `if' is one: a
|
||||||
|
;; directive between `?' and `:' does not compile
|
||||||
|
(maps "conditional argument" "123")
|
||||||
|
(test-assert "holds no directive"
|
||||||
|
(let ((start (string-contains c-out "123")))
|
||||||
|
(not (string-contains (substring c-out start (string-contains c-out "125" start))
|
||||||
|
"#line")))))
|
||||||
|
|
||||||
;; A location is (file . line); every directive must name the file the
|
;; A location is (file . line); every directive must name the file the
|
||||||
;; form was read from.
|
;; form was read from.
|
||||||
|
|||||||
Reference in New Issue
Block a user