-
Notifications
You must be signed in to change notification settings - Fork 1
/
def.cljc
212 lines (154 loc) · 5.47 KB
/
def.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(ns fif.def
(:require
[clojure.string :as str]
[fif.stack-machine :as stack]
[fif.stack-machine.evaluators :as evaluators]
[fif.stack-machine.error-handling :as error-handling]
[fif.stack-machine.exceptions :as exceptions]
[fif.stack-machine.verification :as verification]
[fif.stack-machine.words :as words]
[fif.stack-machine.variable :as variable]))
(defn wrap-code-eval
[args]
(fn [sm]
(-> sm
(evaluators/eval-fn args)
(stack/set-step-num 0))))
(defn wrap-function-with-arity
"Wraps a clojure function `f` which accepts `num-args`, and returns
the function wrapped to be used in a stack machine.
The returned function accepts `num-args` values on the stack, drops
`num-args` after processing the wrapped function, and pushes the
result of `(apply f args)` back onto the stack.
Notes:
This wrapper always returns a result on the stack. If you do not
wish to return a result, use `fif.def/wrap-procedure-with-arity`
instead.
Examples:
(defn add2 [x] (+ x 2))
(def my-stack-machine (-> (fif.stack/new-stack-machine)
(fif.stack/set-word 'add2 (fif.def/wrap-function-with-arity 1 add2))))
(fif.core/with-stack my-stack-machine
(fif.core/reval 1 add2)) ;; => '(3)"
[num-args f]
(fn [sm]
(cond
;; Check to see if the main stack has enough arguments to
;; satisfy the word operation.
(not (verification/stack-satisfies-arity? sm num-args))
(exceptions/raise-incorrect-arity-error sm num-args)
:else
(let [args (take num-args (stack/get-stack sm))
result (apply f (reverse args))
new-stack (as-> sm $
(stack/get-stack $)
(drop num-args $)
(concat [result] $)
(apply list $))]
(-> sm
(stack/set-stack new-stack)
stack/dequeue-code)))))
(defn wrap-procedure-with-arity
"Wraps a clojure function `f` which accepts `num-args`, and returns
the function wrapped to be used in a stack machine.
The returned function accepts `num-args` values on the stack, drops
`num-args` after processing the wrapped function.
Notes:
This wrapper never returns a result on the stack. If you wish to
return a result, use `fif.def/wrap-function-with-arity` instead.
Examples:
(def val (atom nil))
(defn set-val! [x] (reset! val x))
(def my-stack-machine (-> (fif.stack/new-stack-machine)
(fif.stack/set-word 'set-val!
(fif.def/wrap-procedure-with-arity 1 set-val!))))
(fif.core/with-stack my-stack-machine
(fif.core/reval 1 set-val!)
(deref val)) ;; => 1
"
[num-args f]
(fn [sm]
(cond
;; Check to see if the main stack has enough arguments to
;; satisfy the word function.
(not (verification/stack-satisfies-arity? sm num-args))
(exceptions/raise-incorrect-arity-error sm num-args)
:else
(let [args (take num-args (stack/get-stack sm))
_ (apply f (reverse args))
new-stack (->> sm stack/get-stack (drop num-args))]
(-> sm
(stack/set-stack (into '() new-stack))
stack/dequeue-code)))))
(defmacro defcode-eval
"Allows you to define functions that contain fif code, which can then
be passed through a fif stack machine to be evaluated.
Example:
(defcode-eval import-add2-library
fn add2
+ 2
endfn)
(def custom-stack-machine
(-> fif.core/*default-stack*
import-add2-library))
(fif.core/with-stack custom-stack-machine
(fif.core/reval 2 add2)) ;; => '(4)"
[name & body]
`(def ~name (wrap-code-eval (quote ~body))))
;;
;; Define Stack Functions
;;
(defmacro defstack-func-0 [name f]
`(def ~name (wrap-function-with-arity 0 ~f)))
(defmacro defstack-func-1 [name f]
`(def ~name (wrap-function-with-arity 1 ~f)))
(defmacro defstack-func-2 [name f]
`(def ~name (wrap-function-with-arity 2 ~f)))
(defmacro defstack-func-3 [name f]
`(def ~name (wrap-function-with-arity 3 ~f)))
;;
;; Define Stack Procedures
;;
(defmacro defstack-proc-0 [name f]
`(def ~name (wrap-procedure-with-arity 0 ~f)))
(defmacro defstack-proc-1 [name f]
`(def ~name (wrap-procedure-with-arity 1 ~f)))
(defmacro defstack-proc-2 [name f]
`(def ~name (wrap-procedure-with-arity 2 ~f)))
(defmacro defstack-proc-3 [name f]
`(def ~name (wrap-procedure-with-arity 3 ~f)))
;;
;; Define Global Variables
;;
(def wrap-variable variable/wrap-global-variable)
(defn set-word-variable
"Creates a new word variable in `sm` with the symbol name `wname` and
with the value `value`. Optionally, you can include a docstring, and
a group key.
Notes:
- `value` is automatically wrapped for you. This is not the case
with `set-word-definition`."
[sm wname value & {:keys [doc group]}]
(words/set-global-word-defn
sm
wname (wrap-variable value)
:doc doc :group group
:stdlib? false
:variable? true))
;;
;; Define Global Words
;;
(defn set-word-function
"Creates a new word function in `sm` with the symbol name `wname` and
with the word function defined by `wfunc`.
Notes:
- `wfunc` is a stack-machine function. Normal clojure functions can
be turned into stack-machine functions via
`wrap-function-with-arity` and `wrap-procedure-with-arity`."
[sm wname wfunc & {:keys [doc group]}]
(words/set-global-word-defn
sm
wname wfunc
:doc doc :group group
:stdlib? false
:variable? false))