Created
December 15, 2012 15:10
-
-
Save ympbyc/4296165 to your computer and use it in GitHub Desktop.
Kマシンでプリミティブとコンスタントを扱う方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;((-> (a b (** + a b))) 5 6) | |
( | |
(,CLOSURE ((,STOP))) | |
(,CLOSURE ( | |
(,GRAB a) | |
(,GRAB b) | |
(,CLOSURE ((,PRIMITIVE +) (,CONTINUE))) | |
(,ACCESS a) | |
(,ACCESS b))) | |
(,DEFINE +) | |
(,CLOSURE ((,CONSTANT 5) (,CONTINUE))) | |
(,CLOSURE ((,CONSTANT 6) (,CONTINUE))) | |
(,ACCESS +) | |
(,CONTINUE) | |
) | |
;( | |
; (:= (+ a b) (** + a b)) | |
; (:= (x) 7) | |
; (+ x 6) | |
;) | |
( | |
(,CLOSURE ((,STOP))) | |
(,CLOSURE ( | |
(,GRAB a) | |
(,GRAB b) | |
(,CLOSURE ((,PRIMITIVE +) (,CONTINUE))) | |
(,ACCESS a) | |
(,ACCESS b) | |
(,CONTINUE))) | |
(,DEFINE +) | |
(,CLOSURE ((,CONSTANT 7) (,CONTINUE))) | |
(,DEFINE x) | |
(,CLOSURE ((,ACCESS x) (,CONTINUE))) | |
(,CLOSURE ((,CONSTANT 6) (,CONTINUE))) | |
(,ACCESS +) | |
(,CONTINUE) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
((-> (x) (+ x 2)) 5) | |
( | |
(CLOSURE ((HALT))) | |
(CLOSURE ( | |
(GRAB) | |
(PRIM +) | |
(ACCESS 0) | |
(CONTINUE))) | |
(DEFINE plus) | |
(LABEL ( | |
(CONSTANT 5) | |
(CONTINUE))) | |
(GRAB) | |
(CONSTANT 2) | |
(ACCESS plus) | |
(ACCESS 0) | |
(CONTINUE) | |
) | |
HALTは定数用スタックのcarを返して終了
クロージャを値として使えるようにするのがむずい
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LABELはCLOSUREの別名
CONSTANTは定数用スタックに値を積む命令
PRIMは定数用スタックから引数を拾って計算結果を定数用スタックに積む
DEFINEはグローバルなハッシュテーブルにスタックの値を保存しとく