2003-01-19 11:57:27 -05:00
|
|
|
(define-structure surflet surflet-interface
|
|
|
|
(open surflets
|
2003-04-16 08:30:57 -04:00
|
|
|
surflets/my-input-fields
|
2003-02-17 05:09:24 -05:00
|
|
|
surflet-requests
|
2002-10-02 11:14:53 -04:00
|
|
|
handle-fatal-error
|
|
|
|
let-opt
|
2002-12-08 10:49:27 -05:00
|
|
|
scheme-with-scsh)
|
2002-09-27 11:31:26 -04:00
|
|
|
(begin
|
|
|
|
|
2002-10-02 15:13:14 -04:00
|
|
|
;; This doesn't use c-a-l-l-b-a-c-k-s anymore.
|
2002-09-27 11:31:26 -04:00
|
|
|
|
|
|
|
(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
|
2003-04-16 08:30:57 -04:00
|
|
|
name "operator"
|
2002-09-27 11:31:26 -04:00
|
|
|
(lambda (operator-string)
|
2003-04-16 08:30:57 -04:00
|
|
|
(let ((operator (assoc operator-string *operator-alist*)))
|
|
|
|
(if operator
|
|
|
|
operator
|
|
|
|
(error "no such operator" operator-string))))
|
|
|
|
'()
|
|
|
|
(lambda (input-field)
|
|
|
|
`(select (@ (name ,name))
|
|
|
|
,@(map (lambda (operator)
|
|
|
|
`(option ,(operator-symbol operator)))
|
|
|
|
*operator-alist*))))))
|
2002-09-27 11:31:26 -04:00
|
|
|
|
2002-10-02 11:14:53 -04:00
|
|
|
|
2003-07-08 17:22:06 -04:00
|
|
|
(define (make-number-field/default default)
|
2002-10-02 11:14:53 -04:00
|
|
|
(if default
|
2003-07-08 17:22:06 -04:00
|
|
|
(make-number-field default)
|
|
|
|
(make-number-field)))
|
2002-10-02 11:14:53 -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-02 14:22:19 -04:00
|
|
|
(calculate-button (make-submit-button "Calculate"))
|
|
|
|
(change-button (make-submit-button "Change operator"))
|
2002-10-02 11:14:53 -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-02 11:14:53 -04:00
|
|
|
,new-url
|
|
|
|
(table
|
|
|
|
(tr (td "Do calculation:"))
|
|
|
|
(tr (td ,number-field1)
|
|
|
|
(td ,(operator-symbol operator-pair))
|
|
|
|
(td ,number-field2)
|
|
|
|
(td " = ")
|
2002-10-02 14:22:19 -04:00
|
|
|
(td ,calculate-button)))
|
|
|
|
(hr)
|
|
|
|
(p "You may choose another operator:")
|
2002-10-02 11:14:53 -04:00
|
|
|
(table
|
|
|
|
(tr (td ,operator-input-field)
|
2002-10-21 04:38:46 -04:00
|
|
|
(td ,change-button)))
|
|
|
|
(hr)
|
2003-03-09 15:15:08 -05:00
|
|
|
(p (url "/" "Return to main menu."))))))))
|
2002-10-02 11:14:53 -04:00
|
|
|
(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:38:46 -04:00
|
|
|
(cond
|
2003-02-19 13:42:45 -05:00
|
|
|
((returned-via? calculate-button bindings)
|
2002-10-02 14:22:19 -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
|
2002-10-21 04:38:46 -04:00
|
|
|
"Please enter a valid first number.")))
|
2003-02-19 13:42:45 -05:00
|
|
|
((returned-via? change-button bindings)
|
2002-10-21 04:38:46 -04:00
|
|
|
(with-fatal-error-handler
|
|
|
|
(lambda (c d)
|
|
|
|
;; This should never happen.
|
|
|
|
(show-page operator-pair #f #f
|
|
|
|
"Internal error. Please retry or report."))
|
|
|
|
(show-page (input-field-value operator-input-field
|
|
|
|
bindings)
|
|
|
|
number1 number2)))
|
|
|
|
(else
|
|
|
|
;; This should never happen.
|
|
|
|
(show-page operator-pair #f #f
|
|
|
|
"Internal error. Please retry or report."))))))
|
|
|
|
|
2002-10-02 11:14:53 -04:00
|
|
|
(define (calculate operator-pair number1 number2)
|
|
|
|
(let ((operator (operator-operator operator-pair)))
|
2002-09-27 11:31:26 -04:00
|
|
|
(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
|
2002-10-21 04:38:46 -04:00
|
|
|
" = " ,result)
|
|
|
|
(hr)
|
2003-03-09 15:15:08 -05:00
|
|
|
(p (url "calculate.scm" "Make new calculation") (br)
|
|
|
|
(url "/" "Return to main menu."))))))
|
2002-09-27 11:31:26 -04:00
|
|
|
|
|
|
|
(define (main req)
|
2002-10-02 11:14:53 -04:00
|
|
|
(show-page (car *operator-alist*) #f #f)
|
2002-09-27 11:31:26 -04:00
|
|
|
(error "This does not return"))
|
|
|
|
))
|