Skip to content
Extraits de code Groupes Projets
Valider 03964095 rédigé par Nicolas Verbois's avatar Nicolas Verbois
Parcourir les fichiers

Tentative step 2 de mieux gérer les erreurs et de faire un lookup

parent 5d6efe83
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
#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))
(define (add p)
(point (+ x (p 'getx)) (+ y (p 'gety))))
(define (self m)
(cond ((eq? m 'getx) x)
((eq? m 'gety) y)
((eq? m 'type) 'point)
((eq? m 'info) (list (self 'type) (self 'getx) (self 'gety)) )
((eq? m 'setx!) setx )
((eq? m 'add) add)
(else (begin (display "Message not understood") #f))))
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)
(if (lookup p m)
(p m)
(display "Message d'erreur redondant")
))
(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") )
)
(define (lookup p m)
(if (eq? #f (p m) )
#f
#t)
)
; 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 p (send p1 'add p2))
(display ( send p 'info )) ; should display : (point 4 6)
(newline)
(send p1 'setx! 5)
(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)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter