1
0
forked from alex-eg/sex

add reader syntax for []

Now [...] reads to (¤ ...) for easy semantic processing
This commit is contained in:
2025-10-21 21:22:22 +03:00
committed by Pavel Kulyov
parent 036f82facf
commit 45d9199c27
4 changed files with 73 additions and 4 deletions

View File

@@ -2,9 +2,15 @@
(include "utils.macros.scm")
(import (chicken pathname)
brev-separate
fmt)
(import
(chicken base)
(chicken io)
(chicken pathname)
(chicken port)
(chicken read-syntax)
(chicken string)
brev-separate
fmt)
(define (read-forms acc)
(let ((r (read)))
@@ -16,7 +22,41 @@
(with-input-from-file (pathname-strip-directory file)
(fn (read-forms (list))))))
(define (read-bracket port)
(let loop ((c (read-char port))
(str (string)))
(cond ((char=? c #\])
(cons '¤
(with-input-from-string str
(fn (port-map identity read)))))
((char=? c #\[)
(loop port (conc )))
(else
(loop (read-char port)
(conc str c))))))
(define open-bracket-counter (make-parameter 0))
(define (read-raw-forms input-source)
(let ((bracket-end (gensym)))
(set-read-syntax!
#\]
(lambda (port)
(when (= 0 (open-bracket-counter))
(error "Unmatched closing bracket"))
(open-bracket-counter (- (open-bracket-counter) 1))
bracket-end))
(set-read-syntax!
#\[
(lambda (port)
(open-bracket-counter (+ (open-bracket-counter) 1))
(let loop ((r (read port))
(acc (list)))
(if (eq? r bracket-end)
(cons '¤ (reverse acc))
(loop (read port)
(cons r acc)))))))
(if (eq? input-source 'stdin)
(read-forms (list))
(read-from-file input-source)))