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

53 lines
1.9 KiB
Racket

8 years ago
#lang br/quicklang
8 years ago
(provide (matching-identifiers-out #rx"^b-" (all-defined-out)))
(define-macro (b-line NUM STATEMENT ...)
(with-pattern ([LINE-NUM (prefix-id "line-" #'NUM
#:source #'NUM)])
(syntax/loc caller-stx
(define (LINE-NUM) (void) STATEMENT ...))))
9 years ago
8 years ago
(define-macro (b-module-begin (b-program LINE ...))
8 years ago
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
8 years ago
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))])
8 years ago
#'(#%module-begin
LINE ...
8 years ago
(define line-table
8 years ago
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
8 years ago
(void (run line-table)))))
8 years ago
(provide (rename-out [b-module-begin #%module-begin]))
8 years ago
8 years ago
(struct end-program-signal ())
(struct change-line-signal (val))
8 years ago
8 years ago
(define (b-end) (raise (end-program-signal)))
8 years ago
(define (b-goto expr) (raise (change-line-signal expr)))
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 ([end-program-signal? (λ (exn-val) (void))])
8 years ago
(for/fold ([line-idx 0])
8 years ago
([i (in-naturals)]
#:break (>= line-idx (vector-length line-vec)))
8 years ago
(define line-num (vector-ref line-vec line-idx))
8 years ago
(define line-func (hash-ref line-table line-num))
8 years ago
(with-handlers
8 years ago
([change-line-signal?
8 years ago
(λ (cls)
8 years ago
(define clsv (change-line-signal-val cls))
8 years ago
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
8 years ago
(error (format "error in line ~a: line ~a not found"
line-num clsv))))])
8 years ago
(line-func)
8 years ago
(add1 line-idx)))))
8 years ago
8 years ago
(define (b-rem val) (void))
(define (b-print . vals)
(displayln (string-append* (map ~a vals))))
(define (b-sum . vals) (apply + vals))
(define (b-expr expr)
8 years ago
(if (integer? expr) (inexact->exact expr) expr))