-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor.cljc
33 lines (27 loc) · 938 Bytes
/
processor.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
(ns fif.stack-machine.processor
(:require
[fif.stack-machine :as stack]
[fif.stack-machine.words :as stack-machine.words]
[fif.stack-machine.pointer :as pointer]))
(defn has-flags? [sm]
(not (empty? (stack/get-flags sm))))
(defn process-mode [sm]
(let [arg (-> sm stack/get-code first)
current-mode (peek (stack/get-flags sm))]
(if-let [modefn (-> sm :modes (get current-mode))]
(modefn sm)
(throw (ex-info "Unable to find mode function for flagged mode: " current-mode)))))
(defn process-arg [sm]
(let [arg (-> sm stack/get-code first)]
(cond
(symbol? arg)
(let [wfn (stack/get-word sm arg)]
(if-not (= wfn stack-machine.words/not-found)
(wfn sm)
(-> sm
(stack/push-stack (pointer/trim-pointer-once arg))
stack/dequeue-code)))
:else
(-> sm
(stack/push-stack arg)
stack/dequeue-code))))