-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoctet.go
More file actions
108 lines (89 loc) · 2.58 KB
/
octet.go
File metadata and controls
108 lines (89 loc) · 2.58 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
// Copyright (c) 2024, Peter Ohler, All rights reserved.
package slip
import (
"math/big"
)
// OctetSymbol is the symbol with a value of "octet".
const OctetSymbol = Symbol("octet")
// Octet is a unsigned 8 bit integer.
type Octet byte
// String representation of the Object.
func (obj Octet) String() string {
return string(obj.Readably(nil, &printer))
}
// Append a buffer with a representation of the Object.
func (obj Octet) Append(b []byte) []byte {
return obj.Readably(b, &printer)
}
// Readably appends the object to a byte slice. If p.Readbly is true the
// objects is appended in a readable format otherwise a simple append which
// may or may not be readable.
func (obj Octet) Readably(b []byte, p *Printer) []byte {
return Fixnum(obj).Readably(b, p)
}
// Simplify the Object into an int64.
func (obj Octet) Simplify() any {
return int64(obj)
}
// Equal returns true if this Object and the other are equal in value.
func (obj Octet) Equal(other Object) (eq bool) {
switch to := other.(type) {
case Octet:
eq = obj == to
case Fixnum:
eq = Fixnum(obj) == to
case SingleFloat:
eq = SingleFloat(obj) == to
case DoubleFloat:
eq = DoubleFloat(obj) == to
case *LongFloat:
eq = big.NewFloat(float64(obj)).Cmp((*big.Float)(to)) == 0
case *Ratio:
rat := (*big.Rat)(to)
eq = rat.IsInt() && rat.Num().IsInt64() && rat.Num().Int64() == int64(obj)
case *Bignum:
num := (*big.Int)(to)
eq = num.IsInt64() && num.Int64() == int64(obj)
}
return
}
// Hierarchy returns the class hierarchy as symbols for the instance.
func (obj Octet) Hierarchy() []Symbol {
return []Symbol{OctetSymbol, IntegerSymbol, RationalSymbol, RealSymbol, NumberSymbol, TrueSymbol}
}
// IntegerType returns 'octet.
func (obj Octet) IntegerType() Symbol {
return OctetSymbol
}
// RationalType returns 'octet.
func (obj Octet) RationalType() Symbol {
return OctetSymbol
}
// RealType returns 'octet.
func (obj Octet) RealType() Symbol {
return OctetSymbol
}
// NumberType returns 'octet.
func (obj Octet) NumberType() Symbol {
return OctetSymbol
}
// Eval returns self.
func (obj Octet) Eval(s *Scope, depth int) Object {
return obj
}
// RealValue of the number as a float64.
func (obj Octet) RealValue() float64 {
return float64(obj)
}
// IsInt64 returns true if the instance can be represented by an int64.
func (obj Octet) IsInt64() bool {
return true
}
// Int64 of the number.
func (obj Octet) Int64() int64 {
return int64(obj)
}
// LoadForm returns a form that can be evaluated to create the object.
func (obj Octet) LoadForm() Object {
return List{coerceSymbol, Fixnum(obj), List{quoteSymbol, OctetSymbol}}
}