2021-05-20 10:53:11 +02:00
|
|
|
#lang typed/racket
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2021-05-20 10:53:11 +02:00
|
|
|
(require)
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-11-30 23:54:45 +01:00
|
|
|
(provide Symbol VariableMapping
|
|
|
|
eval-with eval1-with)
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-05-16 23:09:00 +02:00
|
|
|
(module+ test
|
2020-11-30 23:54:45 +01:00
|
|
|
(require typed/rackunit))
|
|
|
|
|
|
|
|
;;; ===================
|
|
|
|
;;; HashTable injection
|
|
|
|
;;; ===================
|
|
|
|
|
|
|
|
(define-type Variable Symbol)
|
|
|
|
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
|
|
|
|
|
|
|
|
(: eval-with (-> (VariableMapping Any) Any AnyValues))
|
|
|
|
(define (eval-with ht expr)
|
|
|
|
(parameterize ([current-namespace (make-base-namespace)])
|
|
|
|
(for ([(x val) (in-hash ht)]) (namespace-set-variable-value! x val))
|
|
|
|
(eval expr)))
|
|
|
|
|
|
|
|
(: eval1-with (-> (VariableMapping Any) Any Any))
|
|
|
|
(define (eval1-with ht expr)
|
|
|
|
(call-with-values (λ () (eval-with ht expr))
|
|
|
|
(λ args (car args))))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(test-case "eval-with"
|
|
|
|
(check-equal? (eval1-with (hash 'a 1 'b 2) '(+ a b 1))
|
|
|
|
4)
|
|
|
|
(define ht : (VariableMapping Integer) (hash 'a 1 'b 2))
|
|
|
|
(define expr : Any '(+ a b 1))
|
|
|
|
(check-equal? (eval1-with ht expr)
|
|
|
|
4)))
|