add reader syntax for []

Now [...] reads to (¤ ...) for easy semantic processing
This commit is contained in:
2025-10-21 21:22:22 +03:00
parent cca63fb4df
commit 4165fbe299
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)))

View File

@@ -19,7 +19,7 @@
(test '%define (atom-to-fmt-c 'define))
(test '%pointer (atom-to-fmt-c 'pointer))
(test '%array (atom-to-fmt-c 'array))
(test 'vector-ref (atom-to-fmt-c '@))
(test 'vector-ref (atom-to-fmt-c '¤))
(test '%include (atom-to-fmt-c 'include))
(test '%cast (atom-to-fmt-c 'cast))

28
tests/reader.scm Normal file
View File

@@ -0,0 +1,28 @@
(import (chicken port))
(test-group "reader"
;; []-syntax. For array types and array access expressions
(test '((¤ * char))
(with-input-from-string "[* char]"
(lambda ()
(read-raw-forms 'stdin))))
(test '((¤ * * char const 512))
(with-input-from-string "[* * char const 512]"
(lambda ()
(read-raw-forms 'stdin))))
(test '((¤))
(with-input-from-string "[]"
(lambda ()
(read-raw-forms 'stdin))))
(test '((¤ (¤)))
(with-input-from-string "[[]]"
(lambda ()
(read-raw-forms 'stdin))))
(test '((¤ (¤ const char)))
(with-input-from-string "[[const char]]"
(lambda ()
(read-raw-forms 'stdin)))))

View File

@@ -9,6 +9,7 @@
(include "basic.scm")
(include "semen.scm")
(include "reader.scm")
(include "utils.scm")
;;; Should be the last in the test suite