forge: compile / reg lisp expressions, setup-builtins
This commit is contained in:
parent
1ef29cd06c
commit
ca1f56e11a
2 changed files with 31 additions and 5 deletions
|
|
@ -8,12 +8,13 @@
|
||||||
(:util :scopes/util))
|
(:util :scopes/util))
|
||||||
(:export #:forge-env #:vocabulary #:stack #:current-package
|
(:export #:forge-env #:vocabulary #:stack #:current-package
|
||||||
#:*forge-env* #:*input* #:*code*
|
#:*forge-env* #:*input* #:*code*
|
||||||
|
#:comp-item
|
||||||
#:word #:comp-word
|
#:word #:comp-word
|
||||||
#:repl #:exec-list #:exec-string #:exec-stream #:run
|
#:repl #:exec-list #:exec-string #:exec-stream #:run
|
||||||
#:exec-input #:comp-input #:comp-list #:call
|
#:exec-input #:comp-input #:comp-list #:compile-lisp #:call
|
||||||
#:comp-item
|
|
||||||
#:next #:reg #:reg1 #:reg2 #:reg-code
|
#:next #:reg #:reg1 #:reg2 #:reg-code
|
||||||
#:pushd #:popd #:peekd))
|
#:pushd #:popd #:peekd
|
||||||
|
#:setup-builtins))
|
||||||
|
|
||||||
(in-package :scopes/forge)
|
(in-package :scopes/forge)
|
||||||
|
|
||||||
|
|
@ -85,9 +86,10 @@
|
||||||
(defun input-line (&key prompt)
|
(defun input-line (&key prompt)
|
||||||
(if (null prompt)
|
(if (null prompt)
|
||||||
(setf prompt (format nil "~a> " (length (stack *forge-env*)))))
|
(setf prompt (format nil "~a> " (length (stack *forge-env*)))))
|
||||||
(rl:readline :prompt prompt))
|
(rl:readline :prompt prompt :add-history t))
|
||||||
|
|
||||||
(defun repl ()
|
(defun repl ()
|
||||||
|
; TODO: load history, don't abort on errors, save history
|
||||||
(do ((input (input-line) (input-line)))
|
(do ((input (input-line) (input-line)))
|
||||||
((string= input "q") (stack *forge-env*))
|
((string= input "q") (stack *forge-env*))
|
||||||
(handler-bind
|
(handler-bind
|
||||||
|
|
@ -124,6 +126,9 @@
|
||||||
(let ((*buffer* nil))
|
(let ((*buffer* nil))
|
||||||
(iter:process (iter:list-iterator lst) #'comp-item)
|
(iter:process (iter:list-iterator lst) #'comp-item)
|
||||||
(pushd (reverse *buffer*))))
|
(pushd (reverse *buffer*))))
|
||||||
|
|
||||||
|
(defun compile-lisp (lst)
|
||||||
|
(compile nil (list 'lambda nil lst)))
|
||||||
|
|
||||||
(defun call (code)
|
(defun call (code)
|
||||||
;(util:lgi code)
|
;(util:lgi code)
|
||||||
|
|
@ -132,6 +137,10 @@
|
||||||
((null fn))
|
((null fn))
|
||||||
(funcall fn))))
|
(funcall fn))))
|
||||||
|
|
||||||
|
(defun x-call (code)
|
||||||
|
(let ((*code* (iter:list-iterator code)))
|
||||||
|
(iter:process *code* #'funcall)))
|
||||||
|
|
||||||
(defun reg (sym fn &optional (cls 'word))
|
(defun reg (sym fn &optional (cls 'word))
|
||||||
(let ((sym (intern (symbol-name sym) (current-package *forge-env*))))
|
(let ((sym (intern (symbol-name sym) (current-package *forge-env*))))
|
||||||
(setf (gethash sym (vocabulary *forge-env*)) (make-instance cls :func fn))
|
(setf (gethash sym (vocabulary *forge-env*)) (make-instance cls :func fn))
|
||||||
|
|
@ -172,7 +181,9 @@
|
||||||
#:ptr #:get #:put
|
#:ptr #:get #:put
|
||||||
#:in #:next
|
#:in #:next
|
||||||
#:call #:call-if #:call-while #:comp #:lit #:val
|
#:call #:call-if #:call-while #:comp #:lit #:val
|
||||||
#:<comp #:reg #:regc #:/>))
|
#:<comp #:reg #:regc #:reg-lisp #:/>
|
||||||
|
#:setup-builtins
|
||||||
|
))
|
||||||
|
|
||||||
(in-package :sf-builtin)
|
(in-package :sf-builtin)
|
||||||
|
|
||||||
|
|
@ -219,7 +230,14 @@
|
||||||
|
|
||||||
(f:reg 'reg #'f:reg-code)
|
(f:reg 'reg #'f:reg-code)
|
||||||
(f:reg 'regc #'(lambda () (f:reg-code 'f:comp-word)))
|
(f:reg 'regc #'(lambda () (f:reg-code 'f:comp-word)))
|
||||||
|
(f:reg 'reg-lisp (lambda () (f:reg (f:popd) (f:compile-lisp (f:popd)))))
|
||||||
|
|
||||||
(f:reg '/> #'(lambda () (iter:stop f:*input*)) 'f:comp-word)
|
(f:reg '/> #'(lambda () (iter:stop f:*input*)) 'f:comp-word)
|
||||||
|
|
||||||
;;;; forge-code word definitions
|
;;;; forge-code word definitions
|
||||||
|
|
||||||
|
(defun setup-builtins ()
|
||||||
|
(f:run
|
||||||
|
(in in comp swp reg) comp in def reg
|
||||||
|
(in in swp reg-lisp) comp in def-lisp reg
|
||||||
|
))
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@
|
||||||
(progn
|
(progn
|
||||||
;(forge:setup-builtins)
|
;(forge:setup-builtins)
|
||||||
(test-exec)
|
(test-exec)
|
||||||
|
(test-setup-builtins)
|
||||||
(test-def)
|
(test-def)
|
||||||
(test-val)
|
(test-val)
|
||||||
|
(test-comp)
|
||||||
(test-if)))
|
(test-if)))
|
||||||
(util:lgi (forge:stack forge:*forge-env*))
|
(util:lgi (forge:stack forge:*forge-env*))
|
||||||
(t:show-result)))
|
(t:show-result)))
|
||||||
|
|
@ -35,6 +37,9 @@
|
||||||
(forge:exec-stream (make-string-input-stream "12 square"))
|
(forge:exec-stream (make-string-input-stream "12 square"))
|
||||||
(== (forge:popd) 144))
|
(== (forge:popd) 144))
|
||||||
|
|
||||||
|
(deftest test-setup-builtins ()
|
||||||
|
(sf-builtin:setup-builtins))
|
||||||
|
|
||||||
(deftest test-def ()
|
(deftest test-def ()
|
||||||
(forge:run <comp in <comp swp reg /> in <def reg)
|
(forge:run <comp in <comp swp reg /> in <def reg)
|
||||||
(forge:exec-string "<def cube dup dup mul mul />")
|
(forge:exec-string "<def cube dup dup mul mul />")
|
||||||
|
|
@ -58,6 +63,9 @@
|
||||||
(deftest test-comp ()
|
(deftest test-comp ()
|
||||||
(forge:run (2 3 mul) comp call)
|
(forge:run (2 3 mul) comp call)
|
||||||
(== (forge:popd) 6)
|
(== (forge:popd) 6)
|
||||||
|
(forge:run def cubic (dup dup mul mul)
|
||||||
|
4 cubic)
|
||||||
|
(== (forge:popd) 64)
|
||||||
)
|
)
|
||||||
|
|
||||||
(deftest test-if ()
|
(deftest test-if ()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue