do while macro definition

9 views
Skip to first unread message

Damien Mattei

unread,
Apr 6, 2017, 1:20:08 PM4/6/17
to BiwaScheme
i try to set up a macro in biwascheme:

(define-macro (do b1 . Larg)
   (let ((lst (gensym)) ;; list
     (rev-lst (gensym)) ;; reversed list
     (pred (gensym)) ;; predicate
     (q-while (gensym)) ;; MUST be (quote while)
     (lst-instr (gensym))) ;; instructions to execute list

      (print "do")
      `(let loop ((,lst (cons
             (quote ,b1)
             (quote ,Larg))) ;; list
          (,rev-lst '()) ;; reversed list
          (,pred '()) ;; predicate
          (,q-while '()) ;; MUST be (quote while)
          (,lst-instr '())) ;; instructions to execute list
     
      (print "passed")
      (if (< (length ,lst) 2)
          (print "ERROR: too few arguments: must not be less than 2")
          (begin
         (set! ,rev-lst (reverse ,lst))
         (print ,rev-lst)
         (set! ,pred (car ,rev-lst))
         (set! ,q-while (car (cdr ,rev-lst)))
         (print ,pred)
         (print ,q-while)
         (print "passed")
         (if (not
              (or (equal? ,q-while '(quote while))
              (equal? ,q-while '(quote WHILE))))
             (print "ERROR: WHILE key-word not found")
             (begin
            (print "passed 2")
            (set! ,lst-instr (reverse
                      (cdr
                       (cdr ,rev-lst))))
            (print "passed set!")
            (print ,lst-instr)
            (begin
               ,@lst-instr)
            (when ,pred (loop)))))))))


it crashed on the unquote-splicing at the end....

any ideas?

when macro is working well and use also unquote-splicing

(define-macro (when test s1 . Larg)
  `(if ,test
       (begin ,s1 ,@Larg)
       #f))

i use the macro this way:
(define i 0)
(do (print "hello") (set! i (+ i 1)) 'while (< i 4))

Damien

Damien Mattei

unread,
Apr 7, 2017, 10:32:24 AM4/7/17
to BiwaScheme
right solution:
;; (do (print i) (if (< i 4) (print "inf") (print "sup")) (set! i (+ i 1)) while (< i 4))
;; 0
;; inf
;; 1
;; inf
;; 2
;; inf
;; 3
;; inf


(define-macro (do b1 . Larg)
   (let ((lst (gensym)) ;; list
     (rev-lst (gensym)) ;; reversed list
     (pred (gensym)) ;; predicate
     (q-while (gensym)) ;; MUST be (quote while)
     (lst-instr (gensym))) ;; instructions to execute list

      `(let ((,lst (cons

            (quote ,b1)
            (quote ,Larg))) ;; list
         (,rev-lst '()) ;; reversed list
         (,pred '()) ;; predicate
         (,q-while '()) ;; MUST be (quote while)
         (,lst-instr '())) ;; instructions to execute list
     
      (if (< (length ,lst) 2)
          "ERROR: too few arguments: must not be less than 2"
          (begin
         (set! ,rev-lst (reverse ,lst))
         (set! ,pred (car ,rev-lst))
         (set! ,q-while (car (cdr ,rev-lst)))
         (if (not
              (or (equal? ,q-while (quote while))
              (equal? ,q-while (quote WHILE))))
             "ERROR: WHILE key-word not found"
             (begin

            (set! ,lst-instr (reverse
                      (cdr
                       (cdr ,rev-lst))))
            (let loop ()
               (map eval ,lst-instr)
               (if (eval ,pred) (loop))))))))))
Reply all
Reply to author
Forward
0 new messages