You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/basic-demo-3/expander.rkt

55 lines
1.8 KiB
Racket

#lang br/quicklang
(require "struct.rkt" "run.rkt" "elements.rkt" "setup.rkt")
(provide (rename-out [b-module-begin #%module-begin])
(all-from-out "elements.rkt"))
(define-macro (b-module-begin (b-program LINE ...))
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))]
[(VAR-ID ...) (find-property 'b-id #'(LINE ...))]
[(IMPORT-NAME ...)
(find-property 'b-import-name #'(LINE ...))]
[(EXPORT-NAME ...)
(find-property 'b-export-name #'(LINE ...))]
[((SHELL-ID SHELL-IDX) ...)
(for/list ([idx (in-range 10)])
(list (suffix-id #'arg idx #:context caller-stx)
idx))]
[(UNIQUE-ID ...)
(unique-ids
(syntax->list #'(VAR-ID ... SHELL-ID ...)))])
#'(#%module-begin
(module configure-runtime br
(require basic-demo-3/setup)
(do-setup!))
(require IMPORT-NAME) ...
(provide EXPORT-NAME ...)
(define UNIQUE-ID 0) ...
(let ([clargs (current-command-line-arguments)])
(set! SHELL-ID (get-clarg clargs SHELL-IDX)) ...)
LINE ...
(define line-table
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
(parameterize
([current-output-port (basic-output-port)])
(void (run line-table))))))
(define (get-clarg clargs idx)
(with-handlers ([exn:fail? (λ (exn) 0)])
(let ([val (vector-ref clargs idx)])
(or (string->number val) val))))
(begin-for-syntax
(require racket/list)
(define (unique-ids stxs)
(remove-duplicates stxs #:key syntax->datum))
(define (find-property which line-stxs)
(unique-ids
(for/list ([stx (in-list (stx-flatten line-stxs))]
#:when (syntax-property stx which))
stx))))