forge/sf: store words in sf-builtin:... variables; work in progress: use generic functions/methods

This commit is contained in:
Helmut Merz 2024-09-09 12:05:55 +02:00
parent 3e26a361c4
commit ae4495e939
5 changed files with 51 additions and 25 deletions

View file

@ -6,13 +6,10 @@
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:iter :scopes/util/iter)) (:local-nicknames (:iter :scopes/util/iter))
(:export #:*input* #:*stack* (:export #:*input* #:*stack*
#:proc-list #:proc-input #:exec-list #:exec-input
#:add #:mul #:dup #:reg #:reg2
#:pushd #:popd #:peekd)) #:pushd #:popd #:peekd))
(defpackage :sf-builtin)
(defpackage :sf-user)
(in-package :scopes/forge/sf) (in-package :scopes/forge/sf)
(defvar *stack* nil) (defvar *stack* nil)
@ -20,24 +17,28 @@
;;;; core definitions ;;;; core definitions
(defun proc-list (lst) (defclass word () ())
(defclass comp-word (word) ())
(defun exec-list (lst)
(setf *input* (make-instance 'iter:list-iterator :data lst)) (setf *input* (make-instance 'iter:list-iterator :data lst))
(proc-input)) (exec-input))
(defun proc-input () (defun exec-input ()
(let ((inp *input*)) (iter:process *input* #'exec-item))
(do ((end (iter:next inp) (iter:next inp)))
(end)
(proc-item (iter:value inp)))))
(defun proc-item (item) (defun exec-item (item)
(typecase item (typecase item
(symbol (funcall item)) (function (funcall item))
(symbol (funcall (symbol-value item)))
(t (pushd item)))) (t (pushd item))))
(defun reg (sym fn)
(setf (symbol-value sym) fn))
(defun reg2 (sym fn) (defun reg2 (sym fn)
(setf (fdefinition sym) (reg sym #'(lambda () (pushd (funcall fn (popd) (popd))))))
#'(lambda () (pushd (funcall fn (popd) (popd))))))
(defun pushd (v) (defun pushd (v)
(push v *stack*)) (push v *stack*))
@ -50,7 +51,14 @@
;;;; builtins ;;;; builtins
(reg2 'add #'+) (defpackage :sf-builtin
(reg2 'mul #'*) (:use :common-lisp)
(:local-nicknames (:f :scopes/forge/sf))
(:export #:add #:mul #:dup))
(defun dup () (pushd (peekd))) (in-package :sf-builtin)
(f:reg2 'add #'+)
(f:reg2 'mul #'*)
(f:reg 'dup #'(lambda () (pushd (peekd))))

View file

@ -33,5 +33,5 @@
:perform (test-op (o c) :perform (test-op (o c)
(symbol-call :scopes/test-config :run) (symbol-call :scopes/test-config :run)
(symbol-call :scopes/test-core :run) (symbol-call :scopes/test-core :run)
(symbol-call :scopes/test-forge :run) ;(symbol-call :scopes/test-forge :run)
(symbol-call :scopes/test-sf :run))) (symbol-call :scopes/test-sf :run)))

View file

@ -87,6 +87,8 @@
) )
(deftest test-util-iter () (deftest test-util-iter ()
(let ((it (make-instance 'iter:list-iterator :data '(1 2 3))))
(== (iter:value it) nil))
) )
(deftest test-shape() (deftest test-shape()

View file

@ -3,7 +3,8 @@
;;;; testing facility for scopes/forge ;;;; testing facility for scopes/forge
(defpackage :scopes/test-sf (defpackage :scopes/test-sf
(:use :common-lisp) (:use :common-lisp
:sf-builtin)
(:local-nicknames (:forge :scopes/forge/sf) (:local-nicknames (:forge :scopes/forge/sf)
(:util :scopes/util) (:util :scopes/util)
(:t :scopes/testing)) (:t :scopes/testing))
@ -20,8 +21,8 @@
(test-exec))) (test-exec)))
(deftest test-exec () (deftest test-exec ()
;(forge:proc-str "4 2 add") ;(forge:exec-str "4 2 add")
(forge:proc-list '(4 2 forge:add)) (forge:exec-list '(4 2 add))
(util:lgi forge:*stack*) (util:lgi forge:*stack*)
(== (forge:popd) 6) (== (forge:popd) 6)
(t:show-result)) (t:show-result))

View file

@ -5,17 +5,32 @@
(defpackage :scopes/util/iter (defpackage :scopes/util/iter
(:use :common-lisp) (:use :common-lisp)
(:export #:next #:value (:export #:next #:value #:process
#:list-iterator)) #:list-iterator))
(in-package :scopes/util/iter) (in-package :scopes/util/iter)
;;;; iterators ;;;; iterators
(defclass abstract-iterator () ())
(defgeneric next (it)) (defgeneric next (it))
(defgeneric value (it)) (defgeneric value (it))
(defclass list-iterator () (defgeneric next-value (it)
(:method ((it abstract-iterator))
(next it)
(value it)))
(defgeneric process (it fn &optional stop)
(:method ((it abstract-iterator) fn &optional (stop (constantly nil)))
(do ((eoi (next it) (next it)))
((or eoi (funcall stop)))
(funcall fn (value it)))))
;;;; list iterator implementation
(defclass list-iterator (abstract-iterator)
((data :reader data :initarg :data :initform nil) ((data :reader data :initarg :data :initform nil)
(cur :accessor cur))) (cur :accessor cur)))