Código: waev2.rkt
Descarga aquí
#lang plai
(define-type WAE
[id (i symbol?)]
[num (n number?)]
[binop (f procedure?) (izq WAE?) (der WAE?)]
[with (name symbol?) (named-expr WAE?) (body WAE?)])
(define (parse sexp)
(match sexp
[(? symbol?) (id sexp)]
[(? number?) (num sexp)]
[(list 'with (list id val) body) (with id (parse val) (parse body))]
[(list op l r) (binop (elige op) (parse l) (parse r))]))
(define (elige sexp)
(match sexp
['+ +]
['- -]
['* *]
['/ /]))
(define (subst expr sub-id val)
(match expr
[(id i) (if (symbol=? i sub-id) val expr)]
[(num n) expr]
[(binop f izq der) (binop f (subst izq sub-id val) (subst der sub-id val))]
[(with bound-id named-expr body)
(if (symbol=? bound-id sub-id)
(with
bound-id
(subst named-expr sub-id val)
body)
(with
bound-id
(subst named-expr sub-id val)
(subst body sub-id val)))]))
(define (interp expr)
(error 'interp "Función no implementada"))