Created
April 24, 2020 22:07
-
-
Save jbclements/c4ce62ac76b2b870a211fea9a9236610 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**" | |
;; abstract syntax of TL | |
(define-type ExprC (U StrAppdC StrC IdC AppC)) | |
(struct StrC ([s : String]) #:transparent) | |
(struct IdC ([var : Symbol]) #:transparent) | |
(struct StrAppdC ([l : ExprC] [r : ExprC]) #:transparent) | |
(struct AppC ([fun : Symbol] [arg : ExprC]) #:transparent) | |
(struct FundefC ([name : Symbol] [param : Symbol] [body : ExprC])) | |
;; parse concrete stx of | |
(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"))) | |
;; interp a function in TL | |
(define (interp [e : ExprC] [fns : (Listof FundefC)] [env : Env]) : String | |
(match e | |
[(StrC s) s] | |
[(StrAppdC l r) (string-append (interp l fns env) (interp r fns env))] | |
[(IdC var) (env-lookup env var)] | |
[(AppC f a) | |
(define fn (fun-lookup f fns)) | |
(match fn | |
[(FundefC name param body) | |
(define argval (interp a fns env)) | |
(define new-env (extend-env empty-env param argval)) | |
(interp body fns new-env)])] | |
)) | |
;; dynamic scope: scopes are periods of time | |
;; lexical scope: scopes are parts of the program | |
'{{func {f z} | |
{+ z x}} | |
{func {g x} | |
{f 9}} | |
{func {main} | |
{g 1234}}} | |
#| | |
def g(x): | |
return x + y | |
g(7) | |
|# | |
(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