AE : Concrete syntax AE : Abstract syntax parse : sexp AE interp : AE number ...
AE : Concrete syntax
<AE> ::= <num>
| {+ <AE> <AE>}
| {- <AE> <AE>}
AE : Abstract syntax
(define-type AE
[num (n number?)]
[add (lhs AE?)
(rhs AE?)]
[sub (lhs AE?)
(rhs AE?)])
parse : sexp -> AE
(define (parse sexp)
(cond
[(number? sexp) (num sexp)]
[(list? sexp)
(case (first sexp)
[(+) (add (parse (second sexp))
(parse (third sexp)))]
[(-) (sub (parse (second sexp))
(parse (third sexp)))])]))
(define (parse sexp)
(cond
[(number? sexp) (num sexp)]
[(and (= 3 (length sexp))
(eq? (first sexp) ’+))
(add (parse (second sexp))
(parse (third sexp)))]
[(and (= 3 (length sexp))
(eq? (first sexp) ’-))
(sub (parse (second sexp))
(parse (third sexp)))]
[else (error ’parse "bad syntax: ~a" sexp)]))
(define (parse sexp)
(cond
[(number? sexp) (num sexp)]
[(and (= 3 (length sexp)) (eq? (first sexp) '+))
(add (parse (second sexp)) (parse (third sexp)))]
[(and (= 3 (length sexp)) (eq? (first sexp) '-))
(sub (parse (second sexp)) (parse (third sexp)))]
[else (error 'parse "bad syntax: ~a" sexp)]))
(define (parse sexp)
(match sexp
[(? number?) (num sexp)]
[(list '+ l r) (add (parse l) (parse r))]
[(list '- l r) (sub (parse l) (parse r))]
[else (error 'parse "bad syntax: ~a" sexp)]))
interp : AE -> number
(define (interp ae)
(type-case AE ae
[num (n) n]
[add (l r) (+ (interp l) (interp r))]
[sub (l r) (- (interp l) (interp r))]))