don't crash on errors in evaluate

master
opfez 2021-04-09 06:05:47 +00:00
parent 7a75544e46
commit dc699e006b
1 changed files with 10 additions and 4 deletions

View File

@ -3,7 +3,9 @@
(use-modules (ice-9 rdelim) (use-modules (ice-9 rdelim)
(ice-9 textual-ports) (ice-9 textual-ports)
(ice-9 format)) (ice-9 format)
(ice-9 control)
(rnrs exceptions))
(define +channels+ '("#tildetown" "#bots")) (define +channels+ '("#tildetown" "#bots"))
@ -67,13 +69,18 @@
(format stream "JOIN ~a\r\n" chn)) (format stream "JOIN ~a\r\n" chn))
+channels+)) +channels+))
(define (send-message stream chn msg) (define (send-message stream chn msg)
(format stream "PRIVMSG ~a :~a\r\n" chn msg)) (format stream "PRIVMSG ~a :~a\r\n" chn msg))
(define (send-action stream chn action) (define (send-action stream chn action)
(format stream "PRIVMSG ~a :\x01ACTION ~a\x01\r\n" chn action)) (format stream "PRIVMSG ~a :\x01ACTION ~a\x01\r\n" chn action))
(define (evaluate str)
(guard (ex
(else (begin (display "error encountered!\n")
'error)))
(eval (read (open-input-string str)) (interaction-environment))))
(define (main-loop stream) (define (main-loop stream)
(let* ((inl (read-line stream)) (let* ((inl (read-line stream))
(data (string-tokenize inl)) (data (string-tokenize inl))
@ -88,8 +95,7 @@
(send-message stream chn "Hello! I respond to !rollcall, !anna, and !eval <s-expr>.")) (send-message stream chn "Hello! I respond to !rollcall, !anna, and !eval <s-expr>."))
((equal? ":!eval" (slist-ref data 3)) ((equal? ":!eval" (slist-ref data 3))
(let* ((expr (string-concatenate (intersperse " " (cdr (member ":!eval" data)))))) (let* ((expr (string-concatenate (intersperse " " (cdr (member ":!eval" data))))))
(send-message stream chn (eval (read (open-input-string expr)) (send-message stream chn (evaluate expr))))
(interaction-environment)))))
((and (= 7 (length data)) ((and (= 7 (length data))
(equal? (list-tail data 4) '("ACTION" "shoots" "anna"))) (equal? (list-tail data 4) '("ACTION" "shoots" "anna")))
(send-action stream chn "dies") (send-action stream chn "dies")