;; This abstracts away the storage format of the target dictionary. (module target mzscheme (require (lib "match.ss") "list-utils.ss" "dict.ss") (provide (all-defined)) ;; Dictionary contains macros of the form: ;; constant: ( () ()) ;; word: ( () (
execute)) ;; concatenative macro: ( () ) ;; macro w. locals: ( ) ;; fetch a constant from the dictionary (define (number?-or x thing) (if (number? x) x thing)) (define (entry-extract entry type) (and entry (case type ((data) (match entry ((name () (addr)) (number?-or addr #f)) (else #f))) ((code) (match entry ((name () (addr 'execute)) (number?-or addr #f)) (else #f)))))) (define (dict-find-data dict tag) (entry-extract (assoc tag dict) 'data)) (define (dict-find-code dict tag) (entry-extract (assoc tag dict) 'code)) (define (dict-shadow-data dict tag value) (dict-shadow dict tag `(() (,value)))) (define (dict-set-data dict tag value) (dict-set dict tag `(() (,value)))) (define (dict-shadow-code dict tag value) (dict-shadow dict tag `(() (,value execute)))) (define dict-entry-type (match-lambda ((name () ((= number? #t))) 'data) ((name () ((= number? #t) 'execute)) 'code) (else 'macro))) ;; Utilities ;; Obtain inverse code and data maps. (define (dict-inverse dict type) (fold (lambda (entry rest) (if (eq? type (dict-entry-type entry)) (cons (cons (entry-extract entry type) (car entry)) rest) rest)) '() dict)) )