yongchao li avatar yongchao li committed cf6c29d

add dsl demo

Comments (0)

Files changed (1)

src/clj_test/dsl.clj

+(ns clj_test.dsl)
+(defmulti emit-bash
+  (fn [form] (class form)))
+
+(defmethod emit-bash
+  clojure.lang.PersistentList
+  [form]
+  (case (name (first form))
+    "println" (str "echo " (second form))
+    nil))
+
+(defmethod emit-bash
+  java.lang.String
+  [form]
+  form)
+
+(defmethod emit-bash
+  java.lang.Long
+  [form]
+  (str form))
+
+(defmethod emit-bash
+  java.lang.Double
+  [form]
+  (str form))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+(def ^{:dynamic true} *current-implementation*)
+(defmulti emit
+  (fn [form] [*current-implementation* (class form)]))
+
+(defmethod emit [::common java.lang.String]
+  [form]
+  form)
+
+(defmethod emit [::common java.lang.Long]
+  [form]
+  (str form))
+
+(defmethod emit [::common java.lang.Double]
+  [form]
+  (str form))
+
+(defmethod emit [::bash clojure.lang.PersistentList]
+  [form]
+  (case (name (first form))
+    "println" (str "echo " (second form))
+    nil))
+
+(defmethod emit [::batch clojure.lang.PersistentList]
+  [form]
+  (case (name (first form))
+    "println" (str "ECHO " (second form))
+    nil))
+
+	(defmacro script [form]
+ 	  '(emit '~form))
+
+	(defmacro with-implementation
+ 	  [impl body]
+ 	  '(binding [*current-implementation* impl]
+ 	    ~@body))
+
+ 	(macroexpand (with-implementation ::bash
+ 	  (script
+ 	    (println "a"))))
+
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;(defn emit-bash-form [a]
+;  (case (class a)
+;    clojure.lang.PersistentList
+;    (case (name (first a))
+;      "println" (str "echo " (second a))
+;      nil)
+;    java.lang.String a
+;    java.lang.Integer (str a)
+;    java.lang.Double (str a)
+;    nil))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.