-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.go
71 lines (66 loc) · 1.42 KB
/
symbols.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
//
// Copyright (c) 2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"github.com/markkurossi/scheme/types"
)
var symbolBuiltins = []Builtin{
{
Name: "symbol?",
Args: []string{"obj"},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
_, ok := args[0].(*Identifier)
return Boolean(ok), nil
},
},
{
Name: "symbol->string",
Args: []string{"symbol"},
Return: types.String,
Native: func(scm *Scheme, args []Value) (Value, error) {
id, ok := args[0].(*Identifier)
if !ok {
return nil, fmt.Errorf("not a symbol: %v", args[0])
}
return String(id.Name), nil
},
},
{
Name: "symbol=?",
Args: []string{"symbol1", "symbol2", "symbol3..."},
Return: types.Boolean,
Native: func(scm *Scheme, args []Value) (Value, error) {
var last *Identifier
for idx, arg := range args {
id, ok := arg.(*Identifier)
if !ok {
return nil, fmt.Errorf("invalid symbol: %v", arg)
}
if idx > 0 && id.Name != last.Name {
return Boolean(false), nil
}
last = id
}
return Boolean(true), nil
},
},
{
Name: "string->symbol",
Args: []string{"string"},
Return: types.Symbol,
Native: func(scm *Scheme, args []Value) (Value, error) {
str, ok := args[0].(String)
if !ok {
return nil, fmt.Errorf("not a string: %v", args[0])
}
return &Identifier{
Name: string(str),
}, nil
},
},
}