2020-02-19 22:49:50 +01:00
|
|
|
#lang racket
|
2020-02-16 21:39:42 +01:00
|
|
|
|
|
|
|
;;; dds/utils
|
|
|
|
|
|
|
|
;;; Various utilities.
|
|
|
|
|
2020-02-17 00:16:44 +01:00
|
|
|
(require (for-syntax syntax/parse)
|
|
|
|
(for-syntax racket/list))
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-02-19 23:37:06 +01:00
|
|
|
(provide
|
2020-02-20 14:19:30 +01:00
|
|
|
;; Functions
|
2020-02-19 23:37:06 +01:00
|
|
|
(contract-out [eval-with (-> variable-mapping? any/c any)]
|
2020-02-20 14:19:30 +01:00
|
|
|
[extract-symbols (-> any/c list?)]
|
2020-02-22 10:29:34 +01:00
|
|
|
[any->string (-> any/c string?)]
|
2020-02-22 10:36:03 +01:00
|
|
|
[variable-mapping-stringify (-> variable-mapping? string-variable-mapping?)])
|
|
|
|
;; Contracts
|
|
|
|
(contract-out [variable-mapping? contract?]
|
2020-02-22 10:35:01 +01:00
|
|
|
[string-variable-mapping? contract?])
|
2020-02-19 23:37:06 +01:00
|
|
|
;; Syntax
|
|
|
|
auto-hash-ref/explicit auto-hash-ref/:)
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-02-18 00:00:18 +01:00
|
|
|
;;; ===================
|
2020-02-16 21:39:42 +01:00
|
|
|
;;; HashTable Injection
|
2020-02-18 00:00:18 +01:00
|
|
|
;;; ===================
|
2020-02-16 21:39:42 +01:00
|
|
|
|
|
|
|
;;; This section of the file contains some utilities to streamline the
|
|
|
|
;;; usage of hash tables mapping symbols to values. The goal is
|
|
|
|
;;; essentially to avoid having to write explicit hash-ref calls.
|
|
|
|
|
2020-02-20 00:21:10 +01:00
|
|
|
;;; A variable mapping is a hash table mapping symbols to values.
|
2020-02-22 10:35:01 +01:00
|
|
|
(define (variable-mapping? dict) (hash/c symbol? any/c))
|
2020-02-19 23:25:00 +01:00
|
|
|
|
2020-02-16 21:39:42 +01:00
|
|
|
;;; Given a (HashTable Symbol a) and a sequence of symbols, binds
|
|
|
|
;;; these symbols to the values they are associated to in the hash
|
|
|
|
;;; table, then puts the body in the context of these bindings.
|
2020-02-17 00:16:01 +01:00
|
|
|
;;;
|
|
|
|
;;; > (let ([ht #hash((a . 1) (b . 2))])
|
|
|
|
;;; (auto-hash-ref/explicit (ht a b) (+ a (* 2 b))))
|
|
|
|
;;; 5
|
|
|
|
;;;
|
2020-02-17 00:27:04 +01:00
|
|
|
;;; Note that only one expression can be supplied in the body.
|
2020-02-16 21:39:42 +01:00
|
|
|
(define-syntax (auto-hash-ref/explicit stx)
|
|
|
|
(syntax-parse stx
|
|
|
|
[(_ (ht:id xs:id ...) body:expr)
|
|
|
|
#`(let #,(for/list ([x (syntax->list #'(xs ...))])
|
|
|
|
#`[#,x (hash-ref ht '#,x)])
|
|
|
|
body)]))
|
|
|
|
|
2020-02-17 00:16:44 +01:00
|
|
|
;;; Given an expression and a (HashTable Symbol a), looks up the
|
|
|
|
;;; symbols with a leading semicolon and binds them to the value they
|
|
|
|
;;; are associated to in the hash table.
|
|
|
|
;;;
|
|
|
|
;;; > (let ([ht #hash((a . 1) (b . 2))])
|
|
|
|
;;; (auto-hash-ref/: ht (+ :a (* 2 :b))))
|
|
|
|
;;; 5
|
|
|
|
;;;
|
|
|
|
;;; Note that the symbol :a is matched to the key 'a in the hash
|
|
|
|
;;; table.
|
2020-02-17 00:27:04 +01:00
|
|
|
;;;
|
|
|
|
;;; Note that only one expression can be supplied in the body.
|
2020-02-17 00:16:44 +01:00
|
|
|
(define-syntax (auto-hash-ref/: stx)
|
|
|
|
(syntax-parse stx
|
|
|
|
[(_ ht:id body)
|
|
|
|
(let* ([names/: (collect-colons (syntax->datum #'body))])
|
|
|
|
#`(let #,(for/list ([x names/:])
|
|
|
|
;; put x in the same context as body
|
|
|
|
#`[#,(datum->syntax #'body x)
|
|
|
|
(hash-ref ht '#,(strip-colon x))])
|
|
|
|
body))]))
|
|
|
|
|
|
|
|
;;; The helper functions for auto-hash-ref/:.
|
|
|
|
(begin-for-syntax
|
|
|
|
;; Collect all the symbols starting with a colon in datum.
|
|
|
|
(define (collect-colons datum)
|
|
|
|
(remove-duplicates
|
|
|
|
(flatten
|
|
|
|
(for/list ([token datum])
|
|
|
|
(cond
|
|
|
|
[(symbol? token)
|
|
|
|
(let ([name (symbol->string token)])
|
|
|
|
(if (eq? #\: (string-ref name 0))
|
|
|
|
token
|
|
|
|
'()))]
|
|
|
|
[(list? token)
|
|
|
|
(collect-colons token)]
|
|
|
|
[else '()])))))
|
2020-02-16 21:39:42 +01:00
|
|
|
|
2020-02-17 00:16:44 +01:00
|
|
|
;; Strip the leading colon off x.
|
|
|
|
(define (strip-colon x)
|
|
|
|
(let ([x-str (symbol->string x)])
|
|
|
|
(if (eq? #\: (string-ref x-str 0))
|
|
|
|
(string->symbol (substring x-str 1))
|
|
|
|
x))))
|
2020-02-17 23:52:15 +01:00
|
|
|
|
|
|
|
;;; Temporarily injects the mappings from the given hash table as
|
2020-02-19 22:51:49 +01:00
|
|
|
;;; bindings in a namespace including racket/base and then evaluates
|
|
|
|
;;; the expression.
|
2020-02-17 23:52:15 +01:00
|
|
|
;;;
|
|
|
|
;;; > (let ([ht #hash((a . 1) (b . 1))])
|
|
|
|
;;; (eval-with ht '(+ b a 1)))
|
|
|
|
;;; 3
|
|
|
|
;;;
|
|
|
|
;;; The local bindings from the current lexical scope are not
|
|
|
|
;;; conserved. Therefore, the following outputs an error about a
|
|
|
|
;;; missing identifier:
|
|
|
|
;;;
|
|
|
|
;;; > (let ([ht #hash((a . 1) (b . 1))]
|
|
|
|
;;; [z 1])
|
|
|
|
;;; (eval-with ht '(+ b z a 1)))
|
|
|
|
;;;
|
|
|
|
(define (eval-with ht expr)
|
2020-02-19 22:49:50 +01:00
|
|
|
(parameterize ([current-namespace (make-base-namespace)])
|
2020-02-20 15:36:29 +01:00
|
|
|
(hash-for-each ht (λ (x val)
|
2020-02-17 23:52:15 +01:00
|
|
|
(namespace-set-variable-value! x val)))
|
|
|
|
(eval expr)))
|
|
|
|
|
|
|
|
;;; Same as eval-with, but returns only the first value produced by
|
|
|
|
;;; the evaluated expression.
|
|
|
|
(define (eval-with1 ht expr)
|
|
|
|
(let ([vals (call-with-values (λ () (eval-with ht expr))
|
|
|
|
(λ vals vals))])
|
|
|
|
(car vals)))
|
2020-02-19 22:11:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
;;; ==============================
|
|
|
|
;;; Analysis of quoted expressions
|
|
|
|
;;; ==============================
|
|
|
|
|
|
|
|
;;; Produces a list of symbols appearing in the quoted expression
|
|
|
|
;;; passed in the first argument.
|
|
|
|
(define (extract-symbols form)
|
|
|
|
(cond
|
|
|
|
[(symbol? form)
|
2020-02-19 22:49:50 +01:00
|
|
|
(list form)]
|
2020-02-19 22:11:44 +01:00
|
|
|
[(list? form)
|
2020-02-19 22:49:50 +01:00
|
|
|
(flatten (for/list ([x form]) (extract-symbols x)))]
|
2020-02-19 22:11:44 +01:00
|
|
|
[else '()]))
|
2020-02-21 18:01:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
;;; =============================
|
|
|
|
;;; Variable mapping and Org-mode
|
|
|
|
;;; =============================
|
|
|
|
|
|
|
|
;;; Org-mode supports laying out the output of code blocks as tables,
|
|
|
|
;;; which is very practical for various variable mappings (e.g.,
|
|
|
|
;;; states). However, when the hash table maps variables to lists,
|
|
|
|
;;; Org-mode will create a column per list element, which may or may
|
|
|
|
;;; not be the desired effect. In this section I define some
|
|
|
|
;;; utilities for handling such situations.
|
|
|
|
|
|
|
|
;;; Converts any value to string.
|
|
|
|
(define (any->string x)
|
|
|
|
(with-output-to-string (λ () (display x))))
|
2020-02-22 10:29:34 +01:00
|
|
|
|
|
|
|
;;; A string variable mapping is a mapping from variables to strings.
|
2020-02-22 10:35:01 +01:00
|
|
|
(define (string-variable-mapping? dict) (hash/c symbol? string?))
|
2020-02-22 10:29:34 +01:00
|
|
|
|
|
|
|
;;; Converts all the values of a variable mapping to string.
|
|
|
|
(define (variable-mapping-stringify ht)
|
|
|
|
(make-hash (hash-map ht (λ (key val)
|
|
|
|
(cons key (any->string val))))))
|