chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Concise syntax


From: Felix Winkelmann
Subject: Re: [Chicken-users] Concise syntax
Date: Thu, 05 Jun 2003 09:32:01 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.0) Gecko/20020530

Joerg F. Wittenberger wrote:
Felix Winkelmann <address@hidden> writes:


I wonder what people think about alternative, more
concise syntaxes, like in GOO or ARC. Would something
like that desirable? I'd be happy to add some hooks
in the reader for experimentation.


I'd be happy to have the hooks (and experiement with them) as long as
the base system is kept "clean".


Hi!

Here is a somewhat cleaner interface to the read-syntax hook:

;;;; readsyntax.scm

(declare (export read-syntax current-read-syntax))

(define-record read-syntax rsm)

(define (read-syntax . maps)
  (let ([s (make-vector 256 #f)])
    (let loop ([maps maps])
      (if (null? maps)
          (make-read-syntax s)
          (let ([rs (car maps)])
            (cond [(##sys#structure? s 'read-syntax)
                   (do ([i 0 (fx+ i 1)])
                       ((fx>= i 256) (loop (cdr maps)))
                     (let ([rs (vector-ref rs i)])
                       (when rs (vector-set! s i rs)) ) ) ]
                  [(char? rs)
                   (let ([next (cadr maps)])
                     (vector-set!
                      s (char->integer rs)
                      (if (char? next)
                          (vector-ref s (char->integer next))
                          (lambda (c port)
                            (read-char port)
                            (next port) ) ) )
                     (loop (cddr maps)) ) ]
[else (error "bad argument type - not a valid read-syntax specification" rs)] ) ) ) ) ) )

(define current-read-syntax
  (case-lambda
   [() ##sys#special-read-syntax-table]
   [(rs) (set! ##sys#special-read-syntax-table (read-syntax-rsm rs))] ) )

You should be able to do

% csi -setup readsyntax.scm

And then:

(require 'readsyntax)

(define (qlist-reader p)
  (let loop ([lst '()])
    (let skip ()
      (let ([c (peek-char p)])
        (cond [(eof-object? c) (error "unexpected end of [...] list")]
              [(char=? c #\])
               (read-char p)
               `(list ,@(reverse lst)) ]
              [(char-whitespace? c)
               (read-char p)
               (skip)]
              [else (loop (cons (read p) lst))] ) ) ) ) )

(current-read-syntax
 (read-syntax
  #\[ qlist-reader) )

(with-input-from-string "[1 2 3]" read)  ; --> (1 2 3)

To use it in the compiler, you have to use the `-extend' option.
But it doesn't respect the include path (this will be fixed), so you
have to have a file like this somewhere:

;;; use-read-syntax.scm
(require 'readsyntax)

and compile like this:

% csc myfile.scm -X use-read-syntax.scm

BTW, there is a new facility in the lolevel unit, that allows
ARC-style aggregate accessing using function-call syntax:

(invalid-procedure-call-handler
  (lambda (proc args)
    (match args
      [((? string? s) i c) (string-set! s i c)]
      [((? string? s) i) (string-ref s i)]
      [((? vector? v) i x) (vector-set! s i x)]
      [((? vector? v) i) (vector-ref s i)]
      [_ (error "call of non-procedure" proc)] ) ) )

("hello" 4)    ==>  #\o
(define v '#(1 2 3))
(v 2 99)       ; v == #(1 2 99)

Works even in compiled code without performance-penalties
for normal calls (but doesn't work with "-unsafe")


cheers,
felix






reply via email to

[Prev in Thread] Current Thread [Next in Thread]