-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
46 lines (37 loc) · 887 Bytes
/
sequence.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
package main
// sequence is an object which contains multiple values,
// which have explicit order, can be accessed by index.
type sequence interface {
size() int
index(idx int) *obj
slice(start, end int) *obj
}
type strSequence struct {
runes []rune
}
func (s *strSequence) size() int {
return len(s.runes)
}
func (s *strSequence) index(idx int) *obj {
return &obj{typ: tStr, bytes: []byte(string(s.runes[idx]))}
}
func (s *strSequence) slice(start, end int) *obj {
rs := s.runes[start:end]
var ret string
for _, r := range rs {
ret += string(r)
}
return &obj{typ: tStr, bytes: []byte(ret)}
}
type listSequence struct {
vals []*obj
}
func (s *listSequence) size() int {
return len(s.vals)
}
func (s *listSequence) index(idx int) *obj {
return s.vals[idx]
}
func (s *listSequence) slice(start, end int) *obj {
return &obj{typ: tList, list: s.vals[start:end]}
}