From c6f70b1e66d9893ca1133009270c7f9aa4b1a68c Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 5 Apr 2020 19:26:36 +1200 Subject: [PATCH] stmt/class: support `self::` calling our renamed static methods --- README.md | 3 ++- node.go | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 341e6a7..f687a63 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/node.go b/node.go index 0d44f33..65e4fb5 100644 --- a/node.go +++ b/node.go @@ -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