diff --git a/step_2.rkt b/step_2.rkt index adbab249741931324f4a48f55916492b97ea6fe0..108b0930c639e837fc01508e82d030064a623dfb 100644 --- a/step_2.rkt +++ b/step_2.rkt @@ -1,5 +1,17 @@ #lang r5rs +; Authors : Nicolas Verbois - 1366 1600 +; Nicolas Rosar - 1443 1900 +; Group F +; In this step, we have added a new operator, whose name is "send". +; It will allow us to improve the syntax of the code, by allowing to +; send messages to objects in a clearer manner. + +; Defined below, the send operater first looks if the receiver object is indeed +; an appropriate receiver object, else it returns a specific error. +; Then, it will try to apply the message.s to the object. + +; A point object, constituted of two coordinates and returning a self function. (define (point x y) (define (setx value) (set! x value)) @@ -15,30 +27,69 @@ (else (display "Message not understood")))) self) + +; The new send operator +; It takes as argument a point and at least one message. + +; If send receives only a point and a message ( two parameters ), +; then it will simply call the self function on this message. +; Thus, (send p m) => (p m) + +; If send receives more than two arguments, it will assume that +; it is a message with additional parameters, such as the add operator, +; that should be translated as follow : +; (send p m arg) => ((p m) arg) (define (send p m . args) (if (procedure? p) (cond ((null? args) (p m)) - ; Checker length args - ;((eq? m 'add) (p 'add - (else ((p m) (car args) ) ) + (else ((p m) (car args))) ; We have to take the "car" of the args list ) + ; If the first parameter, p, is not a procedure, then we return a special error (display "Inappropriate receiver object") ) ) +; Sample code illustrating the execution of the step + +; We start by creating two points, p1 and p2 + +(define p1 (point 1 2)) +(define p2 (point 3 4)) + +(display (p1 'getx)) ; should display : 1 +(newline) + +(display (p1 'gety)) ; should display : 2 +(newline) + +(display '----------) +(newline) + +(display (p2 'getx)) ; should display : 3 +(newline) + +(display (p2 'gety)) ; should display : 4 +(newline) + +(display '----------) +(newline) -( define p1 ( point 1 2 ) ) -( define p2 ( point 3 4 ) ) -(display ( send p1 'getx )) ; 1 -(display ( send p1 'gety )) ; 2 -(display ( send p2 'getx )) ; 3 -(display ( send p2 'gety )) ; 4 -;(display ( send p1 'add p2)) ;(4 6) (define p (send p1 'add p2)) -(display ( send p 'info )) ; ( p o i n t 4 6) +(display ( send p 'info )) ; should display : (point 4 6) +(newline) (send p1 'setx! 5) -(display (send p1 'getx )) ; r e t u r n s 5 -(display (send 'add 'getx)) \ No newline at end of file +(display (send p1 'getx )) ; should display : 5 +(newline) + +(display ( send 'not-a-point 'info )) ; should display : Inappropriate receiver object +(newline) + +(display (send p 'foo)) ; should display : Message not understood +(newline) + +(display (send p 'bar 2)) ; should display : Message not understood, then crash +(newline) +