-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolean.go
161 lines (146 loc) · 3.16 KB
/
boolean.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"github.com/markkurossi/scheme/types"
)
// Boolean implements boolean values.
type Boolean bool
// IsBoolean tests if the value is boolean.
func IsBoolean(value Value) (v bool, ok bool) {
var b Boolean
b, ok = value.(Boolean)
if !ok {
return
}
return bool(b), true
}
// Scheme returns the value as a Scheme string.
func (v Boolean) Scheme() string {
return v.String()
}
// Eq tests if the argument value is eq? to this value.
func (v Boolean) Eq(o Value) bool {
return v.Equal(o)
}
// Equal tests if the argument value is equal to this value.
func (v Boolean) Equal(o Value) bool {
ov, ok := o.(Boolean)
return ok && v == ov
}
// Type implements the Value.Type().
func (v Boolean) Type() *types.Type {
return types.Boolean
}
func (v Boolean) String() string {
return BooleanToScheme(bool(v))
}
// IsTrue tests if the argument value is true according to Scheme
// truth semantics i.e. the value is true unless it is boolean #f.
func IsTrue(v Value) bool {
b, ok := v.(Boolean)
if !ok {
return true
}
return bool(b)
}
// BooleanToScheme returns the bool as Scheme boolean literal.
func BooleanToScheme(v bool) string {
var ch rune
if v {
ch = 't'
} else {
ch = 'f'
}
return fmt.Sprintf("#%c", ch)
}
// Equal tests if the values are equal?.
func Equal(a, b Value) bool {
if a == nil && b == nil {
return true
}
if a == nil && b != nil {
return false
}
if a != nil && b == nil {
return false
}
return a.Equal(b)
}
// Eq tests if the values are eq?.
func Eq(a, b Value) bool {
if a == nil && b == nil {
return true
}
if a == nil && b != nil {
return false
}
if a != nil && b == nil {
return false
}
return a.Eq(b)
}
var booleanBuiltins = []Builtin{
{
Name: "not",
Args: []string{"obj"},
Return: types.Boolean,
Flags: FlagConst,
Native: func(scm *Scheme, args []Value) (Value, error) {
return Boolean(!IsTrue(args[0])), nil
},
},
{
Name: "boolean?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
_, ok := args[0].(Boolean)
return Boolean(ok), nil
},
},
{
Name: "scheme::boolean=?",
Args: []string{"bool1", "bool2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
bool1, ok := args[0].(Boolean)
if !ok {
return nil, fmt.Errorf("invalid boolean: %v", args[0])
}
bool2, ok := args[1].(Boolean)
if !ok {
return nil, fmt.Errorf("invalid boolean: %v", args[1])
}
return Boolean(bool1 == bool2), nil
},
},
{
Name: "eqv?",
Args: []string{"obj1", "obj2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
return Boolean(Eq(args[0], args[1])), nil
},
},
{
Name: "eq?",
Args: []string{"obj1", "obj2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
return Boolean(Eq(args[0], args[1])), nil
},
},
{
Name: "equal?",
Args: []string{"obj1", "obj2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
return Boolean(Equal(args[0], args[1])), nil
},
},
}