stmt/class: support `self::` calling our renamed static methods

This commit is contained in:
mappu 2020-04-05 19:26:36 +12:00
parent 564be76bef
commit c6f70b1e66
2 changed files with 27 additions and 5 deletions

View File

@ -43,7 +43,8 @@ The goal is to produce idiomatic, maintainable Go code as part of a one-off conv
- [X] super
- [X] Need to track current conversion state through into function generator, to select the concrete parent
- [X] parent::
- [ ] static, self
- [X] self::
- [ ] static::
- [ ] Traits / `use`
- [ ] Abstract methods
[ ] Type inference

29
node.go
View File

@ -538,12 +538,21 @@ func (this *conversionState) convert(n_ node.Node) (string, error) {
return "", parseErr{n, err}
}
callTarget := className + "." + funcName
if className == "self" {
if this.currentClassName == "" {
return "", parseErr{n, fmt.Errorf("Made a self::Static method call while not in class context")}
}
// We're making a static call, and we renamed those to be top-level functions
callTarget = this.currentClassName + funcName
}
callParams, err := this.convertFuncCallArgsCommon(n.ArgumentList)
if err != nil {
return "", parseErr{n, err}
}
return className + "." + funcName + callParams, nil // expr only, no semicolon/newline
return callTarget + callParams, nil // expr only, no semicolon/newline
case *expr.New:
// new foo(xx)
@ -870,11 +879,23 @@ func (this *conversionState) resolveName(n node.Node) (string, error) {
// Handle class lookups
if strings.ToLower(paramType) == "parent" {
if this.currentClassParentName != "" {
return `this.` + this.currentClassParentName, nil
} else {
if this.currentClassParentName == "" {
return "", parseErr{n, fmt.Errorf("Lookup of 'parent' while not in an inherited child class context")}
}
return `this.` + this.currentClassParentName, nil
} else if strings.ToLower(paramType) == "self" {
// Let it through as-is
// return "", parseErr{n, fmt.Errorf("Lookup of 'self::' should have been resolved already")}
/*
if this.currentClassName == "" {
return "", parseErr{n, fmt.Errorf("Lookup of 'self' while not in class context")}
}
return `this`, nil
*/
} else if strings.ToLower(paramType) == "static" {
return "", parseErr{n, fmt.Errorf("'static::' is not yet supported")}
}
return paramType, nil