cl-scopes/forge/sf.lisp

56 lines
1.1 KiB
Common Lisp

;;;; cl-scopes/forge - may the forge be with you!
;;;; A Forth-like interpreter implemented in Common Lisp.
(defpackage :scopes/forge/sf
(:use :common-lisp)
(:local-nicknames (:iter :scopes/util/iter))
(:export #:*input* #:*stack*
#:proc-list #:proc-input
#:add #:mul #:dup
#:pushd #:popd #:peekd))
(defpackage :sf-builtin)
(defpackage :sf-user)
(in-package :scopes/forge/sf)
(defvar *stack* nil)
(defvar *input* nil)
;;;; core definitions
(defun proc-list (lst)
(setf *input* (make-instance 'iter:list-iterator :data lst))
(proc-input))
(defun proc-input ()
(let ((inp *input*))
(do ((end (iter:next inp) (iter:next inp)))
(end)
(proc-item (iter:value inp)))))
(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))))))
(defun pushd (v)
(push v *stack*))
(defun popd ()
(pop *stack*))
(defun peekd ()
(car *stack*))
;;;; builtins
(reg2 'add #'+)
(reg2 'mul #'*)
(defun dup () (pushd (peekd)))