read: hash constants and directives

This commit is contained in:
Peter McGoron 2024-09-27 15:40:30 -04:00
parent 0547488917
commit b94e12ee33
2 changed files with 38 additions and 13 deletions

View File

@ -35,7 +35,6 @@
;;; All tokens are procedure-encapsulated objects, since the reader should ;;; All tokens are procedure-encapsulated objects, since the reader should
;;; never return a literal procedure. Each procedure has a TYPE message. ;;; never return a literal procedure. Each procedure has a TYPE message.
(load "chez-compat.scm") (load "chez-compat.scm")
(load "util.scm") (load "util.scm")
(load "set.scm") (load "set.scm")
@ -515,16 +514,6 @@
port)))))) port))))))
loop)) loop))
;;; Encapsulate LINKED-LIST object with an additional value for the
;;; toplevel table.
(define linked-list/toplevel:new
(lambda (toplevel)
(let ((ll (linked-list:new)))
(lambda (op . args)
(cond
((eq? op 'toplevel) toplevel)
(else (apply ll op args)))))))
(define read:datum-label (define read:datum-label
(lambda (location toplevel) (lambda (location toplevel)
(let ((finalized? #f) (let ((finalized? #f)
@ -597,9 +586,42 @@
(port 'restore-mutable! mutable)) (port 'restore-mutable! mutable))
(readtable:next toplevel #f port))) (readtable:next toplevel #f port)))
;;; String map from constants to procedures with formal arguments
;;; (TOPLEVEL PORT)
;;; with TOPLEVEL being the current toplevel table and PORT being the
;;; current port being read from.
;;;
;;; Each one must return something. Directives that return nothing must
;;; call the toplevel again.
(define read:hash-constants
(smap:insert-many
'()
(cons "true" (lambda unused #t))
(cons "false" (lambda unused #f))
(cons "t" (lambda unused #t))
(cons "f" (lambda unused #f))
(cons "!fold-case"
(lambda (toplevel port)
(port 'fold-case! #t)
(readtable:act toplevel (port 'read) #f port)))
(cons "!no-fold-case"
(lambda (toplevel port)
(port 'fold-case! #f)
(readtable:act toplevel (port 'read) #f port)))))
(define read:read-hash-constants
(lambda (_ char acc port)
(let ((ident (readtable:read-ident #f char #f port)))
(let ((container (smap:search read:hash-constants
(ident 'value))))
(if (null? container)
(error 'read-hash-constants 'unknown-constant
(ident 'value))
((map:val container) acc port))))))
(define readtable:hash (define readtable:hash
(readtable:process (readtable:process
(readtable:empty/default (readtable:error 'hash "unimplemented")) (readtable:empty/default read:read-hash-constants)
(list readtable:update #\| (readtable:jump/next readtable:block-comment)) (list readtable:update #\| (readtable:jump/next readtable:block-comment))
(list readtable:update #\; readtable:datum-comment) (list readtable:update #\; readtable:datum-comment)
(list readtable:update-list readtable:digits ; Datum labels (list readtable:update-list readtable:digits ; Datum labels
@ -712,3 +734,6 @@
(read-all "#(a b #(c #|close#|comment|#|#y))") (read-all "#(a b #(c #|close#|comment|#|#y))")
(read-all "(this has a #;(call with (current continuation)) datum comment)") (read-all "(this has a #;(call with (current continuation)) datum comment)")
(read-all "#0=(#0# not unix)") (read-all "#0=(#0# not unix)")
(read-all "#!no-fold-case #!fold-case #!NO-FOLD-CASE #false")
(read-all "#!fold-case #TRUE")

View File

@ -463,7 +463,7 @@
(define smap:insert-many (define smap:insert-many
(lambda (smap . pairs) (lambda (smap . pairs)
(fold (lambda (pair smap) (fold (lambda (pair smap)
(smap:insert smap (car pair) (cdr pair))) (car (smap:insert smap (car pair) (cdr pair))))
smap smap
pairs))) pairs)))