From bf54db427b236c25693f646014bf17f5c4590762 Mon Sep 17 00:00:00 2001 From: skwak22 Date: Tue, 14 Jul 2020 16:05:20 +0900 Subject: [PATCH] Some changes for CLJS compatibility --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/propeller/.DS_Store | Bin 0 -> 6148 bytes src/propeller/core.cljc | 0 src/propeller/genome.cljc | 0 src/propeller/gp.cljc | 0 src/propeller/problems/simple_regression.cljc | 3 ++- .../problems/software/number_io.cljc | 9 ++++--- src/propeller/problems/software/smallest.cljc | 3 ++- .../problems/string_classification.cljc | 3 ++- src/propeller/push/.DS_Store | Bin 0 -> 6148 bytes src/propeller/push/core.cljc | 0 src/propeller/push/instructions/bool.cljc | 4 +++- .../push/instructions/character.cljc | 10 +++++--- src/propeller/push/instructions/code.cljc | 15 ++++++++---- .../push/instructions/input_output.cljc | 3 +++ src/propeller/push/instructions/numeric.cljc | 22 ++++++++++++------ .../push/instructions/polymorphic.cljc | 8 +++++-- src/propeller/push/instructions/string.cljc | 6 +++-- src/propeller/push/instructions/vector.cljc | 5 +++- src/propeller/push/interpreter.cljc | 8 ++++--- src/propeller/push/state.cljc | 0 src/propeller/push/utils/helpers.cljc | 16 +++++++++---- .../push/utils/{macros.cljc => macros.clj} | 0 src/propeller/selection.cljc | 0 src/propeller/session.cljc | 0 src/propeller/tools/calculus.cljc | 0 src/propeller/tools/character.cljc | 0 src/propeller/tools/distributions.cljc | 0 src/propeller/tools/math.cljc | 0 src/propeller/tools/metrics.cljc | 0 src/propeller/utils.cljc | 0 src/propeller/variation.cljc | 0 33 files changed, 82 insertions(+), 33 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/propeller/.DS_Store mode change 100644 => 100755 src/propeller/core.cljc mode change 100644 => 100755 src/propeller/genome.cljc mode change 100644 => 100755 src/propeller/gp.cljc mode change 100644 => 100755 src/propeller/problems/simple_regression.cljc mode change 100644 => 100755 src/propeller/problems/software/number_io.cljc mode change 100644 => 100755 src/propeller/problems/software/smallest.cljc mode change 100644 => 100755 src/propeller/problems/string_classification.cljc create mode 100644 src/propeller/push/.DS_Store mode change 100644 => 100755 src/propeller/push/core.cljc mode change 100644 => 100755 src/propeller/push/instructions/bool.cljc mode change 100644 => 100755 src/propeller/push/instructions/character.cljc mode change 100644 => 100755 src/propeller/push/instructions/code.cljc mode change 100644 => 100755 src/propeller/push/instructions/input_output.cljc mode change 100644 => 100755 src/propeller/push/instructions/numeric.cljc mode change 100644 => 100755 src/propeller/push/instructions/polymorphic.cljc mode change 100644 => 100755 src/propeller/push/instructions/string.cljc mode change 100644 => 100755 src/propeller/push/instructions/vector.cljc mode change 100644 => 100755 src/propeller/push/interpreter.cljc mode change 100644 => 100755 src/propeller/push/state.cljc mode change 100644 => 100755 src/propeller/push/utils/helpers.cljc rename src/propeller/push/utils/{macros.cljc => macros.clj} (100%) mode change 100644 => 100755 mode change 100644 => 100755 src/propeller/selection.cljc mode change 100644 => 100755 src/propeller/session.cljc mode change 100644 => 100755 src/propeller/tools/calculus.cljc mode change 100644 => 100755 src/propeller/tools/character.cljc mode change 100644 => 100755 src/propeller/tools/distributions.cljc mode change 100644 => 100755 src/propeller/tools/math.cljc mode change 100644 => 100755 src/propeller/tools/metrics.cljc mode change 100644 => 100755 src/propeller/utils.cljc mode change 100644 => 100755 src/propeller/variation.cljc diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5a9bc5582b4d295c6ea69cfe148e677c7d6f08d8 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8ZV<5tL65n3>mk*mCn3~&@Fqm`pwcEyv4NOKlUlS!@)-I?zJagf z%cNXhnSt4FGC!N_m#~`=Ldfsd-m1=Eic||F;s@Bw%H%up9?&ssI?T;?- z>cr270okW%8qaJ$IEx08Zhhl0O>#d<1|wY@g#$>rJdctvowU<&5@vcH&jc*XQdYOV zIi0qghP|`v6b*a2zYlh6cdsZcwYl9qIPTrtKTIEsr)NW{gs)G_I^hSrfI+JZvwj@F z^I?cwcit$8)8raF9}{4dg&AN5n1O|7fHpm~y6`LI1u_H7z;7AA{lNxBbO}p?dh38e zw*Y`OaBBe@dkL(u30=a{AVffW*#-2dw&9+&}UU@;kxwT{Ga^id4Ccoc7eY5~8E2B1q=8Uzmr{|IOrcwh$pl!12>5mYz; literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..70cf7220f114527fc0bf76e9298cdbbf64487b02 GIT binary patch literal 6148 zcmeHKPfNov6i?>Vlp*dQ>M<8@J8VogJP2j#!JAOggUZ&j;tI7HUFXm_>9^2t$8+8XurL`K|hF90MU=2A4h|%TK(jOwc`55rVyeew#1#+QzI|) zv*DoSr#INU@UvcE?MtQNne7KxQ8;RscTQB2`B4(4x;W~FkaBw+C0#XYsbSI`=y5y~ za2!WC?egw;TyIol<7iTm<7Ts3k%#rhWa5augWBIT_G)u7$63I%K+{V5){$3m>bkv2Q>Km zi18vK3fOp;K$I3;i@8CFfN)a^XiB-gVsKLqerfYui@8Bl&bXc##<4SVdqd%RcJND` z&bVukT4I10m}g+kbjx`DpZ$FQpD&^wF+dEg6a&0?>YcV=N%m}ASR9_U60`$~f_b^Y lSqT{WD27}-iuXaafM22k=vvGTf(L|t1T+oQ5Ceb8z!zLdQ)>VK literal 0 HcmV?d00001 diff --git a/src/propeller/.DS_Store b/src/propeller/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0d0fca78de24838f90dd5274333c79e136f90e21 GIT binary patch literal 6148 zcmeH~F^kyg=4u>tuyyIXzgm zT8trHk9M-;bv4;Kdpj(L56e57Pcby>?XbdxW;LK71*E`CfsdY#e*Pcnf6f217Nt@^ z3j8w#Y&e__d%jejt-oH+>t9**b)%DUIm63O024ooKj~rIFTNmavURdT(~m&Npdba_ Gs=yP*T@u9r literal 0 HcmV?d00001 diff --git a/src/propeller/core.cljc b/src/propeller/core.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/genome.cljc b/src/propeller/genome.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/gp.cljc b/src/propeller/gp.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/problems/simple_regression.cljc b/src/propeller/problems/simple_regression.cljc old mode 100644 new mode 100755 index bc734ff..9606826 --- a/src/propeller/problems/simple_regression.cljc +++ b/src/propeller/problems/simple_regression.cljc @@ -58,4 +58,5 @@ (assoc individual :behaviors outputs :errors errors - :total-error (apply +' errors)))) + :total-error #?(:clj (apply +' errors) + :cljs (apply + errors))))) diff --git a/src/propeller/problems/software/number_io.cljc b/src/propeller/problems/software/number_io.cljc old mode 100644 new mode 100755 index ab42d25..698feaf --- a/src/propeller/problems/software/number_io.cljc +++ b/src/propeller/problems/software/number_io.cljc @@ -5,7 +5,8 @@ [propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.utils :as utils] [propeller.push.state :as state] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + #?(:cljs [cljs.reader :refer [read-string]]))) ;; ============================================================================= ;; Tom Helmuth, thelmuth@cs.umass.edu @@ -78,7 +79,8 @@ inputs) parsed-outputs (map (fn [output] (try (read-string output) - (catch Exception e 1000.0))) + #?(:clj (catch Exception e 1000.0) + :cljs (catch js/Error. e 1000.0)))) outputs) errors (map (fn [correct-output output] (min 1000.0 (math/abs (- correct-output output)))) @@ -87,4 +89,5 @@ (assoc individual :behaviors parsed-outputs :errors errors - :total-error (apply +' errors))))) + :total-error #?(:clj (apply +' errors) + :cljs (apply + errors)))))) diff --git a/src/propeller/problems/software/smallest.cljc b/src/propeller/problems/software/smallest.cljc old mode 100644 new mode 100755 index 9641cfa..7e8c3fb --- a/src/propeller/problems/software/smallest.cljc +++ b/src/propeller/problems/software/smallest.cljc @@ -89,4 +89,5 @@ (assoc individual :behaviors outputs :errors errors - :total-error (apply +' errors))))) + :total-error #?(:clj (apply +' errors) + :cljs (apply + errors)))))) diff --git a/src/propeller/problems/string_classification.cljc b/src/propeller/problems/string_classification.cljc old mode 100644 new mode 100755 index 9811d2a..4d29056 --- a/src/propeller/problems/string_classification.cljc +++ b/src/propeller/problems/string_classification.cljc @@ -68,4 +68,5 @@ (assoc individual :behaviors outputs :errors errors - :total-error (apply +' errors)))) \ No newline at end of file + :total-error #?(:clj (apply +' errors) + :cljs (apply + errors))))) \ No newline at end of file diff --git a/src/propeller/push/.DS_Store b/src/propeller/push/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8544a8f52b6bed10c3852173e96b82c09b2ebd14 GIT binary patch literal 6148 zcmeHKyH3ME5S)b|MG8eq%KHWWz>2~bo7o2_mXnqO%}RUYo7>%! zr|^0K*yeP22G#(UbVrk7xjOozqItW!5zO(+((lW!3Y z>k_p}0Vyz5;6Ar&@Bc6K8}t7(NjoVZ1^$%+w%G2r8@^KY*4fK>uWj^K`j0U-!ns5% kCPpjf##`~zMO^b|o_B>~Vx*IgbfSI)To;)X_zMNT0S{>#ga7~l literal 0 HcmV?d00001 diff --git a/src/propeller/push/core.cljc b/src/propeller/push/core.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/push/instructions/bool.cljc b/src/propeller/push/instructions/bool.cljc old mode 100644 new mode 100755 index aebc7fd..07004f9 --- a/src/propeller/push/instructions/bool.cljc +++ b/src/propeller/push/instructions/bool.cljc @@ -1,6 +1,8 @@ (ns propeller.push.instructions.bool + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction]])) (:require [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction]])) + #?(:clj [propeller.push.utils.macros :refer [def-instruction]]))) ;; ============================================================================= ;; BOOLEAN Instructions diff --git a/src/propeller/push/instructions/character.cljc b/src/propeller/push/instructions/character.cljc old mode 100644 new mode 100755 index 1ed50aa..d858a5b --- a/src/propeller/push/instructions/character.cljc +++ b/src/propeller/push/instructions/character.cljc @@ -1,9 +1,13 @@ (ns propeller.push.instructions.character + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) (:require [propeller.push.state :as state] [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction - generate-instructions]] - [propeller.tools.character :as char])) + [propeller.tools.character :as char] + #?(:clj + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]]))) ;; ============================================================================= ;; CHAR Instructions diff --git a/src/propeller/push/instructions/code.cljc b/src/propeller/push/instructions/code.cljc old mode 100644 new mode 100755 index 010f61d..405fdc9 --- a/src/propeller/push/instructions/code.cljc +++ b/src/propeller/push/instructions/code.cljc @@ -1,9 +1,13 @@ (ns propeller.push.instructions.code + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) (:require [propeller.utils :as utils] [propeller.push.state :as state] [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction - generate-instructions]])) + #?(:clj + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]]))) ;; ============================================================================= ;; CODE Instructions @@ -64,8 +68,11 @@ popped-state (state/push-to-stack popped-state :exec - (list (+' current-index increment) - destination-index + (list #? (:clj + (+' current-index increment) + :cljs + (+ current-index increment)) + destination-index :exec_do_range to-do)))] (state/push-to-stack diff --git a/src/propeller/push/instructions/input_output.cljc b/src/propeller/push/instructions/input_output.cljc old mode 100644 new mode 100755 index ed9b6b5..e5fccd4 --- a/src/propeller/push/instructions/input_output.cljc +++ b/src/propeller/push/instructions/input_output.cljc @@ -1,4 +1,7 @@ (ns propeller.push.instructions.input-output + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) (:require [propeller.push.state :as state] [propeller.push.utils.helpers :refer [make-instruction]] [propeller.push.utils.macros :refer [def-instruction diff --git a/src/propeller/push/instructions/numeric.cljc b/src/propeller/push/instructions/numeric.cljc old mode 100644 new mode 100755 index 19eadc3..0822081 --- a/src/propeller/push/instructions/numeric.cljc +++ b/src/propeller/push/instructions/numeric.cljc @@ -1,8 +1,12 @@ (ns propeller.push.instructions.numeric + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) (:require [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction - generate-instructions]] - [propeller.tools.math :as math])) + [propeller.tools.math :as math] + #?(:cljs [cljs.reader :refer [read-string]] + :clj [propeller.push.utils.macros + :refer [def-instruction generate-instructions]]))) ;; ============================================================================= ;; FLOAT and INTEGER Instructions (polymorphic) @@ -40,20 +44,23 @@ (def _add ^{:stacks #{}} (fn [stack state] - (make-instruction state +' [stack stack] stack))) + #?(:clj (make-instruction state +' [stack stack] stack) + :cljs (make-instruction state + [stack stack] stack)))) ;; Pushes the difference of the top two items (i.e. the second item minus the ;; top item) onto the same stack (def _subtract ^{:stacks #{}} (fn [stack state] - (make-instruction state -' [stack stack] stack))) + #?(:clj (make-instruction state -' [stack stack] stack) + :cljs (make-instruction state - [stack stack] stack)))) ;; Pushes the product of the top two items onto the same stack (def _mult ^{:stacks #{}} (fn [stack state] - (make-instruction state *' [stack stack] stack))) + #?(:clj (make-instruction state *' [stack stack] stack) + :cljs (make-instruction state * [stack stack] stack)))) ;; Pushes the quotient of the top two items (i.e. the second item divided by the ;; top item) onto the same stack. If the top item is zero, pushes 1 @@ -105,7 +112,8 @@ (fn [stack state] (make-instruction state #(try ((if (= stack :integer) int float) (read-string %)) - (catch Exception e)) + #?(:clj (catch Exception e) + :cljs (catch js/Error. e))) [:string] stack))) diff --git a/src/propeller/push/instructions/polymorphic.cljc b/src/propeller/push/instructions/polymorphic.cljc old mode 100644 new mode 100755 index c2958d2..696ef39 --- a/src/propeller/push/instructions/polymorphic.cljc +++ b/src/propeller/push/instructions/polymorphic.cljc @@ -1,9 +1,13 @@ (ns propeller.push.instructions.polymorphic + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]])) (:require [propeller.utils :as utils] [propeller.push.state :as state] [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction - generate-instructions]])) + #?(:clj + [propeller.push.utils.macros :refer [def-instruction + generate-instructions]]))) ;; ============================================================================= ;; Polymorphic Instructions diff --git a/src/propeller/push/instructions/string.cljc b/src/propeller/push/instructions/string.cljc old mode 100644 new mode 100755 index 103c65c..7d88e03 --- a/src/propeller/push/instructions/string.cljc +++ b/src/propeller/push/instructions/string.cljc @@ -1,9 +1,11 @@ (ns propeller.push.instructions.string + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [def-instruction]])) (:require [clojure.string :as string] [propeller.utils :as utils] [propeller.push.utils.helpers :refer [make-instruction]] - [propeller.push.utils.macros :refer [def-instruction]] - [propeller.push.state :as state])) + [propeller.push.state :as state] + #?(:clj [propeller.push.utils.macros :refer [def-instruction]]))) ;; ============================================================================= ;; STRING Instructions diff --git a/src/propeller/push/instructions/vector.cljc b/src/propeller/push/instructions/vector.cljc old mode 100644 new mode 100755 index fa9fd10..cf88860 --- a/src/propeller/push/instructions/vector.cljc +++ b/src/propeller/push/instructions/vector.cljc @@ -1,10 +1,13 @@ (ns propeller.push.instructions.vector + #?(:cljs (:require-macros + [propeller.push.utils.macros :refer [generate-instructions]])) (:require [clojure.string] [propeller.utils :as utils] [propeller.push.state :as state] [propeller.push.utils.helpers :refer [get-vector-literal-type make-instruction]] - [propeller.push.utils.macros :refer [generate-instructions]])) + #?(:clj + [propeller.push.utils.macros :refer [generate-instructions]]))) ;; ============================================================================= ;; VECTOR Instructions diff --git a/src/propeller/push/interpreter.cljc b/src/propeller/push/interpreter.cljc old mode 100644 new mode 100755 index 4aa9d24..0e01fb2 --- a/src/propeller/push/interpreter.cljc +++ b/src/propeller/push/interpreter.cljc @@ -9,7 +9,7 @@ [state] (let [popped-state (state/pop-stack state :exec) instruction (first (:exec state)) - literal-type (get-literal-type instruction)] ; nil for non-literals + literal-type (get-literal-type instruction)] ; nil for non-literals (cond ;; ;; Recognize functional instruction or input instruction @@ -31,8 +31,10 @@ (update popped-state :exec #(concat %2 %1) instruction) ;; :else - (throw (Exception. (str "Unrecognized Push instruction in program: " - (name instruction))))))) + (throw #?(:clj (Exception. (str "Unrecognized Push instruction in program: " + (name instruction))) + :cljs (js/Error. (str "Unrecognized Push instruction in program: " + (name instruction)))))))) (defn interpret-program "Runs the given problem starting with the stacks in start-state." diff --git a/src/propeller/push/state.cljc b/src/propeller/push/state.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/push/utils/helpers.cljc b/src/propeller/push/utils/helpers.cljc old mode 100644 new mode 100755 index 1feb201..46eb572 --- a/src/propeller/push/utils/helpers.cljc +++ b/src/propeller/push/utils/helpers.cljc @@ -1,7 +1,10 @@ (ns propeller.push.utils.helpers (:require [clojure.set] [propeller.push.core :as push] - [propeller.push.state :as state])) + [propeller.push.state :as state] + #?(:cljs [goog.string :as gstring]) + #?(:cljs [goog.string.format]))) + ;; Takes a state and a collection of stacks to take args from. If there are ;; enough args on each of the desired stacks, returns a map with keys @@ -42,8 +45,12 @@ (doseq [[instruction-name function] @push/instruction-table] (assert (:stacks (meta function)) - (format "ERROR: Instruction %s does not have :stacks defined in metadata." - (name instruction-name)))) + #?(:clj (format + "ERROR: Instruction %s does not have :stacks defined in metadata." + (name instruction-name)) + :cljs (gstring/format + "ERROR: Instruction %s does not have :stacks defined in metadata." + (name instruction-name))))) (for [[instruction-name function] @push/instruction-table :when (clojure.set/subset? (:stacks (meta function)) stacks)] instruction-name)) @@ -80,6 +87,7 @@ (defn print-state [state] (doseq [stack (keys state/empty-state)] - (printf "%-15s = " stack) + #?(:clj (printf "%-15s = " stack) + :cljs (print (gstring/format "%-15s = " stack))) (prn (if (get state stack) (get state stack) '())) (flush))) diff --git a/src/propeller/push/utils/macros.cljc b/src/propeller/push/utils/macros.clj old mode 100644 new mode 100755 similarity index 100% rename from src/propeller/push/utils/macros.cljc rename to src/propeller/push/utils/macros.clj diff --git a/src/propeller/selection.cljc b/src/propeller/selection.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/session.cljc b/src/propeller/session.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/tools/calculus.cljc b/src/propeller/tools/calculus.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/tools/character.cljc b/src/propeller/tools/character.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/tools/distributions.cljc b/src/propeller/tools/distributions.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/tools/math.cljc b/src/propeller/tools/math.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/tools/metrics.cljc b/src/propeller/tools/metrics.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/utils.cljc b/src/propeller/utils.cljc old mode 100644 new mode 100755 diff --git a/src/propeller/variation.cljc b/src/propeller/variation.cljc old mode 100644 new mode 100755