-
Notifications
You must be signed in to change notification settings - Fork 3
/
defs.clj
132 lines (108 loc) · 3.08 KB
/
defs.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(ns geex.java.defs
(:require [clojure.spec.alpha :as spec]
[bluebell.utils.wip.specutils :as specutils]
[geex.core.datatypes :as dt]))
(spec/def ::typed-argument (spec/cat :type any?
:name symbol?))
(defn parsed-typed-argument? [x]
(and (map? x)
(contains? x :type)
(contains? x :name)))
(defn parsed-typed-arguments? [x]
(and (or (sequential? x)
(nil? x))
(every? parsed-typed-argument? x)))
(spec/def ::typed-arguments (spec/spec (spec/* ::typed-argument)))
(spec/def ::defn-args (spec/cat :name symbol?
:arglist ::typed-arguments
:body (spec/* any?)))
(defn parsed-defn-args? [x]
(and (map? x)
(parsed-typed-arguments? (:arglist x))
(symbol? (:name x))
(let [b (:body x)]
(or (sequential? b)
(nil? b)))))
;; https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
(def binary-math-operators ["+" "-" "*" "/"])
(def comparison-operators ["<" "<=" ">=" ">" "==" "!="])
(def logical-operators ["&&" "||" "!"])
(def bit-operators ["~"
"<<"
">>"
">>>"
"&"
"^"
"|"])
(def boolean-result (constantly Boolean/TYPE))
(defn make-operator-info-map [result-fn operators]
(into {} (map (fn [s]
[s {:result-fn result-fn
:name s}])
operators)))
(def operator-info-map
(merge
(make-operator-info-map
dt/math-op-result-type
binary-math-operators)
(make-operator-info-map
boolean-result
comparison-operators)
(make-operator-info-map
boolean-result
logical-operators)
(make-operator-info-map
dt/bit-op-result-type
bit-operators)))
(spec/def ::math-fn-decl (spec/cat :key (spec/? keyword?)
:java-name string?
:arg-count (spec/? number?)))
(defn normalize-math-fn-decl [sp]
(let [conformed (specutils/force-conform ::math-fn-decl (if (string? sp) [sp] sp))]
[(or (:key conformed)
(keyword (:java-name conformed)))
(:java-name conformed)
(or (:arg-count conformed)
1)]))
(def math-functions
(mapv normalize-math-fn-decl
["asin"
"atan"
["atan2" 2]
"cbrt"
"ceil"
["copySign" 2]
"cos"
"cosh"
"exp"
"expm1"
"floor"
["floorDiv" 2]
["floorMod" 2]
"getExponent"
["hypot" 2]
"log"
"log10"
"log1p"
[:math-min "max" 2]
[:math-max "min" 2]
"nextDown"
"nextUp"
"negateExact"
["multiplyExact" 2]
["pow" 2]
"rint"
"round"
["scalb" 2]
"signum"
"sin"
"sinh"
"signum"
"sqrt"
"tan"
"tanh"
"toDegrees"
"toIntExact"
"toRadians"
"ulp"
]))