node: support define()

This commit is contained in:
mappu 2020-04-08 20:24:44 +12:00
parent 142e26bbfe
commit 750bfd9bb2
1 changed files with 22 additions and 0 deletions

22
node.go
View File

@ -590,6 +590,28 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error)
ret += "this." + this.currentClassParentName + " = *super // copy by value\n"
return ret, nil
}
if fnname, err := this.resolveName(fncall.Function); err == nil && fnname == "define" {
// define() gets converted to const
if len(fncall.ArgumentList.Arguments) != 2 {
return "", parseErr{fncall, fmt.Errorf("expected define() to have 2 arguments, found %d", len(fncall.ArgumentList.Arguments))}
}
defineName := fncall.ArgumentList.Arguments[0].(*node.Argument).Expr // that much is always possible
defineNameStr, ok := defineName.(*scalar.String)
if !ok {
return "", parseErr{fncall, fmt.Errorf("can't handle a complex expression in define() name")}
}
rawDefineName, err := phpUnquote(defineNameStr.Value)
if err != nil {
return "", parseErr{fncall, err}
}
// Convert to a final Const statement
return this.convert(stmt.NewConstList([]node.Node{stmt.NewConstant(node.NewIdentifier(rawDefineName), fncall.ArgumentList.Arguments[1].(*node.Argument).Expr, "")}))
}
}
// Assignment expressions can take on better error-handling behaviour