-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
56 lines (47 loc) · 1.64 KB
/
parse.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
package gmnlisp
import (
"io"
"github.com/hymkor/gmnlisp/pkg/parser"
)
var (
ampRest = NewSymbol("&rest")
commaSymbol = NewSymbol(",")
nulSymbol = NewSymbol("")
quoteSymbol = NewSymbol("quote")
backQuoteSymbol = NewSymbol("backquote")
slashSymbol = NewSymbol("/")
colonRest = NewKeyword(":rest")
)
type stdFactory struct{}
func (stdFactory) Cons(car, cdr Node) Node { return &Cons{Car: car, Cdr: cdr} }
func (stdFactory) Int(n int64) Node { return Integer(n) }
func (stdFactory) Float(f float64) Node { return Float(f) }
func (stdFactory) String(s string) Node { return String(s) }
func (stdFactory) Array(list []Node, dim []int) Node { return &Array{list: list, dim: dim} }
func (stdFactory) Keyword(s string) Node { return NewKeyword(s) }
func (stdFactory) Rune(r rune) Node { return Rune(r) }
func (stdFactory) Symbol(s string) Node { return NewSymbol(s) }
func (stdFactory) Null() Node { return Null }
func (stdFactory) True() Node { return True }
func ReadNode(rs io.RuneScanner) (Node, error) {
return parser.Read[Node](stdFactory{}, rs)
}
func tryParseAsNumber(token string) (Node, bool, error) {
return parser.TryParseAsNumber[Node](stdFactory{}, token)
}
func newQuote(value Node) Node {
return parser.NewQuote[Node](stdFactory{}, value)
}
func ReadAll(rs io.RuneScanner) ([]Node, error) {
result := []Node{}
for {
token, err := ReadNode(rs)
if err != nil {
if err == io.EOF {
return result, nil
}
return nil, err
}
result = append(result, token)
}
}