diff --git a/reader.scm b/reader.scm index 25ccee8..d83555b 100644 --- a/reader.scm +++ b/reader.scm @@ -114,7 +114,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) @@ -196,6 +196,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 @@ -356,7 +366,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