-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
203 lines (157 loc) · 3.13 KB
/
types.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
package main
import (
"fmt"
"io"
"math/big"
"strings"
"os"
)
type Value interface {
isValue()
}
type Boolean bool
func (Boolean) isValue() {}
type Symbol uint
func (Symbol) isValue() {}
// Builtin symbols are listed in builtins.go
type Char rune
func (Char) isValue() {}
type Vector struct {
v *[]Value
}
func (Vector) isValue() {}
type Scope struct {
m map[Symbol]Value
super *Scope
}
func (*Scope) isValue() {}
type Procedure struct {
Scope Scope
Args Value
Ins []Ins
Builtin func(int) error
CallCC func(*Procedure, int) error
Macros map[Symbol]SyntaxRules
IsCont bool
StackRest *Stack
}
func (Procedure) isValue() {}
type Pair struct {
Car *Value
Cdr *Value
}
func (*Pair) isValue() {}
var Empty Value = &Pair{nil, nil}
type Rational big.Rat
func (Rational) isValue() {}
type Integer big.Int
func (Integer) isValue() {}
type String struct {
s *string
}
func (String) isValue() {}
type InputPort struct {
*os.File
}
func (InputPort) isValue() {}
type OutputPort struct {
io.WriteCloser
}
func (OutputPort) isValue() {}
type Scoped struct {
Symbol Symbol
Scope Symbol
}
func (Scoped) isValue() {}
type Eof struct {}
func (Eof) isValue() {}
func WriteValue(v Value, display bool) error {
port := OutputPortStack[len(OutputPortStack) - 1]
switch v.(type) {
case Boolean:
if v.(Boolean) {
port.Write([]byte("#t"))
} else {
port.Write([]byte("#f"))
}
case Symbol:
fmt.Fprint(port, SymbolNames[v.(Symbol)])
case String:
if !display {
fmt.Fprintf(port, "\"%s\"", *v.(String).s)
} else {
fmt.Fprint(port, *v.(String).s)
}
case Char:
ch := rune(v.(Char))
if display {
fmt.Fprintf(port, "%c", ch)
} else {
if ch == '\n' {
fmt.Fprint(port, "#\\newline")
} else if ch == ' ' {
fmt.Fprint(port, "#\\space")
} else {
fmt.Fprintf(port, "#\\%c", ch)
}
}
case Vector:
fmt.Fprint(port, "#(")
for i, item := range *v.(Vector).v {
if i != 0 {
fmt.Fprint(port, " ")
}
WriteValue(item, display)
}
fmt.Fprint(port, ")")
case *Pair:
fmt.Fprint(port, "(")
cur := v.(*Pair)
for cur != Empty {
WriteValue(*cur.Car, display)
if p, ok := (*cur.Cdr).(*Pair); ok {
if p != Empty {
fmt.Fprint(port, " ")
}
cur = (*cur.Cdr).(*Pair)
} else {
fmt.Fprint(port, " . ")
WriteValue(*cur.Cdr, display)
break
}
}
fmt.Fprint(port, ")")
case Integer:
i := big.Int(v.(Integer))
fmt.Fprint(port, i.String())
case Rational:
r := big.Rat(v.(Rational))
fmt.Fprint(port, r.String())
case *Procedure:
fmt.Fprint(port, "[procedure]")
case Procedure:
fmt.Fprint(port, "[imm_procedure]")
case *Scope:
fmt.Fprint(port, "[scope]")
case Scoped:
WriteValue(v.(Scoped).Symbol, display)
case Eof:
fmt.Fprint(port, "[EOF]")
default:
fmt.Fprintf(port, "[??? (%T)]", v)
}
return nil
}
func PrintValue(v Value) error {
return WriteValue(v, false)
}
func Str2Sym(str string) Symbol {
str = strings.ToLower(str) // Symbols are case-insensitive
for i, val := range SymbolNames {
if val == str {
return Symbol(i)
}
}
SymbolNames = append(SymbolNames, str)
return Symbol(len(SymbolNames) - 1)
}