1 package frost.core
2
3 class Bit : Value, HashKey<Bit>, Formattable {
4 @private
5 def value:builtin_bit
6
7 @implicit
8 init(value:builtin_bit) {
9 self.value := value
10 }
11
12 @override
13 function get_toString():String {
14 if self {
15 return "true"
16 }
17 else {
18 return "false"
19 }
20 }
21
22 @override
23 function =(other:Bit):Bit {
24 return value = other.value
25 }
26
27 @override
28 function !=(other:Bit):Bit {
29 return value != other.value
30 }
31
32 @override
33 function get_hash():Int {
34 if value {
35 return 1
36 }
37 return 0
38 }
39
40 ================================================================================================
41 Returns the logical inverse of this `Bit`, so `!true` is `false` and `!false` is `true`.
42 ================================================================================================
43 function !():Bit {
44 return Bit(!value)
45 }
46
47 ================================================================================================
48 Returns the logical OR of two `Bit`s.
49 ================================================================================================
50 -- note that the '|' and '&' functions do not have any way to provide for short circuiting, so
51 -- these operators require special support in the compiler. These implementations are therefore
52 -- only actually called in unusual circumstances, such as via dynamic method references.
53 function |(other:Bit):Bit {
54 return value | other.value
55 }
56
57 ================================================================================================
58 Returns the logical AND of two `Bit`s.
59 ================================================================================================
60 function &(other:Bit):Bit {
61 return value & other.value
62 }
63
64 ================================================================================================
65 Returns the logical XOR of two `Bit`s.
66 ================================================================================================
67 function ~(other:Bit):Bit {
68 return Bit(value ~ other.value)
69 }
70
71 ================================================================================================
72 Returns `ifTrue` or `ifFalse` based on the value of this `Bit`.
73 ================================================================================================
74 function choose<T>(ifTrue:T, ifFalse:T):T {
75 if self {
76 return ifTrue
77 }
78 return ifFalse
79 }
80
81 ================================================================================================
82 Chooses one of two strings based on the value of this `Bit`. The format string must be of the
83 form `trueString|falseString`, and the result will be `trueString` if this `Bit` is true,
84 otherwise `falseString`. If multiple `'|'` characters occur in the format string, only the first
85 one is significant and the others are part of the `false` string.
86 ================================================================================================
87 @override
88 function format(fmt:String):String {
89 def s := fmt.split("|")
90 assert s.count = 2, "invalid format string '\{fmt}'"
91 if self {
92 return s[0]
93 }
94 return s[1]
95 }
96 }