Skip to content

Instantly share code, notes, and snippets.

@Sose

Sose/spec.cljs Secret

Last active May 25, 2021 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sose/b4b319baacc219ba03b919f5b5ad0014 to your computer and use it in GitHub Desktop.
Save Sose/b4b319baacc219ba03b919f5b5ad0014 to your computer and use it in GitHub Desktop.
(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