[private] fix stop-list bug for define

This commit is contained in:
Ben Greenman 2016-06-09 22:33:52 -04:00
parent 4ef1c7a10b
commit 5d04125c55
5 changed files with 55 additions and 22 deletions

View File

@ -8,13 +8,13 @@
trivial/vector) trivial/vector)
(module+ test (module+ test
(require typed/rackunit) (require typed/rackunit typed/racket/class)
(check-equal? (check-equal?
(let () (let ()
(define: n 3) (define: n 3) ;; TODO define is broken
(let: ([m n]) (let: ([m n])
(ann (-: n m) Zero))) (ann (-: m n) Zero)))
0) 0)
(check-equal? (check-equal?
@ -35,4 +35,20 @@
[ys '(4 3 1)]) [ys '(4 3 1)])
(map: f xs ys))) (map: f xs ys)))
'("hello(hi) and 100" "hello(hi) and 11" "hello(HI) and 1")) '("hello(hi) and 100" "hello(hi) and 11" "hello(HI) and 1"))
;; Should be okay with "Indiana-style" defines
(let ()
(define fact : (-> Integer Integer)
(lambda (n)
(if (< n 2) 1 (* n (fact (- n 1))))))
(check-equal? (fact 5) 120))
;; Also with classes
(let ()
(define f% : (Rec t (Class (yolo (-> (Instance t)))))
(class object%
(super-new)
(define/public (yolo)
(new f%))))
(check-false (not (new f%))))
) )

View File

@ -47,8 +47,11 @@
#:attr evidence #'p+ #:attr evidence #'p+
#:attr expanded #'e+))) #:attr expanded #'e+)))
(define-syntax-rule (log stx msg arg* ...)
(begin (printf "[LOG:~a:~a] " (syntax-line stx) (syntax-column stx)) (printf msg arg* ...) (newline)))
(define (expand-expr stx) (define (expand-expr stx)
(local-expand stx 'expression '())) (local-expand stx 'expression (*STOP-LIST*)))
(define (quoted-stx-value? stx) (define (quoted-stx-value? stx)
(and (and
@ -94,11 +97,15 @@
(lambda (stx) (lambda (stx)
(syntax-parse stx #:literals (tr:#%plain-lambda) (syntax-parse stx #:literals (tr:#%plain-lambda)
[(_ name:id v) [(_ name:id v)
#:with (tr:#%plain-lambda (_) v+) (expand-expr (syntax/loc stx (tr:lambda (name) v))) #:with (tr:#%plain-lambda (_) v+)
(parameterize ([*STOP-LIST* (cons #'name (*STOP-LIST*))])
(expand-expr (syntax/loc stx (tr:lambda (name) v))))
#:when (syntax-e (syntax/loc stx v+)) #:when (syntax-e (syntax/loc stx v+))
#:with m (f-parse (syntax/loc stx v+)) #:with m (f-parse (syntax/loc stx v+))
#:when (syntax-e (syntax/loc stx m)) #:when (syntax-e (syntax/loc stx m))
(free-id-table-set! tbl #'name (syntax-e #'m)) (free-id-table-set! tbl #'name (syntax-e #'m))
(when (*TRIVIAL-LOG*)
(log stx "define ~a" sym))
(syntax/loc stx (syntax/loc stx
(tr:define name v+))] (tr:define name v+))]
[_ #f]))) [_ #f])))
@ -109,6 +116,8 @@
#:with (v+* ...) (map expand-expr (syntax-e (syntax/loc stx (v* ...)))) #:with (v+* ...) (map expand-expr (syntax-e (syntax/loc stx (v* ...))))
#:with (m* ...) (map f-parse (syntax-e (syntax/loc stx (v+* ...)))) #:with (m* ...) (map f-parse (syntax-e (syntax/loc stx (v+* ...))))
#:when (andmap syntax-e (syntax-e (syntax/loc stx (m* ...)))) #:when (andmap syntax-e (syntax-e (syntax/loc stx (m* ...))))
(when (*TRIVIAL-LOG*)
(log stx "let ~a" sym))
(quasisyntax/loc stx (quasisyntax/loc stx
(tr:let ([name* v+*] ...) (tr:let ([name* v+*] ...)
(tr:let-syntax ([name* (make-rename-transformer (tr:let-syntax ([name* (make-rename-transformer
@ -125,7 +134,7 @@
(cond (cond
[(parser stx) [(parser stx)
=> (lambda (r) => (lambda (r)
(when (*TRIVIAL-LOG*) (printf "[LOG] alias ~a\n" (syntax->datum id-stx))) (when (*TRIVIAL-LOG*) (log stx "alias ~a" (syntax->datum id-stx)))
r)] r)]
[else [else
(syntax-parse stx (syntax-parse stx
@ -136,17 +145,14 @@
#`(app-stx #,id-stx e* ...)])])) #`(app-stx #,id-stx e* ...)])]))
(define ((make-keyword-alias id-sym parser) stx) (define ((make-keyword-alias id-sym parser) stx)
(cond (or (with-handlers ((exn:fail? (lambda (e) #f))) (parser stx))
[(parser stx) ;; 2016-06-08: sometimes parser raises error ... i.e. "unbound local member name"
=> (lambda (r) (parser stx)
(when (*TRIVIAL-LOG*) (printf "[LOG] keyword ~a\n" id-sym)) (syntax-parse stx
r)] [(_ e* ...)
[else #:with id-stx (case id-sym
(syntax-parse stx [(define) #'tr:define]
[(_ e* ...) [(let) #'tr:let]
#:with id-stx (case id-sym [(set!) #'tr:set!]
[(define) #'tr:define] [else (error 'trivial "Unknown keyword '~a'" id-sym)])
[(let) #'tr:let] (syntax/loc stx (id-stx e* ...))])))
[(set!) #'tr:set!]
[else (error 'trivial "Unknown keyword '~a'" id-sym)])
(syntax/loc stx (id-stx e* ...))])]))

View File

@ -2,10 +2,18 @@
(provide (provide
*TRIVIAL-LOG* *TRIVIAL-LOG*
*STOP-LIST*
set-trivial-print
) )
;; ============================================================================= ;; =============================================================================
(: *TRIVIAL-LOG* (Parameterof Boolean)) (: *TRIVIAL-LOG* (Parameterof Boolean))
(define *TRIVIAL-LOG* (make-parameter #f)) (define *TRIVIAL-LOG* (make-parameter #t))
(: *STOP-LIST* (Parameterof (Listof Identifier)))
(define *STOP-LIST* (make-parameter '()))
(define (set-trivial-print)
(*TRIVIAL-LOG* #t)
(void))

View File

@ -21,7 +21,7 @@
;; ============================================================================= ;; =============================================================================
(define TRIVIAL-LOG-PREFIX "[LOG]") (define TRIVIAL-LOG-PREFIX "[LOG")
(define *ANNIHILATE* (make-parameter #f)) (define *ANNIHILATE* (make-parameter #f))

View File

@ -1,5 +1,8 @@
#lang typed/racket/base #lang typed/racket/base
;; TODO:
;; - use unsafe-vector* operations? (will we ever have an impersonator? not yet)
(provide (provide
define-vector: define-vector:
let-vector: let-vector: