new forge/sf: first working version processing input list
This commit is contained in:
parent
79afabb4b8
commit
a61dc2f1dc
3 changed files with 44 additions and 8 deletions
|
@ -5,7 +5,8 @@
|
|||
(defpackage :scopes/forge/sf
|
||||
(:use :common-lisp)
|
||||
(:local-nicknames (:iter :scopes/util/iter))
|
||||
(:export #:*stack*
|
||||
(:export #:*input* #:*stack*
|
||||
#:proc-list #:proc-input
|
||||
#:add
|
||||
#:pushd #:popd))
|
||||
|
||||
|
@ -15,19 +16,35 @@
|
|||
(in-package :scopes/forge/sf)
|
||||
|
||||
(defvar *stack* nil)
|
||||
(defvar *input* nil)
|
||||
|
||||
;;;; builtins
|
||||
;;;; core definitions
|
||||
|
||||
(defun proc-list (lst)
|
||||
(setf *input* (make-instance 'iter:list-iterator :data lst))
|
||||
(proc-input))
|
||||
|
||||
(defun proc-input ()
|
||||
(let ((inp *input*))
|
||||
(do ((item (iter:next inp) (iter:next inp)))
|
||||
((null item))
|
||||
(proc-item item))))
|
||||
|
||||
(defun proc-item (item)
|
||||
(typecase item
|
||||
(symbol (funcall item))
|
||||
(t (pushd item))))
|
||||
|
||||
(defun reg2 (sym fn)
|
||||
(setf (fdefinition sym) #'(lambda ()
|
||||
(pushd (funcall fn (popd) (popd))))))
|
||||
|
||||
(reg2 'add #'+)
|
||||
|
||||
;;;; core definitions
|
||||
|
||||
(defun pushd (v)
|
||||
(push v *stack*))
|
||||
|
||||
(defun popd ()
|
||||
(pop *stack*))
|
||||
|
||||
;;;; builtins
|
||||
|
||||
(reg2 'add #'+)
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
(forge:pushd 2)
|
||||
(forge:add)
|
||||
(== (forge:popd) 6)
|
||||
(forge:proc-list '(4 2 forge:add))
|
||||
(== (forge:popd) 6)
|
||||
;(forge:exec-str "4 2 +")
|
||||
;(== (car (forge:dstack)) 6))
|
||||
(t:show-result))
|
||||
|
|
|
@ -5,9 +5,26 @@
|
|||
|
||||
(defpackage :scopes/util/iter
|
||||
(:use :common-lisp)
|
||||
(:export #:list-iterator))
|
||||
(:export #:current #:next
|
||||
#:list-iterator))
|
||||
|
||||
(in-package :scopes/util/iter)
|
||||
|
||||
;;;; iterators
|
||||
|
||||
(defgeneric next (it))
|
||||
(defgeneric current (it))
|
||||
|
||||
(defclass list-iterator ()
|
||||
((data :reader data :initarg :data :initform nil)))
|
||||
((data :reader data :initarg :data :initform nil)
|
||||
(cur :accessor cur)))
|
||||
|
||||
(defmethod initialize-instance :after ((it list-iterator) &key &allow-other-keys)
|
||||
(setf (cur it) (cons nil (data it))))
|
||||
|
||||
(defmethod next ((it list-iterator))
|
||||
(pop (cur it))
|
||||
(current it))
|
||||
|
||||
(defmethod current ((it list-iterator))
|
||||
(car (cur it)))
|
||||
|
|
Loading…
Add table
Reference in a new issue