-
Notifications
You must be signed in to change notification settings - Fork 1
/
pylist.go
51 lines (47 loc) · 1.16 KB
/
pylist.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
package vm
type PyList struct {
data []PyObject
Dict map[string]func(this *PyList, args []PyObject) PyObject
}
func NewPyList(items []PyObject) *PyList {
dict := make(map[string]func(this *PyList, args []PyObject) PyObject)
dict["__len__"] = func(this *PyList, args []PyObject) PyObject {
return NewPyInt(len(this.data))
}
dict["__getitem__"] = func(this *PyList, args []PyObject) PyObject {
return this.data[args[0].(*PyInt).getVal()]
}
dict["__setitem__"] = func(this *PyList, args []PyObject) PyObject {
this.data[args[0].(*PyInt).getVal()] = args[1]
return NewPyNone()
}
// TODO
// __iter__
dict["__str__"] = func(this *PyList, args []PyObject) PyObject {
return NewPyStr(this.toString())
}
dict["__type__"] = func(this *PyList, args []PyObject) PyObject {
return this.getType()
}
return &PyList{
data: items,
Dict: dict,
}
}
func (this *PyList) getType() *PyType {
return PyTypes[PyListType]
}
func (this *PyList) toString() string {
ret := "["
ret += this.data[0].toString()
if len(this.data) == 1 {
ret += "]"
return ret
}
for i := 1; i < len(this.data); i++ {
ret += ", "
ret += this.data[i].toString()
}
ret += ")"
return ret
}