forked from flosch/GoPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyint.go
75 lines (67 loc) · 1.39 KB
/
pyint.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
package vm
import (
"fmt"
"strconv"
)
type PyInt struct {
PyObjectData
value int64
}
func (pi *PyInt) getValue() interface{} {
return pi.value
}
func (pi *PyInt) isTrue() bool {
return pi.value == 1
}
func (pi *PyInt) asString() *string {
s := strconv.Itoa(int(pi.value))
return &s
}
// Returns (PyException, resulting object)
func (pi *PyInt) operation(op int, obj2 PyObject, inplace bool) (PyObject, PyObject) {
switch op {
case OpMultiply:
value, isInt := obj2.(*PyInt)
if !isInt {
fmt.Println("TypeError! Multiply on int can only be done with integers.")
return PyTypeError, nil
}
if inplace {
pi.value *= value.value
} else {
return nil, NewPyInt(pi.value * value.value)
}
case OpAdd:
value, isInt := obj2.(*PyInt)
if !isInt {
fmt.Println("TypeError! Multiply on int can only be done with integers.")
return PyTypeError, nil
}
if inplace {
pi.value += value.value
} else {
return nil, NewPyInt(pi.value + value.value)
}
case OpSubtract:
value, isInt := obj2.(*PyInt)
if !isInt {
fmt.Println("TypeError! Multiply on int can only be done with integers.")
return PyTypeError, nil
}
if inplace {
pi.value -= value.value
} else {
return nil, NewPyInt(pi.value - value.value)
}
default:
return PyTypeError, nil
}
return nil, pi
}
func NewPyInt(value int64) PyObject {
pi := &PyInt{
value: value,
}
pi.pyObjInit()
return pi
}