File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 124124 [(Vec (->>
125125 lets
126126 flatten
127- (filter #(not = {:Sym 'def} %1 ))
127+ (remove #(= {:Sym 'def} %1 ))
128128 ; ; Handle RHS is mapping
129129 (partition 2 )
130130 (map #(let [[k v] %1 ]
Original file line number Diff line number Diff line change 66
77(ns yamlscript.transformer
88 (:require
9- [yamlscript.ast :refer [Lst Sym QSym]]
9+ [yamlscript.ast :refer [Lst Sym QSym Vec ]]
1010 [yamlscript.common]
1111 [yamlscript.transformers]
1212 [ys.dwim])
128128 [lhs rhs]
129129 [lhs rhs]))
130130
131- (defn transform-def-ops [lhs rhs]
132- (when (and
133- (vector? lhs)
134- (= 3 (count lhs))
135- (= 'def (:Sym (first lhs))))
136- (let [[a b c] lhs
137- lhs [a b]
138- op (:Sym c)
139- op (Sym (or ({'|| 'or
140- '||| 'or?
141- '+ 'add+
142- '* 'mul+
143- '/ 'div+
144- '** 'pow} op) op))
145- rhs (Lst [op b rhs])]
146- [lhs rhs])))
147-
148131(defn swap-underscores [lhs rhs]
149132 (if-lets [_ (get-in lhs [0 :Sym ])
150133 _ (some (partial = {:Sym '_}) lhs)
155138
156139(defn apply-transformer [key val]
157140 (let [[key val] (swap-underscores key val)]
158- (or (if-lets [name (or
141+ (or
142+ (when-lets [name (or
159143 (get-in key [:Sym ])
160144 (get-in key [0 :Sym ]))
161145 sym (symbol (str " transform_" name))
162146 transformer (ns-resolve transformers-ns sym)]
163- (transformer key val)
164- (transform-def-ops key val))
147+ (transformer key val))
165148 [key val])))
166149
167150(defn transform-xmap [node]
Original file line number Diff line number Diff line change 4141 (transform-with-else lhs rhs (Sym " =>" )))
4242
4343; ;-----------------------------------------------------------------------------
44- ; ; defn and fn
44+ ; ; def, defn and fn
4545; ;-----------------------------------------------------------------------------
4646
47+ (comment
48+ (yamlscript.compiler/compile "
49+ !ys-0
50+ defn x():
51+ a b =: c d" )
52+ )
53+
54+ (defn transform_def [lhs rhs]
55+ (cond
56+ (= 2 (count lhs))
57+ (let [rhs (if (and (vector? rhs) (> (count rhs) 1 ))
58+ (Vec rhs)
59+ rhs)]
60+ [lhs rhs])
61+ (= 3 (count lhs))
62+ (let [[a b c] lhs
63+ lhs [a b]
64+ op (:Sym c)
65+ op (Sym (or ({'|| 'or
66+ '||| 'or?
67+ '+ 'add+
68+ '* 'mul+
69+ '/ 'div+
70+ '** 'pow} op) op))
71+ rhs (Lst [op b rhs])]
72+ [lhs rhs])
73+ :else [lhs rhs]))
74+
4775(defn transform_defn [lhs rhs]
4876 (when-lets [lhs (remove nil? lhs)
4977 lhs (vec lhs)
5381 xmap (:xmap rhs)
5482 _ (every? :Lst (->> xmap (partition 2 ) (map first)))
5583 xmap (reduce
56- (fn [acc [lhs rhs]]
57- (let [lhs (Vec (:Lst lhs))]
58- (conj acc lhs rhs)))
59- []
60- (partition 2 xmap))
84+ (fn [acc [lhs rhs]]
85+ (let [lhs (Vec (:Lst lhs))]
86+ (conj acc lhs rhs)))
87+ []
88+ (partition 2 xmap))
6189 rhs {:xmap xmap}]
6290 [lhs rhs]))
6391
Original file line number Diff line number Diff line change 33
44(ns yamlscript.util )
55
6- (declare die )
6+ (defn die
7+ " Throw a string as an exception"
8+ ([] (throw (Exception. " Died" )))
9+ ([msg] (throw (Exception. (str msg " \n " ))))
10+ ([x & xs] (die (apply str x xs))))
711
8- (defmacro condf [x & clauses]
12+ (defmacro condf
13+ " Like condp but with a function"
14+ [x & clauses]
915 `(condp (fn [f# x#] (f# x#)) ~x ~@clauses))
1016
1117(defmacro cond-lets
12- " if-lets but works like cond "
18+ " Like cond-let but with more than one binding "
1319 {:style/indent [0 ]}
1420 [& clauses]
1521 (when clauses
1925 (die " Odd number of forms" ))
2026 (cond-lets ~@(nnext clauses)))))
2127
22- (defn die
23- ([] (throw (Exception. " Died" )))
24- ([msg] (throw (Exception. (str msg " \n " ))))
25- ([x & xs] (die (apply str x xs))))
26-
27- (defn eprint [& xs]
28+ (defn eprint
29+ " Print to stderr"
30+ [& xs]
2831 (binding [*out* *err*]
2932 (apply print xs)))
3033
31- (defn eprintln [& xs]
34+ (defn eprintln
35+ " Print to stderr with a newline"
36+ [& xs]
3237 (binding [*out* *err*]
3338 (apply println xs)))
3439
3540(defmacro if-lets
41+ " Like if-let but with more than one binding"
3642 ([bindings then]
3743 `(if-lets ~bindings ~then nil ))
3844 ([bindings then else]
4248 ~else)
4349 then)))
4450
45- (defn macro? [x]
51+ (defn macro?
52+ " Check if a symbol is a macro"
53+ [x]
4654 (and
4755 (symbol? x)
4856 (when-let [x (resolve x)]
4957 (:macro (meta x)))))
5058
51- (defn type-name [x]
59+ (defn type-name
60+ " Get the name of a type"
61+ [x]
5262 (condf x
5363 map? " Map"
5464 set? " Set"
5868 (type x)))
5969
6070(defmacro when-lets
71+ " Like when-let but with more than one binding"
6172 ([bindings & body]
6273 (if (seq bindings)
6374 `(when-let [~(first bindings) ~(second bindings)]
Original file line number Diff line number Diff line change 742742 (let [[a b c] [1 2 3] {:d d, :e e} {:d 4, :e 5}] (add+ a b c)))
743743
744744
745+ - name : Destructuring let with multiple RHS
746+ yamlscript : |
747+ !ys-0
748+ defn foo():
749+ +[a b c] =: 1 2 3
750+ =>: a + b + c
751+ clojure : |
752+ (defn foo [] (let [[a b c] [1 2 3]] (add+ a b c)))
753+
754+
745755 - name : Destructuring def
746756 yamlscript : |
747757 !ys-0
750760 (+def [a b c] [1 2 3])
751761
752762
763+ - name : Destructuring def with multiple RHS
764+ yamlscript : |
765+ !ys-0
766+ +[a b c] =: 1 2 3
767+ clojure : |
768+ (+def [a b c] [1 2 3])
769+
770+
753771 - name : Multi-arity defn
754772 yamlscript : |
755773 !ys-0
You can’t perform that action at this time.
0 commit comments