Skip to content

Commit b7f78ef

Browse files
author
Oron Port
authored
Merge pull request #157 from soronpo/bitwise
Add bitwise operations
2 parents 70ab7e8 + 2041958 commit b7f78ef

File tree

6 files changed

+58
-0
lines changed

6 files changed

+58
-0
lines changed

src/main/scala/singleton/ops/impl/GeneralMacros.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ trait GeneralMacros {
7575
val != = symbolOf[OpId.!=]
7676
val && = symbolOf[OpId.&&]
7777
val || = symbolOf[OpId.||]
78+
val BitwiseAnd = symbolOf[OpId.BitwiseAnd]
79+
val BitwiseOr = symbolOf[OpId.BitwiseOr]
7880
val Pow = symbolOf[OpId.Pow]
7981
val Min = symbolOf[OpId.Min]
8082
val Max = symbolOf[OpId.Max]
@@ -1207,6 +1209,16 @@ trait GeneralMacros {
12071209
case _ => unsupported()
12081210
}
12091211
}
1212+
def BitwiseAnd : Calc = (a, b) match {
1213+
case (CalcVal(at : Int, att), CalcVal(bt : Int, btt)) => CalcVal(at & bt, q"$att & $btt")
1214+
case (CalcVal(at : Long, att), CalcVal(bt : Long, btt)) => CalcVal(at & bt, q"$att & $btt")
1215+
case _ => unsupported()
1216+
}
1217+
def BitwiseOr : Calc = (a, b) match {
1218+
case (CalcVal(at : Int, att), CalcVal(bt : Int, btt)) => CalcVal(at | bt, q"$att | $btt")
1219+
case (CalcVal(at : Long, att), CalcVal(bt : Long, btt)) => CalcVal(at | bt, q"$att | $btt")
1220+
case _ => unsupported()
1221+
}
12101222
def Or : Calc = a match {
12111223
case CalcLit.Boolean(ab) => //`Or` expressions where the LHS is a literal can be inlined
12121224
if (!ab) b match {
@@ -1350,6 +1362,8 @@ trait GeneralMacros {
13501362
case funcTypes.!= => Neq
13511363
case funcTypes.&& => And
13521364
case funcTypes.|| => Or
1365+
case funcTypes.BitwiseAnd => BitwiseAnd
1366+
case funcTypes.BitwiseOr => BitwiseOr
13531367
case funcTypes.Pow => Pow
13541368
case funcTypes.Min => Min
13551369
case funcTypes.Max => Max

src/main/scala/singleton/ops/impl/OpId.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ object OpId {
4747
sealed trait != extends OpId
4848
sealed trait || extends OpId
4949
sealed trait && extends OpId
50+
sealed trait BitwiseOr extends OpId
51+
sealed trait BitwiseAnd extends OpId
5052
sealed trait Min extends OpId
5153
sealed trait Max extends OpId
5254
sealed trait Substring extends OpId

src/main/scala/singleton/ops/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ package object ops {
126126
type !=[P1, P2] = OpMacro[OpId.!=, P1, P2, NP]
127127
type &&[P1, P2] = OpMacro[OpId.&&, P1, P2, NP]
128128
type ||[P1, P2] = OpMacro[OpId.||, P1, P2, NP]
129+
type BitwiseAnd[P1, P2] = OpMacro[OpId.BitwiseAnd, P1, P2, NP]
130+
type BitwiseOr[P1, P2] = OpMacro[OpId.BitwiseOr, P1, P2, NP]
129131
type SubSequence[S, IBeg, IEnd] = OpMacro[OpId.SubSequence, S, IBeg, IEnd]
130132
type Substring[S, I] = OpMacro[OpId.Substring, S, I, NP]
131133
type StartsWith[S, Prefix] = OpMacro[OpId.StartsWith, S, Prefix, NP]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package singleton.ops
2+
3+
import org.scalacheck.Properties
4+
import singleton.TestUtils._
5+
6+
class BitwiseAndSpec extends Properties("BitwiseAnd") {
7+
property("Int checks") = wellTyped {
8+
implicitly[Require[(W.`9`.T BitwiseAnd W.`1`.T) == W.`1`.T]]
9+
implicitly[Require[(W.`9`.T BitwiseAnd W.`8`.T) == W.`8`.T]]
10+
implicitly[Require[(W.`9`.T BitwiseAnd W.`9`.T) == W.`9`.T]]
11+
implicitly[Require[(W.`9`.T BitwiseAnd W.`0`.T) == W.`0`.T]]
12+
}
13+
property("Long checks") = wellTyped {
14+
implicitly[Require[(W.`9L`.T BitwiseAnd W.`1L`.T) == W.`1L`.T]]
15+
implicitly[Require[(W.`9L`.T BitwiseAnd W.`8L`.T) == W.`8L`.T]]
16+
implicitly[Require[(W.`9L`.T BitwiseAnd W.`9L`.T) == W.`9L`.T]]
17+
implicitly[Require[(W.`9L`.T BitwiseAnd W.`0L`.T) == W.`0L`.T]]
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package singleton.ops
2+
3+
import org.scalacheck.Properties
4+
import singleton.TestUtils._
5+
6+
class BitwiseOrSpec extends Properties("BitwiseOr") {
7+
property("Int checks") = wellTyped {
8+
implicitly[Require[(W.`9`.T BitwiseOr W.`1`.T) == W.`9`.T]]
9+
implicitly[Require[(W.`9`.T BitwiseOr W.`8`.T) == W.`9`.T]]
10+
implicitly[Require[(W.`1`.T BitwiseOr W.`8`.T) == W.`9`.T]]
11+
implicitly[Require[(W.`9`.T BitwiseOr W.`0`.T) == W.`9`.T]]
12+
}
13+
property("Long checks") = wellTyped {
14+
implicitly[Require[(W.`9L`.T BitwiseOr W.`1L`.T) == W.`9L`.T]]
15+
implicitly[Require[(W.`9L`.T BitwiseOr W.`8L`.T) == W.`9L`.T]]
16+
implicitly[Require[(W.`1L`.T BitwiseOr W.`8L`.T) == W.`9L`.T]]
17+
implicitly[Require[(W.`9L`.T BitwiseOr W.`0L`.T) == W.`9L`.T]]
18+
}
19+
}

src/test/scala/singleton/ops/UnsupportedSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class UnsupportedSpec extends Properties("UnsupportedSpec") {
4949
property("!=") = wellTyped {illTyped("""implicitly[W.`true`.T != W.`2`.T]""")}
5050
property("&&") = wellTyped {illTyped("""implicitly[W.`1`.T && W.`2`.T]""")}
5151
property("||") = wellTyped {illTyped("""implicitly[W.`1`.T || W.`2`.T]""")}
52+
property("BitwiseAnd") = wellTyped {illTyped("""implicitly[W.`1`.T BitwiseAnd W.`true`.T]""")}
53+
property("BitwiseOr") = wellTyped {illTyped("""implicitly[W.`1`.T BitwiseOr W.`true`.T]""")}
5254
property("Pow") = wellTyped {illTyped("""implicitly[Pow[W.`true`.T, W.`2`.T]]""")}
5355
property("Min") = wellTyped {illTyped("""implicitly[Min[W.`true`.T, W.`2`.T]]""")}
5456
property("Max") = wellTyped {illTyped("""implicitly[Max[W.`true`.T, W.`2`.T]]""")}

0 commit comments

Comments
 (0)