-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol.go
159 lines (139 loc) · 4.15 KB
/
symbol.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
package vesper
var defaultSymtab = initSymbolTable()
// Intern - internalize the name into the global symbol table
func (vm *VM) Intern(name string) *Object {
sym, ok := vm.Symbols[name]
if !ok {
sym = &Object{text: name}
if IsValidKeywordName(name) {
sym.Type = KeywordType
} else if IsValidTypeName(name) {
sym.Type = TypeType
} else if IsValidSymbolName(name) {
sym.Type = SymbolType
} else {
// This is wrong, but it's the best we can do
if StringType == nil {
panic("StringType is nil " + name)
}
sym.Type = StringType
}
vm.Symbols[name] = sym
}
return sym
}
// IsValidSymbolName returns true if the string is a valid symbol
func IsValidSymbolName(name string) bool {
return len(name) > 0
}
// IsValidTypeName returns true if the string is a typename
func IsValidTypeName(s string) bool {
n := len(s)
return n > 2 && s[0] == '<' && s[n-1] == '>'
}
// IsValidKeywordName returns true if the string is a keyword
func IsValidKeywordName(s string) bool {
n := len(s)
return n > 1 && s[n-1] == ':'
}
// ToKeyword converts the object to a keyword, if possible
func (vm *VM) ToKeyword(obj *Object) (*Object, error) {
switch obj.Type {
case KeywordType:
return obj, nil
case TypeType:
return vm.Intern(obj.text[1:len(obj.text)-1] + ":"), nil
case SymbolType:
return vm.Intern(obj.text + ":"), nil
case StringType:
if IsValidKeywordName(obj.text) {
return vm.Intern(obj.text), nil
} else if IsValidSymbolName(obj.text) {
return vm.Intern(obj.text + ":"), nil
}
}
return nil, Error(ArgumentErrorKey, "to-keyword expected a <keyword>, <type>, <symbol>, or <string>, got a ", obj.Type)
}
func typeNameString(s string) string {
return s[1 : len(s)-1]
}
// TypeName returns the name of the type
func (vm *VM) TypeName(t *Object) (*Object, error) {
if !IsType(t) {
return nil, Error(ArgumentErrorKey, "type-name expected a <type>, got a ", t.Type)
}
return vm.Intern(typeNameString(t.text)), nil
}
// KeywordName returns the keyword as a string
func (vm *VM) KeywordName(t *Object) (*Object, error) {
if !IsKeyword(t) {
return nil, Error(ArgumentErrorKey, "keyword-name expected a <keyword>, got a ", t.Type)
}
return vm.unkeyworded(t)
}
func keywordNameString(s string) string {
return s[:len(s)-1]
}
func unkeywordedString(k *Object) string {
if IsKeyword(k) {
return keywordNameString(k.text)
}
return k.text
}
func (vm *VM) unkeyworded(obj *Object) (*Object, error) {
if IsSymbol(obj) {
return obj, nil
}
if IsKeyword(obj) {
return vm.Intern(keywordNameString(obj.text)), nil
}
return nil, Error(ArgumentErrorKey, "Expected <keyword> or <symbol>, got ", obj.Type)
}
// ToSymbol converts the object to a symbol, or returns an error if
// the object cannot be converted.
func (vm *VM) ToSymbol(obj *Object) (*Object, error) {
switch obj.Type {
case KeywordType:
return vm.Intern(keywordNameString(obj.text)), nil
case TypeType:
return vm.Intern(typeNameString(obj.text)), nil
case SymbolType:
return obj, nil
case StringType:
if IsValidSymbolName(obj.text) {
return vm.Intern(obj.text), nil
}
}
return nil, Error(ArgumentErrorKey, "to-symbol expected a <keyword>, <type>, <symbol>, or <string>, got a ", obj.Type)
}
func initSymbolTable() map[string]*Object {
syms := make(map[string]*Object)
TypeType = &Object{text: "<type>"}
TypeType.Type = TypeType //mutate to bootstrap type type
syms[TypeType.text] = TypeType
KeywordType = &Object{Type: TypeType, text: "<keyword>"}
syms[KeywordType.text] = KeywordType
SymbolType = &Object{Type: TypeType, text: "<symbol>"}
syms[SymbolType.text] = SymbolType
StringType = &Object{Type: TypeType, text: "<string>"}
syms[StringType.text] = StringType
// Fixup empty string
EmptyString.Type = StringType
return syms
}
// Symbol creates a symbol from the given objects
func (vm *VM) Symbol(names []*Object) (*Object, error) {
if len(names) < 1 {
return nil, Error(ArgumentErrorKey, "symbol expected at least 1 argument, got none")
}
name := ""
for _, o := range names {
switch o.Type {
case StringType, SymbolType:
name += o.text
default:
return nil, Error(ArgumentErrorKey, "symbol name component invalid: ", o)
}
}
return vm.Intern(name), nil
}