-
Notifications
You must be signed in to change notification settings - Fork 1
/
pystr.go
41 lines (37 loc) · 869 Bytes
/
pystr.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
package vm
type PyStr struct {
val string
Dict map[string]func(this *PyStr, args []PyObject) PyObject
}
func NewPyStr(s string) *PyStr {
dict := make(map[string]func(this *PyStr, args []PyObject) PyObject)
dict["__type__"] = func(this *PyStr, args []PyObject) PyObject {
return this.getType()
}
dict["__len__"] = func(this *PyStr, args []PyObject) PyObject {
return NewPyInt(len(this.val))
}
dict["__str__"] = func(this *PyStr, args []PyObject) PyObject {
return this
}
dict["__add__"] = func(this *PyStr, args []PyObject) PyObject {
return NewPyStr(this.val + args[0].(*PyStr).val)
}
// TODO
// __float__
// __int__
// __bool__
// __eq__
// __getitem__
// __iter__
return &PyStr{
val: s,
Dict: dict,
}
}
func (this *PyStr) getType() *PyType {
return PyTypes[PyStrType]
}
func (this *PyStr) toString() string {
return this.val
}