forge/sf: register compiler words; reverse word index: use for printing functions
This commit is contained in:
parent
180bcca22e
commit
aa8017be9c
2 changed files with 26 additions and 12 deletions
|
@ -6,8 +6,9 @@
|
||||||
(:use :common-lisp)
|
(:use :common-lisp)
|
||||||
(:local-nicknames (:iter :scopes/util/iter))
|
(:local-nicknames (:iter :scopes/util/iter))
|
||||||
(:export #:*input* #:*stack*
|
(:export #:*input* #:*stack*
|
||||||
|
#:word #:comp-word
|
||||||
#:exec-list #:exec-input #:comp-input #:call
|
#:exec-list #:exec-input #:comp-input #:call
|
||||||
#:lit #:reg #:reg2
|
#:lit #:reg #:reg2 #:reg-code
|
||||||
#:pushd #:popd #:peekd))
|
#:pushd #:popd #:peekd))
|
||||||
|
|
||||||
(in-package :scopes/forge/sf)
|
(in-package :scopes/forge/sf)
|
||||||
|
@ -16,6 +17,13 @@
|
||||||
(defvar *input* nil)
|
(defvar *input* nil)
|
||||||
(defvar *buffer* nil)
|
(defvar *buffer* nil)
|
||||||
(defvar *code* nil)
|
(defvar *code* nil)
|
||||||
|
(defvar *func-index* (make-hash-table))
|
||||||
|
|
||||||
|
(defmethod print-object :around ((fn function) s)
|
||||||
|
(let ((sym (gethash fn *func-index*)))
|
||||||
|
(if sym
|
||||||
|
(print-unreadable-object (fn s) (format s "~s" sym))
|
||||||
|
(call-next-method))))
|
||||||
|
|
||||||
;;;; core definitions
|
;;;; core definitions
|
||||||
|
|
||||||
|
@ -32,7 +40,7 @@
|
||||||
(:method ((it symbol))
|
(:method ((it symbol))
|
||||||
(comp-item (symbol-value it))))
|
(comp-item (symbol-value it))))
|
||||||
|
|
||||||
;;;; class word
|
;;;; class word*
|
||||||
|
|
||||||
(defclass word ()
|
(defclass word ()
|
||||||
((func :reader func :initarg :func)))
|
((func :reader func :initarg :func)))
|
||||||
|
@ -68,12 +76,18 @@
|
||||||
(let ((*code* (make-instance 'iter:list-iterator :data code)))
|
(let ((*code* (make-instance 'iter:list-iterator :data code)))
|
||||||
(iter:process *code* #'funcall)))
|
(iter:process *code* #'funcall)))
|
||||||
|
|
||||||
(defun reg (sym fn)
|
(defun reg (sym fn &optional (cls 'word))
|
||||||
(setf (symbol-value sym) (make-instance 'word :func fn)))
|
(setf (symbol-value sym) (make-instance cls :func fn))
|
||||||
|
(setf (gethash fn *func-index*) sym))
|
||||||
|
|
||||||
(defun reg2 (sym fn)
|
(defun reg2 (sym fn)
|
||||||
(reg sym #'(lambda () (pushd (funcall fn (popd) (popd))))))
|
(reg sym #'(lambda () (pushd (funcall fn (popd) (popd))))))
|
||||||
|
|
||||||
|
(defun reg-code (&optional (cls 'word))
|
||||||
|
(let ((name (popd))
|
||||||
|
(code (popd)))
|
||||||
|
(reg name #'(lambda () (call code)) cls)))
|
||||||
|
|
||||||
(defun pushd (v)
|
(defun pushd (v)
|
||||||
(push v *stack*))
|
(push v *stack*))
|
||||||
|
|
||||||
|
@ -93,7 +107,7 @@
|
||||||
(:local-nicknames (:f :scopes/forge/sf)
|
(:local-nicknames (:f :scopes/forge/sf)
|
||||||
(:iter :scopes/util/iter))
|
(:iter :scopes/util/iter))
|
||||||
(:export #:add #:mul #:dup #:in #:? #:lit
|
(:export #:add #:mul #:dup #:in #:? #:lit
|
||||||
#:<comp #:reg))
|
#:<comp #:reg #:regc #:/>))
|
||||||
|
|
||||||
(in-package :sf-builtin)
|
(in-package :sf-builtin)
|
||||||
|
|
||||||
|
@ -108,8 +122,7 @@
|
||||||
|
|
||||||
(f:reg '<comp #'f:comp-input)
|
(f:reg '<comp #'f:comp-input)
|
||||||
|
|
||||||
(f:reg 'reg
|
(f:reg 'reg #'f:reg-code)
|
||||||
#'(lambda ()
|
(f:reg 'regc #'(lambda () (f:reg-code 'f:comp-word)))
|
||||||
(let ((name (popd))
|
|
||||||
(code (popd)))
|
(f:reg '/> #'(lambda () (iter:stop f:*input*)) 'f:comp-word)
|
||||||
(f:reg name #'(lambda () (f:call code))))))
|
|
||||||
|
|
|
@ -23,11 +23,12 @@
|
||||||
(deftest test-exec ()
|
(deftest test-exec ()
|
||||||
;(forge:exec-str "4 2 add")
|
;(forge:exec-str "4 2 add")
|
||||||
(forge:exec-list '(4 2 add))
|
(forge:exec-list '(4 2 add))
|
||||||
(util:lgi forge:*stack*)
|
|
||||||
(== (forge:popd) 6)
|
(== (forge:popd) 6)
|
||||||
(forge:exec-list '(<comp dup mul))
|
(forge:exec-list '(<comp dup mul))
|
||||||
(forge:exec-list '(in square))
|
|
||||||
(util:lgi forge:*stack*)
|
(util:lgi forge:*stack*)
|
||||||
|
(forge:exec-list '(in square reg))
|
||||||
|
(forge:exec-list '(7 square))
|
||||||
|
(== (forge:popd) 49)
|
||||||
(t:show-result))
|
(t:show-result))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue