Some changes for CLJS compatibility

This commit is contained in:
skwak22 2020-07-14 16:05:20 +09:00
parent 7820d5529b
commit bf54db427b
33 changed files with 82 additions and 33 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/propeller/.DS_Store vendored Normal file

Binary file not shown.

0
src/propeller/core.cljc Normal file → Executable file
View File

0
src/propeller/genome.cljc Normal file → Executable file
View File

0
src/propeller/gp.cljc Normal file → Executable file
View File

3
src/propeller/problems/simple_regression.cljc Normal file → Executable file
View File

@ -58,4 +58,5 @@
(assoc individual (assoc individual
:behaviors outputs :behaviors outputs
:errors errors :errors errors
:total-error (apply +' errors)))) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors)))))

9
src/propeller/problems/software/number_io.cljc Normal file → Executable file
View File

@ -5,7 +5,8 @@
[propeller.push.utils.helpers :refer [get-stack-instructions]] [propeller.push.utils.helpers :refer [get-stack-instructions]]
[propeller.utils :as utils] [propeller.utils :as utils]
[propeller.push.state :as state] [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 ;; Tom Helmuth, thelmuth@cs.umass.edu
@ -78,7 +79,8 @@
inputs) inputs)
parsed-outputs (map (fn [output] parsed-outputs (map (fn [output]
(try (read-string output) (try (read-string output)
(catch Exception e 1000.0))) #?(:clj (catch Exception e 1000.0)
:cljs (catch js/Error. e 1000.0))))
outputs) outputs)
errors (map (fn [correct-output output] errors (map (fn [correct-output output]
(min 1000.0 (math/abs (- correct-output output)))) (min 1000.0 (math/abs (- correct-output output))))
@ -87,4 +89,5 @@
(assoc individual (assoc individual
:behaviors parsed-outputs :behaviors parsed-outputs
:errors errors :errors errors
:total-error (apply +' errors))))) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors))))))

3
src/propeller/problems/software/smallest.cljc Normal file → Executable file
View File

@ -89,4 +89,5 @@
(assoc individual (assoc individual
:behaviors outputs :behaviors outputs
:errors errors :errors errors
:total-error (apply +' errors))))) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors))))))

3
src/propeller/problems/string_classification.cljc Normal file → Executable file
View File

@ -68,4 +68,5 @@
(assoc individual (assoc individual
:behaviors outputs :behaviors outputs
:errors errors :errors errors
:total-error (apply +' errors)))) :total-error #?(:clj (apply +' errors)
:cljs (apply + errors)))))

BIN
src/propeller/push/.DS_Store vendored Normal file

Binary file not shown.

0
src/propeller/push/core.cljc Normal file → Executable file
View File

4
src/propeller/push/instructions/bool.cljc Normal file → Executable file
View File

@ -1,6 +1,8 @@
(ns propeller.push.instructions.bool (ns propeller.push.instructions.bool
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction]]))
(:require [propeller.push.utils.helpers :refer [make-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 ;; BOOLEAN Instructions

10
src/propeller/push/instructions/character.cljc Normal file → Executable file
View File

@ -1,9 +1,13 @@
(ns propeller.push.instructions.character (ns propeller.push.instructions.character
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction
generate-instructions]]))
(:require [propeller.push.state :as state] (:require [propeller.push.state :as state]
[propeller.push.utils.helpers :refer [make-instruction]] [propeller.push.utils.helpers :refer [make-instruction]]
[propeller.push.utils.macros :refer [def-instruction [propeller.tools.character :as char]
generate-instructions]] #?(:clj
[propeller.tools.character :as char])) [propeller.push.utils.macros :refer [def-instruction
generate-instructions]])))
;; ============================================================================= ;; =============================================================================
;; CHAR Instructions ;; CHAR Instructions

15
src/propeller/push/instructions/code.cljc Normal file → Executable file
View File

@ -1,9 +1,13 @@
(ns propeller.push.instructions.code (ns propeller.push.instructions.code
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction
generate-instructions]]))
(:require [propeller.utils :as utils] (:require [propeller.utils :as utils]
[propeller.push.state :as state] [propeller.push.state :as state]
[propeller.push.utils.helpers :refer [make-instruction]] [propeller.push.utils.helpers :refer [make-instruction]]
[propeller.push.utils.macros :refer [def-instruction #?(:clj
generate-instructions]])) [propeller.push.utils.macros :refer [def-instruction
generate-instructions]])))
;; ============================================================================= ;; =============================================================================
;; CODE Instructions ;; CODE Instructions
@ -64,8 +68,11 @@
popped-state popped-state
(state/push-to-stack popped-state (state/push-to-stack popped-state
:exec :exec
(list (+' current-index increment) (list #? (:clj
destination-index (+' current-index increment)
:cljs
(+ current-index increment))
destination-index
:exec_do_range :exec_do_range
to-do)))] to-do)))]
(state/push-to-stack (state/push-to-stack

3
src/propeller/push/instructions/input_output.cljc Normal file → Executable file
View File

@ -1,4 +1,7 @@
(ns propeller.push.instructions.input-output (ns propeller.push.instructions.input-output
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction
generate-instructions]]))
(:require [propeller.push.state :as state] (:require [propeller.push.state :as state]
[propeller.push.utils.helpers :refer [make-instruction]] [propeller.push.utils.helpers :refer [make-instruction]]
[propeller.push.utils.macros :refer [def-instruction [propeller.push.utils.macros :refer [def-instruction

22
src/propeller/push/instructions/numeric.cljc Normal file → Executable file
View File

@ -1,8 +1,12 @@
(ns propeller.push.instructions.numeric (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]] (:require [propeller.push.utils.helpers :refer [make-instruction]]
[propeller.push.utils.macros :refer [def-instruction [propeller.tools.math :as math]
generate-instructions]] #?(:cljs [cljs.reader :refer [read-string]]
[propeller.tools.math :as math])) :clj [propeller.push.utils.macros
:refer [def-instruction generate-instructions]])))
;; ============================================================================= ;; =============================================================================
;; FLOAT and INTEGER Instructions (polymorphic) ;; FLOAT and INTEGER Instructions (polymorphic)
@ -40,20 +44,23 @@
(def _add (def _add
^{:stacks #{}} ^{:stacks #{}}
(fn [stack state] (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 ;; Pushes the difference of the top two items (i.e. the second item minus the
;; top item) onto the same stack ;; top item) onto the same stack
(def _subtract (def _subtract
^{:stacks #{}} ^{:stacks #{}}
(fn [stack state] (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 ;; Pushes the product of the top two items onto the same stack
(def _mult (def _mult
^{:stacks #{}} ^{:stacks #{}}
(fn [stack state] (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 ;; 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 ;; top item) onto the same stack. If the top item is zero, pushes 1
@ -105,7 +112,8 @@
(fn [stack state] (fn [stack state]
(make-instruction state (make-instruction state
#(try ((if (= stack :integer) int float) (read-string %)) #(try ((if (= stack :integer) int float) (read-string %))
(catch Exception e)) #?(:clj (catch Exception e)
:cljs (catch js/Error. e)))
[:string] [:string]
stack))) stack)))

8
src/propeller/push/instructions/polymorphic.cljc Normal file → Executable file
View File

@ -1,9 +1,13 @@
(ns propeller.push.instructions.polymorphic (ns propeller.push.instructions.polymorphic
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction
generate-instructions]]))
(:require [propeller.utils :as utils] (:require [propeller.utils :as utils]
[propeller.push.state :as state] [propeller.push.state :as state]
[propeller.push.utils.helpers :refer [make-instruction]] [propeller.push.utils.helpers :refer [make-instruction]]
[propeller.push.utils.macros :refer [def-instruction #?(:clj
generate-instructions]])) [propeller.push.utils.macros :refer [def-instruction
generate-instructions]])))
;; ============================================================================= ;; =============================================================================
;; Polymorphic Instructions ;; Polymorphic Instructions

6
src/propeller/push/instructions/string.cljc Normal file → Executable file
View File

@ -1,9 +1,11 @@
(ns propeller.push.instructions.string (ns propeller.push.instructions.string
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [def-instruction]]))
(:require [clojure.string :as string] (:require [clojure.string :as string]
[propeller.utils :as utils] [propeller.utils :as utils]
[propeller.push.utils.helpers :refer [make-instruction]] [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 ;; STRING Instructions

5
src/propeller/push/instructions/vector.cljc Normal file → Executable file
View File

@ -1,10 +1,13 @@
(ns propeller.push.instructions.vector (ns propeller.push.instructions.vector
#?(:cljs (:require-macros
[propeller.push.utils.macros :refer [generate-instructions]]))
(:require [clojure.string] (:require [clojure.string]
[propeller.utils :as utils] [propeller.utils :as utils]
[propeller.push.state :as state] [propeller.push.state :as state]
[propeller.push.utils.helpers :refer [get-vector-literal-type [propeller.push.utils.helpers :refer [get-vector-literal-type
make-instruction]] make-instruction]]
[propeller.push.utils.macros :refer [generate-instructions]])) #?(:clj
[propeller.push.utils.macros :refer [generate-instructions]])))
;; ============================================================================= ;; =============================================================================
;; VECTOR Instructions ;; VECTOR Instructions

8
src/propeller/push/interpreter.cljc Normal file → Executable file
View File

@ -9,7 +9,7 @@
[state] [state]
(let [popped-state (state/pop-stack state :exec) (let [popped-state (state/pop-stack state :exec)
instruction (first (:exec state)) 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 (cond
;; ;;
;; Recognize functional instruction or input instruction ;; Recognize functional instruction or input instruction
@ -31,8 +31,10 @@
(update popped-state :exec #(concat %2 %1) instruction) (update popped-state :exec #(concat %2 %1) instruction)
;; ;;
:else :else
(throw (Exception. (str "Unrecognized Push instruction in program: " (throw #?(:clj (Exception. (str "Unrecognized Push instruction in program: "
(name instruction))))))) (name instruction)))
:cljs (js/Error. (str "Unrecognized Push instruction in program: "
(name instruction))))))))
(defn interpret-program (defn interpret-program
"Runs the given problem starting with the stacks in start-state." "Runs the given problem starting with the stacks in start-state."

0
src/propeller/push/state.cljc Normal file → Executable file
View File

16
src/propeller/push/utils/helpers.cljc Normal file → Executable file
View File

@ -1,7 +1,10 @@
(ns propeller.push.utils.helpers (ns propeller.push.utils.helpers
(:require [clojure.set] (:require [clojure.set]
[propeller.push.core :as push] [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 ;; 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 ;; enough args on each of the desired stacks, returns a map with keys
@ -42,8 +45,12 @@
(doseq [[instruction-name function] @push/instruction-table] (doseq [[instruction-name function] @push/instruction-table]
(assert (assert
(:stacks (meta function)) (:stacks (meta function))
(format "ERROR: Instruction %s does not have :stacks defined in metadata." #?(:clj (format
(name instruction-name)))) "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 (for [[instruction-name function] @push/instruction-table
:when (clojure.set/subset? (:stacks (meta function)) stacks)] :when (clojure.set/subset? (:stacks (meta function)) stacks)]
instruction-name)) instruction-name))
@ -80,6 +87,7 @@
(defn print-state (defn print-state
[state] [state]
(doseq [stack (keys state/empty-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) '())) (prn (if (get state stack) (get state stack) '()))
(flush))) (flush)))

0
src/propeller/selection.cljc Normal file → Executable file
View File

0
src/propeller/session.cljc Normal file → Executable file
View File

0
src/propeller/tools/calculus.cljc Normal file → Executable file
View File

0
src/propeller/tools/character.cljc Normal file → Executable file
View File

0
src/propeller/tools/distributions.cljc Normal file → Executable file
View File

0
src/propeller/tools/math.cljc Normal file → Executable file
View File

0
src/propeller/tools/metrics.cljc Normal file → Executable file
View File

0
src/propeller/utils.cljc Normal file → Executable file
View File

0
src/propeller/variation.cljc Normal file → Executable file
View File