forge:repl: allow list input over multiple lines, using handler-bind and restart-case in string-iterator
This commit is contained in:
parent
685df331b0
commit
3c3e9b15d7
2 changed files with 20 additions and 11 deletions
|
|
@ -85,7 +85,11 @@
|
||||||
(defun repl ()
|
(defun repl ()
|
||||||
(do ((input (read-line) (read-line)))
|
(do ((input (read-line) (read-line)))
|
||||||
((string= input "q") (stack *forge-env*))
|
((string= input "q") (stack *forge-env*))
|
||||||
(exec-string input)))
|
(handler-bind
|
||||||
|
((end-of-file
|
||||||
|
(lambda (c)
|
||||||
|
(invoke-restart 'iter:try-with-more-input (read-line)))))
|
||||||
|
(exec-string input))))
|
||||||
|
|
||||||
(defun exec-list (lst)
|
(defun exec-list (lst)
|
||||||
(let ((*input* (iter:list-iterator lst)))
|
(let ((*input* (iter:list-iterator lst)))
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
(defpackage :scopes/util/iter
|
(defpackage :scopes/util/iter
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:export #:stop #:next #:value #:next-value #:process
|
(:export #:stop #:next #:value #:next-value #:process
|
||||||
#:list-iterator #:string-iterator #:stream-iterator))
|
#:list-iterator #:string-iterator #:stream-iterator
|
||||||
|
#:try-with-more-input))
|
||||||
|
|
||||||
(in-package :scopes/util/iter)
|
(in-package :scopes/util/iter)
|
||||||
|
|
||||||
|
|
@ -56,7 +57,7 @@
|
||||||
;;;; string iterator implementation
|
;;;; string iterator implementation
|
||||||
|
|
||||||
(defclass string-iterator (iterator)
|
(defclass string-iterator (iterator)
|
||||||
((data :reader data :initarg :data :initform "")
|
((data :accessor data :initarg :data :initform "")
|
||||||
(curpos :accessor curpos :initform 0)
|
(curpos :accessor curpos :initform 0)
|
||||||
(value :accessor value :initform nil)))
|
(value :accessor value :initform nil)))
|
||||||
|
|
||||||
|
|
@ -64,6 +65,7 @@
|
||||||
(make-instance 'string-iterator :data s))
|
(make-instance 'string-iterator :data s))
|
||||||
|
|
||||||
(defmethod next ((it string-iterator))
|
(defmethod next ((it string-iterator))
|
||||||
|
(restart-case
|
||||||
(let ((pos (curpos it)))
|
(let ((pos (curpos it)))
|
||||||
(if (< pos (length (data it)))
|
(if (< pos (length (data it)))
|
||||||
(multiple-value-bind (val newpos)
|
(multiple-value-bind (val newpos)
|
||||||
|
|
@ -71,7 +73,10 @@
|
||||||
(setf (curpos it) newpos
|
(setf (curpos it) newpos
|
||||||
(value it) val)
|
(value it) val)
|
||||||
nil)
|
nil)
|
||||||
t)))
|
t))
|
||||||
|
(try-with-more-input (more)
|
||||||
|
(setf (data it) (str:join " " (list (data it) more)))
|
||||||
|
(next it))))
|
||||||
|
|
||||||
;;;; stream iterator implementation
|
;;;; stream iterator implementation
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue