-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.go
226 lines (211 loc) · 4.46 KB
/
character.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"strconv"
"unicode"
"github.com/markkurossi/scheme/types"
)
// Character implements character values.
type Character rune
// Scheme returns the value as a Scheme string.
func (v Character) Scheme() string {
return CharacterToScheme(rune(v))
}
// Eq tests if the argument value is eq? to this value.
func (v Character) Eq(o Value) bool {
return v.Equal(o)
}
// Equal tests if the argument value is equal to this value.
func (v Character) Equal(o Value) bool {
ch, ok := o.(Character)
return ok && v == ch
}
// Type implements the Value.Type().
func (v Character) Type() *types.Type {
return types.Character
}
func (v Character) String() string {
return fmt.Sprintf("%c", v)
}
var characters = map[string]rune{
"alarm": 0x0007,
"backspace": 0x0008,
"tab": 0x0009,
"newline": 0x000A,
"linefeed": 0x000A,
"vtab": 0x000B,
"page": 0x000C,
"return": 0x000D,
"space": 0x0020,
"delete": 0x007F,
// C0 control character names.
"nul": 0,
"soh": 1,
"stx": 2,
"etx": 3,
"eot": 4,
"enq": 5,
"ack": 6,
"bel": 7,
"bs": 8,
"ht": 9,
"lf": 10,
"vt": 11,
"ff": 12,
"cr": 13,
"so": 14,
"si": 15,
"dle": 16,
"dc1": 17,
"dc2": 18,
"dc3": 19,
"dc4": 20,
"nak": 21,
"syn": 22,
"etb": 23,
"can": 24,
"em": 25,
"sub": 26,
"esc": 27,
"fs": 28,
"gs": 29,
"rs": 30,
"us": 31,
"sp": 32,
}
func lookupCharacter(name []rune) (rune, error) {
if len(name) == 0 {
return 0, fmt.Errorf("invalid character name")
}
if len(name) == 1 {
return name[0], nil
}
str := string(name)
ch, ok := characters[str]
if ok {
return ch, nil
}
// #\xhhhh
if name[0] == 'x' {
uval, err := strconv.ParseUint(str[1:], 16, 64)
if err == nil {
return rune(uval), nil
}
}
// #\ooo
valid8 := true
for i := 0; i < len(name); i++ {
if !isDigit8(name[i]) {
valid8 = false
break
}
}
if valid8 {
uval, err := strconv.ParseUint(str, 8, 64)
if err == nil {
return rune(uval), nil
}
}
return 0, fmt.Errorf("unknown character '%s'", str)
}
// CharacterToScheme returns the rune as Scheme character literal.
func CharacterToScheme(r rune) string {
for name, ch := range characters {
if r == ch {
return fmt.Sprintf("#\\%s", name)
}
}
if unicode.IsPrint(r) {
return fmt.Sprintf("#\\%c", r)
}
return fmt.Sprintf("#\\x%x", r)
}
var characterBuiltins = []Builtin{
// rnrs
{
Name: "char?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
_, ok := args[0].(Character)
return Boolean(ok), nil
},
},
{
Name: "char->integer",
Args: []string{"obj"},
Return: types.InexactInteger,
Native: func(scm *Scheme, args []Value) (Value, error) {
ch, ok := args[0].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[0])
}
return NewNumber(int64(ch)), nil
},
},
{
Name: "integer->char",
Args: []string{"obj"},
Return: types.Character,
Native: func(scm *Scheme, args []Value) (Value, error) {
n, err := Int64(args[0])
if err != nil {
return nil, fmt.Errorf("invalid number %v", args[0])
}
return Character(rune(n)), nil
},
},
{
Name: "scheme::char=?",
Args: []string{"char1", "char2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
char1, ok := args[0].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[0])
}
char2, ok := args[1].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[1])
}
return Boolean(char1 == char2), nil
},
},
{
Name: "scheme::char<?",
Args: []string{"char1", "char2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
char1, ok := args[0].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[0])
}
char2, ok := args[1].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[1])
}
return Boolean(char1 < char2), nil
},
},
{
Name: "scheme::char>?",
Args: []string{"char1", "char2"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
char1, ok := args[0].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[0])
}
char2, ok := args[1].(Character)
if !ok {
return nil, fmt.Errorf("invalid character %v", args[1])
}
return Boolean(char1 > char2), nil
},
},
}