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/expander.rkt

62 lines
2.0 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
(provide (rename-out [b-module-begin #%module-begin])
(matching-identifiers-out #rx"^b-" (all-defined-out)))
9 years ago
8 years ago
(define-macro (b-module-begin (b-program LINE ...))
8 years ago
(with-pattern
([(LINE-NUM ...)
(filter-stx-prop 'b-line-number
(stx-flatten #'(LINE ...)))]
[(LINE-ID ...) (prefix-ids "line-" #'(LINE-NUM ...))])
8 years ago
#'(#%module-begin
LINE ...
8 years ago
(define line-table
(apply hasheqv (append (list LINE-NUM LINE-ID) ...)))
8 years ago
(run line-table))))
8 years ago
8 years ago
(define-macro (b-line LINE-NUMBER STATEMENT ...)
8 years ago
(with-pattern
([LINE-NUMBER-ID (prefix-id "line-" #'LINE-NUMBER
#:source #'LINE-NUMBER)]
[ORIG-LOC caller-stx])
8 years ago
(syntax/loc caller-stx
(define (LINE-NUMBER-ID #:srcloc? [loc #f])
(if loc
8 years ago
(syntax-srcloc #'ORIG-LOC)
(begin (void) STATEMENT ...))))))
8 years ago
8 years ago
(define b-rem void)
(define (b-print [val ""]) (displayln val))
(define (b-sum . nums) (apply + nums))
8 years ago
(define (b-num-expr expr)
8 years ago
(if (integer? expr) (inexact->exact expr) expr))
8 years ago
8 years ago
(struct $program-end-signal ())
(define (b-end) (raise ($program-end-signal)))
8 years ago
8 years ago
(struct $change-line-signal (val))
8 years ago
(define (b-goto expr) (raise ($change-line-signal expr)))
8 years ago
8 years ago
(define-exn line-not-found exn:fail)
8 years ago
8 years ago
(define (run line-table)
8 years ago
(define line-vec
(list->vector (sort (hash-keys line-table) <)))
8 years ago
(with-handlers ([$program-end-signal? void])
8 years ago
(for/fold ([line-idx 0])
([i (in-naturals)])
(unless (< line-idx (vector-length line-vec)) (b-end))
8 years ago
(define line-num (vector-ref line-vec line-idx))
(define line-proc (hash-ref line-table line-num))
8 years ago
(with-handlers
([$change-line-signal?
(λ (cls)
(define clsv ($change-line-signal-val cls))
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
(raise-line-not-found
(line-proc #:srcloc? #t))))])
8 years ago
(line-proc)
(add1 line-idx)))))