-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
191 lines (177 loc) · 3.64 KB
/
reader.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
package gosdlisp
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
type Reader struct {
ru rune
line []rune
indexOfLine int
lineLength int
br *bufio.Reader
}
func NewReader(in io.Reader) *Reader { return &Reader{br: bufio.NewReader(in)} }
// read is an S-expression reader.
func (r *Reader) Read() (T, error) {
line, _, err := r.br.ReadLine()
if err != nil {
return nil, fmt.Errorf("cannot read line: %v", err)
}
r.lineLength = utf8.RuneCountInString(string(line))
r.line = []rune(string(line) + `\0`)
r.getRune()
return r.getSexp()
}
// getRune reads one character from the rune slice,
// sets a value to rune, and proceeds to indexOfLine.
func (r *Reader) getRune() {
r.ru = r.line[r.indexOfLine]
r.indexOfLine++
}
// getSexp reads an S-expression.
func (r *Reader) getSexp() (T, error) {
for {
r.skipSpace()
switch r.ru {
case '(':
return r.makeList()
case '\'':
return r.makeQuote()
case '-':
return r.makeMinusNumber()
default:
if unicode.IsDigit(r.ru) {
return r.makeNumber()
}
return r.makeSymbol()
}
}
}
// makeNumber reads the Number.
func (r *Reader) makeNumber() (T, error) {
var str strings.Builder
if r.ru == '-' {
str.WriteRune('-')
r.getRune()
}
for ; r.indexOfLine <= r.lineLength; r.getRune() {
if r.ru == '(' || r.ru == ')' {
break
}
if unicode.IsSpace(r.ru) {
break
}
if !unicode.IsDigit(r.ru) {
r.indexOfLine--
return r.makeSymbolInternal(str.String()), nil
}
str.WriteRune(r.ru)
}
value, err := strconv.Atoi(str.String())
if err != nil {
return nil, fmt.Errorf("cannot convert int: %v", err)
}
return NewInteger(value), nil
}
// makeMinusNumber reads a negative number.
func (r *Reader) makeMinusNumber() (T, error) {
nru := r.line[r.indexOfLine]
if !unicode.IsDigit(nru) {
var str strings.Builder
str.WriteRune(r.ru)
return r.makeSymbolInternal(str.String()), nil
}
return r.makeNumber()
}
// makeSymbol reads a symbol.
func (r *Reader) makeSymbol() (T, error) {
r.ru = unicode.ToUpper(r.ru)
var str strings.Builder
str.WriteRune(r.ru)
return r.makeSymbolInternal(str.String()), nil
}
// makeSymbolInternal reads a symbol in the middle of a string.
func (r *Reader) makeSymbolInternal(str string) T {
for r.indexOfLine < r.lineLength {
r.getRune()
if r.ru == '(' || r.ru == ')' {
break
}
if unicode.IsSpace(r.ru) {
break
}
r.ru = unicode.ToUpper(r.ru)
str += string(r.ru)
}
return NewSymbol(str)
}
// makeList reads the list.
func (r *Reader) makeList() (T, error) {
r.getRune()
r.skipSpace()
if r.ru == ')' {
r.getRune()
return nil, nil
}
top := NewCons(nil, nil)
list := top
for {
sexp, err := r.getSexp()
if err != nil {
return nil, err
}
list.Car = sexp
r.skipSpace()
if r.indexOfLine > r.lineLength {
return nil, nil
}
if r.ru == ')' {
break
}
if r.ru == '.' {
r.getRune()
sexp, err := r.getSexp()
if err != nil {
return nil, err
}
list.Cdr = sexp
r.skipSpace()
r.getRune()
return top, nil
}
list.Cdr = NewCons(nil, nil)
l, ok := list.Cdr.(*Cons)
if !ok {
return nil, fmt.Errorf("cannot convert Cons: %v", list.Cdr)
}
list = l
}
r.getRune()
return top, nil
}
// makeQuote reads the quote.
func (r *Reader) makeQuote() (T, error) {
top := NewCons(nil, nil)
list := top
list.Car = NewSymbol("QUOTE")
list.Cdr = NewCons(nil, nil)
l, ok := list.Cdr.(*Cons)
if !ok {
return nil, fmt.Errorf("cannot convert Cons: %v", list.Cdr)
}
list = l
r.getRune()
list.Car, _ = r.getSexp()
return top, nil
}
// SkipSpace skips whitespace.
func (r *Reader) skipSpace() {
for unicode.IsSpace(r.ru) {
r.getRune()
}
}