2003-01-19 11:57:27 -05:00
|
|
|
(define-structure surflet surflet-interface
|
|
|
|
(open surflets
|
2003-04-14 05:10:54 -04:00
|
|
|
surflets/callbacks ;make-callback
|
2003-02-17 05:09:24 -05:00
|
|
|
surflet-requests
|
2002-10-21 04:41:00 -04:00
|
|
|
handle-fatal-error
|
|
|
|
let-opt
|
2002-12-08 10:49:27 -05:00
|
|
|
scheme-with-scsh)
|
2002-10-21 04:41:00 -04:00
|
|
|
(begin
|
|
|
|
|
|
|
|
;; This uses callbacks.
|
|
|
|
|
|
|
|
(define *operator-alist*
|
|
|
|
`(("+" . ,+)
|
|
|
|
("-" . ,-)
|
|
|
|
("*" . ,*)
|
|
|
|
("/" . ,/)))
|
|
|
|
|
|
|
|
(define operator-symbol car)
|
|
|
|
(define operator-operator cdr)
|
|
|
|
|
|
|
|
(define operator-input-field
|
|
|
|
(let ((name (generate-input-field-name "operator")))
|
|
|
|
(make-input-field
|
|
|
|
name
|
|
|
|
(lambda (operator-string)
|
|
|
|
(cond
|
|
|
|
((assoc operator-string *operator-alist*) =>
|
|
|
|
(lambda (a) a))
|
|
|
|
(else
|
|
|
|
(error "no such operator" operator-string))))
|
|
|
|
`(select (@ (name ,name))
|
|
|
|
,@(map (lambda (operator)
|
|
|
|
`(option ,(operator-symbol operator)))
|
|
|
|
*operator-alist*)))))
|
|
|
|
|
|
|
|
(define (change-operator-callback)
|
|
|
|
(make-callback
|
|
|
|
(lambda (req)
|
|
|
|
(change-operator
|
2002-11-07 15:41:35 -05:00
|
|
|
;; This yields an error only when the browser is doing wrong.
|
2002-10-21 04:41:00 -04:00
|
|
|
(input-field-value operator-input-field
|
|
|
|
(get-bindings req))))
|
|
|
|
))
|
|
|
|
|
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-number-field/default default)
|
2002-10-21 04:41:00 -04:00
|
|
|
(if default
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-number-field `(@ (value ,default)))
|
|
|
|
(make-number-field)))
|
2002-10-21 04:41:00 -04:00
|
|
|
|
|
|
|
(define (show-page operator-pair number1 number2 . maybe-update-text)
|
|
|
|
(let* ((update-text (:optional maybe-update-text ""))
|
2003-07-08 17:22:06 -04:00
|
|
|
(number-field1 (make-number-field/default number1))
|
|
|
|
(number-field2 (make-number-field/default number2))
|
2002-10-21 04:41:00 -04:00
|
|
|
(req
|
|
|
|
(send-html/suspend
|
|
|
|
(lambda (new-url)
|
|
|
|
`(html
|
|
|
|
(title "Simple calculator")
|
|
|
|
(body (h1 "Simple calculator")
|
|
|
|
(font (@ (color "red")) ,update-text)
|
2003-01-19 11:57:27 -05:00
|
|
|
(surflet-form
|
2002-10-21 04:41:00 -04:00
|
|
|
,new-url
|
|
|
|
(table
|
|
|
|
(tr (td "Do calculation:"))
|
|
|
|
(tr (td ,number-field1)
|
|
|
|
(td ,(operator-symbol operator-pair))
|
|
|
|
(td ,number-field2)
|
|
|
|
(td " = ")
|
|
|
|
(td ,(make-submit-button '(@ (value "calculate")))))))
|
|
|
|
(hr)
|
|
|
|
(p "You may choose another operator:")
|
2003-01-19 11:57:27 -05:00
|
|
|
(surflet-form
|
2002-10-21 04:41:00 -04:00
|
|
|
,(change-operator-callback)
|
|
|
|
(table
|
|
|
|
(tr (td ,operator-input-field)
|
|
|
|
(td ,(make-submit-button
|
|
|
|
'(@ (value "change operator"))))))))))))
|
|
|
|
(bindings (get-bindings req)))
|
2002-11-07 15:41:35 -05:00
|
|
|
(let ((number1 (input-field-value number-field1 bindings))
|
|
|
|
(number2 (input-field-value number-field2 bindings)))
|
2002-10-21 04:41:00 -04:00
|
|
|
(if number1
|
|
|
|
(if number2
|
|
|
|
(calculate operator-pair number1 number2)
|
|
|
|
(show-page operator-pair number1 number2 "Please enter a valid second number."))
|
|
|
|
(show-page operator-pair number1 number2 "Please enter a valid first number."))
|
|
|
|
)))
|
|
|
|
|
|
|
|
(define (change-operator to-operation)
|
|
|
|
(show-page to-operation #f #f))
|
|
|
|
|
|
|
|
(define (calculate operator-pair number1 number2)
|
|
|
|
(let ((operator (operator-operator operator-pair)))
|
|
|
|
(show-result number1 (operator-symbol operator-pair) number2
|
|
|
|
(operator number1 number2))))
|
|
|
|
|
|
|
|
(define (show-result number1 operator-symbol number2 result)
|
|
|
|
(send-html
|
|
|
|
`(html (title "Calculation Result")
|
|
|
|
(body (h1 "Result")
|
|
|
|
(p ,number1 " " ,operator-symbol " " ,number2
|
|
|
|
" = " ,result)))))
|
|
|
|
|
|
|
|
(define (main req)
|
|
|
|
(show-page (car *operator-alist*) #f #f)
|
|
|
|
(error "This does not return"))
|
|
|
|
))
|