-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
109 lines (93 loc) · 1.9 KB
/
value.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
//
// Copyright (c) 2022-2023 Markku Rossi
//
// All rights reserved.
//
package scheme
import (
"fmt"
"strings"
"github.com/markkurossi/scheme/types"
)
var (
_ Value = &BigInt{}
_ Value = &Bytevector{}
_ Value = &Frame{}
_ Value = &Identifier{}
_ Value = &Lambda{}
_ Value = &PlainPair{}
_ Value = &Port{}
_ Value = &Vector{}
_ Value = Boolean(true)
_ Value = Character('@')
_ Value = Float(0.0)
_ Value = Int(0)
_ Value = Keyword(0)
_ Value = String("string")
)
// Value implements a Scheme value.
type Value interface {
Scheme() string
Eq(o Value) bool
Equal(o Value) bool
Type() *types.Type
}
// ToString returns a display representation of the value.
func ToString(v Value) string {
if v == nil {
return "'()"
}
return fmt.Sprintf("%v", v)
}
// ToScheme returns a Scheme representation of the value.
func ToScheme(v Value) string {
if v == nil {
return "'()"
}
return v.Scheme()
}
// Flags define symbol flags.
type Flags int
// Symbol flags.
const (
FlagDefined Flags = 1 << iota
FlagConst
)
func (f Flags) String() string {
var result string
if f&FlagDefined != 0 {
result += " defined"
}
if f&FlagConst != 0 {
result += " const"
}
return strings.TrimSpace(result)
}
// Identifier implements identifier values.
type Identifier struct {
Name string
Point Point
GlobalType *types.Type
Global Value
Flags Flags
}
// Scheme returns the value as a Scheme string.
func (v *Identifier) Scheme() string {
return v.String()
}
// Eq tests if the argument value is eq? to this value.
func (v *Identifier) Eq(o Value) bool {
return v.Equal(o)
}
// Equal tests if the argument value is equal to this value.
func (v *Identifier) Equal(o Value) bool {
ov, ok := o.(*Identifier)
return ok && v.Name == ov.Name
}
// Type implements Value.Type.
func (v *Identifier) Type() *types.Type {
return types.Symbol
}
func (v *Identifier) String() string {
return v.Name
}