Skip to content

Commit 7a05a3c

Browse files
committed
More work on basic types
1 parent 1c83e6c commit 7a05a3c

File tree

5 files changed

+126
-6
lines changed

5 files changed

+126
-6
lines changed

marshal/marshal.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"github.com/ncw/gpython/py"
99
"io"
10+
"math/big"
1011
"strconv"
1112
)
1213

@@ -160,9 +161,16 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
160161
// FIXME should be ValueError
161162
return nil, errors.New("bad marshal data (digit out of range in long)")
162163
}
163-
// FIXME actually convert into something...
164+
// Convert into a big.Int
165+
r := new(big.Int)
166+
t := new(big.Int)
167+
for _, digit := range digits {
168+
r.Lsh(r, 15)
169+
t.SetInt64(int64(digit))
170+
r.Add(r, t)
171+
}
164172
// FIXME try to fit into int64 if possible
165-
return digits, nil
173+
return (*py.BigInt)(r), nil
166174
case TYPE_STRING, TYPE_INTERNED, TYPE_UNICODE:
167175
var size int32
168176
err = binary.Read(r, binary.LittleEndian, &size)

py/code.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ type Code struct {
3030
weakreflist Object // to support weakrefs to code objects
3131
}
3232

33+
var CodeType = NewType("code")
34+
35+
// Type of this object
36+
func (o *Code) Type() *Type {
37+
return CodeType
38+
}
39+
40+
// Make sure it satisfies the interface
41+
var _ Object = (*Code)(nil)
42+
3343
const (
3444
// Masks for flags above
3545
CO_OPTIMIZED = 0x0001
@@ -88,7 +98,7 @@ func NewCode(argcount int32, kwonlyargcount int32,
8898
code Object, consts_ Object, names_ Object,
8999
varnames_ Object, freevars_ Object, cellvars_ Object,
90100
filename_ Object, name_ Object, firstlineno int32,
91-
lnotab_ Object) (co *Code) {
101+
lnotab_ Object) *Code {
92102

93103
var cell2arg *byte
94104

py/py.go

+80-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// Python global definitions
22
package py
33

4+
import (
5+
"math/big"
6+
)
7+
48
// A python object
5-
type Object interface{}
9+
type Object interface {
10+
Type() *Type
11+
}
612

713
// Some well known objects
814
var (
@@ -12,13 +18,86 @@ var (
1218
// Some python types
1319
// FIXME factor into own files probably
1420

21+
var TupleType = NewType("tuple")
22+
1523
type Tuple []Object
24+
25+
// Type of this Tuple object
26+
func (o Tuple) Type() *Type {
27+
return TupleType
28+
}
29+
30+
var ListType = NewType("list")
31+
1632
type List []Object
33+
34+
// Type of this List object
35+
func (o List) Type() *Type {
36+
return ListType
37+
}
38+
39+
var BytesType = NewType("bytes")
40+
1741
type Bytes []byte
1842

43+
// Type of this Bytes object
44+
func (o Bytes) Type() *Type {
45+
return BytesType
46+
}
47+
48+
var Int64Type = NewType("int64")
49+
1950
type Int64 int64
51+
52+
// Type of this Int64 object
53+
func (o Int64) Type() *Type {
54+
return Int64Type
55+
}
56+
57+
var Float64Type = NewType("float64")
58+
2059
type Float64 float64
60+
61+
// Type of this Float64 object
62+
func (o Float64) Type() *Type {
63+
return Float64Type
64+
}
65+
66+
var Complex64Type = NewType("complex64")
67+
2168
type Complex64 complex64
2269

70+
// Type of this Complex64 object
71+
func (o Complex64) Type() *Type {
72+
return Complex64Type
73+
}
74+
75+
var DictType = NewType("dict")
76+
2377
type Dict map[Object]Object
78+
79+
// Type of this Dict object
80+
func (o Dict) Type() *Type {
81+
return DictType
82+
}
83+
84+
var SetType = NewType("set")
85+
2486
type Set map[Object]struct{}
87+
88+
// Type of this Set object
89+
func (o Set) Type() *Type {
90+
return SetType
91+
}
92+
93+
type BigInt big.Int
94+
95+
var BigIntType = NewType("bigint")
96+
97+
// Type of this BigInt object
98+
func (o *BigInt) Type() *Type {
99+
return BigIntType
100+
}
101+
102+
// Make sure it satisfies the interface
103+
var _ Object = (*BigInt)(nil)

py/string.go

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import (
88

99
type String string
1010

11+
var StringType = NewType("string")
12+
13+
// Type of this object
14+
func (s String) Type() *Type {
15+
return StringType
16+
}
17+
1118
// Intern s possibly returning a reference to an already interned string
1219
func (s String) Intern() String {
1320
fmt.Printf("FIXME interning %q\n", s)

py/type.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
package py
44

55
type Type struct {
6+
name string // For printing, in format "<module>.<name>"
67
/*
7-
PyObject_VAR_HEAD
8-
const char *tp_name; // For printing, in format "<module>.<name>"
98
Py_ssize_t tp_basicsize, tp_itemsize; // For allocation
109
1110
// Methods to implement standard operations
@@ -84,3 +83,20 @@ type Type struct {
8483
destructor tp_finalize;
8584
*/
8685
}
86+
87+
var TypeType = NewType("type")
88+
89+
// Type of this object
90+
func (o *Type) Type() *Type {
91+
return TypeType
92+
}
93+
94+
// Make a new type from a name
95+
func NewType(name string) *Type {
96+
return &Type{
97+
name: name,
98+
}
99+
}
100+
101+
// Make sure it satisfies the interface
102+
var _ Object = (*Type)(nil)

0 commit comments

Comments
 (0)