Guest User

Untitled

a guest
Jul 1st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (require ['clojure.string :as 'string])  
  2.  
  3. (def state-machine
  4.   [
  5.    (fn [c] (case c
  6.                  (\a \b) (get state-machine 1)
  7.                  "Error 1"))
  8.    
  9.    (fn [c] (case c
  10.                  (\b) (get state-machine 1)
  11.                  (\c) (get state-machine 2)
  12.                  "Error 2"))
  13.    
  14.    (fn [c] (case c
  15.                  (\d) (get state-machine 3)
  16.                  "Error 3") )
  17.  
  18.    (fn [c] (case c
  19.                  :end "Win"
  20.                  "Error 4"))
  21.    ])
  22.  
  23. (defn state-machine-driver [v machine]
  24.   (loop [fun (get machine 0) st v]
  25.     (println (first st))
  26.     (let [ans (fun (first st))]
  27.       (cond (empty? st) (fun :end)
  28.             (string? ans) ans
  29.             :else (recur ans (rest st))))))
  30.  
  31.  
  32. ;; I think this needs to be a macro because the case statement can only take compile-time
  33. ;; constants and I think I need a macro to write them in
  34. ;; Of course, I'm a noob. I could be wrong
  35. (defmacro make-sm-func [idx or-list self-list star-list]  
  36.   (if (empty? star-list)
  37.     `#(case %
  38.             ~or-list (get ~state-machine (inc ~idx))  ;; (a|b)c*d, this would be at, say, c* and us choosing (\d) to go on
  39.             ~self-list (get ~state-machine ~idx)      ;; (a|b)c*d, this is where we pick more c instead of going to (\d)
  40.             "Error")
  41.     `#(case %
  42.             ~star-list (get ~state-machine (inc ~idx)) ;;In (a|b)c*d, this is (\c) going on to the '(\c)' state
  43.             ~or-list (get ~state-machine (+ 2 ~idx)) ;; In (a|b)c*d,  this will be (\a \b) for (a|b)
  44.             ~self-list (get ~state-machine ~idx)     ;; We picked a (\c) and this is the option for more (\c)
  45.             "Error")))
  46.  
  47. (defn get-parenth [r depth prefix]
  48.   (let [fc (first r)]
  49.     (println fc r depth prefix)
  50.     (case fc
  51.           nil :error
  52.           \(  (recur (rest r) (inc depth) (conj prefix fc))        
  53.           \)  (if (= depth 0)
  54.                 (conj prefix \) )
  55.                 (recur (rest r) (dec depth) (conj prefix fc)))
  56.           (recur (rest r) depth (conj prefix fc)))))
  57.                
  58.  
  59. (defn get-command [re]
  60.   ;; Obviously this is really ugly
  61.   (if (empty? re)
  62.     [:empty []]
  63.     (if (= \( (first re))
  64.       (let [a (let [exp (get-parenth (rest re) 0 [\(])]
  65.                 (if (keyword? exp)
  66.                   :error                                    
  67.                   (subvec exp 1 (- (count exp) 1))))
  68.             b (nth re (+ 2 (count a)))
  69.             re-tail (drop (+ (count a) 3) re)]        
  70.         (cond (= \* b) [:star   a re-tail]
  71.               (= \| b) [:or     a re-tail]
  72.               :else    [:concat a re-tail]))
  73.       (let [a (vector (first re))
  74.             b (nth re 1)
  75.             re-tail (rest re)]
  76.         (cond (= \* b) [:star   a re-tail]
  77.               (= \| b) [:or     a re-tail]
  78.               :else    [:concat a re-tail])))))
  79.  
  80.  
  81.  
  82.  
  83. (defn make-regex [re]
  84.   (let [comm (get-command re)
  85.         fc (first comm)]
  86.     (cond (empty? fc) (make-terminal fc)
  87.           (parenth? fc) (make-regex (contents fc))
  88.           (starred? fc) (make-none+ fc)
  89.           (concat? fc) (make-concat fc)
  90.           (or? fc)  (make-or fc)
  91.           :else   "Unknown error")))
  92.      
  93.  
  94. ;;   (...)  
  95. ;;   a*   highest priority
  96. ;;   ab   medium priority
  97. ;;   a|b  lowest priority
  98. ;;
Add Comment
Please, Sign In to add comment