Handle class methods.

This commit is contained in:
Greg 2019-04-11 17:00:36 -04:00
parent 3ecb3b1eeb
commit 0b0b73c6bd
2 changed files with 22 additions and 5 deletions

3
.gitignore vendored
View File

@ -2,3 +2,6 @@ clast
ast.txt ast.txt
*.ast *.ast
simple simple
complex
program
types

View File

@ -135,6 +135,7 @@ type Parameter struct {
type Method struct { type Method struct {
Name, Type, Type2, Class string Name, Type, Type2, Class string
ClassMethod bool
Parameters []Parameter Parameters []Parameter
} }
@ -150,7 +151,10 @@ func (w Wrapper) isObject(tp string) bool { // takes a goType
} }
func (w Wrapper) cparamlist(m Method) string { func (w Wrapper) cparamlist(m Method) string {
ret := []string{"void* obj"} ret := make([]string,0)
if !m.ClassMethod {
ret = append(ret,"void* obj")
}
for _,p := range m.Parameters { for _,p := range m.Parameters {
tp := typeOrType2(p.Type,p.Type2) tp := typeOrType2(p.Type,p.Type2)
gtp,_ := goType(tp,m.Class) gtp,_ := goType(tp,m.Class)
@ -205,7 +209,10 @@ func (w Wrapper) goparamlist(m Method) (string,[]string,bool) {
} }
func (w Wrapper) goparamnames(m Method) string { func (w Wrapper) goparamnames(m Method) string {
ret := []string{"o.ptr"} ret := make([]string,0)
if !m.ClassMethod {
ret = append(ret,"o.ptr")
}
for _,p := range m.Parameters { for _,p := range m.Parameters {
gname := p.Vname gname := p.Vname
if goreserved[gname] { if goreserved[gname] {
@ -281,6 +288,7 @@ func (w *Wrapper) add(name string, ns []ast.Node) {
Type: x.Type, Type: x.Type,
Type2: x.Type2, Type2: x.Type2,
Class: name, Class: name,
ClassMethod: x.ClassMethod,
} }
m.Parameters, avail = w.GetParms(x,name) m.Parameters, avail = w.GetParms(x,name)
if avail { if avail {
@ -443,7 +451,7 @@ func New%s() *%s {
w.cCode.WriteString(fmt.Sprintf(` w.cCode.WriteString(fmt.Sprintf(`
%s* %s*
New%s() { New%s() {
return [[%s alloc] init]; return [%s alloc];
} }
`, v.Name, v.Name, v.Name)) `, v.Name, v.Name, v.Name))
@ -499,11 +507,17 @@ func (o *%s) %s(%s) %s%s {
if !y.isVoid() { if !y.isVoid() {
cret = "return " cret = "return "
} }
var cobj string
if y.ClassMethod {
cobj = v.Name
} else {
cobj = "(id)obj"
}
w.cCode.WriteString(fmt.Sprintf(` w.cCode.WriteString(fmt.Sprintf(`
%s %s
%s_%s(%s) { %s_%s(%s) {
%s[(id)obj %s]; %s[%s %s];
}`, cmtype, v.Name, y.Name, w.cparamlist(y), cret, y.objcparamlist())) }`, cmtype, v.Name, y.Name, w.cparamlist(y), cret, cobj, y.objcparamlist()))
} }
} }
fmt.Println(`package main fmt.Println(`package main