From 68b9ce044be52b14010e6a3ea7f277539bf9cdf7 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Mon, 19 Oct 2026 14:48:43 +0300 Subject: [PATCH] read a quoted name as the name '-fields read as (- fields), so (cat name '-fields) built name(- fields). A quoted list keeps the rule: it is code a macro returns as often as not. --- reader.scm | 15 +++++++++++++-- tests/reader.scm | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/reader.scm b/reader.scm index fa17cc3..5110972 100644 --- a/reader.scm +++ b/reader.scm @@ -111,7 +111,7 @@ (let ((line (current-line))) (stamp (read-comment port) line))) ((char=? c #\") (read-string-lit port)) - ((char=? c #\') (get-ch port) (list 'quote (read-datum port))) + ((char=? c #\') (get-ch port) (list 'quote (read-quoted port))) ((char=? c #\`) (get-ch port) (list 'quasiquote (read-datum port))) ((char=? c #\,) (get-ch port) @@ -193,6 +193,16 @@ (finish-atom (list->string (reverse chars))) (begin (get-ch port) (loop (cons c chars))))))) +;;; ...except quoted, alone: '-fields is a name, as in (cat name '-fields). +;;; A quoted list is code a macro hands back as often as not, and keeps +;;; the rule +(define negating? (make-parameter #t)) + +(define (read-quoted port) + (if (memv (peek port) '(#\( #\[ #\' #\` #\, #\# #\" #\;)) + (read-datum port) + (parameterize ((negating? #f)) (read-datum port)))) + (define (finish-atom s) (let ((digits (if (substring-index "_" s) (or (ungrouped s (or (c-radix s) 10)) s) s))) ;; 017 is 17 to Scheme and 15 to C: whoever reads the Sex and whoever @@ -353,7 +363,8 @@ ;;; -INFINITY is (- INFINITY), as -1 is minus one. No name begins with ;;; a dash, and an operator that does goes on with a non-letter: -> -- (define (negated-name? s) - (and (> (string-length s) 1) + (and (negating?) + (> (string-length s) 1) (char=? #\- (string-ref s 0)) (let ((c (string-ref s 1))) (or (char-alphabetic? c) (char=? c #\_))))) diff --git a/tests/reader.scm b/tests/reader.scm index 9c6d078..25a6895 100644 --- a/tests/reader.scm +++ b/tests/reader.scm @@ -60,6 +60,10 @@ (reader-test '((- _x)) "-_x") (reader-test '(-1 -1.5) "-1 -1.5") (reader-test '(- -> ->> -- -=) "- -> ->> -- -=") + ;; ...but a quoted name is a name: (cat name '-fields) built `name(- fields)' + (reader-test '((quote -fields)) "'-fields") + ;; while a quoted list is code a macro hands back as often as not + (reader-test '((quote ((- a) b))) "'(-a b)") ;; a number in another radix, spelled the way C does or the way ;; Scheme does; either is an ordinary integer from here on