From dcbe7da02a128b85d29f7157c0318a8eaff545b4 Mon Sep 17 00:00:00 2001 From: yfzhe Date: Sun, 14 Jan 2024 16:52:37 +0800 Subject: [PATCH] eval: handle the case when code only contains the #lang line --- eval-bot/eval.rkt | 2 +- eval-bot/main.rkt | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/eval-bot/eval.rkt b/eval-bot/eval.rkt index a299951..f27f6ba 100644 --- a/eval-bot/eval.rkt +++ b/eval-bot/eval.rkt @@ -22,7 +22,7 @@ ;; access its value as `read-syntax` which read code in interactive mode. (define (split-code code) - (match (regexp-match #rx"^#lang (.+)\n(.+)" code) + (match (regexp-match #rx"^#lang (.+)\n?(.*)" code) [(list _ lang body) (values (string->symbol lang) body)] [_ diff --git a/eval-bot/main.rkt b/eval-bot/main.rkt index 91972ef..2932d12 100644 --- a/eval-bot/main.rkt +++ b/eval-bot/main.rkt @@ -34,9 +34,9 @@ [(regexp #rx"^/help") (help message)] [(regexp #px"^/eval\\b(@[a-z_]+)?(.+)$" (list _ _ code)) - (eval message (string-trim code))] + (eval message (string-trim code) 'racket)] [(regexp #px"^/eval_chez\\b(@[a-z_]+)?(.+)$" (list _ _ code)) - (eval-chez message (string-trim code))] + (eval message (string-trim code) 'chez)] [_ (cond [(equal? (ref (message : message) .chat .type) @@ -72,19 +72,15 @@ END #:parse-mode "MarkdownV2" #:text "Not supported command")) -(define (eval message code) - (define result (eval-code code)) +;; mode: 'racket or 'chez +(define (eval message code mode) + (define result + (match mode + ['racket (eval-code code)] + ['chez (eval-code/chez code)])) (make-response #:chat-id (ref (message : message) .chat .id) #:reply (make-reply-params - #:message-id (ref (message : message) .id)) - #:parse-mode "HTML" - #:text result)) - -(define (eval-chez message code) - (define result (eval-code/chez code)) - (make-response #:chat-id (ref (message : message) .chat .id) - #:reply (make-reply-params - #:message-id (ref (message : message) .id)) + #:message-id (message-id message)) #:parse-mode "HTML" #:text result))