php2go

command module
v0.0.0-...-6a25f69 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2020 License: MIT Imports: 1 Imported by: 0

README

php2go

php2go is a transpiler from a small subset of PHP to Go.

This tool is written in Go and uses z7zmey/php-parser.

Get started

Install
go get github.com/i582/php2go
CLI
php2go [flags]
flag type description
-i string input file
-o string output file

What is currently supported

Types:

  1. Integer
  2. Float
  3. String
  4. Bool
  5. Null

Union types composed of the types above are also supported.

For union types supported:

  1. Comparison with any type above;
  2. Use in conditions and boolean expressions;
  3. Printing.

is_t functions

  1. is_int
  2. is_float
  3. is_string
  4. is_bool
  5. is_null

Operators

Supported arithmetic operators (+,-,*,/ ,.,++,--) and

boolean (<,>,<=,>=, &&, ||)

Arrays

Arrays are supported, both regular and associative, but they must consist of elements of the same type and with the same type of keys.

It supports index/key access, assignment to an element and a construction like $arr[] = Elem;

Constructs

The following language constructs are available:

  1. if-else
  2. for
  3. while
  4. foreach

Output

The echo operator is supported for output.

Currently only code in function is supported.

TODO

  1. Add support for all operators;
  2. Add support for other types;
  3. Add multi-file support;
  4. And much more...

Example

<?php

function Foo() {
  // int
  $a = 100;
  // float
  $b = 1.5;
  // string
  $c = "Hello";
  // bool
  $d = true;
  // output
  echo $a;
  echo $b;
  echo $c;
  echo $d;
  // if-else
  if ($a == 100) {
    // variable inside
    $e = 10;
  } else {
    // and here, so variable should be inside
    $e = 10;
  }
  // output this variable
  echo $e;
  // another if-else
  if ($a == 100) {
    // variable with int type
    $f = 10;
  } else {
    // here is string
    $f = "10";
  }
  // so $f has type int|string
  echo $f;
  // support only single-type array
  $f = [1,2,3];
  echo $f;
  // fetch by index
  echo $f[1];
  // assign by index
  $f[1] = 10;
  echo $f;
  // simple array
  $g = [1,2,3];
  echo $g;
  // adding element
  $g[] = 100;
  echo $g;
  // and associative array with single-type keys
  $f = ["Key1" => 1, "Key2" => 2, "Key3" => 3];
  echo $f;
  // fetch by key
  echo $f["Key1"];
  // assign by key
  $f["Key1"] = 5;
  echo $f;
  // while
  $i = 0;
  while ($i < 20) {
    echo $i;
    $i++;
  }
  // for
  for ($i = 0; $i < 20; $i++) {
    echo $i + 5;
  }
  $qw = 1.5;
  // different operators
  echo $qw + 5 - 56.56 * 6 / 56;
}

Output:

// Code generated by php2go. PLEASE DO NOT EDIT.
package index

import (
   "fmt"
)

func Foo() {
	a := 100
	b := 1.5
	c := "Hello"
	d := true
	fmt.Print(a)
	fmt.Print(b)
	fmt.Print(c)
	fmt.Print(d)
	var e int64
	var f Var
	if a == 100 {
		e = 10
	} else {
		e = 10
	}
	fmt.Print(e)
	if a == 100 {
		f.Setint64(10)
	} else {
		f.Setstring("10")
	}
	fmt.Print(f.String())
	f.SetElementTypeint64([]int64{1, 2, 3})
	fmt.Print(f.GetElementTypeint64())
	fmt.Print(f.GetElementTypeint64()[1])
	f.GetElementTypeint64()[1] = 10
	fmt.Print(f.GetElementTypeint64())
	g := []int64{1, 2, 3}
	fmt.Print(g)
	g = append(g, 100)
	fmt.Print(g)
	f.SetmapWithKeystringWithValueint64(map[string]int64{"Key1": 1, "Key2": 2, "Key3": 3})
	fmt.Print(f.GetmapWithKeystringWithValueint64())
	fmt.Print(f.GetmapWithKeystringWithValueint64()["Key1"])
	f.GetmapWithKeystringWithValueint64()["Key1"] = 5
	fmt.Print(f.GetmapWithKeystringWithValueint64())
	i := 0
	for i < 20 {
		fmt.Print(i)
		i++
	}
	for i = 0; i < 20; i++ {
		fmt.Print(i + 5)
	}
	qw := 1.5
	fmt.Print(qw + float64(5) - 56.56 * float64(6) / float64(56))
}

The Var structure is a container for Union types.

Contacts

Name: Petr Makhnev

E-Mail: mr.makhneff@gmail.com

Telegram: @petr_makhnev

VK: @petrmakhnev

License

This library is released under the MIT license. For more information refer to the LICENSE file provided with this project.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
src
ctx
php
A Parser for PHP written in Go Features: * Fully support PHP5 and PHP7 syntax * Abstract syntax tree representation * Traversing AST * Namespace resolver Install: go get github.com/i582/php2go/src/php CLI dumper: $GOPATH/bin/php-parser -php5 /path/to/file/or/dir Package usage example: package main import ( "fmt" "bytes" "os" "github.com/i582/php2go/src/php/php7" "github.com/i582/php2go/src/php/visitor" ) func main() { src := bytes.NewBufferString(`<? echo "Hello world";`) parser := php7.NewParser(src, "example.php") parser.Parse() for _, e := range parser.GetErrors() { fmt.Println(e) } visitor := visitor.Dumper{ Writer: os.Stdout, Indent: "", Comments: parser.GetComments(), Positions: parser.GetPositions(), } rootNode := parser.GetRootNode() rootNode.Walk(visitor) }
A Parser for PHP written in Go Features: * Fully support PHP5 and PHP7 syntax * Abstract syntax tree representation * Traversing AST * Namespace resolver Install: go get github.com/i582/php2go/src/php CLI dumper: $GOPATH/bin/php-parser -php5 /path/to/file/or/dir Package usage example: package main import ( "fmt" "bytes" "os" "github.com/i582/php2go/src/php/php7" "github.com/i582/php2go/src/php/visitor" ) func main() { src := bytes.NewBufferString(`<? echo "Hello world";`) parser := php7.NewParser(src, "example.php") parser.Parse() for _, e := range parser.GetErrors() { fmt.Println(e) } visitor := visitor.Dumper{ Writer: os.Stdout, Indent: "", Comments: parser.GetComments(), Positions: parser.GetPositions(), } rootNode := parser.GetRootNode() rootNode.Walk(visitor) }
php/php5
line php5/php5.y:2
line php5/php5.y:2
php/php7
line php7/php7.y:2
line php7/php7.y:2
php/scanner
line scanner/scanner.rl:1
line scanner/scanner.rl:1
php/visitor
Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations
Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations Package visitor contains walker.visitor implementations
php/walker
Package walker declares walking behavior
Package walker declares walking behavior

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL