|
| 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 | + racket/list |
| 14 | + resyntax/default-recommendations/analyzers/identifier-usage |
| 15 | + resyntax/default-recommendations/let-replacement/private/let-binding |
| 16 | + resyntax/default-recommendations/private/lambda-by-any-name |
| 17 | + syntax/parse) |
| 18 | + |
| 19 | + |
| 20 | +;@---------------------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | + |
| 23 | +;; A short lambda suitable for fusing with a for loop. For multi-body lambdas, we need to |
| 24 | +;; separate the prefix forms (all but last) from the result form (the last). |
| 25 | +(define-syntax-class fuseable-map-lambda |
| 26 | + #:attributes (x single-body [multi-body 1] [prefix-forms 1] result-form) |
| 27 | + |
| 28 | + ;; Lambdas with let expressions that can be refactored |
| 29 | + (pattern |
| 30 | + (_:lambda-by-any-name (x:id) |
| 31 | + original-body:body-with-refactorable-let-expression) |
| 32 | + #:with (multi-body ...) #'(original-body.refactored ...) |
| 33 | + #:do [(define refactored-forms (attribute original-body.refactored)) |
| 34 | + (define prefix-list (if (null? refactored-forms) '() (drop-right refactored-forms 1))) |
| 35 | + (define result (if (null? refactored-forms) #'(begin) (last refactored-forms)))] |
| 36 | + #:attr [prefix-forms 1] prefix-list |
| 37 | + #:attr result-form result |
| 38 | + #:attr single-body #'(begin original-body.refactored ...)) |
| 39 | + |
| 40 | + ;; Lambdas with multiple body forms (two or more) |
| 41 | + (pattern (_:lambda-by-any-name (x:id) prefix-form ... last-form) |
| 42 | + #:when (not (null? (attribute prefix-form))) |
| 43 | + #:with (multi-body ...) #'(prefix-form ... last-form) |
| 44 | + #:attr [prefix-forms 1] (attribute prefix-form) |
| 45 | + #:attr result-form #'last-form |
| 46 | + #:attr single-body #'(begin prefix-form ... last-form)) |
| 47 | + |
| 48 | + ;; Short lambdas with a single body form |
| 49 | + (pattern (_:lambda-by-any-name (x:id) only-form) |
| 50 | + #:with (multi-body ...) #'(only-form) |
| 51 | + #:attr [prefix-forms 1] '() |
| 52 | + #:attr result-form #'only-form |
| 53 | + #:attr single-body #'only-form)) |
| 54 | + |
| 55 | + |
| 56 | +(define-definition-context-refactoring-rule fuse-map-with-for*-rule |
| 57 | + #:description |
| 58 | + "A `map` expression producing a list for a `for*` loop can be fused with the loop." |
| 59 | + #:analyzers (list identifier-usage-analyzer) |
| 60 | + #:literals (define map in-list for*) |
| 61 | + (~seq body-before ... |
| 62 | + (define ys:id (map function:fuseable-map-lambda list-expr:expr)) |
| 63 | + (for*-id:for* |
| 64 | + (~and original-clauses |
| 65 | + ([y-var:id (in-list ys-usage:id)] remaining-clause ...)) |
| 66 | + for-body ...) |
| 67 | + body-after ...) |
| 68 | + |
| 69 | + ;; Check that ys is only used in the for loop, not elsewhere |
| 70 | + #:when (free-identifier=? (attribute ys) (attribute ys-usage)) |
| 71 | + #:when (equal? (syntax-property #'ys 'usage-count) 1) |
| 72 | + |
| 73 | + ;; Generate the refactored code - fuse as nested clauses |
| 74 | + (body-before ... |
| 75 | + (for*-id ([function.x (in-list list-expr)] |
| 76 | + [y-var (in-list function.single-body)] |
| 77 | + remaining-clause ...) |
| 78 | + for-body ...) |
| 79 | + body-after ...)) |
| 80 | + |
| 81 | + |
| 82 | +;; Rule for when there are single-clause for loops (not for*) - use internal definition |
| 83 | +(define-definition-context-refactoring-rule fuse-map-with-for-single-clause-rule |
| 84 | + #:description |
| 85 | + "A `map` expression producing a list for a single-clause `for` loop can be fused with the loop." |
| 86 | + #:analyzers (list identifier-usage-analyzer) |
| 87 | + #:literals (define map in-list for) |
| 88 | + (~seq body-before ... |
| 89 | + (define ys:id (map function:fuseable-map-lambda list-expr:expr)) |
| 90 | + (for-id:for |
| 91 | + (~and original-clauses |
| 92 | + ([y-var:id (in-list ys-usage:id)])) |
| 93 | + for-body ...) |
| 94 | + body-after ...) |
| 95 | + |
| 96 | + ;; Check that ys is only used in the for loop, not elsewhere |
| 97 | + #:when (free-identifier=? (attribute ys) (attribute ys-usage)) |
| 98 | + #:when (equal? (syntax-property #'ys 'usage-count) 1) |
| 99 | + |
| 100 | + ;; Generate the refactored code - use internal definition |
| 101 | + (body-before ... |
| 102 | + (for-id ([function.x (in-list list-expr)]) |
| 103 | + function.prefix-forms ... |
| 104 | + (define y-var function.result-form) |
| 105 | + for-body ...) |
| 106 | + body-after ...)) |
| 107 | + |
| 108 | + |
| 109 | +(define-refactoring-suite fuse-map-with-for |
| 110 | + #:rules (fuse-map-with-for*-rule |
| 111 | + fuse-map-with-for-single-clause-rule)) |
0 commit comments