aboutsummaryrefslogtreecommitdiffstats
path: root/read.scm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-09-27 11:16:25 -0400
committerGravatar Peter McGoron 2024-09-27 11:16:25 -0400
commitdf698263126d34dd6ce771483526bd1f0142d1f9 (patch)
tree4ae8d1a829fb959c6a6f892782e4e006e9bc4653 /read.scm
parentRevert "object: change to a stateful table" (diff)
Revert "add object helper functions"
Diffstat (limited to 'read.scm')
-rw-r--r--read.scm138
1 files changed, 68 insertions, 70 deletions
diff --git a/read.scm b/read.scm
index 07d708c..9040772 100644
--- a/read.scm
+++ b/read.scm
@@ -39,7 +39,6 @@
(load "chez-compat.scm")
(load "util.scm")
(load "set.scm")
-(load "object.scm")
(load "linked-list.scm")
;;; My text editor cannot parse Scheme's character syntax.
@@ -68,73 +67,72 @@
;;; (PEEK): Read character, push it back, and return it.
;;; (FOLD-CASE?): Returns a boolean if case folding is enabled.
;;; (FOLD-CASE! BOOL): Sets the case folding to BOOL.
-
(define port->read
(lambda (read-function filename)
- (letrec ((line-number 1)
- (offset 0)
- (pushback-buffer '())
- (datum-labels '())
- (fold-case? #f)
- (this
- (object/procedures
- 'process
- (lambda (ch)
- (this 'update-position! ch)
- (cond
- ((or (eof-object? ch) (not ch)) ch)
- (fold-case? (char-downcase ch))
- (else ch)))
- '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)))))
- '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))
- 'dump-mutable
- (lambda ()
- (list datum-labels fold-case?))
- 'restore-mutable!
- (lambda (state)
- (set! datum-labels (car state))
- (set! fold-case? (cadr state)))
- 'pos
- (lambda () (list filename line-number offset))
- 'read
- (lambda ()
- (this 'process
- (if (null? pushback-buffer)
- (read-function)
- (let ((ch (car pushback-buffer)))
- (set! pushback-buffer (cdr pushback-buffer))
- ch))))
- 'peek
- (lambda ()
- (let ((ch (this 'read)))
- (this 'push! ch)
- ch))
- 'push!
- (lambda (ch)
- (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)))
- 'fold-case?
- (lambda () fold-case?)
- 'fold-case!
- (lambda (val) (set! fold-case? val)))))
- this)))
+ (let ((line-number 1)
+ (offset 0)
+ (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))))))
+ (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)))
+ (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
+ ((or (eof-object? ch) (not ch)) ch)
+ (fold-case? (char-downcase ch))
+ (else ch))))
+ (port
+ (lambda (op . args)
+ ;; TODO: turn into string map?
+ (cond
+ ((eq? op 'pos) (list filename line-number offset))
+ ((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))
+ (else (error "read->port: invalid" (cons op args)))))))
+ port))))
;;; ;;;;;;;;;;;;;;
;;; Character maps
@@ -288,13 +286,13 @@
;;; Push back CHAR and return ACC.
(define readtable:return-acc-keep-char
(lambda (table char acc port)
- (port 'push! char)
+ (port 'push char)
acc))
;;; Push CHAR to ACC and continue reading from TABLE.
(define readtable:push-char
(lambda (table char acc port)
- (acc 'push-tail! char)
+ (acc 'push-tail char)
(readtable:act table (port 'read) acc port)))
;;; Define a readtable that constructs an identifier by accepting all
@@ -328,7 +326,7 @@
(define readtable:read-ident
(lambda (table char acc port)
(let ((lst (linked-list:new)))
- (lst 'push! char)
+ (lst 'push char)
(list->string
((readtable:act readtable:identifier
(port 'read) lst port)
@@ -387,7 +385,7 @@
port)))
(cond
((eqv? value 'end-of-list) (acc 'to-list))
- (else (acc 'push-tail! value)
+ (else (acc 'push-tail value)
(loop)))))))
(loop)))))