-
-
Save Sose/b4b319baacc219ba03b919f5b5ad0014 to your computer and use it in GitHub Desktop.
This file contains 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
(ns kilppari.spec | |
(:require [clojure.spec.alpha :as s])) | |
(s/def ::type keyword?) | |
(s/def ::v any?) | |
(s/def ::variable-spec (s/and #(= :variable (:type %)) | |
#(keyword? (:v %)))) | |
(s/def ::value-spec (s/and #(= :value (:type %)) | |
#(number? (:v %)))) | |
(s/def ::script-spec (s/and #(= :script (:type %)) | |
#(s/valid? ::script (:v %)))) | |
(s/def ::argument | |
(s/and | |
(s/keys :req-un [::type ::v]) | |
(s/or ::variable ::variable-spec | |
::value ::value-spec | |
::script ::script-spec))) | |
(s/def ::command keyword?) | |
(s/def ::arguments (s/coll-of ::argument)) | |
(s/def ::instruction (s/tuple ::command ::arguments)) | |
(s/def ::script (s/coll-of ::instruction)) | |
(defn test-it [it] | |
(s/conform ::instruction it)) | |
(def repeat-instr | |
[:repeat [{:type :value :v 1} | |
{:type :script :v [[:move [{:type :value :v 3}]]]}]]) | |
(defn test-stuff [] | |
[(test-it [:move [{:type :value :v 10}]]) ;; ok | |
(test-it [:turn [{:type :variable :v :x}]]) ;; ok | |
(test-it [:move [{:type :value :v :y}]]) ;; fail | |
(test-it [:move [{:type :variable :v 10}]]) ;; fail | |
(test-it repeat-instr)]) ;; ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment