lang-party / Summer2022

Lang Party 2022
24 stars 1 forks source link

Sew - Remove a file's outermost indentation levels #22

Open rocketnia opened 1 year ago

rocketnia commented 1 year ago

Sew

Sew is a very simple language extension to reduce one or two levels indentation at the outermost level of a file. Using #lang sew and 8<-plan-from-here makes it easy to add and refactor boilerplate that surrounds a whole file without having to change the whole file's indentation.

#lang sew racket

(require (only-in sew 8<-plan-from-here))

[8<-plan-from-here [<> ...]
  #'(begin
      (provide main)

      (define (main)
        <> ...))]

(displayln "Hello, world!")

The above is equivalent to this:

#lang racket

(define (main)
  (displayln "Hello, world!"))

But the former makes it a little easier to write the file as though it were an imperative script.

More sophisticated uses of #lang sew could break up a file into multiple sections using a syntax/parse pattern and then use the same section in more than one place. For instance, here's a file that exports its "main" code as both a function and a syntax object while also having a "test" section that becomes a test submodule:

#lang sew racket

(require (for-syntax (only-in syntax/parse expr)))

(require (only-in rackunit check-equal?))

(require (only-in sew 8<-plan-from-here))

[8<-plan-from-here [<main>:expr ... #:test <test>:expr ...]
  #'(begin
      (provide (for-syntax main-code))
      (provide main)

      (define-for-syntax main-code #'(begin <main> ...))
      (define (main)
        <main> ...)

      (module+ test
        <test> ...))]

(displayln "Hello, world!")

#:test

(check-equal? (+ 1 2) 3)

The Sew documentation goes into some more detail about usage. The Sew codebase is on GitHub.

License

Please confirm that the text in this issue is licensed under the Creative Commons Attribution 4.0 International License http://creativecommons.org/licenses/by/4.0/

Yes.

PS Your language can use any license.