Skip to content

Commit

Permalink
move bindings into the expander to support submodule commands
Browse files Browse the repository at this point in the history
  • Loading branch information
countvajhula committed Jun 23, 2021
1 parent 108c3d6 commit 5da9a0c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
47 changes: 38 additions & 9 deletions expander.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,47 @@
racket/base)
syntax/parse
syntax/parse/define
racket/list)
racket/list
mischief/shorthand
version-case)

(version-case
[(version< (version) "7.9.0.22")
(define-alias define-syntax-parse-rule define-simple-macro)]
[else])

(provide run
command
help
help-clause ; not intended to be used directly -- use help instead
flag
cli-command
(rename-out (cli-module-begin #%module-begin))
(except-out (all-from-out racket/base)
#%module-begin)
#%top #%app #%datum #%top-interaction)

(define-syntax-parser cli-module-begin
[(_ EXPR ...)
(with-syntax ([~usage-help (datum->syntax this-syntax '~usage-help)]
[~help-labels (datum->syntax this-syntax '~help-labels)]
[~help-ps (datum->syntax this-syntax '~help-ps)]
[~once-each (datum->syntax this-syntax '~once-each)]
[~once-any (datum->syntax this-syntax '~once-any)]
[~multi (datum->syntax this-syntax '~multi)]
[~final (datum->syntax this-syntax '~final)])
#'(#%module-begin

(define ~usage-help (list ""))
(define ~help-labels (list ""))
(define ~help-ps (list ""))
(define ~once-each (list))
(define ~once-any (make-hash))
(define ~multi (list))
(define ~final (list))

EXPR ...))])

;; This is so that we can use the `help` macro
;; to encapsulate all help-related configuration
;; and still be able to modify source-location bindings
Expand Down Expand Up @@ -101,10 +130,6 @@
(cons (list 'name short-flag verbose-flag description handler #'handler)
~final))))])

(define-simple-macro (cli-module-begin EXPR ...)
(#%module-begin
EXPR ...))

(define (read-spec spec)
(list (list (second spec) (third spec))
;; the user would write a lambda without
Expand Down Expand Up @@ -178,7 +203,11 @@
...)
(list desc ...)))))])

(define-syntax-parser run
[(_ name)
#'(module+ main
(name (current-command-line-arguments)))])
(define-syntax-parse-rule (cli-command name:id body ...)
'(module name cli/expander
body ...
(provide name)))

(define-syntax-parse-rule (run name:id)
(module+ main
(name (current-command-line-arguments))))
4 changes: 3 additions & 1 deletion info.rkt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#lang info
(define collection "cli")
(define deps '("base"))
(define deps '("base"
"mischief"
"version-case"))
(define build-deps '("scribble-lib"
"scribble-abbrevs"
"racket-doc"
Expand Down
20 changes: 9 additions & 11 deletions reader.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
(provide read-syntax)

(define (read-syntax path port)
(define src-datums (sequence->list (in-port read port)))
(define module-datum `(module cli-mod cli/expander
(define ~usage-help (list ""))
(define ~help-labels (list ""))
(define ~help-ps (list ""))
(define ~once-each (list))
(define ~once-any (make-hash))
(define ~multi (list))
(define ~final (list))
;; this reads symexes from the source file
(define src-datums (sequence->list (in-port read port)))
(define module-datum `(module cli-mod cli/expander

,@src-datums))
(datum->syntax #f module-datum))
;; since the cli lang syntax is symex-oriented
;; we just use the read input directly.
;; the individual forms of the language will
;; be compiled by the expander
,@src-datums))
(datum->syntax #f module-datum))

0 comments on commit 5da9a0c

Please sign in to comment.