Advertisement
Guest User

pene

a guest
Nov 25th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.86 KB | None | 0 0
  1. #lang plai
  2.  
  3. (require "grammars.rkt")
  4. (require "parser.rkt")
  5.  
  6. (define (interp expr env)
  7.     (match expr
  8.         [(id i) (lookup i env)]
  9.         [(num n) (numV n)]
  10.         [(binop op izq der)
  11.             (let ([lhs (interp izq env)] [rhs (interp der env)])
  12.                 (cond
  13.                     [(exceptionV? lhs) lhs]
  14.                     [(exceptionV? rhs) rhs]
  15.                     [else (numV (op (numV-n lhs) (numV-n rhs)))]))]
  16.         [(if0 test-expr then-expr else-expr)
  17.             (let ([cnd (interp test-expr env)])
  18.                  (if (exceptionV? cnd)
  19.                      cnd
  20.                      (if (zero? (numV-n cnd))
  21.                          (interp then-expr env)
  22.                          (interp else-expr env))))]
  23.         [(rec id value body)
  24.             (interp body (cyclically-bind-and-interp id value env))]
  25.         [(throws exception-id)
  26.             (let/cc k (exceptionV exception-id k))]
  27.         [(try/catch catched-exception-id fail-expr try-expr)
  28.             (let ([expr-val (interp try-expr env)])
  29.                 (if (and (exceptionV? expr-val) (equal? (exceptionV-exception-id expr-val) catched-exception-id))
  30.                     ((exceptionV-continuation expr-val) (interp fail-expr env))
  31.                     expr-val))]
  32.         [(fun param body)
  33.             (closureV param body env)]
  34.         [(app fun-expr arg)
  35.             (let ([fun-val (interp fun-expr env)])
  36.                 (if (exceptionV? fun-val)
  37.                     fun-val
  38.                     (interp
  39.                         (closureV-body fun-val)
  40.                         (aSub
  41.                             (closureV-param fun-val)
  42.                             (interp arg env)
  43.                             (closureV-env fun-val)))))]))
  44.  
  45. (define (lookup id env)
  46.     (match env
  47.         [(mtSub) (error 'lookup "Free identifier")]
  48.         [(aSub sub-id value rest-env)
  49.             (if (symbol=? id sub-id)
  50.                 value
  51.                 (lookup id rest-env))]
  52.         [(aRecSub sub-id value rest-env)
  53.             (if (symbol=? id sub-id)
  54.                 (unbox value)
  55.                 (lookup id rest-env))]))
  56.  
  57. (define (cyclically-bind-and-interp id value env)
  58.     (let* ([value-holder (box (numV 1729))]
  59.            [new-env (aRecSub id value-holder env)]
  60.            [named-expr-val (interp value new-env)])
  61.         (begin
  62.             (set-box! value-holder named-expr-val)
  63.             new-env)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement