php2go/README.md

107 lines
4.3 KiB
Markdown
Raw Normal View History

2020-04-05 04:35:44 +00:00
# php2go
Convert PHP source code to Go by AST walking.
The goal is to produce idiomatic, maintainable Go code as part of a one-off conversion. This is not generally possible for highly dynamic PHP code, that may require manual fixups.
## Progress
[X] Convert some small programs
2020-04-05 06:24:31 +00:00
[X] Error handling
- [X] All functions return `(type, error)`
2020-04-05 04:35:44 +00:00
- [X] Convert `throw` to err return
- [ ] Elide error return for functions that cannot throw
- ?? Could be a standalone Go refactoring tool
2020-04-08 08:26:08 +00:00
- [X] Non-leaf function calls need to check + bubble errors
2020-04-05 06:24:31 +00:00
- [ ] Behaviour of function calls in rvalue cases where err cannot be checked
2020-04-05 04:35:44 +00:00
[ ] Comprehensive coverage of all AST node types
2020-04-05 07:26:49 +00:00
- [ ] Node
- [ ] Stmt
2020-04-05 04:35:44 +00:00
- [ ] Expr
2020-04-05 06:24:31 +00:00
- [X] Binary
2020-04-05 04:35:44 +00:00
- [ ] Scalar
2020-04-09 07:40:36 +00:00
- [ ] Heredocs/nowdocs
2020-04-05 06:24:31 +00:00
[X] Array handling
- [X] Infer whether to use slice/map for PHP array
2020-04-05 04:35:44 +00:00
- [ ] Infer whether map use should be order-preserving
[ ] Multi-file programs
- [ ] Include/Require
2020-04-08 08:26:08 +00:00
[X] Namespaces
- [X] Basic support for `namespace` -> package name transformation
- [ ] Namespace resolution in calls
- [ ] `use`
- [ ] Support scoped namespace (`namespace { ... }`) and multiple namespaces in file
2020-04-05 04:35:44 +00:00
[ ] Generators
2020-04-05 06:24:31 +00:00
[ ] Numbered break/continue
[ ] Goto
2020-04-09 07:40:36 +00:00
[X] `null` -> `nil`
2020-04-08 08:26:08 +00:00
[X] Consts and `define()`
2020-04-09 07:40:36 +00:00
- [X] Magic constants (`__LINE__`,`__FILE__` etc)
2020-04-05 07:26:49 +00:00
[ ] `isset`/`unset`
[ ] `instanceof`
2020-04-08 08:26:08 +00:00
[X] `die`
2020-04-07 11:26:58 +00:00
[X] `try`/`catch`/`finally`
2020-04-08 08:26:08 +00:00
- [X] Implement using err checks, not panic/recover
2020-04-07 11:26:58 +00:00
- Any statements within the try block should capture the finally/catch to run within any interior err-check blocks
2020-04-08 08:26:08 +00:00
- [ ] `finally`
- Runs before any return statement inside the try block or catch block; also runs after the try block is complete
- Maybe `finally` would be more concisely implemented as defer inside an IIFE, but that is not idiomatic, maintainable Go code
2020-04-05 07:26:49 +00:00
[ ] Abandon upon sight of `eval` / `extract` / ...
[ ] Assignment expressions
- Go doesn't support assignment in rvalues, only as a statement
- When walking below stmt level, need to first fully walk and check for any function calls + assignments that may need hoisting (and we can probably only do that correctly if there is no short-circuiting)
- [X] Add subtree walk + catch error for this case
- [ ] Add hoisting pass
2020-04-05 06:24:31 +00:00
[ ] Closures
- [ ] Handle value/reference captures
2020-04-05 04:35:44 +00:00
[X] Convert top-level calls to `init()`/`main()`
- *Currently always `init`*
[ ] Class/object transformations
- [X] Convert `new X` constructor calls to `NewX`
2020-04-05 07:26:49 +00:00
- [X] Visibility
2020-04-07 09:53:15 +00:00
- [X] Apply PHP visibility modifiers as Go case change
2020-04-05 07:26:49 +00:00
- [ ] Call mangled function name at call sites
- *PHP variable names are case-sensitive, function names are case-insensitive*
2020-04-05 06:24:31 +00:00
- [X] Static methods
- [X] Inheritance *partial*
2020-04-07 09:53:00 +00:00
- [X] Interfaces
2020-04-08 08:26:08 +00:00
- [X] Class constants
2020-04-05 07:11:05 +00:00
- [X] super
- [X] Need to track current conversion state through into function generator, to select the concrete parent
- [X] parent::
- [X] self::
- [ ] static::
2020-04-05 04:35:44 +00:00
- [ ] Traits / `use`
- [ ] Abstract methods
[ ] Type inference
2020-04-09 07:40:36 +00:00
- [ ] Type declarations for literals (string, slice/map with constant initializer, etc)
2020-04-05 06:24:31 +00:00
- [ ] Convert known PHP typenames to Go equivalents
2020-04-05 04:35:44 +00:00
- [ ] Parse extra types from phpdoc blocks
[ ] Infer whether to declare variable (`var` / `:=`) or reuse (`=`)
- [ ] Track current visibility scope
2020-04-05 06:24:31 +00:00
[ ] Library/environment transformations
- [ ] Standard library transformations
2020-04-08 08:26:08 +00:00
- [X] Track golang package imports
2020-04-05 06:24:31 +00:00
- [ ] Handle conflicts between golang stdlib packages / local variable names
2020-04-07 09:53:15 +00:00
- [ ] uasort -> sort.Slice
2020-04-07 11:26:58 +00:00
- [ ] Output buffering (`ob_start` / `ob_get_clean`)
- Can push/pop os.Stdout onto a private stack, then we can keep using `fmt.Print`
2020-04-05 06:24:31 +00:00
- [ ] PHP Superglobal transformations
- [ ] `$_SERVER['argv']` to os.Args
- [ ] `$_GET['name']` to r.FormValue('name')
- [ ] Common 3rd party package transformations to popular equivalents (optional)
- [ ] Guzzle
- [ ] AWS SDK
- [ ] PHPUnit -> Go Test
- [ ] Replace Composer/PSR-4 autoloading with Go imports
2020-04-05 04:35:44 +00:00
[X] Variadic function parameters
2020-04-07 06:27:20 +00:00
[X] Preserve comments
2020-04-05 06:24:31 +00:00
[ ] Preserve rough line spacing
2020-04-05 04:35:44 +00:00
[ ] Convert wordpress / mediawiki / symfony
2020-04-05 06:24:31 +00:00
- [ ] Option to convert with preset=cli, preset=web, webroot path
- [ ] Option to generate built-in web server
[ ] Generate source maps
2020-04-08 08:26:08 +00:00
- The parser does have full positional information - exporting at the stmt level would be reasonable
- But the gofmt pass may lose this information
2020-04-05 06:24:31 +00:00
[ ] Command-line tool option to `go run`