|
| 1 | +#lang racket/base |
| 2 | + |
| 3 | + |
| 4 | +(require racket/contract/base) |
| 5 | + |
| 6 | + |
| 7 | +(provide |
| 8 | + (contract-out |
| 9 | + [fuse-map-with-for refactoring-suite?])) |
| 10 | + |
| 11 | + |
| 12 | +(require resyntax/base |
| 13 | + resyntax/default-recommendations/analyzers/identifier-usage |
| 14 | + resyntax/default-recommendations/let-replacement/private/let-binding |
| 15 | + resyntax/default-recommendations/private/lambda-by-any-name |
| 16 | + syntax/parse) |
| 17 | + |
| 18 | + |
| 19 | +;@---------------------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | + |
| 22 | +;; A short lambda suitable for fusing with a for loop. Needs both single-attribute and |
| 23 | +;; multi-attribute versions of the body for different template contexts. |
| 24 | +(define-syntax-class fuseable-map-lambda |
| 25 | + #:attributes (x single-body [multi-body 1]) |
| 26 | + |
| 27 | + ;; Lambdas with let expressions that can be refactored - must come first as it's most specific |
| 28 | + (pattern |
| 29 | + (_:lambda-by-any-name (x:id) |
| 30 | + original-body:body-with-refactorable-let-expression) |
| 31 | + #:attr [multi-body 1] (attribute original-body.refactored) |
| 32 | + ;; For single body, we need to wrap multiple forms in a begin |
| 33 | + #:attr single-body #'(begin original-body.refactored ...)) |
| 34 | + |
| 35 | + ;; Lambdas with multiple body forms (two or more) |
| 36 | + (pattern (_:lambda-by-any-name (x:id) first-form second-form rest-form ...) |
| 37 | + #:attr [multi-body 1] (cons (attribute first-form) |
| 38 | + (cons (attribute second-form) (attribute rest-form))) |
| 39 | + #:attr single-body #'(begin first-form second-form rest-form ...)) |
| 40 | + |
| 41 | + ;; Short lambdas with a single body form |
| 42 | + (pattern (_:lambda-by-any-name (x:id) only-form) |
| 43 | + #:attr [multi-body 1] (list (attribute only-form)) |
| 44 | + #:attr single-body #'only-form)) |
| 45 | + |
| 46 | + |
| 47 | +(define-definition-context-refactoring-rule fuse-map-with-for-rule |
| 48 | + #:description |
| 49 | + "A `map` expression producing a list for a `for` loop can be fused with the loop." |
| 50 | + #:analyzers (list identifier-usage-analyzer) |
| 51 | + #:literals (define map in-list for for*) |
| 52 | + (~seq body-before ... |
| 53 | + (define ys:id (map function:fuseable-map-lambda list-expr:expr)) |
| 54 | + ((~or for-id:for for-id:for*) |
| 55 | + (~and original-clauses |
| 56 | + ([y-var:id (in-list ys-usage:id)] remaining-clause ...+)) |
| 57 | + for-body ...) |
| 58 | + body-after ...) |
| 59 | + |
| 60 | + ;; Check that ys is only used in the for loop, not elsewhere |
| 61 | + #:when (free-identifier=? (attribute ys) (attribute ys-usage)) |
| 62 | + #:when (equal? (syntax-property #'ys 'usage-count) 1) |
| 63 | + |
| 64 | + ;; Generate the refactored code - fuse as nested clauses |
| 65 | + (body-before ... |
| 66 | + (for-id ([function.x (in-list list-expr)] |
| 67 | + [y-var (in-list (function.multi-body ...))] |
| 68 | + remaining-clause ...) |
| 69 | + for-body ...) |
| 70 | + body-after ...)) |
| 71 | + |
| 72 | + |
| 73 | +;; Rule for when there are no remaining clauses - use internal definition |
| 74 | +(define-definition-context-refactoring-rule fuse-map-with-for-single-clause-rule |
| 75 | + #:description |
| 76 | + "A `map` expression producing a list for a `for` loop can be fused with the loop." |
| 77 | + #:analyzers (list identifier-usage-analyzer) |
| 78 | + #:literals (define map in-list for for*) |
| 79 | + (~seq body-before ... |
| 80 | + (define ys:id (map function:fuseable-map-lambda list-expr:expr)) |
| 81 | + ((~or for-id:for for-id:for*) |
| 82 | + (~and original-clauses |
| 83 | + ([y-var:id (in-list ys-usage:id)])) |
| 84 | + for-body ...) |
| 85 | + body-after ...) |
| 86 | + |
| 87 | + ;; Check that ys is only used in the for loop, not elsewhere |
| 88 | + #:when (free-identifier=? (attribute ys) (attribute ys-usage)) |
| 89 | + #:when (equal? (syntax-property #'ys 'usage-count) 1) |
| 90 | + |
| 91 | + ;; Generate the refactored code - use internal definition |
| 92 | + (body-before ... |
| 93 | + (for-id ([function.x (in-list list-expr)]) |
| 94 | + (define y-var function.single-body) |
| 95 | + for-body ...) |
| 96 | + body-after ...)) |
| 97 | + |
| 98 | + |
| 99 | +(define-refactoring-suite fuse-map-with-for |
| 100 | + #:rules (fuse-map-with-for-rule |
| 101 | + fuse-map-with-for-single-clause-rule)) |
0 commit comments