friendly-obj - ; obj.scm version 4.0 5/18/2000 ; -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
;;; obj.scm version 4.0 5/18/2000 ;;; -- implementation of the object-oriented syntax ;; By Matt Wright, based on a handout from MIT ;; Revised for STk by Brian Gaeke - removed scm and procedure->macro ;; ASK: send a message to an object ; The dot in the first line of the definition of ASK, below, makes it ; take a variable number of arguments. The first argument is associated ; with the formal parameter OBJECT; the second with MESSAGE; any extra ; actual arguments are put in a list, and that list is associated with ; the formal parameter ARGS. (If there are only two actual args, then ; ARGS will be the empty list.) ; APPLY takes two arguments, a procedure and a list, and applies the ; procedure to the things in the list, which are used as actual ; argument values. (load "tables.scm") (define *traced-method-table* (make-table)) (define *err-port* (current-error-port)) (define (show-args formals actuals) ) (define (indent) "") (define (ask object message . args) (let ((method (object message))) (if (method? method) (let ((trace-info (lookup *traced-method-table* method))) (if trace-info (begin (format *err-port* "~A -> ~A method ~A with " (indent) (car trace-info) (cadr trace-info)) (show-args (cddr trace-info) args) )) (let ((result (apply method args))) (if trace-info (begin (format *err-port* "~A <- ~A method ~A returns ~S\n" (indent) (car trace-info) (cadr trace-info) (result))))) ) (error "Instance of class " (cadr method) " does not respond to message: " message)))) (define (no-method name) (list 'no-method name)) (define (no-method? x) (if (pair? x) (eq? (car x) 'no-method) #f)) (define (method? x) (not (no-method? x)))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
;; INSTANTIATE and INSTANTIATE-PARENT: Create an instance of a class ; The difference is that only INSTANTIATE initializes the new object (define (instantiate class . arguments) (let ((new-instance (apply (class 'instantiate) arguments))) (ask new-instance 'initialize new-instance) new-instance)) (define (instantiate-parent class . arguments) (apply (class 'instantiate) arguments)) ;; GET-METHOD: Send a message to several objects and return the first ;; method found (for multiple inheritance) (define (get-method give-up-name message . objects) (if (null? objects) (no-method give-up-name)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2010 for the course EECS 21281 taught by Professor Harvey during the Spring '10 term at University of California, Berkeley.

Page1 / 6

friendly-obj - ; obj.scm version 4.0 5/18/2000 ; -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online