Skip to content

Commit d30fc55

Browse files
committed
Factor types into own files and rename some
1 parent 810fccf commit d30fc55

14 files changed

+533
-75
lines changed

marshal/marshal.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
8585
if err != nil {
8686
return
8787
}
88-
return py.Int64(n), nil
88+
return py.Int(n), nil
8989
case TYPE_FLOAT:
9090
// Floating point number as a string
9191
var length uint8
@@ -103,14 +103,14 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
103103
if err != nil {
104104
return
105105
}
106-
return py.Float64(f), nil
106+
return py.Float(f), nil
107107
case TYPE_BINARY_FLOAT:
108108
var f float64
109109
err = binary.Read(r, binary.LittleEndian, &f)
110110
if err != nil {
111111
return
112112
}
113-
return py.Float64(f), nil
113+
return py.Float(f), nil
114114
case TYPE_COMPLEX:
115115
// Complex number as a string
116116
// FIXME this is using Go conversion not Python conversion which may differ
@@ -129,14 +129,14 @@ func ReadObject(r io.Reader) (obj py.Object, err error) {
129129
if err != nil {
130130
return
131131
}
132-
return py.Complex64(c), nil
132+
return py.Complex(c), nil
133133
case TYPE_BINARY_COMPLEX:
134134
var c complex64
135135
err = binary.Read(r, binary.LittleEndian, &c)
136136
if err != nil {
137137
return
138138
}
139-
return py.Complex64(c), nil
139+
return py.Complex(c), nil
140140
case TYPE_LONG:
141141
var size int32
142142
err = binary.Read(r, binary.LittleEndian, &size)

py/bool.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Bool objects
2+
3+
package py
4+
5+
type Bool bool
6+
7+
var (
8+
BoolType = NewType("bool")
9+
// Some well known bools
10+
False = Bool(false)
11+
True = Bool(true)
12+
)
13+
14+
// Type of this object
15+
func (s Bool) Type() *Type {
16+
return BoolType
17+
}

py/bytes.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Bytes objects
2+
3+
package py
4+
5+
var BytesType = NewType("bytes")
6+
7+
type Bytes []byte
8+
9+
// Type of this Bytes object
10+
func (o Bytes) Type() *Type {
11+
return BytesType
12+
}

py/complex.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Complex objects
2+
3+
package py
4+
5+
var ComplexType = NewType("complex64")
6+
7+
type Complex complex64
8+
9+
// Type of this Complex object
10+
func (o Complex) Type() *Type {
11+
return ComplexType
12+
}

py/dict.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Dict and StringDict type
2+
//
3+
// The idea is that most dicts just have strings for keys so we use
4+
// the simpler StringDict and promote it into a Dict when necessary
5+
6+
package py
7+
8+
var StringDictType = NewType("dict")
9+
10+
// String to object dictionary
11+
//
12+
// Used for variables etc where the keys can only be strings
13+
type StringDict map[string]Object
14+
15+
// Type of this StringDict object
16+
func (o StringDict) Type() *Type {
17+
return StringDictType
18+
}
19+
20+
// Make a new dictionary
21+
func NewStringDict() StringDict {
22+
return make(StringDict)
23+
}

py/exception.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Exception objects
2+
3+
package py
4+
5+
// A python Exception object
6+
type Exception struct {
7+
Name string // name of the exception FIXME should be part of class machinery
8+
Args Object
9+
Traceback Object
10+
Context Object
11+
Cause Object
12+
SuppressContext bool
13+
Other StringDict // anything else that we want to stuff in
14+
}
15+
16+
var (
17+
ExceptionType = NewType("exception")
18+
19+
// Some well known exceptions - these should be types?
20+
// FIXME exceptions should be created in builtins probably
21+
// their names should certainly go in there!
22+
NotImplemented = NewException("NotImplemented")
23+
StopIteration = NewException("StopIteration")
24+
)
25+
26+
// Type of this object
27+
func (o *Exception) Type() *Type {
28+
return ExceptionType
29+
}
30+
31+
// Define a new exception
32+
//
33+
// FIXME need inheritance machinery to make this work properly
34+
func NewException(name string) *Exception {
35+
m := &Exception{
36+
Name: name,
37+
}
38+
return m
39+
}

py/float.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Float objects
2+
3+
package py
4+
5+
var FloatType = NewType("float")
6+
7+
type Float float64
8+
9+
// Type of this Float64 object
10+
func (o Float) Type() *Type {
11+
return FloatType
12+
}

py/frame.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Frame objects
2+
3+
package py
4+
5+
import (
6+
"fmt"
7+
)
8+
9+
// Store information about try blocks
10+
type TryBlock struct {
11+
Type int // what kind of block this is
12+
Handler int // where to jump to find handler
13+
Level int // value stack level to pop to
14+
}
15+
16+
// A python Frame object
17+
type Frame struct {
18+
// Back *Frame // previous frame, or nil
19+
Code *Code // code segment
20+
Builtins StringDict // builtin symbol table
21+
Globals StringDict // global symbol table
22+
Locals StringDict // local symbol table
23+
Valuestack *Object // points after the last local
24+
// Next free slot in f_valuestack. Frame creation sets to f_valuestack.
25+
// Frame evaluation usually NULLs it, but a frame that yields sets it
26+
// to the current stack top.
27+
Stacktop *Object
28+
Trace Object // Trace function
29+
30+
// In a generator, we need to be able to swap between the exception
31+
// state inside the generator and the exception state of the calling
32+
// frame (which shouldn't be impacted when the generator "yields"
33+
// from an except handler).
34+
// These three fields exist exactly for that, and are unused for
35+
// non-generator frames. See the save_exc_state and swap_exc_state
36+
// functions in ceval.c for details of their use.
37+
Exc_type Object
38+
Exc_value *Object
39+
Exc_traceback *Object
40+
// Borrowed reference to a generator, or NULL
41+
Gen Object
42+
43+
// FIXME Tstate *PyThreadState
44+
Lasti int // Last instruction if called
45+
// Call PyFrame_GetLineNumber() instead of reading this field
46+
// directly. As of 2.3 f_lineno is only valid when tracing is
47+
// active (i.e. when f_trace is set). At other times we use
48+
// PyCode_Addr2Line to calculate the line from the current
49+
// bytecode index.
50+
Lineno int // Current line number
51+
Iblock int // index in f_blockstack
52+
Executing byte // whether the frame is still executing
53+
Blockstack []TryBlock // for try and loop blocks
54+
Localsplus []Object // locals+stack, dynamically sized
55+
}
56+
57+
var FrameType = NewType("frame")
58+
59+
// Type of this object
60+
func (o *Frame) Type() *Type {
61+
return FrameType
62+
}
63+
64+
// Python names are looked up in three scopes
65+
//
66+
// First the local scope
67+
// Next the module global scope
68+
// And finally the builtins
69+
func (f *Frame) Lookup(name string) (obj Object) {
70+
var ok bool
71+
72+
// Lookup in locals
73+
fmt.Printf("locals = %v\n", f.Locals)
74+
if obj, ok = f.Locals[name]; ok {
75+
return
76+
}
77+
78+
// Lookup in globals
79+
fmt.Printf("globals = %v\n", f.Globals)
80+
if obj, ok = f.Globals[name]; ok {
81+
return
82+
}
83+
84+
// Lookup in builtins
85+
fmt.Printf("builtins = %v\n", Builtins.Globals)
86+
if obj, ok = f.Builtins[name]; ok {
87+
return
88+
}
89+
90+
// FIXME this should be a NameError
91+
panic(fmt.Sprintf("NameError: name '%s' is not defined", name))
92+
}

py/int.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Int and BigInt objects
2+
3+
package py
4+
5+
import (
6+
"math/big"
7+
)
8+
9+
var IntType = NewType("int")
10+
11+
type Int int64
12+
13+
// Type of this Int object
14+
func (o Int) Type() *Type {
15+
return IntType
16+
}
17+
18+
type BigInt big.Int
19+
20+
var BigIntType = NewType("bigint")
21+
22+
// Type of this BigInt object
23+
func (o *BigInt) Type() *Type {
24+
return BigIntType
25+
}
26+
27+
// Make sure it satisfies the interface
28+
var _ Object = (*BigInt)(nil)

py/list.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// List objects
2+
3+
package py
4+
5+
var ListType = NewType("list")
6+
7+
type List []Object
8+
9+
// Type of this List object
10+
func (o List) Type() *Type {
11+
return ListType
12+
}

0 commit comments

Comments
 (0)