Skip to content

Instantly share code, notes, and snippets.

@RealyUniqueName
Created August 13, 2016 03:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealyUniqueName/2dc44c3d7a5e1f02df71c3a8186e5f43 to your computer and use it in GitHub Desktop.
Save RealyUniqueName/2dc44c3d7a5e1f02df71c3a8186e5f43 to your computer and use it in GitHub Desktop.
New php generator for Haxe
package example;
import php7.Exception;
class TestSuper {
function new () {}
function superMethod () return [Std.random(2), Std.random(2)][Std.random(2)];
}
class Param<T> {
public var value:T;
public function new (v:T) value = v;
}
interface IFace {
public function const () : Void ;
}
typedef SuperTypeDef = TestSuper;
private class PrivateTest {
public function new (arg:String = 'f') {
Test.dump(arg);
}
}
@:keep
class Test extends SuperTypeDef implements IFace {
/** static inline vars get translated into class constants */
static public inline var STATIC_INLINE_VAR = 'static_inline_var';
/**
* This is multiline doc
* With leading asterisks
*/
static public var staticVarWithExpr = [1, 2, 3, 4];
/**
This is multiline doc
without leading asterisks
*/
static var staticWithoutExpr:Test;
var instanceFieldWithDefaultValue : Int = 10;
var instanceField : Null<Test> = null;
var instanceProperty (get,set) : Bool;
@:protected
private var protectedVar : Int = 0;
/**
Haxe initialization magic
**/
static function __init__ () {
trace('__init__');
trace('wtf');
}
static public function main () {}
static public function dump<T> (?v:T) {
trace(v);
}
static public function rnd () : Bool {
return Std.random(2) == 0;
}
static function arguments (a:Test, b:Bool, i:Int, s:String) {
optionalArguments();
}
static function optionalArguments (?a:Test, a:Test = null, b:Bool = true, ?n:Bool, v:Null<Int> = 2) {
}
@:protected
static private function protectedMethod () {}
/**
Constructor example
*/
public function new (){
super();
dump(Test);
}
public function const () {
dump(123);
dump(12.3);
dump("string");
dump(true);
dump(null);
dump(this);
dump(super.superMethod());
}
function varLocal () {
var w = Std.random(10);
dump(w);
dump(w);
}
function arrayDeclaration () {
var small = (rnd() ? [] : [1]);
dump(small);
var big = [1, 2, 3, 4, 5];
dump(big);
}
function binop () {
var i1 = Std.random(10);
var i2 = Std.random(10);
var f = Math.random() + 0.01;
dump(i1 + i2);
dump(i1 * i2);
dump(i1 / i2);
dump(i1 - i2);
dump(i1 = i2);
dump(i1 == i2);
dump(i1 != i2);
dump(i1 > i2);
dump(i1 >= i2);
dump(i1 < i2);
dump(i1 <= i2);
dump(i1 & i2);
dump(i1 | i2);
dump(i1 ^ i2);
var b1 = rnd();
var b2 = rnd();
dump(b1 || b2);
dump(b1 && b2);
dump(i1 << i2);
dump(i1 >> i2);
dump(i1 % i2);
dump(i1 << i2);
dump(i1 >>> i2);
dump(i1 += i2);
dump(i1 *= i2);
dump(f /= i2);
dump(i1 -= i2);
dump(i1 &= i2);
dump(i1 |= i2);
dump(i1 ^= i2);
dump(i1 <<= i2);
dump(i1 >>= i2);
dump(i1 %= i2);
dump(i1 >>>= i2);
var s = 'qwerty'.split('')[Std.random('qwerty'.length)];
dump(s + 2);
dump(s += 2);
}
function unop () {
var q = Std.random(10);
dump(q++);
dump(q++);
dump(++q);
dump(++q);
dump(q--);
dump(q--);
dump(--q);
dump(--q);
dump(~q);
dump(!rnd());
dump(-q);
}
function fieldAccess () {
dump(Test.staticWithoutExpr);
dump("sdsf".length);
dump(this.instanceField);
Test.dump();
dump(super.superMethod());
var s = "wtf".split("")[Std.random(3)];
dump(s.length);
dump(s.toUpperCase());
}
function objectDeclaration () {
var obj = {
field1 : "wtf",
field2 : Std.random(100),
"fei'ld\"-" : 0.15,
's"q\'' : "sdsfsfd",
me : new PrivateTest(),
what : function (?arg = true) dump(arg),
nestedObj : {
hellow : 12,
world : ["world"]
}
}
dump(obj);
dump(obj);
}
function newConstr () {
dump(new PrivateTest('sdf'));
}
function functionClosure () {
var fn = function () return 'sdfsd';
dump(fn);
}
function returnExpr () {
var functions = {
voidResult : function () return,
someResult : function () return Std.string(2)
}
dump(functions);
}
function ifElseTernary () {
if (rnd()) {
dump('hey!');
dump(1);
} else if (rnd()) {
dump('ho!');
dump(2);
} else {
dump('haw!');
dump(2);
}
if (rnd()) dump('single line if');
dump(rnd() ? 1 : 2);
}
function whileAndDoWhile () {
while (Std.random(10) > 0) {
dump('while');
break;
}
do {
dump('while');
if (rnd()) continue;
dump('after');
} while (Std.random(10) > 0);
}
function forLoop () {
for (i in 0...Std.random(10)) {
dump(i);
}
}
function switchCase () {
switch (Std.random(10)) {
case 1 : dump(Std.random(1));
case 2 : dump(Std.random(2));
case _ : dump('default');
}
switch ([Std.random(10), Std.random(10)]) {
case [1, 2] : dump(Std.random(1));
case [_, 3] : dump(Std.random(2));
case [2, 5] : dump(Std.random(3));
case _ : dump('default');
}
var obj = {
sw : switch (Std.random(10)) {
case 9 : dump(1); 'whooo';
case 8 : dump(1); 'whooo';
case _: dump(2); 'oops';
}
}
dump(obj);
}
function tryCatchThrow () {
var e = rnd();
dump(e);
try {
if (rnd()) {
throw "exception";
} else if (rnd()) {
throw 12;
} else if (rnd()) {
throw new Param('ho');
} else if (rnd()) {
throw new Param(12);
} else if (rnd()) {
throw function () return rnd();
} else if (rnd()) {
throw this;
} else {
throw new Exception('native');
}
} catch (e:Dynamic) {
dump(e);
dump(e);
}
dump(e);
try {
var q : Dynamic = {};
dump(q);
throw q;
} catch (e:Float) {
dump(e);
}
try {
throw 'test';
} catch (e:IFace) {
dump(e);
} catch (e:Int) {
dump(e);
} catch (e:Bool) {
dump(e);
} catch (e:Float) {
dump(e);
} catch (e:String) {
dump(e);
} catch (e:Exception) {
dump(e);
} catch (e:Param<Dynamic>) {
dump(e);
} catch (e:Dynamic) {
dump(e);
}
}
function get_instanceProperty () return Std.random(2) == 1;
function set_instanceProperty (value) return value && Std.random(2) == 1;
}
<?php
namespace example;
use \example\IFace;
use \php7\Boot;
use \example\_Test\PrivateTest;
use \haxe\Log;
use \example\Param;
use \example\TestSuper;
use \example\Test;
use \php7\HException;
class Test extends TestSuper implements IFace
{
/**
* @var string
* static inline vars get translated into class constants
*/
const STATIC_INLINE_VAR = "static_inline_var";
/**
* @var \Array
* This is multiline doc
* With leading asterisks
*/
static public $staticVarWithExpr;
/**
* @var Test
* This is multiline doc
* without leading asterisks
*/
static public $staticWithoutExpr;
/**
* @var Test
*/
public $instanceField;
/**
* @var int
*/
public $instanceFieldWithDefaultValue;
/**
* @var bool
*/
public $instanceProperty;
/**
* @var int
*/
protected $protectedVar;
/**
* @param Test $a
* @param bool $b
* @param int $i
* @param string $s
*
* @return void
*/
static public function arguments ($a, $b, $i, $s)
{
Test::optionalArguments(null, null, null, null, null);
}
/**
* @param mixed $v
*
* @return void
*/
static public function dump ($v = null)
{
Log::trace($v, (object)[
"fileName" => "Test.hx",
"lineNumber" => 70,
"className" => "example.Test",
"methodName" => "dump",
]);
}
/**
* @return void
*/
static public function main ()
{
}
/**
* @param Test $a
* @param Test $a
* @param bool $b
* @param bool $n
* @param int $v
*
* @return void
*/
static public function optionalArguments ($a = null, $a1 = null, $b = true, $n = null, $v = 2)
{
if ($b === null) {
$b = true;
}
if ($v === null) {
$v = 2;
}
}
/**
* @return void
*/
static protected function protectedMethod ()
{
}
/**
* @return bool
*/
static public function rnd ()
{
return \Std::random(2) === 0;
}
/**
* Constructor example
*
* @return void
*/
public function __construct ()
{
$this->protectedVar = 0;
$this->instanceField = null;
$this->instanceFieldWithDefaultValue = 10;
parent();
Test::dump(Test);
}
/**
* @return void
*/
public function arrayDeclaration ()
{
Test::dump(Test::rnd() ? [] : [1]);
Test::dump([
1,
2,
3,
4,
5,
]);
}
/**
* @return void
*/
public function binop ()
{
$i1 = \Std::random(10);
$i2 = \Std::random(10);
$f = \Math::random() + 0.01;
Test::dump($i1 + $i2);
Test::dump($i1 * $i2);
Test::dump($i1 / $i2);
Test::dump($i1 - $i2);
$i1 = $i2;
Test::dump($i2);
Test::dump($i2 === $i2);
Test::dump($i2 !== $i2);
Test::dump($i2 > $i2);
Test::dump($i2 >= $i2);
Test::dump($i2 < $i2);
Test::dump($i2 <= $i2);
Test::dump($i2 & $i2);
Test::dump($i2 | $i2);
Test::dump($i2 ^ $i2);
$b1 = Test::rnd();
$b2 = Test::rnd();
Test::dump($b1 || $b2);
Test::dump($b1 && $b2);
Test::dump($i2 << $i2);
Test::dump($i2 >> $i2);
Test::dump($i2 % $i2);
Test::dump($i2 << $i2);
Test::dump(Boot::shiftRightUnsigned($i2, $i2));
$i1 = ($i2 + $i2);
Test::dump($i1);
$i1 *= $i2;
Test::dump($i1);
$f /= $i2;
Test::dump($f);
$i1 -= $i2;
Test::dump($i1);
$i1 &= $i2;
Test::dump($i1);
$i1 |= $i2;
Test::dump($i1);
$i1 ^= $i2;
Test::dump($i1);
$i1 <<= $i2;
Test::dump($i1);
$i1 >>= $i2;
Test::dump($i1);
$i1 %= $i2;
Test::dump($i1);
$i1 = Boot::shiftRightUnsigned($i1, $i2);
Test::dump($i1);
$s = "qwerty"->split("")[\Std::random(strlen("qwerty"))];
Test::dump($s . 2);
$s .= 2;
Test::dump($s);
}
/**
* @return void
*/
public function const ()
{
Test::dump(123);
Test::dump(12.3);
Test::dump("string");
Test::dump(true);
Test::dump(null);
Test::dump($this);
Test::dump(parent->superMethod());
}
/**
* @return void
*/
public function fieldAccess ()
{
Test::dump(Test::$staticWithoutExpr);
Test::dump(strlen("sdsf"));
Test::dump($this->instanceField);
Test::dump(null);
Test::dump(parent->superMethod());
$s = "wtf"->split("")[\Std::random(3)];
Test::dump(strlen($s));
Test::dump($s->toUpperCase());
}
/**
* @return void
*/
public function forLoop ()
{
$_g1 = 0;
$_g = \Std::random(10);
while ($_g1 < $_g) {
Test::dump($_g1++);
};
}
/**
* @return void
*/
public function functionClosure ()
{
Test::dump(function () {
return "sdfsd";
});
}
/**
* @return bool
*/
public function get_instanceProperty ()
{
return \Std::random(2) === 1;
}
/**
* @return void
*/
public function ifElseTernary ()
{
if (Test::rnd()) {
Test::dump("hey!");
Test::dump(1);
} else if (Test::rnd()) {
Test::dump("ho!");
Test::dump(2);
} else {
Test::dump("haw!");
Test::dump(2);
}
if (Test::rnd()) {
Test::dump("single line if");
}
Test::dump(Test::rnd() ? 1 : 2);
}
/**
* @return void
*/
public function newConstr ()
{
Test::dump(new PrivateTest("sdf"));
}
/**
* @return void
*/
public function objectDeclaration ()
{
$obj = (object)[
"field1" => "wtf",
"field2" => \Std::random(100),
"fei'ld\"-" => 0.15,
"s\"q'" => "sdsfsfd",
"me" => new PrivateTest(null),
"what" => function ($arg = true) {
if ($arg === null) {
$arg = true;
}
Test::dump($arg);
},
"nestedObj" => (object)[
"hellow" => 12,
"world" => ["world"],
],
];
Test::dump($obj);
Test::dump($obj);
}
/**
* @return void
*/
public function returnExpr ()
{
Test::dump((object)[
"voidResult" => function () {
return;
},
"someResult" => function () {
return "2";
},
]);
}
/**
* @param bool $value
*
* @return bool
*/
public function set_instanceProperty ($value)
{
if ($value) {
return \Std::random(2) === 1;
} else {
return false;
}
}
/**
* @return void
*/
public function switchCase ()
{
switch (\Std::random(10)) {
case 1:
Test::dump(\Std::random(1));
break;
case 2:
Test::dump(\Std::random(2));
break;
default:
Test::dump("default");
break;
}
$_g1 = \Std::random(10);
switch (\Std::random(10)) {
case 1:
switch ($_g1) {
case 2:
Test::dump(\Std::random(1));
break;
case 3:
Test::dump(\Std::random(2));
break;
default:
Test::dump("default");
break;
}
break;
case 2:
switch ($_g1) {
case 3:
Test::dump(\Std::random(2));
break;
case 5:
Test::dump(\Std::random(3));
break;
default:
Test::dump("default");
break;
}
break;
default:
if ($_g1 === 3) {
Test::dump(\Std::random(2));
} else {
Test::dump("default");
}
break;
}
$_g3 = \Std::random(10);
$tmp = null;
switch ($_g3) {
case 8:
Test::dump(1);
$tmp = "whooo";
break;
case 9:
Test::dump(1);
$tmp = "whooo";
break;
default:
Test::dump(2);
$tmp = "oops";
break;
}
Test::dump((object)[
"sw" => $tmp,
]);
}
/**
* @return void
*/
public function tryCatchThrow ()
{
$e = Test::rnd();
Test::dump($e);
try {
if (Test::rnd()) {
throw new HException("exception");
} else if (Test::rnd()) {
throw new HException(12);
} else if (Test::rnd()) {
throw new HException(new Param("ho"));
} else if (Test::rnd()) {
throw new HException(new Param(12));
} else if (Test::rnd()) {
throw new HException(function () {
return Test::rnd();
});
} else if (Test::rnd()) {
throw new HException($this);
} else {
throw new \Exception("native", null);
}
} catch (\Exception $__hx__caught_e) {
$__hx__real_e = ($__hx__caught_e instanceof HException ? $__hx__caught_e->e : $__hx__caught_e);
$e1 = $__hx__real_e;
Test::dump($e1);
Test::dump($e1);
}
Test::dump($e);
try {
$q = new \StdClass();
Test::dump($q);
throw throw (is_object($__hx__throw = $q) && $__hx__throw instanceof \Exception ? $__hx__throw : new HException($__hx__throw));
} catch (\Exception $__hx__caught_e) {
$__hx__real_e = ($__hx__caught_e instanceof HException ? $__hx__caught_e->e : $__hx__caught_e);
if (is_float($__hx__real_e)) {
$e2 = $__hx__real_e;
Test::dump($e2);
} else throw $__hx__caught_e;
}
try {
throw new HException("test");
} catch (\Exception $__hx__caught_e) {
$__hx__real_e = ($__hx__caught_e instanceof HException ? $__hx__caught_e->e : $__hx__caught_e);
if ($__hx__real_e instanceof IFace) {
$e3 = $__hx__real_e;
Test::dump($e3);
} else if (is_int($__hx__real_e)) {
$e4 = $__hx__real_e;
Test::dump($e4);
} else if (is_float($__hx__real_e)) {
$e5 = $__hx__real_e;
Test::dump($e5);
} else if (is_float($__hx__real_e)) {
$e6 = $__hx__real_e;
Test::dump($e6);
} else if (is_string($__hx__real_e)) {
$e7 = $__hx__real_e;
Test::dump($e7);
} else if ($__hx__real_e instanceof \Exception) {
$e8 = $__hx__real_e;
Test::dump($e8);
} else if ($__hx__real_e instanceof Param) {
$e9 = $__hx__real_e;
Test::dump($e9);
} else {
$e10 = $__hx__real_e;
Test::dump($e10);
}
}
}
/**
* @return void
*/
public function unop ()
{
$q = \Std::random(10);
Test::dump($q++);
Test::dump($q++);
Test::dump(++$q);
Test::dump(++$q);
Test::dump($q--);
Test::dump($q--);
Test::dump(--$q);
Test::dump(--$q);
Test::dump(~$q);
Test::dump(!Test::rnd());
Test::dump(-$q);
}
/**
* @return void
*/
public function varLocal ()
{
$w = \Std::random(10);
Test::dump($w);
Test::dump($w);
}
/**
* @return void
*/
public function whileAndDoWhile ()
{
while (\Std::random(10) > 0) {
Test::dump("while");
break;
};
while (true) {
Test::dump("while");
if (Test::rnd()) {
if (!(\Std::random(10) > 0)) {
break;
} else {
continue;
}
}
Test::dump("after");
if (!(\Std::random(10) > 0)) {
break;
}
};
}
/**
* @internal
* @access private
*/
static public function __hx__init ()
{
static $called = false;
if ($called) return;
$called = true;
Log::trace("__init__", (object)[
"fileName" => "Test.hx",
"lineNumber" => 62,
"className" => "example.Test",
"methodName" => "__init__",
]);
Log::trace("wtf", (object)[
"fileName" => "Test.hx",
"lineNumber" => 63,
"className" => "example.Test",
"methodName" => "__init__",
]);
self::$staticVarWithExpr = [
1,
2,
3,
4,
];
}
}
\example\Test::__hx__init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment