Repository navigation
Expand file tree
/
Copy pathcore_impl.clj
More file actions
119 lines (106 loc) · 3.5 KB
/
Copy pathcore_impl.clj
File metadata and controls
119 lines (106 loc) · 3.5 KB
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
(ns piplin.types.core-impl
"This namespace provides multimethod implementations of many
clojure core functions. It has them by default dispatch to clojure's
core implementation, but opens the possibility to adding support for
many bitwise and numeric types in piplin.
There are also several macros useful for making unary and binary functions
that delegate to clojure.core implementations, which is useful for
adding support for currently unsupported operations."
(:refer-clojure :exclude [= not= bit-and bit-or bit-xor bit-not + - * inc dec > >= < <= cast and or bit-shift-left bit-shift-right pos? neg? zero?])
(:require [piplin.types])
(:require [piplin.types.numbers])
(:require [clojure.core :as clj])
(:use [slingshot.slingshot])
(:use [piplin.types.binops]))
(defn- make-core-unop-fn
"Makes syntax for a unop using the core
implementation for a given hierarchy key"
[op key]
`(defmethod ~op ~key
[~'x]
(~(symbol "clojure.core" (name op)) ~'x)))
(defn- make-core-binop-fn
"Makes syntax for a binop using the core
implementation for a given hierarchy key"
[op key]
`(defmethod ~op [~key ~key]
[~'x ~'y]
(~(symbol "clojure.core" (name op)) ~'x ~'y)))
(defmacro def-n-ary-binop
"Defines a generic function for a binary operation
on numeric types. Uses the function in clojure/core
to provide implementations for Numbers. Handles
nullary and n-ary invocations by returning zero and
the left-associative folds, respectively. Existing-types
is a vector of hierarchy elements whose implementations
in clojure.core should be integrated."
[op zero existing-types]
(let [core-methods (map #(make-core-binop-fn
op %)
existing-types)
core-unary (map #(make-core-unop-fn
op %)
existing-types)]
`(do
(defmulti ~op nary-dispatch :hierarchy piplin.types/types)
(defmethod ~op :piplin.types.binops/nullary [] ~zero)
(defmethod ~op :piplin.types.binops/n-ary
[~'x ~'y & ~'more]
(if (seq ~'more)
(recur (~op ~'x ~'y) (first ~'more) (next ~'more))
(~op ~'x ~'y)))
~@core-methods
~@core-unary)))
(defmacro def-binary-binop
"Like def-n-ary-binop, but without nullary or left-associative
folds."
[op existing-types]
(let [core-methods (map #(make-core-binop-fn
op %)
existing-types)]
`(do
(defmulti ~op binary-dispatch :hierarchy piplin.types/types)
~@core-methods)))
(def-n-ary-binop + 0 [:j-num])
(def-n-ary-binop - 0 [:j-num])
(def-n-ary-binop * 0 [:j-num])
(def-n-ary-binop bit-and 0 [:j-num])
(def-n-ary-binop bit-or 0 [:j-num])
(def-n-ary-binop bit-xor 0 [:j-num])
(defmulti bit-not
piplin.types/piplin-clojure-dispatch
:hierarchy piplin.types/types)
(defmethod bit-not :use-core-impl
[x]
(clj/not x))
(defmethod bit-not :default
[x]
(clj/not x))
(defn inc
"Increments x"
[x]
(+ x 1))
(defn dec
"Decrements x"
[x]
(- x 1))
;Define the given math operators as extensible piplin operators
;that support AST generation
(def-binary-binop > [:j-num])
(def-binary-binop < [:j-num])
(def-binary-binop >= [:j-num])
(def-binary-binop <= [:j-num])
(def-binary-binop bit-shift-left [:j-num])
(def-binary-binop bit-shift-right [:j-num])
(defn zero?
"Returns true if x is 0"
[x]
(= x 0))
(defn pos?
"Returns true if x is positive"
[x]
(> x 0))
(defn neg?
"Returns true if x is negative"
[x]
(< x 0))