Skip to content

Instantly share code, notes, and snippets.

@Sose

Sose/spec.cljs Secret

Created May 26, 2021 07:54
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/32aa813eea3004ecf7c3176b8b7c5a68 to your computer and use it in GitHub Desktop.
Save Sose/32aa813eea3004ecf7c3176b8b7c5a68 to your computer and use it in GitHub Desktop.
(ns kilppari.spec
(:require [clojure.spec.alpha :as s]
[clojure.test.check.generators :as gen]))
(s/def :command/v (s/or :variable keyword?
:value number?))
;(s/def :command/type #{:move :turn :repeat :pen :let :function :call})
(defmulti instruction (fn [[command _]] command))
(defmethod instruction :move [_]
(s/tuple #{:move} :command/v))
(defmethod instruction :turn [_]
(s/tuple #{:turn} :command/v))
(defmethod instruction :repeat [_]
(s/tuple #{:repeat} :command/v :command/script))
(defmethod instruction :pen [_]
(s/tuple #{:pen} #{:up :down}))
(defmethod instruction :let [_]
(s/tuple #{:let} keyword? number?))
(defmethod instruction :function [_]
(s/tuple #{:function} keyword? (s/coll-of keyword?) :command/script))
(defmethod instruction :call [_]
(s/tuple #{:call} keyword? number?))
(s/def :command/instruction (s/multi-spec instruction (fn [genv _tag] genv)))
(s/def :command/start (s/tuple #{:start} number? number? number?))
;; a (sub)script is a vector of instructions
(s/def :command/script (s/coll-of :command/instruction))
;; main scripts must have a "start instruction" at the beginning
(s/def :command/main-script (s/cat :start :command/start
:script (s/* :command/instruction)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment