50 lines
1,009 B
Common Lisp
50 lines
1,009 B
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
|
|
#:pushd #:popd))
|
|
|
|
(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 ((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))))))
|
|
|
|
(defun pushd (v)
|
|
(push v *stack*))
|
|
|
|
(defun popd ()
|
|
(pop *stack*))
|
|
|
|
;;;; builtins
|
|
|
|
(reg2 'add #'+)
|