-
Notifications
You must be signed in to change notification settings - Fork 1
/
macro.cljc
114 lines (86 loc) · 3 KB
/
macro.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(ns fif.stdlib.macro
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.sub-stack :as sub-stack]
[fif.stack-machine.words :refer [set-global-word-defn]]
[fif.stack-machine.exceptions :as exceptions]))
(def macro-define-mode-flag :macro-define-mode)
(def arg-start-macro 'macro)
(def arg-end-macro 'endmacro)
(def macro-store-mode-flag :macro-store-mode)
(def arg-start-macro-store '_!)
(def arg-end-macro-store '!_)
(def arg-create-macro-stack 'macro/stack-create)
(def arg-transfer-macro-stack 'macro/stack->code-stack)
(defn wrap-compiled-macro [wbody]
(fn [sm]
(stack/set-code sm
(concat
[arg-create-macro-stack]
wbody
[arg-transfer-macro-stack]
(-> sm stack/dequeue-code stack/get-code)))))
(defn macro-define-mode
[sm]
(let [arg (-> sm stack/get-code first)
stash (stack/get-stash sm)]
(cond
(= arg arg-end-macro)
(let [macro-content (reverse (sub-stack/get-sub-stack stash))
[wname & wbody] macro-content]
(-> sm
(stack/set-word wname (wrap-compiled-macro wbody))
(stack/set-stash (sub-stack/remove-sub-stack stash))
stack/pop-flag
stack/dequeue-code))
:else
(-> sm
(stack/set-stash (sub-stack/push-sub-stack stash arg))
stack/dequeue-code))))
(defn start-macro
[sm]
(let [stash (stack/get-stash sm)]
(-> sm
(stack/push-flag macro-define-mode-flag)
(stack/set-stash (sub-stack/create-sub-stack stash))
stack/dequeue-code)))
(defn macro-store-mode
[sm]
(let [arg (-> sm stack/get-code first)
temp (stack/get-temp-macro sm)]
(cond
(= arg arg-end-macro-store)
(-> sm
stack/pop-flag
stack/dequeue-code)
:else
(-> sm
(stack/set-temp-macro (sub-stack/push-sub-stack temp arg))
stack/dequeue-code))))
(defn op-create-temp-macro-stack
[sm]
(let [temp (stack/get-temp-macro sm)]
(-> sm
(stack/set-temp-macro (sub-stack/create-sub-stack temp))
stack/dequeue-code)))
(defn op-transfer-macro-stack
[sm]
(let [temp (stack/get-temp-macro sm)
macro-content (-> sm stack/get-temp-macro peek reverse)]
(-> sm
(stack/set-code (concat macro-content (-> sm stack/dequeue-code stack/get-code)))
(stack/set-temp-macro (sub-stack/remove-sub-stack temp)))))
(defn start-macro-store
[sm]
(-> sm
(stack/push-flag macro-store-mode-flag)
stack/dequeue-code))
(defn import-stdlib-macro-mode
[sm]
(-> sm
(set-global-word-defn arg-start-macro start-macro :stdlib? true)
(set-global-word-defn arg-start-macro-store start-macro-store :stdlib? true)
(set-global-word-defn arg-create-macro-stack op-create-temp-macro-stack :stdlib? true)
(set-global-word-defn arg-transfer-macro-stack op-transfer-macro-stack :stdlib? true)
(stack/set-mode macro-define-mode-flag macro-define-mode)
(stack/set-mode macro-store-mode-flag macro-store-mode)))