2024-09-07 17:47:10 -04:00
|
|
|
;;; Copyright (C) Peter McGoron 2024
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation, version 3 of the License.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful,
|
|
|
|
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; R7RS reader. This is the lexer-parser end, so it returns tokens and
|
|
|
|
;;; not concrete objects.
|
2024-09-22 09:46:58 -04:00
|
|
|
;;;
|
|
|
|
;;; Notes:
|
|
|
|
;;;
|
|
|
|
;;; Port stores datum labels. Datum labels are stored for the entirety of
|
|
|
|
;;; a READ: this is to emulate MIT Scheme, which allows for datum labels
|
|
|
|
;;; outside of the datum that the label appears in.
|
|
|
|
;;;
|
|
|
|
;;; The reader does not return Scheme data: it returns annotated data
|
|
|
|
;;; containing the source location, datum label number, resolved datum
|
|
|
|
;;; label pointer. This is for advanced syntax systems.
|
|
|
|
;;;
|
|
|
|
;;; How datum labels could work:
|
|
|
|
;;;
|
|
|
|
;;; When encountering #[number]=, allocate a datum label and assign it
|
|
|
|
;;; nothing. Then call READ after "=", and destructively update the
|
|
|
|
;;; datum label with the resulting datum. A pass over the new read
|
|
|
|
;;; structure to convert it to regular Scheme data will resolve the
|
|
|
|
;;; indirection.
|
|
|
|
;;;
|
2024-09-22 11:48:26 -04:00
|
|
|
;;; All tokens are procedure-encapsulated objects, since the reader should
|
|
|
|
;;; never return a literal procedure. Each procedure has a TYPE message.
|
|
|
|
|
2024-09-07 17:47:10 -04:00
|
|
|
(load "chez-compat.scm")
|
|
|
|
(load "util.scm")
|
|
|
|
(load "set.scm")
|
2024-09-20 19:11:36 -04:00
|
|
|
(load "linked-list.scm")
|
2024-09-07 17:47:10 -04:00
|
|
|
|
2024-09-20 22:27:17 -04:00
|
|
|
;;; My text editor cannot parse Scheme's character syntax.
|
|
|
|
|
|
|
|
(define %bol #\()
|
|
|
|
(define %eol #\))
|
|
|
|
|
2024-09-07 17:47:10 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Port reader wrapper
|
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(define port->read-function
|
|
|
|
(lambda (port)
|
|
|
|
(lambda ()
|
|
|
|
(read-char port))))
|
|
|
|
|
2024-09-07 18:44:18 -04:00
|
|
|
;;; READ:
|
|
|
|
;;;
|
2024-09-22 09:46:58 -04:00
|
|
|
;;; Stream readers contain mutable state. This is the case-folding mode
|
|
|
|
;;; and the current list of datum labels.
|
|
|
|
;;;
|
2024-09-07 18:44:18 -04:00
|
|
|
;;; (POS): Return (LIST FILENAME LINE-NUMBER OFFSET).
|
|
|
|
;;; (READ): Read the next character in the stream. Returns #F on EOF.
|
|
|
|
;;; (PUSH CHAR): Push CHAR such that it will be the next character read
|
|
|
|
;;; when (READ) is called.
|
2024-09-08 08:44:42 -04:00
|
|
|
;;; (PEEK): Read character, push it back, and return it.
|
2024-09-07 21:10:40 -04:00
|
|
|
;;; (FOLD-CASE?): Returns a boolean if case folding is enabled.
|
|
|
|
;;; (FOLD-CASE! BOOL): Sets the case folding to BOOL.
|
2024-09-07 17:47:10 -04:00
|
|
|
(define port->read
|
2024-10-13 10:32:28 -04:00
|
|
|
(lambda (read-function filename)
|
|
|
|
(let ((line-number 1)
|
|
|
|
(offset 0)
|
2024-09-27 11:16:25 -04:00
|
|
|
(pushback-buffer '())
|
|
|
|
(datum-labels '())
|
|
|
|
(fold-case? #f))
|
|
|
|
(letrec ((update-position!
|
|
|
|
(lambda (ch)
|
|
|
|
(cond
|
|
|
|
((eqv? ch #\newline)
|
|
|
|
(set! line-number (+ 1 line-number)) (set! offset 0))
|
|
|
|
;; OFFSET is sometimes set to #F to denote an unknown
|
|
|
|
;; offset.
|
|
|
|
(offset (set! offset (+ 1 offset))))))
|
2024-09-27 11:26:30 -04:00
|
|
|
(location
|
|
|
|
(lambda () (list filename line-number offset)))
|
2024-09-27 11:16:25 -04:00
|
|
|
(set-datum-label!
|
|
|
|
(lambda (label value)
|
|
|
|
(set! datum-labels
|
|
|
|
(car (smap:insert datum-labels label value)))))
|
|
|
|
(get-datum-label
|
|
|
|
(lambda (label)
|
|
|
|
(smap:search datum-labels label)))
|
2024-09-27 12:26:26 -04:00
|
|
|
(clear-datum-labels!
|
|
|
|
(lambda ()
|
|
|
|
(set! datum-labels '())))
|
2024-09-27 11:16:25 -04:00
|
|
|
(dump-mutable
|
|
|
|
(lambda ()
|
|
|
|
(list datum-labels fold-case?)))
|
|
|
|
(restore-mutable!
|
|
|
|
(lambda (state)
|
|
|
|
(set! datum-labels (car state))
|
|
|
|
(set! fold-case? (cadr state))))
|
|
|
|
(process
|
|
|
|
(lambda (ch)
|
|
|
|
(update-position! ch)
|
|
|
|
(cond
|
2024-10-13 10:32:28 -04:00
|
|
|
((or (eof-object? ch) (not ch)) ch)
|
2024-09-27 11:16:25 -04:00
|
|
|
(fold-case? (char-downcase ch))
|
|
|
|
(else ch))))
|
|
|
|
(port
|
|
|
|
(lambda (op . args)
|
|
|
|
;; TODO: turn into string map?
|
|
|
|
(cond
|
2024-09-27 11:26:30 -04:00
|
|
|
((eq? op 'location) (location))
|
2024-09-27 11:16:25 -04:00
|
|
|
((eq? op 'read)
|
|
|
|
(process
|
|
|
|
(if (null? pushback-buffer)
|
|
|
|
(read-function)
|
|
|
|
(let ((ch (car pushback-buffer)))
|
|
|
|
(set! pushback-buffer (cdr pushback-buffer))
|
|
|
|
ch))))
|
|
|
|
((eq? op 'peek)
|
|
|
|
(let ((ch (port 'read)))
|
|
|
|
(port 'push ch)
|
|
|
|
ch))
|
|
|
|
((eq? op 'push)
|
|
|
|
(let ((ch (car args)))
|
|
|
|
(if (eqv? ch #\newline)
|
|
|
|
(begin
|
|
|
|
(set! line-number (- line-number 1))
|
|
|
|
(set! offset #f))
|
|
|
|
(set! offset (- offset 1)))
|
|
|
|
(set! pushback-buffer (cons ch pushback-buffer))))
|
|
|
|
((eq? op 'fold-case?) fold-case?)
|
|
|
|
((eq? op 'fold-case!) (set! fold-case? (car args)))
|
|
|
|
((eq? op 'set-datum-label!) (apply set-datum-label! args))
|
|
|
|
((eq? op 'get-datum-label) (apply get-datum-label args))
|
2024-09-27 12:26:26 -04:00
|
|
|
((eq? op 'clear-datum-labels!) (apply clear-datum-labels! args))
|
|
|
|
((eq? op 'dump-mutable) (apply dump-mutable args))
|
|
|
|
((eq? op 'restore-mutable!) (apply restore-mutable! args))
|
2024-09-27 11:26:30 -04:00
|
|
|
(else (error 'read->port 'invalid (cons op args)))))))
|
2024-09-27 11:16:25 -04:00
|
|
|
port))))
|
2024-09-07 17:47:10 -04:00
|
|
|
|
|
|
|
;;; ;;;;;;;;;;;;;;
|
|
|
|
;;; Character maps
|
|
|
|
;;; ;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(define integer<=>
|
|
|
|
(lambda (x y)
|
|
|
|
(cond
|
|
|
|
((< x y) '<)
|
|
|
|
((= x y) '=)
|
|
|
|
(else '>))))
|
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Comparison on characters extended to #F, which is less than all
|
|
|
|
;;; characters.
|
|
|
|
(define char*<=>
|
2024-09-07 17:47:10 -04:00
|
|
|
(lambda (x y)
|
2024-09-08 08:22:39 -04:00
|
|
|
(cond
|
|
|
|
((and (not x) y) '<)
|
|
|
|
((and x (not y)) '>)
|
|
|
|
((and (not x) (not y) '=))
|
|
|
|
(else (integer<=> (char->integer x)
|
|
|
|
(char->integer y))))))
|
2024-09-07 17:47:10 -04:00
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
(define %charmap:<=> (set:<=>-to-map char*<=>))
|
2024-09-07 17:47:10 -04:00
|
|
|
(define %charmap:update (set:update %charmap:<=>))
|
|
|
|
|
|
|
|
(define charmap:update (map:update %charmap:update))
|
|
|
|
(define charmap:insert (map:insert %charmap:update))
|
|
|
|
(define charmap:search (map:search %charmap:<=>))
|
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;
|
2024-09-07 17:47:10 -04:00
|
|
|
;;; Readtable constructors
|
2024-09-22 00:46:41 -04:00
|
|
|
;;;
|
|
|
|
;;; Readtables are composed of a CHARMAP, which is a map from characters
|
|
|
|
;;; to actions, and a DEFAULT-ACTION, which is taken when there is no
|
|
|
|
;;; match in CHARMAP.
|
|
|
|
;;;
|
|
|
|
;;; An "action" is a procedure that takes four arguments:
|
|
|
|
;;;
|
|
|
|
;;; TABLE: The current table.
|
|
|
|
;;; CHAR: The character that was matched against the CHARMAP in TABLE.
|
|
|
|
;;; ACC: An arbitrary "accumulator" value that is different depending
|
|
|
|
;;; on the readtable in question.
|
|
|
|
;;; PORT: A port reader object.
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;;; (READTABLE:NEW DEFAULT-ACTION CHARMAP)
|
|
|
|
(define readtable:new cons)
|
|
|
|
|
|
|
|
(define %readtable:default-action car)
|
|
|
|
(define %readtable:charmap cdr)
|
|
|
|
|
|
|
|
;;; Run the action in TABLE assigned to CHAR, or the default action of
|
|
|
|
;;; TABLE if there is no entry for CHAR.
|
|
|
|
(define readtable:act
|
|
|
|
(lambda (table char acc port)
|
|
|
|
(let ((node (charmap:search (%readtable:charmap table)
|
|
|
|
char)))
|
|
|
|
(let ((action (if (null? node)
|
|
|
|
(%readtable:default-action table)
|
|
|
|
(map:val node))))
|
|
|
|
(action table char acc port)))))
|
|
|
|
|
2024-09-22 00:00:26 -04:00
|
|
|
;;; Run the action in TABLE with the next character from PORT.
|
|
|
|
(define readtable:next
|
|
|
|
(lambda (table acc port)
|
|
|
|
(readtable:act table (port 'read) acc port)))
|
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Return a new readtable where CHAR is bound to ACTION.
|
|
|
|
(define readtable:update
|
|
|
|
(lambda (table char action)
|
|
|
|
(readtable:new (%readtable:default-action table)
|
|
|
|
(car (charmap:insert
|
|
|
|
(%readtable:charmap table) char action)))))
|
|
|
|
|
2024-09-22 09:46:58 -04:00
|
|
|
;;; Update TABLE to act on all characters in LST with ACTION.
|
|
|
|
(define readtable:update-list
|
|
|
|
(lambda (table lst action)
|
|
|
|
(fold (lambda (char table)
|
|
|
|
(readtable:update table char action))
|
|
|
|
table
|
|
|
|
lst)))
|
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Construct new readtable with no characters in its map and
|
|
|
|
;;; DEFAULT-ACTION as the default action.
|
|
|
|
(define readtable:empty/default
|
2024-09-07 19:14:09 -04:00
|
|
|
(lambda (default-action)
|
2024-09-08 08:22:39 -04:00
|
|
|
(readtable:new default-action '())))
|
|
|
|
|
|
|
|
;;; Each value in FUNCTIONS is a list (PROCEDURE ARGS...) which is called
|
|
|
|
;;; like (PROCEDURE TABLE ARGS...) and returns a table.
|
|
|
|
(define readtable:process
|
|
|
|
(lambda (table . functions)
|
|
|
|
(fold (lambda (function table)
|
|
|
|
(apply (car function) table (cdr function)))
|
2024-09-07 18:37:17 -04:00
|
|
|
table
|
2024-09-08 08:22:39 -04:00
|
|
|
functions)))
|
2024-09-07 17:47:10 -04:00
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;
|
2024-09-22 00:46:41 -04:00
|
|
|
;;; Default actions
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;
|
2024-09-07 17:47:10 -04:00
|
|
|
|
2024-09-20 19:11:36 -04:00
|
|
|
;;; Return an error.
|
|
|
|
(define readtable:error
|
|
|
|
(lambda emsg
|
2024-09-22 11:48:26 -04:00
|
|
|
(lambda (table char acc port)
|
|
|
|
(error emsg char acc table port))))
|
2024-09-20 19:11:36 -04:00
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Discard the current character and continue reading the readtable.
|
|
|
|
(define readtable:skip
|
|
|
|
(lambda (table char acc port)
|
|
|
|
(readtable:act table (port 'read) acc port)))
|
|
|
|
|
2024-09-08 08:44:42 -04:00
|
|
|
;;; Discard char and return constant.
|
|
|
|
(define readtable:return
|
|
|
|
(lambda (return)
|
|
|
|
(lambda (table char acc port)
|
|
|
|
return)))
|
|
|
|
|
|
|
|
;;; Jump to a new readtable, discard it's return, and continue reading
|
|
|
|
;;; in the table.
|
|
|
|
(define readtable:jump-discard
|
|
|
|
(lambda (newtable)
|
|
|
|
(lambda (oldtable char acc port)
|
|
|
|
(readtable:act newtable (port 'read) '() port)
|
|
|
|
(readtable:act oldtable (port 'read) acc port))))
|
|
|
|
|
2024-09-20 19:11:36 -04:00
|
|
|
;;; Jump to a new readtable with the same characters.
|
|
|
|
(define readtable:jump
|
|
|
|
(lambda (newtable)
|
|
|
|
(lambda (oldtable char acc port)
|
|
|
|
(readtable:act newtable char acc port))))
|
|
|
|
|
2024-09-22 00:00:26 -04:00
|
|
|
;;; Jump to a new readtable, reading the new character, with the old
|
|
|
|
;;; readtable as ACC.
|
|
|
|
(define readtable:next/old-as-acc
|
|
|
|
(lambda (newtable)
|
|
|
|
(lambda (oldtable __ _ port)
|
|
|
|
(readtable:next newtable oldtable port))))
|
|
|
|
|
2024-09-22 00:46:41 -04:00
|
|
|
;;; Jump to a new readtable, reading the new character.
|
|
|
|
(define readtable:jump/next
|
|
|
|
(lambda (newtable)
|
|
|
|
(lambda (oldtable _ acc port)
|
|
|
|
(readtable:next newtable acc port))))
|
|
|
|
|
2024-09-21 23:52:55 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Identifier reader
|
|
|
|
;;; ;;;;;;;;;;;;;;;;;
|
|
|
|
|
2024-09-27 11:26:30 -04:00
|
|
|
(define read:ident
|
|
|
|
(lambda (name location)
|
|
|
|
(lambda (op . args)
|
|
|
|
(cond
|
|
|
|
((eq? op 'type) 'ident)
|
|
|
|
((eq? op 'value) name)
|
|
|
|
(else (error 'read:ident "invalid operation" op args))))))
|
|
|
|
|
|
|
|
(define read:ident-builder
|
2024-09-27 12:12:16 -04:00
|
|
|
(lambda (location . start-char)
|
2024-09-27 11:26:30 -04:00
|
|
|
(let ((char-list (linked-list:new)))
|
2024-09-27 12:12:16 -04:00
|
|
|
(if (not (null? start-char))
|
|
|
|
(char-list 'push (car start-char)))
|
2024-09-27 11:26:30 -04:00
|
|
|
(lambda (op . args)
|
|
|
|
(cond
|
|
|
|
((eq? op 'finalize->ident)
|
|
|
|
(read:ident (list->string (char-list 'to-list)) location))
|
|
|
|
(else (apply char-list op args)))))))
|
|
|
|
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Push back CHAR and return ACC.
|
2024-09-27 11:26:30 -04:00
|
|
|
(define readtable:return-acc-as-ident
|
2024-09-08 08:22:39 -04:00
|
|
|
(lambda (table char acc port)
|
2024-09-27 11:16:25 -04:00
|
|
|
(port 'push char)
|
2024-09-27 11:26:30 -04:00
|
|
|
(acc 'finalize->ident)))
|
2024-09-08 08:22:39 -04:00
|
|
|
|
|
|
|
;;; Push CHAR to ACC and continue reading from TABLE.
|
2024-09-20 19:11:36 -04:00
|
|
|
(define readtable:push-char
|
2024-09-08 08:22:39 -04:00
|
|
|
(lambda (table char acc port)
|
2024-09-27 11:16:25 -04:00
|
|
|
(acc 'push-tail char)
|
2024-09-20 19:11:36 -04:00
|
|
|
(readtable:act table (port 'read) acc port)))
|
2024-09-08 08:22:39 -04:00
|
|
|
|
|
|
|
;;; Define a readtable that constructs an identifier by accepting all
|
|
|
|
;;; characters that are not listed.
|
|
|
|
(define readtable:exclude-from-identifiers
|
|
|
|
(lambda (table excluded)
|
|
|
|
(fold (lambda (char table)
|
2024-09-27 11:26:30 -04:00
|
|
|
(readtable:update table char readtable:return-acc-as-ident))
|
2024-09-08 08:22:39 -04:00
|
|
|
table
|
|
|
|
excluded)))
|
|
|
|
|
|
|
|
;;; ASCII whitespace.
|
|
|
|
(define readtable:ASCII-whitespace
|
|
|
|
(list #\newline
|
|
|
|
#\space
|
|
|
|
(integer->char #x09)
|
|
|
|
(integer->char #x0B)
|
|
|
|
(integer->char #x0C)
|
|
|
|
(integer->char #x0D)))
|
|
|
|
|
|
|
|
;;; Readtable for identifiers.
|
|
|
|
(define readtable:identifier
|
|
|
|
(readtable:process
|
2024-09-20 19:11:36 -04:00
|
|
|
(readtable:empty/default readtable:push-char)
|
2024-09-08 08:22:39 -04:00
|
|
|
(list readtable:exclude-from-identifiers
|
|
|
|
readtable:ASCII-whitespace)
|
|
|
|
(list readtable:exclude-from-identifiers
|
2024-09-20 22:27:17 -04:00
|
|
|
(list #\| %bol %eol #\' #\; #f))))
|
2024-09-08 08:22:39 -04:00
|
|
|
|
|
|
|
;;; Read an identifier starting with CHAR.
|
|
|
|
(define readtable:read-ident
|
|
|
|
(lambda (table char acc port)
|
2024-09-27 11:26:30 -04:00
|
|
|
(readtable:act readtable:identifier
|
|
|
|
(port 'read)
|
2024-09-27 12:12:16 -04:00
|
|
|
(read:ident-builder (port 'location)
|
|
|
|
char)
|
2024-09-27 11:26:30 -04:00
|
|
|
port)))
|
2024-09-08 08:22:39 -04:00
|
|
|
|
2024-09-21 23:52:55 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Comments and whitespace reader
|
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2024-09-08 08:44:42 -04:00
|
|
|
;;; Readtable for a line comment.
|
|
|
|
(define readtable:read-to-newline
|
|
|
|
(readtable:process
|
|
|
|
(readtable:empty/default readtable:skip)
|
|
|
|
(list readtable:update #\newline (readtable:return #f))))
|
|
|
|
|
2024-09-21 23:52:55 -04:00
|
|
|
;;; ;;;;;;;;;;;
|
|
|
|
;;; List reader
|
|
|
|
;;;
|
|
|
|
;;; The reader updates the previous readtable to handle ). This means
|
|
|
|
;;; that this read table does not have to handle end-of-line, whitespace,
|
|
|
|
;;; etc.
|
|
|
|
;;; ;;;;;;;;;;;
|
|
|
|
|
2024-09-20 22:27:17 -04:00
|
|
|
;;; Read the end of an improper list.
|
|
|
|
(define readtable:read-improper-cdr
|
|
|
|
(lambda (table acc port)
|
|
|
|
(let ((val
|
|
|
|
(readtable:act (readtable:update table
|
|
|
|
%eol
|
|
|
|
(readtable:error
|
2024-09-27 11:26:30 -04:00
|
|
|
'read-improper-cdr
|
2024-09-20 22:27:17 -04:00
|
|
|
"proper list must have cdr"))
|
|
|
|
(port 'read)
|
|
|
|
#f
|
|
|
|
port)))
|
|
|
|
(acc 'set-cdr! val)
|
|
|
|
(let ((table (readtable:process
|
|
|
|
(readtable:empty/default (readtable:error
|
2024-09-27 11:26:30 -04:00
|
|
|
'read-improper-cdr
|
2024-09-20 22:27:17 -04:00
|
|
|
"improper list has 1 cdr"))
|
2024-09-22 09:46:58 -04:00
|
|
|
(list readtable:update-list
|
|
|
|
readtable:ASCII-whitespace
|
|
|
|
readtable:skip)
|
2024-09-20 22:27:17 -04:00
|
|
|
(list readtable:update %eol
|
2024-09-21 23:52:55 -04:00
|
|
|
(lambda dummy 'end-of-list)))))
|
2024-09-20 22:27:17 -04:00
|
|
|
(readtable:act table (port 'read) acc port)))))
|
|
|
|
|
2024-09-21 23:52:55 -04:00
|
|
|
;;; Generic reader loop for a list. It takes as input the table that has
|
|
|
|
;;; already been updated with end of list and improper list handlers.
|
|
|
|
(define readtable:read-list-loop
|
|
|
|
(lambda (table port)
|
|
|
|
(let ((acc (linked-list:new)))
|
2024-09-20 19:11:36 -04:00
|
|
|
(letrec ((loop
|
|
|
|
(lambda ()
|
|
|
|
(let ((value (readtable:act table
|
|
|
|
(port 'read)
|
2024-09-20 22:27:17 -04:00
|
|
|
acc
|
2024-09-20 19:11:36 -04:00
|
|
|
port)))
|
|
|
|
(cond
|
|
|
|
((eqv? value 'end-of-list) (acc 'to-list))
|
2024-09-27 11:16:25 -04:00
|
|
|
(else (acc 'push-tail value)
|
2024-09-20 19:11:36 -04:00
|
|
|
(loop)))))))
|
|
|
|
(loop)))))
|
|
|
|
|
2024-09-21 23:52:55 -04:00
|
|
|
;;; Readtable for a list, generic to proper and improper list
|
|
|
|
;;; readers.
|
|
|
|
(define readtable:table-for-list
|
|
|
|
(lambda (oldtable on-dot)
|
|
|
|
(readtable:process
|
|
|
|
oldtable
|
|
|
|
(list readtable:update %eol (readtable:return 'end-of-list))
|
|
|
|
(list readtable:update #\.
|
|
|
|
(lambda (table char acc port)
|
|
|
|
(let ((entire-identifier (readtable:read-ident
|
|
|
|
table
|
|
|
|
char
|
|
|
|
#f
|
|
|
|
port)))
|
|
|
|
(if (equal? entire-identifier ".")
|
|
|
|
(on-dot table acc port)
|
|
|
|
entire-identifier)))))))
|
|
|
|
|
|
|
|
;;; Read a proper or improper list.
|
|
|
|
(define readtable:read-list
|
|
|
|
(lambda (oldtable _ __ port)
|
|
|
|
(readtable:read-list-loop (readtable:table-for-list
|
|
|
|
oldtable
|
|
|
|
readtable:read-improper-cdr)
|
|
|
|
port)))
|
|
|
|
|
2024-09-27 15:45:30 -04:00
|
|
|
;;; Read strictly a proper list. This assumes that BOL has been read.
|
2024-09-22 00:00:26 -04:00
|
|
|
(define readtable:read-proper-list
|
|
|
|
(lambda (table port)
|
|
|
|
(readtable:read-list-loop (readtable:table-for-list
|
|
|
|
table
|
2024-09-27 11:26:30 -04:00
|
|
|
(readtable:error
|
|
|
|
'read-proper-list
|
|
|
|
"expected proper list"))
|
2024-09-22 00:00:26 -04:00
|
|
|
port)))
|
|
|
|
|
2024-09-27 15:45:30 -04:00
|
|
|
(define readtable:expect-proper-list
|
|
|
|
(lambda (table port)
|
|
|
|
(if (not (eqv? (port 'read) %bol))
|
|
|
|
(error 'expect-port-list 'no-list-found)
|
|
|
|
(readtable:read-proper-list table port))))
|
|
|
|
|
2024-09-22 00:00:26 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2024-09-22 00:46:41 -04:00
|
|
|
;;; Reader for stuff that start with "#"
|
2024-09-22 00:00:26 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2024-09-22 11:48:26 -04:00
|
|
|
(define readtable:digits
|
|
|
|
(list #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))
|
|
|
|
|
2024-09-22 00:00:26 -04:00
|
|
|
(define readtable:vector
|
|
|
|
(lambda (_ __ toplevel port)
|
|
|
|
(list 'vector (readtable:read-proper-list toplevel port))))
|
|
|
|
|
2024-09-22 00:46:41 -04:00
|
|
|
;;; Block comment reader.
|
|
|
|
;;;
|
|
|
|
;;; The outermost block comment reader is passed the toplevel reader as
|
|
|
|
;;; ACC. When the outermost block is finished, it will tail-call ACC.
|
|
|
|
;;; (It is basically the continuation of the reader.)
|
|
|
|
;;;
|
|
|
|
;;; When a nested block comment is found, it is passed #F as ACC, which
|
|
|
|
;;; it will not call. It will return an unspecified value.
|
|
|
|
;;;
|
|
|
|
;;; Since the read tables are not procedures, references to other tables
|
|
|
|
;;; in the same LETREC declaration must be protected with explicit LAMBDAs.
|
|
|
|
;;; Macros could make this much easier to read.
|
|
|
|
(define readtable:block-comment
|
|
|
|
(letrec ((potential-end
|
|
|
|
(readtable:process
|
|
|
|
(readtable:empty/default
|
|
|
|
(lambda (this char acc port) (readtable:act
|
|
|
|
loop
|
|
|
|
char
|
|
|
|
acc
|
|
|
|
port)))
|
|
|
|
(list readtable:update #\#
|
|
|
|
(lambda (this char acc port)
|
|
|
|
(if acc
|
|
|
|
(readtable:next acc #f port))))))
|
|
|
|
(potential-start
|
|
|
|
(readtable:process
|
|
|
|
(readtable:empty/default
|
|
|
|
(lambda (this char acc port) (readtable:act
|
|
|
|
loop
|
|
|
|
char
|
|
|
|
acc
|
|
|
|
port)))
|
|
|
|
(list readtable:update #\|
|
|
|
|
(lambda (this char acc port)
|
|
|
|
(readtable:next loop #f port)
|
|
|
|
(readtable:next loop acc port)))))
|
|
|
|
(loop
|
|
|
|
(readtable:process
|
|
|
|
(readtable:empty/default readtable:skip)
|
|
|
|
(list readtable:update #\#
|
|
|
|
(lambda (this char acc port)
|
|
|
|
(readtable:next potential-start
|
|
|
|
acc
|
|
|
|
port)))
|
|
|
|
(list readtable:update #\|
|
|
|
|
(lambda (this char acc port)
|
|
|
|
(readtable:next potential-end
|
|
|
|
acc
|
|
|
|
port))))))
|
|
|
|
loop))
|
|
|
|
|
2024-09-27 12:12:16 -04:00
|
|
|
(define read:datum-label
|
|
|
|
(lambda (location toplevel)
|
|
|
|
(let ((finalized? #f)
|
|
|
|
(value '())
|
|
|
|
(name (read:ident-builder location)))
|
|
|
|
(lambda (op . args)
|
|
|
|
(cond
|
|
|
|
((eq? op 'finalize-value)
|
|
|
|
(if finalized?
|
|
|
|
(error 'datum-label "cannot finalize twice"))
|
|
|
|
(set! finalized? #t)
|
|
|
|
(set! value (car args))
|
|
|
|
(set! toplevel #f))
|
|
|
|
((eq? op 'finalize->ident)
|
|
|
|
(set! name (name 'finalize->ident)))
|
|
|
|
((eq? op 'value)
|
|
|
|
(if (not finalized?)
|
|
|
|
(error 'datum-label "value called before finalize"))
|
|
|
|
value)
|
|
|
|
((eq? op 'toplevel) toplevel)
|
|
|
|
((eq? op 'type) 'datum-label)
|
|
|
|
((eq? op 'as-string) (name 'value))
|
|
|
|
(else (apply name op args)))))))
|
|
|
|
|
2024-09-22 11:48:26 -04:00
|
|
|
;;; Readtable for the number part of a datum label / reference. A label
|
|
|
|
;;; looks like "#[NUMBER]=" and a reference looks like "#[NUMBER]#".
|
2024-09-27 12:15:16 -04:00
|
|
|
;;;
|
|
|
|
;;; Datum label assignments #XXX=[DATUM] return DATUM.
|
|
|
|
;;; Datum label references #XXX# return a DATUM-LABEL object, because
|
|
|
|
;;; circular references must be resolved later. (With some trickery it
|
|
|
|
;;; cold be resolved as soon as possible, but that requires knowledge of
|
|
|
|
;;; what is complex data and what is an atom. This requires knowing all
|
|
|
|
;;; possible values that the toplevel could read, which is not possible
|
|
|
|
;;; because new items can be added to the toplevel.)
|
2024-09-22 11:48:26 -04:00
|
|
|
(define readtable:datum-label-next
|
|
|
|
(readtable:process
|
2024-09-27 11:26:30 -04:00
|
|
|
(readtable:empty/default (readtable:error 'datum-label-next
|
|
|
|
"invalid datum label/ref"))
|
2024-09-22 11:48:26 -04:00
|
|
|
(list readtable:update-list readtable:digits readtable:push-char)
|
|
|
|
(list readtable:update #\=
|
|
|
|
(lambda (_ __ acc port)
|
2024-09-27 12:12:16 -04:00
|
|
|
(acc 'finalize->ident)
|
|
|
|
(port 'set-datum-label! (acc 'as-string) acc)
|
|
|
|
(let ((next-value (readtable:next (acc 'toplevel)
|
|
|
|
#f
|
|
|
|
port)))
|
|
|
|
(if (eqv? acc next-value)
|
|
|
|
(error 'datum-label-next "datum label cannot be itself"))
|
|
|
|
(acc 'finalize-value next-value)
|
|
|
|
(acc 'value))))
|
2024-09-22 11:48:26 -04:00
|
|
|
(list readtable:update #\#
|
|
|
|
(lambda (_ __ acc port)
|
2024-09-27 12:12:16 -04:00
|
|
|
(acc 'finalize->ident)
|
|
|
|
(let ((datum-label-container (port 'get-datum-label
|
|
|
|
(acc 'as-string))))
|
|
|
|
(if (null? datum-label-container)
|
|
|
|
(error 'datum-label-next
|
|
|
|
"unknown reference to datum label" label)
|
|
|
|
(map:val datum-label-container)))))))
|
2024-09-22 11:48:26 -04:00
|
|
|
|
2024-09-22 00:49:18 -04:00
|
|
|
;;; Reads the next toplevel datum, discards it, and then continues at the
|
|
|
|
;;; toplevel.
|
2024-09-22 09:46:58 -04:00
|
|
|
;;;
|
2024-09-27 12:26:26 -04:00
|
|
|
;;; The R7RS reader can cause side-effects due to #!FOLD-CASE and datum
|
|
|
|
;;; labels. This must be supressed in datum comments.
|
2024-09-22 00:49:18 -04:00
|
|
|
(define readtable:datum-comment
|
|
|
|
(lambda (_ __ toplevel port)
|
2024-09-27 12:26:26 -04:00
|
|
|
(let ((mutable (port 'dump-mutable)))
|
|
|
|
(readtable:next toplevel #f port)
|
|
|
|
(port 'restore-mutable! mutable))
|
2024-09-22 00:49:18 -04:00
|
|
|
(readtable:next toplevel #f port)))
|
|
|
|
|
2024-09-27 16:14:37 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Handling hash messages
|
|
|
|
;;;
|
|
|
|
;;; A "hash message" is any identifier that immediately follows a "#".
|
|
|
|
;;; This can be a constant, a part of a constant (like for numbers), or
|
|
|
|
;;; a directive (like #!FOLD-CASE).
|
|
|
|
|
2024-09-27 15:40:30 -04:00
|
|
|
;;; 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.
|
2024-09-27 16:14:37 -04:00
|
|
|
(define read:hash-messages
|
2024-09-27 15:40:30 -04:00
|
|
|
(smap:insert-many
|
|
|
|
'()
|
|
|
|
(cons "true" (lambda unused #t))
|
|
|
|
(cons "false" (lambda unused #f))
|
|
|
|
(cons "t" (lambda unused #t))
|
|
|
|
(cons "f" (lambda unused #f))
|
2024-09-27 15:45:30 -04:00
|
|
|
(cons "u8" (lambda (toplevel port)
|
|
|
|
(list 'bytevector
|
|
|
|
(readtable:expect-proper-list toplevel port))))
|
2024-09-27 15:40:30 -04:00
|
|
|
(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)))))
|
|
|
|
|
2024-09-27 16:14:37 -04:00
|
|
|
(define read:read-hash-messages
|
2024-09-27 15:40:30 -04:00
|
|
|
(lambda (_ char acc port)
|
|
|
|
(let ((ident (readtable:read-ident #f char #f port)))
|
2024-09-27 16:14:37 -04:00
|
|
|
(let ((container (smap:search read:hash-messages
|
2024-09-27 15:40:30 -04:00
|
|
|
(ident 'value))))
|
2024-10-13 10:32:28 -04:00
|
|
|
;; TODO: check if the hash message starts a number.
|
2024-09-27 15:40:30 -04:00
|
|
|
(if (null? container)
|
2024-10-13 10:32:28 -04:00
|
|
|
(error 'read-hash-messages 'unknown-constant
|
|
|
|
(ident 'value))
|
2024-09-27 15:40:30 -04:00
|
|
|
((map:val container) acc port))))))
|
|
|
|
|
2024-09-27 16:14:37 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Characters
|
|
|
|
;;;
|
|
|
|
;;; For completeness, this adds a few more newline characters that are C
|
|
|
|
;;; escape sequences but are not standard Scheme.
|
|
|
|
|
|
|
|
(define read:named-characters
|
|
|
|
(smap:insert-many
|
|
|
|
'()
|
|
|
|
(cons "null" (integer->char 0))
|
|
|
|
(cons "alarm" (integer->char #x7))
|
|
|
|
(cons "backspace" (integer->char #x8))
|
|
|
|
(cons "tab" (integer->char #x9))
|
|
|
|
(cons "newline" (integer->char #xA))
|
|
|
|
(cons "vertical-tab" (integer->char #xB))
|
|
|
|
(cons "form-feed" (integer->char #xC))
|
|
|
|
(cons "return" (integer->char #xD))
|
|
|
|
(cons "escape" (integer->char #x1B))
|
|
|
|
(cons "space" (integer->char #x20))
|
|
|
|
(cons "delete" (integer->char #x7F))))
|
|
|
|
|
|
|
|
(define readtable:character
|
|
|
|
(lambda (_ char __ port)
|
|
|
|
(let ((char (port 'read)))
|
|
|
|
(let ((ident (readtable:read-ident #f char #f port)))
|
|
|
|
(let ((container (smap:search read:named-characters
|
|
|
|
(ident 'value))))
|
|
|
|
(display (ident 'value))
|
|
|
|
(newline)
|
|
|
|
(cond
|
|
|
|
((and (null? container)
|
|
|
|
(= (string-length (ident 'value)) 1))
|
|
|
|
(string-ref (ident 'value) 0))
|
|
|
|
((null? container) (error 'character 'unknown-character ident))
|
|
|
|
(else (map:val container))))))))
|
|
|
|
|
2024-09-22 00:00:26 -04:00
|
|
|
(define readtable:hash
|
|
|
|
(readtable:process
|
2024-09-27 16:14:37 -04:00
|
|
|
(readtable:empty/default read:read-hash-messages)
|
2024-09-27 16:15:44 -04:00
|
|
|
(list readtable:update-list readtable:ASCII-whitespace
|
|
|
|
(readtable:error 'hash 'whitespace-after-hash))
|
2024-09-22 00:46:41 -04:00
|
|
|
(list readtable:update #\| (readtable:jump/next readtable:block-comment))
|
2024-09-22 00:49:18 -04:00
|
|
|
(list readtable:update #\; readtable:datum-comment)
|
2024-09-27 16:14:37 -04:00
|
|
|
(list readtable:update #\\ readtable:character)
|
2024-09-22 11:48:26 -04:00
|
|
|
(list readtable:update-list readtable:digits ; Datum labels
|
|
|
|
(lambda (_ char toplevel port)
|
|
|
|
(readtable:act readtable:datum-label-next
|
|
|
|
char
|
2024-09-27 12:12:16 -04:00
|
|
|
(read:datum-label (port 'location) toplevel)
|
2024-09-22 11:48:26 -04:00
|
|
|
port)))
|
2024-09-22 00:00:26 -04:00
|
|
|
(list readtable:update %bol readtable:vector)))
|
|
|
|
|
|
|
|
;;; ;;;;;;;;;;;;;;;;
|
2024-09-08 08:22:39 -04:00
|
|
|
;;; Toplevel reader.
|
2024-09-22 00:00:26 -04:00
|
|
|
;;; ;;;;;;;;;;;;;;;;
|
2024-09-20 19:11:36 -04:00
|
|
|
;;; This is defined as a function so that it dynamically loads each
|
|
|
|
;;; sub-readtable.
|
2024-09-07 17:47:10 -04:00
|
|
|
(define readtable:top
|
2024-09-20 19:11:36 -04:00
|
|
|
(lambda ()
|
|
|
|
(readtable:process
|
|
|
|
(readtable:empty/default readtable:read-ident)
|
2024-09-27 16:15:44 -04:00
|
|
|
(list readtable:update-list readtable:ASCII-whitespace readtable:skip)
|
2024-09-20 19:11:36 -04:00
|
|
|
(list readtable:update #f (readtable:return 'eof))
|
2024-09-20 22:27:17 -04:00
|
|
|
(list readtable:update %bol readtable:read-list)
|
2024-09-27 11:26:30 -04:00
|
|
|
(list readtable:update %eol (readtable:error 'top "unbalanced list"))
|
2024-09-22 00:00:26 -04:00
|
|
|
(list readtable:update #\# (readtable:next/old-as-acc
|
|
|
|
readtable:hash))
|
2024-09-20 19:11:36 -04:00
|
|
|
(list readtable:update #\;
|
|
|
|
(readtable:jump-discard readtable:read-to-newline)))))
|
2024-09-07 17:47:10 -04:00
|
|
|
|
2024-09-27 12:26:26 -04:00
|
|
|
(define read/toplevel
|
|
|
|
(lambda (port)
|
|
|
|
;; Does each invocation of READ keep datum labels from previous reads?
|
|
|
|
;; (port 'clear-datum-labels!)
|
|
|
|
(readtable:next (readtable:top)
|
|
|
|
#f
|
|
|
|
port)))
|
|
|
|
|
2024-09-22 11:48:26 -04:00
|
|
|
(define intset:insert (set:insert (set:update integer<=>)))
|
|
|
|
(define intset:in (set:in integer<=>))
|
|
|
|
|
|
|
|
(define uncycle
|
|
|
|
(lambda (value)
|
|
|
|
(let ((cntr 0)
|
|
|
|
(used-counters '())
|
|
|
|
(pointers '()))
|
|
|
|
(letrec ((uncycle
|
|
|
|
(lambda (value)
|
|
|
|
(cond
|
|
|
|
((pair? value)
|
|
|
|
(let ((pair (assq value pointers)))
|
|
|
|
(if (pair? pair)
|
|
|
|
(begin
|
|
|
|
(set! used-counters
|
|
|
|
(car (intset:insert used-counters (cdr pair))))
|
|
|
|
(list 'ref (cdr pair)))
|
|
|
|
(begin
|
|
|
|
(set! pointers (cons (cons value cntr)
|
|
|
|
pointers))
|
|
|
|
(let ((cur-cntr cntr))
|
|
|
|
(set! cntr (+ 1 cntr))
|
|
|
|
(let ((returned (cons (uncycle (car value))
|
|
|
|
(uncycle (cdr value)))))
|
|
|
|
(if (not (null? (intset:in used-counters cur-cntr)))
|
|
|
|
(list 'def cur-cntr '= returned)
|
|
|
|
returned)))))))
|
2024-09-27 11:26:30 -04:00
|
|
|
((procedure? value)
|
|
|
|
(let ((type (value 'type)))
|
|
|
|
(cond
|
|
|
|
((eq? type 'ident) (value 'value))
|
2024-09-27 12:12:16 -04:00
|
|
|
((eq? type 'datum-label) (uncycle (value 'value)))
|
2024-09-27 11:26:30 -04:00
|
|
|
(else (vector 'unrepresentable type)))))
|
2024-09-22 11:48:26 -04:00
|
|
|
(else value)))))
|
|
|
|
(uncycle value)))))
|
|
|
|
|
2024-09-07 17:47:10 -04:00
|
|
|
;;; ;;;;;;;;;;;
|
|
|
|
;;; Test reader
|
|
|
|
;;; ;;;;;;;;;;;
|
|
|
|
(define %list->read
|
|
|
|
(lambda (seq)
|
|
|
|
(port->read
|
|
|
|
(lambda ()
|
|
|
|
(if (null? seq)
|
|
|
|
#f
|
|
|
|
(let ((ch (car seq)))
|
|
|
|
(set! seq (cdr seq))
|
|
|
|
ch)))
|
|
|
|
"test")))
|
|
|
|
|
2024-09-08 08:44:42 -04:00
|
|
|
(define read-all
|
|
|
|
(lambda (str)
|
|
|
|
(let ((reader (%list->read (string->list str))))
|
|
|
|
(letrec ((loop
|
|
|
|
(lambda ()
|
|
|
|
(if (not (reader 'peek))
|
|
|
|
#t
|
2024-09-27 12:26:26 -04:00
|
|
|
(let ((value (read/toplevel reader)))
|
2024-09-22 11:48:26 -04:00
|
|
|
(display (list "return" (uncycle value)))
|
2024-09-08 08:44:42 -04:00
|
|
|
(newline)
|
|
|
|
(loop))))))
|
|
|
|
(loop)))))
|
|
|
|
|
2024-09-08 08:46:15 -04:00
|
|
|
(read-all "x yy zz ; this is a comment\nx call/cc ")
|
2024-09-20 19:11:36 -04:00
|
|
|
(read-all "(a b c def (ghi j) k )")
|
2024-09-20 22:27:17 -04:00
|
|
|
(read-all "( a . b )")
|
|
|
|
(read-all "( a .b . c)")
|
2024-09-22 00:00:26 -04:00
|
|
|
(read-all "#( a b y)")
|
2024-09-22 00:46:41 -04:00
|
|
|
(read-all "(x y #| this is a block\n comment\n |# z w)")
|
|
|
|
(read-all "#( a b #| this is a #| nested block |# comment|# z w)")
|
|
|
|
(read-all "#(a b #(c #|close#|comment|#|#y))")
|
2024-09-22 09:46:58 -04:00
|
|
|
(read-all "(this has a #;(call with (current continuation)) datum comment)")
|
2024-09-22 11:48:26 -04:00
|
|
|
(read-all "#0=(#0# not unix)")
|
2024-09-27 15:40:30 -04:00
|
|
|
(read-all "#!no-fold-case #!fold-case #!NO-FOLD-CASE #false")
|
|
|
|
(read-all "#!fold-case #TRUE")
|
2024-09-27 15:45:30 -04:00
|
|
|
(read-all "#u8(x y z w)")
|
2024-09-27 16:14:37 -04:00
|
|
|
(read-all "#\\newline")
|
2024-09-27 16:15:44 -04:00
|
|
|
(read-all "#\\a")
|