- cleaned up expect-1* a bit

- fixed a bug in the expect and expect-action-clause macros
This commit is contained in:
frese 2004-08-31 10:16:54 +00:00
parent c608b585a5
commit 571e979360
1 changed files with 101 additions and 104 deletions

View File

@ -229,10 +229,7 @@
(set-task:buf! task (set-task:buf! task
(substring buffer (match:end m) (string-length buffer)))) (substring buffer (match:end m) (string-length buffer))))
(define (expect-1* on-timeout monitor timeout echo max-size (define (handle-input task patterns input do-next monitor)
tasks-patterns-alist)
(let ((handle-input
(lambda (task patterns input do-next)
(let ((s (task:buf task))) (let ((s (task:buf task)))
(let loop ((patterns patterns)) (let loop ((patterns patterns))
(if (null? patterns) (if (null? patterns)
@ -260,10 +257,9 @@
(loop (cdr patterns))))) (loop (cdr patterns)))))
((else) ((else)
((cadr pattern))) ((cadr pattern)))
(else (error "undefined pattern type" (car pattern))) (else (error "undefined pattern type" (car pattern)))))))))
)))))))
(handle-eof (define (handle-eof task patterns do-next monitor)
(lambda (task patterns do-next)
(set-task:pre-match! task (task:buf task)) (set-task:pre-match! task (task:buf task))
(set-task:buf! task "") (set-task:buf! task "")
(monitor task #f) (monitor task #f)
@ -274,13 +270,14 @@
(case (car pattern) (case (car pattern)
((eof) ((cadr pattern))) ((eof) ((cadr pattern)))
((match test else) (loop (cdr patterns))) ((match test else) (loop (cdr patterns)))
(else (error "undefined pattern type" (car pattern))) (else (error "undefined pattern type" (car pattern))))))))
))))))
(handle-timeout
(lambda ()
(monitor #f 'timeout)
(on-timeout))))
(define (handle-timeout monitor on-timeout)
(monitor #f 'timeout)
(on-timeout))
(define (expect-1* on-timeout monitor timeout echo max-size
tasks-patterns-alist)
;; first check existing data in the buffers ;; first check existing data in the buffers
(let loop ((f-tasks-patterns-alist tasks-patterns-alist)) (let loop ((f-tasks-patterns-alist tasks-patterns-alist))
(if (not (null? f-tasks-patterns-alist)) (if (not (null? f-tasks-patterns-alist))
@ -289,7 +286,8 @@
(s (task:buf task))) (s (task:buf task)))
(if (not (zero? (string-length s))) (if (not (zero? (string-length s)))
(handle-input task (cdr task-patterns) s (handle-input task (cdr task-patterns) s
(lambda () (loop (cdr f-tasks-patterns-alist)))) (lambda () (loop (cdr f-tasks-patterns-alist)))
monitor)
(loop (cdr f-tasks-patterns-alist)))) (loop (cdr f-tasks-patterns-alist))))
;; start looking for input, handle it and throw away those ;; start looking for input, handle it and throw away those
@ -302,7 +300,7 @@
tasks-patterns-alist)))) tasks-patterns-alist))))
;; what is this (time) thing in the original? ;; what is this (time) thing in the original?
(if (not (in-select! ivec timeout)) (if (not (in-select! ivec timeout))
(handle-timeout) (handle-timeout monitor on-timeout)
(let iloop ((inports (vector->list ivec)) (let iloop ((inports (vector->list ivec))
(tasks-patterns-alist tasks-patterns-alist) (tasks-patterns-alist tasks-patterns-alist)
(remaining '())) (remaining '()))
@ -313,23 +311,23 @@
(task (car task-patterns)) (task (car task-patterns))
(s (do-input task))) (s (do-input task)))
(if s (if s
(handle-input (handle-input task (cdr task-patterns) s
task (cdr task-patterns) s
(lambda () (lambda ()
(iloop (cdr inports) (iloop (cdr inports)
(cdr tasks-patterns-alist) (cdr tasks-patterns-alist)
(cons task-patterns (cons task-patterns
remaining)))) remaining)))
(handle-eof monitor)
task (cdr task-patterns) (handle-eof task (cdr task-patterns)
(lambda () (lambda ()
(iloop (cdr inports) (iloop (cdr inports)
(cdr tasks-patterns-alist) (cdr tasks-patterns-alist)
remaining))))) remaining))
monitor)))
(iloop (cdr inports) (iloop (cdr inports)
(cdr tasks-patterns-alist) (cdr tasks-patterns-alist)
(cons (car tasks-patterns-alist) (cons (car tasks-patterns-alist)
remaining))))))))))))) remaining))))))))))))
;; Syntax based on procedural interface ;; Syntax based on procedural interface
@ -377,11 +375,10 @@
(expect-action-clauses clause ...))) (expect-action-clauses clause ...)))
((expect-action-clauses (pattern () body ...) clause ...) ((expect-action-clauses (pattern () body ...) clause ...)
(expect-action-clauses (pattern (ignore) body ...) clause ...)) (expect-action-clauses (pattern (ignore) body ...) clause ...))
((expect-action-clauses (pattern (match mvars ...) body ...) clause ...) ((expect-action-clauses (pattern (m mvars ...) body ...) clause ...)
(cons (list 'match (lambda () pattern) (cons (list 'match (lambda () pattern)
(lambda (m) (lambda (m)
(let ((match m)) (let-match m (mvars ...) body ...)))
(let-match m (mvars ...) body ...))))
(expect-action-clauses clause ...))) (expect-action-clauses clause ...)))
((expect-action-clauses (else body ...) clause ...) ((expect-action-clauses (else body ...) clause ...)
(cons (list else (lambda () body ...)) (cons (list else (lambda () body ...))
@ -405,8 +402,8 @@
(syntax-rules () (syntax-rules ()
((expect (x ...) eclause ...) ((expect (x ...) eclause ...)
(let ((r (expect-clauses (x ...) eclause ...))) (let ((r (expect-clauses (x ...) eclause ...)))
(expect* (car r) (cdr r)))) (apply expect* (car r) (cdr r))))
((expect name (var-inits ...) eclause ...) ((expect name (var-inits ...) eclause ...)
(let name (var-inits ...) (let name (var-inits ...)
(let ((r (expect-clauses eclause ...))) (let ((r (expect-clauses eclause ...)))
(expect* (car r) (cdr r))))))) (apply expect* (car r) (cdr r)))))))