-
-
Save jbclements/f120131b65a641bd0ef2773113d6d3f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang typed/racket | |
(require typed/rackunit) | |
;; an S-expression is one of: | |
;; a number | |
;; a string | |
;; a boolean | |
;; a symbol, or | |
;; (list S-expression ...) | |
;; concrete syntax of TL | |
"a string" | |
"abc" | |
"def" | |
'{"abc" + "def"} ; => "abcdef" | |
'{{"a" + "b"} + "cdef"} | |
'{"**" around "abc"} ; => "**abc**" | |
;; sugared syntax | |
(define-type ExprS (U StrAppdS StrS)) | |
(struct StrS ([s : String]) #:transparent) | |
(struct StrAppdS ([l : ExprS] [r : ExprS]) #:transparent) | |
;; abstract syntax of TL | |
(define-type ExprC (U StrAppdC StrC)) | |
(struct StrC ([s : String]) #:transparent) | |
(struct StrAppdC ([l : ExprC] [r : ExprC]) #:transparent) | |
;; parse concrete stx of TL | |
(define (parse [s : Sexp]) : ExprC | |
(match s | |
[(? string? str) (StrC str)] | |
[(list a '+ c) (StrAppdC (parse a) (parse c))])) | |
(check-equal? (parse '"abc") | |
(StrC "abc")) | |
(check-equal? (parse '{{"a" + "b"} + "cdef"}) | |
(StrAppdC (StrAppdC (StrC "a") | |
(StrC "b")) | |
(StrC "cdef"))) | |
;; desugar an S form into a C form | |
(define (desugar [e : ExprS]) : ExprC | |
...) | |
;; interp a function in TL | |
(define (interp [e : ExprC]) : String | |
(match e | |
[(StrC s) s] | |
[(StrAppdC l r) (string-append (interp l) (interp r))])) | |
(check-equal? (interp (StrC "abc")) "abc") | |
(check-equal? (interp (StrAppdC (StrAppdC (StrC "a") | |
(StrC "b")) | |
(StrC "cdef"))) | |
"abcdef") | |
;; map a program to an output | |
(define (top-interp [s : Sexp]) : String | |
(interp (desugar (parse s)))) | |
(top-interp '{"a" + {{"d" + "arj"} + "mwnop"}}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment