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

54 lines
1.9 KiB
Racket

7 years ago
#lang br/quicklang
7 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 ...))))
8 years ago
7 years ago
(define-macro (b-module-begin (b-program LINE ...))
7 years ago
(with-pattern
([((b-line NUM STMT ...) ...) #'(LINE ...)]
7 years ago
[(LINE-FUNC ...) (prefix-id "line-" #'(NUM ...))])
7 years ago
#'(#%module-begin
LINE ...
7 years ago
(define line-table
7 years ago
(apply hasheqv (append (list NUM LINE-FUNC) ...)))
7 years ago
(void (run line-table)))))
7 years ago
(provide (rename-out [b-module-begin #%module-begin]))
7 years ago
7 years ago
(struct end-program-signal ())
(struct change-line-signal (val))
7 years ago
7 years ago
(define (b-end) (raise (end-program-signal)))
7 years ago
(define (b-goto expr) (raise (change-line-signal expr)))
7 years ago
7 years ago
(define (run line-table)
7 years ago
(define line-vec
(list->vector (sort (hash-keys line-table) <)))
7 years ago
(with-handlers ([end-program-signal? (λ (exn-val) (void))])
7 years ago
(for/fold ([line-idx 0])
7 years ago
([i (in-naturals)]
#:break (>= line-idx (vector-length line-vec)))
7 years ago
(define line-num (vector-ref line-vec line-idx))
7 years ago
(define line-func (hash-ref line-table line-num))
7 years ago
(with-handlers
7 years ago
([change-line-signal?
7 years ago
(λ (cls)
7 years ago
(define clsv (change-line-signal-val cls))
7 years ago
(or
(and (exact-positive-integer? clsv)
(vector-member clsv line-vec))
7 years ago
(error
(format "error in line ~a: line ~a not found"
line-num clsv))))])
7 years ago
(line-func)
7 years ago
(add1 line-idx)))))
7 years ago
7 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)
7 years ago
(if (integer? expr) (inexact->exact expr) expr))