Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 6.34 KB | None | 0 0
  1.  
  2. ;accessors for a list of operands
  3. (define (no-operands? ops) (null? ops))
  4. (define (first-operand ops) (car ops))
  5. (define (rest-operands ops) (cdr ops))
  6.  
  7. ;predicate for cond
  8. (define (cond? exp) (tagged-list? exp 'cond))
  9.  
  10. ;accessors for cond
  11. (define (cond-clauses exp) (cdr exp))
  12.  
  13. (define (cond-else-clause? clause)
  14.   (eq? (cond-predicate clause) 'else))
  15.  
  16. (define (cond-predicate clause) (car clause))
  17.  
  18. (define (cond-actions clause) (cdr clause))
  19.  
  20. ;converts a cond to nested ifs
  21. (define (cond->if exp)
  22.   (expand-clauses (cond-clauses exp)))
  23.  
  24. ;converts a cond to nested ifs
  25. ;helper method for cond->if
  26. (define (expand-clauses clauses)
  27.   (if (null? clauses)
  28.       'false                          ; no else clause
  29.       (let ((first (car clauses))
  30.             (rest (cdr clauses)))
  31.         (if (cond-else-clause? first)
  32.             (if (null? rest)
  33.                 (sequence->exp (cond-actions first))
  34.                 (error "ELSE clause isn't last -- COND->IF"
  35.                        clauses))
  36.             (make-if (cond-predicate first)
  37.                      (sequence->exp (cond-actions first))
  38.                      (expand-clauses rest))))))
  39.  
  40. ;;; SECTION 4.1.3
  41.  
  42. (define (true? x)
  43.   (not (eq? x false)))
  44.  
  45. (define (false? x)
  46.   (eq? x false))
  47.  
  48. ;procedure objects are created by lambdas
  49. (define (make-procedure parameters body env)
  50.   (list 'procedure parameters body env))
  51.  
  52.  
  53. (define (compound-procedure? p)
  54.   (tagged-list? p 'procedure))
  55.  
  56.  
  57. (define (procedure-parameters p) (cadr p))
  58. (define (procedure-body p) (caddr p))
  59. (define (procedure-environment p) (cadddr p))
  60.  
  61.  
  62. (define (enclosing-environment env) (cdr env))
  63.  
  64. (define (first-frame env) (car env))
  65.  
  66. (define the-empty-environment '())
  67.  
  68. (define (make-frame variables values)
  69.   (cons variables values))
  70.  
  71. (define (frame-variables frame) (car frame))
  72. (define (frame-values frame) (cdr frame))
  73.  
  74. (define (add-binding-to-frame! var val frame)
  75.   (set-car! frame (cons var (car frame)))
  76.   (set-cdr! frame (cons val (cdr frame))))
  77.  
  78. (define (extend-environment vars vals base-env)
  79.   (if (= (length vars) (length vals))
  80.       (cons (make-frame vars vals) base-env)
  81.       (if (< (length vars) (length vals))
  82.           (error "Too many arguments supplied" vars vals)
  83.           (error "Too few arguments supplied" vars vals))))
  84.  
  85. (define (lookup-variable-value var env)
  86.   (define (env-loop env)
  87.     (define (scan vars vals)
  88.       (cond ((null? vars)
  89.              (env-loop (enclosing-environment env)))
  90.             ((eq? var (car vars))
  91.              (car vals))
  92.             (else (scan (cdr vars) (cdr vals)))))
  93.     (if (eq? env the-empty-environment)
  94.         (error "Unbound variable" var)
  95.         (let ((frame (first-frame env)))
  96.           (scan (frame-variables frame)
  97.                 (frame-values frame)))))
  98.   (env-loop env))
  99.  
  100. (define (set-variable-value! var val env)
  101.   (define (env-loop env)
  102.     (define (scan vars vals)
  103.       (cond ((null? vars)
  104.              (env-loop (enclosing-environment env)))
  105.             ((eq? var (car vars))
  106.              (set-car! vals val))
  107.             (else (scan (cdr vars) (cdr vals)))))
  108.     (if (eq? env the-empty-environment)
  109.         (error "Unbound variable -- SET!" var)
  110.         (let ((frame (first-frame env)))
  111.           (scan (frame-variables frame)
  112.                 (frame-values frame)))))
  113.   (env-loop env))
  114.  
  115. (define (define-variable! var val env)
  116.   (let ((frame (first-frame env)))
  117.     (define (scan vars vals)
  118.       (cond ((null? vars)
  119.              (add-binding-to-frame! var val frame))
  120.             ((eq? var (car vars))
  121.              (set-car! vals val))
  122.             (else (scan (cdr vars) (cdr vals)))))
  123.     (scan (frame-variables frame)
  124.           (frame-values frame))))
  125.  
  126. ;;; SECTION 4.1.4
  127.  
  128. (define (setup-environment)
  129.   (let ((initial-env
  130.          (extend-environment (primitive-procedure-names)
  131.                              (primitive-procedure-objects)
  132.                              the-empty-environment)))
  133.     (define-variable! 'true true initial-env)
  134.     (define-variable! 'false false initial-env)
  135.     initial-env))
  136.  
  137. ;;;; [do later] (define the-global-environment (setup-environment))
  138.  
  139. ;predicate for primitive procedures
  140. (define (primitive-procedure? proc)
  141.   (tagged-list? proc 'primitive))
  142.  
  143. ;accessor for the actual function
  144.   ;primitive values are stored in the frame as as ('primitive car '())
  145. (define (primitive-implementation proc) (cadr proc))
  146.  
  147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  148. ;;; ADD NEW PRIMITIVES HERE (SUCH AS +, -, etc)
  149. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  150.  
  151. ;concrete definition of primitives (mapped to scheme)
  152. (define primitive-procedures
  153.   (list (list 'car car)
  154.         (list 'cdr cdr)
  155.         (list 'cons cons)
  156.         (list 'null? null?)    
  157.         ;;      more primitives
  158.         ))
  159.  
  160. ;gets all primitive names
  161. (define (primitive-procedure-names)
  162.   (map car
  163.        primitive-procedures))
  164.  
  165. ;gets all primitive procedures in the form ('primitive proc)
  166. (define (primitive-procedure-objects)
  167.   (map (lambda (proc) (list 'primitive (cadr proc)))
  168.        primitive-procedures))
  169.  
  170. ; [moved to start of file] (define apply-in-underlying-scheme apply)
  171.  
  172. ;applies calls the built-in apply function to execute primitive procedures
  173. (define (apply-primitive-procedure proc args)
  174.   (apply-in-underlying-scheme
  175.    (primitive-implementation proc) args))
  176.  
  177. ;;; Prompts
  178. (define input-prompt ";;; M-Eval input:")
  179. (define output-prompt ";;; M-Eval value:")
  180.  
  181. ;;; Main loop to drive metacircular interpreter
  182. (define (driver-loop)
  183.   (prompt-for-input input-prompt)
  184.   (let ((input (read)))
  185.     (let ((output (eval input the-global-environment)))
  186.       (announce-output output-prompt)
  187.       (user-print output)))
  188.   (driver-loop))
  189.  
  190. (define (prompt-for-input string)
  191.   (newline) (newline) (display string) (newline))
  192.  
  193. (define (announce-output string)
  194.   (newline) (display string) (newline))
  195.  
  196. (define (user-print object)
  197.   (if (compound-procedure? object)
  198.       (display (list 'compound-procedure
  199.                      (procedure-parameters object)
  200.                      (procedure-body object)
  201.                      '<procedure-env>))
  202.       (display object)))
  203.  
  204. ;;; Following are commented out so as not to be evaluated when
  205. ;;; the file is loaded. They are needed to run the interpreter.
  206. (define the-global-environment (setup-environment))
  207. (driver-loop)
  208.  
  209. 'METACIRCULAR-EVALUATOR-LOADED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement