Files
swift-mirror/stdlib/core/Bool.swift
Dmitri Hrybenko 2cc8fe40d4 stdlib/printing: replace four printing systems with one new one
The old ones were:

- print/println
- printAny
- printf
- Console

The new printing story is just print/println.  Every object can be printed.
You can customize the way it is printed by adopting Printable protocol.  Full
details in comments inside stdlib/core/OutputStream.swift.

Printing is not completely finished yet.  We still have ReplPrintable, which
should be removed, string interpolation still uses String constructors, and
printing objects that don't conform to Printable will result in printing
mangled names.


Swift SVN r18001
2014-05-13 13:07:59 +00:00

137 lines
3.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Bool Datatype and Supporting Operators
//===----------------------------------------------------------------------===//
// Bool is the standard way to reason about truth values.
struct Bool {
var value: Builtin.Int1
/// \brief Default-initialize Boolean value to \c false.
@transparent
init() { value = Builtin.trunc_Word_Int1(0.value) }
@transparent
init(_ v : Builtin.Int1) { value = v }
static var false : Bool {
@transparent
get {
return Bool()
}
}
static var true : Bool {
@transparent
get {
return Bool(Builtin.trunc_Word_Int1(1.value))
}
}
}
var true : Bool {
@transparent
get {
return Bool.true
}
}
var false : Bool {
@transparent
get {
return Bool.false
}
}
extension Bool : LogicValue {
@transparent func _getBuiltinLogicValue() -> Builtin.Int1 {
return value
}
@transparent func getLogicValue() -> Bool { return self }
// Bool can be constructed from LogicValue
init(_ v : LogicValue) {
self = v.getLogicValue()
}
}
extension Bool : ReplPrintable {
func replPrint() {
print(self)
}
}
extension Bool : Printable {
var description: String {
return self ? "true" : "false"
}
}
// This is a magic entrypoint known to the compiler.
@transparent func _getBool(v: Builtin.Int1) -> Bool { return Bool(v) }
//===----------------------------------------------------------------------===//
// Standard Operators
//===----------------------------------------------------------------------===//
// Unary bitwise complement.
@prefix @transparent func ~(a: Bool) -> Bool {
return a ^ true
}
// Unary logical complement.
@prefix @transparent func !(a: Bool) -> Bool {
return ~a
}
@transparent
func ==(lhs: Bool, rhs: Bool) -> Bool {
return Bool(Builtin.cmp_eq_Int1(lhs.value, rhs.value))
}
@transparent
extension Bool : Equatable, Hashable {
var hashValue: Int {
return self ? 1 : 0
}
}
// Bitwise 'and'.
@transparent func & (lhs: Bool, rhs: Bool) -> Bool {
return Bool(Builtin.and_Int1(lhs.value, rhs.value))
}
// Bitwise 'xor'.
@transparent func ^ (lhs: Bool, rhs: Bool) -> Bool {
return Bool(Builtin.xor_Int1(lhs.value, rhs.value))
}
// Bitwise 'or'.
@transparent func | (lhs: Bool, rhs: Bool) -> Bool {
return Bool(Builtin.or_Int1(lhs.value, rhs.value))
}
// Compound assignment (with bitwise and)
@assignment @transparent func &= (inout lhs: Bool, rhs: Bool) {
lhs = lhs & rhs
}
// Compound assignment (with bitwise or)
@assignment @transparent func |= (inout lhs: Bool, rhs: Bool) {
lhs = lhs | rhs
}
// Compound assignment (with bitwise xor)
@assignment @transparent func ^= (inout lhs: Bool, rhs: Bool) {
lhs = lhs ^ rhs
}