Skip to content

Commit 56baf07

Browse files
committed
Make builtin print sort of work
1 parent 427419c commit 56baf07

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package main
55
import (
66
"flag"
77
"fmt"
8+
_ "github.com/ncw/gpython/builtin"
89
"github.com/ncw/gpython/marshal"
910
"github.com/ncw/gpython/py"
1011
"github.com/ncw/gpython/vm"

py/method.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ const (
7575
// A python Method object
7676
type Method struct {
7777
// Name of this function
78-
name string
78+
Name string
7979
// Doc string
80-
doc string
80+
Doc string
8181
// Flags - see METH_* flags
82-
flags int
82+
Flags int
8383
// C function implementation (two definitions, only one is used)
8484
method PyCFunction
8585
methodWithKeywords PyCFunctionWithKeywords
@@ -98,9 +98,9 @@ func NewMethod(name string, method PyCFunction, flags int, doc string) *Method {
9898
panic("Can't set METH_KEYWORDS")
9999
}
100100
return &Method{
101-
name: name,
102-
doc: doc,
103-
flags: flags,
101+
Name: name,
102+
Doc: doc,
103+
Flags: flags,
104104
method: method,
105105
}
106106
}
@@ -111,9 +111,26 @@ func NewMethodWithKeywords(name string, method PyCFunctionWithKeywords, flags in
111111
panic("Must set METH_KEYWORDS")
112112
}
113113
return &Method{
114-
name: name,
115-
doc: doc,
116-
flags: flags,
114+
Name: name,
115+
Doc: doc,
116+
Flags: flags,
117117
methodWithKeywords: method,
118118
}
119119
}
120+
121+
// Call the method with the given arguments
122+
func (m *Method) Call(self Object, args Tuple) Object {
123+
if m.method != nil {
124+
return m.method(self, args)
125+
}
126+
// FIXME or call with empty dict?
127+
return m.methodWithKeywords(self, args, NewDict())
128+
}
129+
130+
// Call the method with the given arguments
131+
func (m *Method) CallWithKeywords(self Object, args Tuple, kwargs Dict) Object {
132+
if m.method != nil {
133+
panic("Can't call method with kwargs")
134+
}
135+
return m.methodWithKeywords(self, args, kwargs)
136+
}

py/module.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
package py
44

5+
import (
6+
"fmt"
7+
)
8+
9+
var (
10+
// Registry of installed modules
11+
modules = make(map[string]*Module)
12+
// Builtin module
13+
Builtins *Module
14+
)
15+
516
// A python Module object
617
type Module struct {
7-
name string
8-
doc string
9-
methods []*Method
18+
Name string
19+
Doc string
20+
Methods map[string]*Method
1021
// dict Dict
1122
}
1223

@@ -19,9 +30,21 @@ func (o *Module) Type() *Type {
1930

2031
// Define a new module
2132
func NewModule(name, doc string, methods []*Method) *Module {
22-
return &Module{
23-
name: name,
24-
doc: doc,
25-
methods: methods,
33+
m := &Module{
34+
Name: name,
35+
Doc: doc,
36+
Methods: make(map[string]*Method),
37+
}
38+
// Insert the methods into the module dictionary
39+
for _, method := range methods {
40+
m.Methods[method.Name] = method
41+
}
42+
// Register the module
43+
modules[name] = m
44+
// Make a note of the builtin module
45+
if name == "builtins" {
46+
Builtins = m
2647
}
48+
fmt.Printf("Registering module %q\n", name)
49+
return m
2750
}

py/py.go

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (o Dict) Type() *Type {
8181
return DictType
8282
}
8383

84+
// Make a new dictionary
85+
func NewDict() Dict {
86+
return make(Dict)
87+
}
88+
8489
var SetType = NewType("set")
8590

8691
type Set map[Object]struct{}

vm/eval.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -704,10 +704,25 @@ func do_CALL_FUNCTION(vm *Vm, argc int32) {
704704
p, q := len(vm.stack)-2*nkwargs, len(vm.stack)
705705
kwargs := vm.stack[p:q]
706706
p, q = p-nargs, p
707-
args := vm.stack[p:q]
707+
args := py.Tuple(vm.stack[p:q])
708708
p, q = p-1, p
709709
fn := vm.stack[p]
710710
fmt.Printf("Call %v with args = %v, kwargs = %v\n", fn, args, kwargs)
711+
// FIXME look the function up
712+
fn_name := string(fn.(py.String))
713+
if method, ok := py.Builtins.Methods[fn_name]; ok {
714+
// FIXME need module as self
715+
self := py.None
716+
if len(kwargs) > 0 {
717+
// FIXME need to convert kwargs to dictionary
718+
kwargsd := py.NewDict()
719+
vm.stack[p] = method.CallWithKeywords(self, args, kwargsd)
720+
} else {
721+
vm.stack[p] = method.Call(self, args)
722+
}
723+
} else {
724+
panic("Couldn't find method")
725+
}
711726
// Drop the args off the stack and put return value in
712727
vm.stack = vm.stack[:q]
713728
vm.stack[p] = py.None

0 commit comments

Comments
 (0)