Advertisement
triclops200

RPN

Nov 24th, 2012
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns rpncore
  2.   (:require [clojure.string :as string]))
  3.  
  4. ;make digit sets for number detection
  5. (def digits (set "0123456789."))
  6.  
  7. ;make sure they are a number
  8. (defn isnumber [N]
  9.   (if (= (count N) 0)
  10.       true
  11.     (let [f (first N)]
  12.           (if (digits f)
  13.             (isnumber (rest N))
  14.             false))))
  15.  
  16. ;error class
  17. (defn error [st]
  18.   (js/alert st)
  19.   (throw (Exception. st)))
  20. (defn join
  21.   ([List] (join List ""))
  22.   ([List st]
  23.  
  24.     ))
  25. (defn caller [nm f n stack]
  26.   (if (>= (count stack) n)
  27.     (if (>= n 0)
  28.       (cons (apply f (take n stack)) (nthnext stack n))
  29.       (f nm stack))
  30.     (error (str nm " takes " n " arguments"))))
  31.  
  32. ;---FUNCTIONS-----
  33.  
  34. ;add
  35. (defn add [a b]
  36.   (+ b a))
  37.  
  38. (defn sub [a b]
  39.   (- b a))
  40.  
  41. (defn mul [a b]
  42.   (* b a))
  43.  
  44. (defn div [a b]
  45.   (/ b a))
  46.  
  47. (defn sqrt [a]
  48.   (.sqrt js/Math a))
  49.  
  50. (defn pow [a b]
  51.   (.pow js/Math b a))
  52.  
  53. (defn sin [a]
  54.   (.sin js/Math a))
  55.  
  56. (defn cos [a]
  57.   (.cos js/Math a))
  58.  
  59. (defn tan [a]
  60.   (.tan js/Math a))
  61.  
  62. (defn log [a]
  63.   (.log js/Math a))
  64.  
  65. (defn logbase [a b]
  66.   (/ (log b) (log a)))
  67. (defn iffun   [a b c]
  68.   (if (= c 0)
  69.     a
  70.     b))
  71. (defn gt  [a b]
  72.   (if (> b a)
  73.     1
  74.     0))
  75. (defn gte  [a b]
  76.   (if (>= b a)
  77.     1
  78.     0))
  79. (defn lt  [a b]
  80.   (if (< b a)
  81.     1
  82.     0))
  83. (defn lte  [a b]
  84.   (if (<= b a)
  85.     1
  86.     0))
  87. (defn eq  [a b]
  88.   (if (= b a)
  89.     1
  90.     0))
  91.  
  92.  
  93. (defn dup [nm stack]
  94.   (if (>= (count stack) 1)
  95.     (cons (first stack) stack)
  96.     (error (str nm " requires 1 argument"))))
  97.  
  98. (defn pi []
  99.   Math/PI)
  100.  
  101. (defn ex []
  102.   Math/E)
  103.  
  104. ;Assign the functions to their characters
  105. (def funcs {"+"     [ add     2]
  106.             "add"   [ add     2]
  107.             "-"     [ sub     2]
  108.             "sub"   [ sub     2]
  109.             "*"     [ mul     2]
  110.             "mul"   [ mul     2]
  111.             "div"   [ div     2]
  112.             "/"     [ div     2]
  113.             "sqrt"  [ sqrt    1]
  114.             "exp"   [ pow     2]
  115.             "pow"   [ pow     2]
  116.             "ln"    [ log     1]
  117.             "sin"   [ sin     1]
  118.             "cos"   [ cos     1]
  119.             "tan"   [ tan     1]
  120.             "pi"    [ pi      0]
  121.             "e"     [ ex      0]
  122.             "dup"   [ dup    -1]
  123.             "$"     [ dup    -1]
  124.             "log"   [ logbase 2]
  125.             "if"    [ iffun   3]
  126.             "<"     [ lt      2]
  127.             "<="    [ lte     2]
  128.             ">"     [ gt      2]
  129.             ">="    [ gte     2]
  130.             "="     [ eq      2]
  131.             })
  132.  
  133.  
  134. ;Stacktraverser
  135. (defn crawler [List stack]
  136.   (if (= (count List) 0)
  137.     stack
  138.     (let [op (first List)]C
  139.       (if (isnumber op)
  140.         (crawler (rest List) (cons (js/parseFloat op) stack))
  141.         (if (contains? funcs op)
  142.           (crawler (rest List) (caller op (first (funcs op)) (second (funcs op)) stack))
  143.           (error (str op " doesn't exist")))))))
  144.  
  145. (defn ^:export evalrpnstring [st]
  146.     (string/join " " (crawler (string/split st #"\s") '())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement