https://github.com/philnguyen/abstract-compilation
Simple DSL reducing some boiler plates in doing abstract compilation
https://github.com/philnguyen/abstract-compilation
Last synced: about 1 month ago
JSON representation
Simple DSL reducing some boiler plates in doing abstract compilation
- Host: GitHub
- URL: https://github.com/philnguyen/abstract-compilation
- Owner: philnguyen
- Created: 2018-03-06T21:48:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-07T03:42:00.000Z (over 7 years ago)
- Last Synced: 2024-11-15T09:43:48.614Z (7 months ago)
- Language: Racket
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-racket - abstract-compilation - DSL reducing boiler plates for doing abstract compilation. (Compilers)
README
[](https://travis-ci.org/philnguyen/abstract-compilation) abstract-compilation
=========================================Simple DSL for defining abstract compilers,
making the task not much more verbose than defining interpreters
by getting rid of some boilerplates.### Install
```
raco pkg install abstract-compilation
```### Example
```racket
#lang typed/racket/base
(require abstract-compilation)
(define-type ρ (Immutable-HashTable Symbol Integer))
(define-type Comp (ρ → Integer))(: ⟦_⟧ : Any → Comp)
(define-compiler ((⟦_⟧ e) ρ)
;; e ::= x | ℤ | (+ e ...) | (- e ...) | (let ([x e]) e)
[(? symbol? x) (intern x)]
[(? exact-integer? n) (intern n)]
[=> `(+ ,es ...)
(for/sum ([f ⟦e⟧s]) (f ρ))
#:recur [(es ...) #:as ⟦e⟧s]]
[=> `(- ,e)
(- (⟦e⟧ ρ))
#:recur e]
[=> `(- ,e₁ ,es ...)
(- (⟦e₁⟧ ρ) (for/sum : Integer ([f ⟦es⟧]) (f ρ)))
#:recur e₁ (es ...)]
[=> `(let ([,(? symbol? x) ,eₓ]) ,e)
(⟦e⟧ (hash-set ρ x (⟦eₓ⟧ ρ)))
#:recur eₓ e])(define intern : ((U Integer Symbol) → Comp)
(let ([m : (Mutable-HashTable (U Integer Symbol) Comp) (make-hasheq)])
(λ (x)
(define (mk) : Comp
(cond [(integer? x) (λ _ x)]
[else (λ (ρ) (hash-ref ρ x (λ () (error x "unbound"))))]))
(hash-ref! m x mk))))
(define prog '(let ([x (+ 1 2)])
(let ([y (- 1 2 3)])
(+ x y))))
(define ⟦prog⟧ (⟦_⟧ prog))
(⟦prog⟧ (hasheq)) ; ==> -1
```