/
destructuring.cljc
68 lines (52 loc) · 1.84 KB
/
destructuring.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
(ns fif.stdlib.destructuring
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.words :as words]
[fif.stack-machine.mode :as mode]
[fif.stack-machine.variable :refer [wrap-local-variable]]
[fif.stack-machine.exceptions :as exceptions]))
(def arg-destructuring-token '&)
(defn- assign-parameters [sm arg-list]
(reduce
(fn [sm [param arg]]
(let [wfunc (wrap-local-variable arg)]
(words/set-word sm param wfunc)))
(concat [sm] (into [] arg-list))))
(defn- check-parameter-types [parameters]
(some (complement symbol?) parameters))
(defn- pop-stack-n [sm n]
(loop [sm sm n n]
(if (<= n 0)
sm
(recur (stack/pop-stack sm) (dec n)))))
(defn destructure-op
[sm]
(let [[parameters & stack] (stack/get-stack sm)
arguments (take (count parameters) stack)
arg-list (zipmap (reverse parameters) arguments)
sm (-> (pop-stack-n sm (inc (count parameters)))
stack/dequeue-code)]
(cond
;; Parameters should be presented as a vector
(not (vector? parameters))
(exceptions/raise-validation-error
sm 0 parameters
"Parameters for destructuring should be presented in the form of a vector")
;; Parameters must be symbols
(check-parameter-types parameters)
(exceptions/raise-validation-error
sm 0 parameters
"One or more parameters within the vector form are not symbols")
;; TODO: check reserved words
:else
(assign-parameters sm arg-list))))
(defn import-stdlib-destructuring
[sm]
(-> sm
(words/set-global-word-defn
arg-destructuring-token destructure-op
:stdlib? true
:doc "( arguments & [parameters] -- ) Destructures values on
the stack defined by `parameters` and places them in local
variables."
:group :stdlib.destructuring)))