Presentation is loading. Please wait.

Presentation is loading. Please wait.

The BRENDA interpreter A interpreter for a subset of Dylan written in Dylan.

Similar presentations


Presentation on theme: "The BRENDA interpreter A interpreter for a subset of Dylan written in Dylan."— Presentation transcript:

1 The BRENDA interpreter A interpreter for a subset of Dylan written in Dylan

2 What’s the point? Better understanding of how the language works Languages are not magic, just another program! You can change the language (we'll do so in PS6) see the environment model in action similar to the expression evaluator that we did in PS3, but more complex expressions (most of Dylan) You’ll see how garbage collection works

3 Interpreter “Walks over” a given high-level program and performs actions specified. Simpler, easier to understand and implement Compiler Translates high-level program to another program in the machine's native language (machine code). More efficient

4 Read-eval-print loop (define read-eval-print-loop (method () (bind ((raw (read)) (result (brenda-eval raw *brenda-global-environment*))) (print "Brenda? " raw) (print "Brenda==> " result) (read-eval-print-loop))))

5 Brenda-eval (define-generic-function brenda-eval ((obj ) (env ))) (add-method brenda-eval (method ((obj ) (env )) obj)) (add-method brenda-eval (method ((obj ) (env )) (lookup obj env)))

6 Brenda-eval (cont.) (add-method brenda-eval (method ((obj ) (env )) (brenda-apply (brenda-eval (head obj) env) (tail obj) env))) (define eval-sequence (method ((seq ) (env )) (cond ((null? seq) #f) ((null? (tail seq)) (brenda-eval (head seq) env)) (else: (brenda-eval (head seq) env) (eval-sequence (tail seq) env)))))

7 Brenda-apply (define-generic-function brenda-apply ((obj ) (args ) (env ))) (add-method brenda-apply (method ((obj ) (args ) (env )) (brenda-error "Apply : First element not a function.” obj)))

8 Brenda-apply for primitive functions (add-method brenda-apply (method ((obj ) (args ) (env )) (bind (((evaluated-args ) (map (method (x) (brenda-eval x env)) args))) (args-params-match? (params obj) evaluated-args) (apply (calls obj) evaluated-args)))

9 Brenda-apply for Brenda special forms (add-method brenda-apply (method ((obj ) (args ) (env )) ((calls obj) args env)))

10 Brenda-apply for user defined methods (add-method brenda-apply (method ((obj ) (args ) (e )) (bind (((evaluated-args ) (map (method (x) (brenda-eval x e)) args))) (args-params-match? (params obj) evaluated-args) (bind ((new-env (expand-environment (params obj) evaluated-args (env obj)))) (eval-sequence (body obj) new-env)))))

11 Bindings (define-class ( ) (variable ) (value )) (define make-brenda-binding (method ((var ) (val )) (make variable: var value: val)))

12 Environments (define-class ( )) (define-class ( ) (bindings ) (previous )) (define make-brenda-local-frame (method ((prev )) (make bindings: '() previous: prev)))

13 Expand an environment p = params a = arguments e = environment (define expand-environment (method ((p ) (a ) (e )) (bind (((e1 ) (make-brenda-local-frame e))) (set! (bindings e1) (map make-brenda-binding (map variable p) a)) e1)))


Download ppt "The BRENDA interpreter A interpreter for a subset of Dylan written in Dylan."

Similar presentations


Ads by Google