Skip to content

Instantly share code, notes, and snippets.

@dannypsnl
Forked from shhyou/0-env.rkt
Created February 18, 2021 13:07
Show Gist options
  • Select an option

  • Save dannypsnl/3ae083d3d59e23280bd41a47fbd7e0f4 to your computer and use it in GitHub Desktop.

Select an option

Save dannypsnl/3ae083d3d59e23280bd41a47fbd7e0f4 to your computer and use it in GitHub Desktop.

Revisions

  1. @shhyou shhyou revised this gist Feb 18, 2021. 2 changed files with 30 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions a-extra.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    #lang racket/base

    (require (for-syntax racket/base))
    (begin-for-syntax
    (struct extrainfo (target data)
    #:property prop:rename-transformer (struct-field-index target))
    (provide extrainfo-data))

    (provide (rename-out [my-extra-surface my-extra]))
    (define-syntax my-extra-surface
    (extrainfo (syntax-property #'my-extra 'not-free-identifier=? #t)
    "mul.rkt: my-extra info"))
    (define (my-extra a b)
    (+ a b))
    16 changes: 16 additions & 0 deletions a-use.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #lang racket/base

    (require "a-extra.rkt")

    my-extra
    (my-extra 2 3)

    (require (for-syntax racket/base
    syntax/parse))
    (define-syntax (expand-to-extra-info stx)
    (syntax-parse stx
    [(_ id)
    (define-values (info target)
    (syntax-local-value/immediate #'id))
    #`(quote #,(extrainfo-data info))]))
    (expand-to-extra-info my-extra)
  2. @shhyou shhyou revised this gist Feb 17, 2021. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion 0-env.rkt
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,31 @@
    ;; Exporting free identifier table operations that close over a global map
    #lang racket/base

    (require syntax/id-table)
    (require racket/list
    syntax/id-table)

    (provide env-ref env-has-id? env-add!)

    (define id-table (make-free-id-table))

    (define (env-ref id)
    (debug-print-binding-info 'env-ref id)
    (free-id-table-ref id-table id))

    (define (env-has-id? id)
    (debug-print-binding-info 'env-has-id? id)
    (with-handlers ([exn:fail? (λ (e) #f)])
    (free-id-table-ref id-table id)
    #t))

    (define (env-add! id value)
    (debug-print-binding-info 'env-add! id)
    (free-id-table-set! id-table id value))

    (define (debug-print-binding-info who id)
    (define binding
    (identifier-binding id))
    (printf "env.rkt: ~a:\n id: ~s\n binding: ~s\n"
    who
    id
    (and (list? binding) (take binding 2))))
  3. @shhyou shhyou revised this gist Feb 17, 2021. 1 changed file with 11 additions and 4 deletions.
    15 changes: 11 additions & 4 deletions 9-use.rkt
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,19 @@

    (require (for-syntax racket/base
    "0-env.rkt")
    (only-in "1-add.rkt" my-fun)
    (rename-in "2-mul.rkt" [my-fun mul:my-fun]))
    "2-mul.rkt")

    (begin-for-syntax
    (printf "use.rkt: my-fun = ~s\n" (env-ref #'my-fun))
    (printf "use.rkt: has-key?(your-fun) = ~s\n" (env-has-id? #'your-fun))
    (printf "use.rkt: mul:my-fun = ~s\n" (env-ref #'mul:my-fun))
    )
    (my-fun 7 (mul:my-fun 8 3))
    (my-fun 8 3)

    #;
    (begin
    (require (only-in "1-add.rkt" [my-fun add:my-fun]))
    (begin-for-syntax
    (printf "use.rkt: add:my-fun = ~s\n" (env-ref #'add:my-fun))
    )
    (add:my-fun 7 (my-fun 8 3))
    )
  4. @shhyou shhyou revised this gist Feb 17, 2021. 6 changed files with 39 additions and 41 deletions.
    File renamed without changes.
    21 changes: 21 additions & 0 deletions 1-add.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #lang racket/base

    (require (for-syntax racket/base
    "0-env.rkt"))

    (provide my-fun
    your-fun)


    (begin-for-syntax
    (env-add! #'my-fun "my-fun from add.rkt"))
    (define (my-fun a b)
    (+ a b))

    (begin-for-syntax
    (env-add! #'your-fun "your-fun from add.rkt"))
    (define (your-fun c)
    (- c))

    ;; See https://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._.Dictionaries_for_free-identifier~3d_%29
    ;; for the caveat of macro-generated definitions
    13 changes: 13 additions & 0 deletions 2-mul.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    #lang racket/base

    (require (for-syntax racket/base
    "0-env.rkt")
    "1-add.rkt")

    (provide my-fun)

    (begin-for-syntax
    (env-add! #'my-fun "my-fun from mul.rkt")
    (printf "mul.rkt: your-fun is: ~s\n" (env-ref #'your-fun)))
    (define (my-fun a b)
    (* a (your-fun b)))
    9 changes: 5 additions & 4 deletions use.rkt → 9-use.rkt
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,13 @@
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt")
    (only-in "add.rkt" my-fun)
    (rename-in "mul.rkt" [my-fun mul:my-fun]))
    "0-env.rkt")
    (only-in "1-add.rkt" my-fun)
    (rename-in "2-mul.rkt" [my-fun mul:my-fun]))

    (begin-for-syntax
    (printf "use.rkt: my-fun = ~s\n" (env-ref #'my-fun))
    (printf "use.rkt: has-key?(your-fun) = ~s\n" (env-has-id? #'your-fun)) ;; => #f
    (printf "use.rkt: has-key?(your-fun) = ~s\n" (env-has-id? #'your-fun))
    (printf "use.rkt: mul:my-fun = ~s\n" (env-ref #'mul:my-fun))
    )
    (my-fun 7 (mul:my-fun 8 3))
    19 changes: 0 additions & 19 deletions add.rkt
    Original file line number Diff line number Diff line change
    @@ -1,19 +0,0 @@
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt"))

    (provide my-fun
    your-fun)

    (define (my-fun a b)
    (+ a b))

    (define (your-fun c)
    (- c))

    ;; https://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._.Dictionaries_for_free-identifier~3d_%29
    ;; Must be placed after the definition of my-fun
    (begin-for-syntax
    (env-add! #'my-fun "my-fun from add.rkt")
    (env-add! #'your-fun "your-fun from add.rkt"))
    18 changes: 0 additions & 18 deletions mul.rkt
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt")
    "add.rkt")

    (provide my-fun)

    (begin-for-syntax
    (printf "mul.rkt: your-fun is: ~s\n" (env-ref #'your-fun)))

    (define (my-fun a b)
    (* a (your-fun b)))

    ;; https://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._.Dictionaries_for_free-identifier~3d_%29
    ;; Must be placed after the definition of my-fun
    (begin-for-syntax
    (env-add! #'my-fun "my-fun from mul.rkt"))
  5. @shhyou shhyou created this gist Feb 17, 2021.
    19 changes: 19 additions & 0 deletions add.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt"))

    (provide my-fun
    your-fun)

    (define (my-fun a b)
    (+ a b))

    (define (your-fun c)
    (- c))

    ;; https://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._.Dictionaries_for_free-identifier~3d_%29
    ;; Must be placed after the definition of my-fun
    (begin-for-syntax
    (env-add! #'my-fun "my-fun from add.rkt")
    (env-add! #'your-fun "your-fun from add.rkt"))
    19 changes: 19 additions & 0 deletions env.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    ;; Exporting free identifier table operations that close over a global map
    #lang racket/base

    (require syntax/id-table)

    (provide env-ref env-has-id? env-add!)

    (define id-table (make-free-id-table))

    (define (env-ref id)
    (free-id-table-ref id-table id))

    (define (env-has-id? id)
    (with-handlers ([exn:fail? (λ (e) #f)])
    (free-id-table-ref id-table id)
    #t))

    (define (env-add! id value)
    (free-id-table-set! id-table id value))
    18 changes: 18 additions & 0 deletions mul.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt")
    "add.rkt")

    (provide my-fun)

    (begin-for-syntax
    (printf "mul.rkt: your-fun is: ~s\n" (env-ref #'your-fun)))

    (define (my-fun a b)
    (* a (your-fun b)))

    ;; https://docs.racket-lang.org/syntax/syntax-helpers.html#%28part._.Dictionaries_for_free-identifier~3d_%29
    ;; Must be placed after the definition of my-fun
    (begin-for-syntax
    (env-add! #'my-fun "my-fun from mul.rkt"))
    16 changes: 16 additions & 0 deletions use.rkt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    ;; Dependency:
    ;; add depends on {env}
    ;; mul depends on {add,env}
    ;; use depends on {mul,add,env}
    #lang racket/base

    (require (for-syntax racket/base
    "env.rkt")
    (only-in "add.rkt" my-fun)
    (rename-in "mul.rkt" [my-fun mul:my-fun]))

    (begin-for-syntax
    (printf "use.rkt: my-fun = ~s\n" (env-ref #'my-fun))
    (printf "use.rkt: has-key?(your-fun) = ~s\n" (env-has-id? #'your-fun)) ;; => #f
    (printf "use.rkt: mul:my-fun = ~s\n" (env-ref #'mul:my-fun))
    )