Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns calculator
- (:require [clojush]
- [clojure.contrib.math])
- (:use [clojush]
- [clojure.contrib.math]))
- (in-ns 'clojush)
- (def push-types '(:exec :integer :float :code :boolean :auxiliary :tag :zip :string :symbol))
- (define-push-state-structure)
- ;(defn isoperator?
- ; "Is operator?"
- ; [x]
- ; (try (do (apply (ns-resolve *ns* (symbol (str x))) [1 1]) true)
- ; (catch Exception e false)))
- (defn recognize-literal
- "If thing is a literal, return its type -- otherwise return false."
- [thing]
- (cond
- (integer? thing) :integer
- (number? thing) :float
- (string? thing) :string
- (or (= thing true) (= thing false)) :boolean
- true false))
- (in-ns 'calculator)
- (defn toInt
- "convert integer to string"
- [x]
- (if (string? x)
- (try (Integer/parseInt x)
- (catch Exception e nil))
- nil))
- (defn findargs
- [x]
- (:arglists (meta x)))
- (defn tostr
- "Convert a list characters to a string"
- [x]
- (apply str x))
- (defn strToStack
- "Puts variables on correct stack"
- [state]
- (let [x (reverse (:string state))
- floats (atom ())
- symbs (atom ())];A list of all keys pressed in correct order
- (loop [stk x]
- (if (not (empty? stk));If not empty list/stack
- (if (number? (toInt (first stk)));If next entry is a number
- (do (reset! floats (not-lazy (cons
- (toInt
- (tostr
- (take-while #(number? (toInt %)) stk))) @floats)))
- (recur (not-lazy (drop-while #(number? (toInt %)) stk))));Add number to number list
- (do (reset! symbs (not-lazy (cons (symbol (first stk)) @symbs)))
- (recur (not-lazy (rest stk)))))));;If not, add oper to oper list
- ;Push input onto relevant stacks
- (let [state1 (loop [stt @floats st state]
- (if (not (empty? stt))
- (recur (rest stt) (push-item (first stt) :integer st))
- st))];Push numbers onto stack
- (loop [stt1 @symbs st1 state1];Put symbols onto stack
- (if (not (empty? stt1))
- (recur (rest stt1) (push-item (first stt1) :symbol st1))
- st1)))))
- ;
- ;(defn call
- ; "apply type to top 2 integer stack"
- ; [type]
- ; (fn [state]
- ; (let [t1 (top-item type state)
- ; i1 (stack-ref :integer 0 state)
- ; i2 (stack-ref :integer 1 state)]
- ;
- ; (push-item (apply (ns-resolve *ns* (symbol (str t1))) [i1 i2])
- ; :integer
- ; (pop-item type
- ; (pop-item :integer
- ; (pop-item :integer state)))))))
- (defn call
- "apply type to top 2 integer stack"
- [type]
- (fn [state]
- (let [t1 (top-item type state)
- i1 (stack-ref :integer 1 state)
- i2 (stack-ref :integer 0 state)]
- (push-item (apply (ns-resolve *ns* (symbol (str t1))) [i1 i2])
- :integer state))))
- (define-registered symbol_call (call :symbol))
- (defn simulate
- "Simulates capturing data from keyboard and putting in stack"
- [x]
- (loop [n (dec (count x)) state (make-push-state)]
- (if (< n 0)
- state
- (recur (dec n) (not-lazy (push-item (str (nth x n)) :string state))))))
- (define-registered in
- (fn [state] (strToStack state)))
- (defn f1
- [x ans]
- (fn [program]
- (doall
- (list
- (let [state (run-push program
- (not-lazy (strToStack (simulate x))))
- top-num (top-item :integer state)]
- (if (number? top-num)
- (abs (- top-num ans))
- 100))))))
- ;
- ;(isoperator? "^")
- ;(isoperator? 1)
- ;(isoperator? "shit")
- (pushgp
- :error-function (f1 "8+3" 11)
- :atom-generators (concat
- (registered-for-type :integer)
- (list
- 'symbol_call
- 'in))
- :max-points 50
- :evalpush-limit 1000)
Add Comment
Please, Sign In to add comment