Skip to content

Commit

Permalink
(wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed Oct 23, 2024
1 parent 79414ff commit 1f8da01
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
:main-opts ["-i" "src/test/cljs_cli/cljs_cli/test_runner.clj"
"-e" "(cljs-cli.test-runner/-main)"]}
:compiler.test {:extra-paths ["src/test/cljs" "src/test/cljs_build" "src/test/cljs_cp"
"src/test/clojure" "src/test/self"]}
"src/test/clojure" "src/test/self"]
:extra-deps {org.clojure/spec.alpha {:mvn/version "0.5.238"}}}
:compiler.test.run {:main-opts ["-i" "src/test/clojure/cljs/test_runner.clj"
"-e" "(cljs.test-runner/-main)"]}
:runtime.test.build {:extra-paths ["src/test/cljs"]
Expand Down
130 changes: 130 additions & 0 deletions src/test/clojure/cljs/analyzer/specs.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
;; Copyright (c) Rich Hickey. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns cljs.analyzer.specs
(:require [clojure.spec.alpha :as s]))

(s/def ::op keyword?)
(s/def ::form any?)
(s/def ::env map?)
(s/def ::context #{:expr :return :statement})

(defmulti node :op)
(s/def ::node (s/multi-spec node :op))

(s/def ::test ::node)
(s/def ::then ::node)
(s/def ::else ::node)

;; TODO: :tag
(s/def ::base
(s/keys
:req-un [::op ::env ::form]))

(defmethod node :if [_]
(s/merge ::base
(s/keys
:req-un [::test ::then]
:opt-un [::else])))

(s/def ::literal? boolean?)
(s/def ::val any?)

(defmethod node :const [_]
(s/merge ::base
(s/keys
:req-un [::literal? ::val]
:opt-un [])))

(s/def ::keys (s/* ::node))
(s/def ::vals (s/* ::node))

(defmethod node :map [_]
(s/merge ::base
(s/keys :req-un [::keys ::vals])))

(s/def ::items (s/* ::node))

(defmethod node :list [_]
(s/merge ::base
(s/keys
:req-un [::items])))

(defmethod node :vector [_]
(s/merge ::base
(s/keys
:req-un [::items])))

(defmethod node :set [_]
(s/merge ::base
(s/keys
:req-un [::items])))

(defmethod node :js-object [_]
(s/merge ::base
(s/keys
:req-un [::keys ::vals])))

(defmethod node :js-array [_]
(s/merge ::base
(s/keys
:req-un [::items])))

(s/def ::var ::node)
(s/def ::sym ::node)
(s/def ::meta map?)

(defmethod node :the-var [_]
(s/merge ::base
(s/keys
:opt-un [::var ::sym ::meta])))

(s/def ::nodes (s/* ::node))
(s/def ::default ::node)

(defmethod node ::case [_]
(s/merge ::base
(s/keys
:req-un [::test ::nodes ::default])))

(defmethod node ::case-node [_]
(s/merge ::base
(s/keys
:req-un [::tests ::then])))

(comment

(s/valid? ::node 1)
(s/valid? ::node
{:op :const
:env {}
:form 1
:literal? true
:val 1})

(s/explain-data ::node
{:op :if
:env {}
:form '(if true true false)
:test {:op :const
:env {}
:form true
:literal? true
:val true}
:then {:op :const
:env {}
:form true
:literal? true
:val true}
:else {:op :const
:env 1
:form false
:literal? true
:val false}})

)

0 comments on commit 1f8da01

Please sign in to comment.