Skip to content

Commit

Permalink
feat: import feature and async function
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed May 25, 2021
1 parent db28908 commit a9eb6f6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
48 changes: 42 additions & 6 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class Compiler extends PrettyPrinterAbstract
{
public $vars = array();
public $mode = 'js';
// Special nodes

protected function pParam(Node\Param$node)
Expand Down Expand Up @@ -591,6 +592,10 @@ protected function pExpr_Cast_Unset(Cast\Unset_$node)

protected function pExpr_FuncCall(Expr\FuncCall$node)
{
$compiled = $this->compilefn($node);
if($compiled){
return $compiled;
}
return $this->pCallLhs($node->name)
. '(' . $this->pMaybeMultiline($node->args) . ')';
}
Expand Down Expand Up @@ -636,13 +641,15 @@ protected function pExpr_Eval(Expr\Eval_$node)
protected function pExpr_Include(Expr\Include_$node)
{
static $map = [
Expr\Include_::TYPE_INCLUDE => 'include',
Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
Expr\Include_::TYPE_REQUIRE => 'require',
Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
Expr\Include_::TYPE_INCLUDE => 'import',
Expr\Include_::TYPE_INCLUDE_ONCE => 'import',
Expr\Include_::TYPE_REQUIRE => 'import',
Expr\Include_::TYPE_REQUIRE_ONCE => 'import',
];

return $map[$node->type] . ' ' . $this->p($node->expr);
$expr = trim($this->p($node->expr),"'");

return $map[$node->type] . ' ' . $expr ." from './".$expr.'.'.$this->mode."'";
}

protected function pExpr_List(Expr\List_$node)
Expand Down Expand Up @@ -942,11 +949,21 @@ protected function pStmt_ClassConst(Stmt\ClassConst$node)

protected function pStmt_Function(Stmt\Function_$node)
{
$is_async = false;
foreach ($node->getComments() as $comment){
$comment = strtolower(trim(trim($comment,'/*')));
if($comment == '@async'){
$is_async = true;
}
}

return $this->pAttrGroups($node->attrGroups)
. ($is_async ? 'async ' : '')
. 'function ' . ($node->byRef ? '&' : '') . $node->name
. '(' . $this->pCommaSeparated($node->params) . ')'
. (null !== $node->returnType ? ' : ' . $this->p($node->returnType) : '')
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';

}

protected function pStmt_Const(Stmt\Const_$node)
Expand Down Expand Up @@ -1095,7 +1112,7 @@ protected function pStmt_Static(Stmt\Static_$node)

protected function pStmt_Global(Stmt\Global_$node)
{
return 'global ' . $this->pCommaSeparated($node->vars) . ';';
return 'export ' . $this->pCommaSeparated($node->vars) . ';';
}

protected function pStmt_StaticVar(Stmt\StaticVar$node)
Expand Down Expand Up @@ -1277,4 +1294,23 @@ protected function pAttrGroups(array $nodes, bool $inline = false): string

return $result;
}

private function compilefn($node){
// $this->pCallLhs($node->name)
// . '(' . $this->pMaybeMultiline($node->args) . ')';
$pArgs = [];
foreach ($node->args as $arg) {
if (null === $arg) {
$pArgs[] = '';
} else {
$pArgs[] = $this->p($arg);
}
}

if($node->name == 'import_from'){
return "import ".trim($pArgs[0],"'")." from ".$pArgs[1];
}
return false;

}
}
20 changes: 12 additions & 8 deletions src/Php2js.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@

class PHP2JS
{
public static function compile($code){
public static function compile($code,$mode = 'js'){
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse('<?php'.PHP_EOL.$code);
} catch (Error $error) {
echo "Parse error: {$error->getMessage()}\n";
return;
}
$errorHandler = new \PhpParser\ErrorHandler\Collecting;

$ast = $parser->parse('<?php'.PHP_EOL.$code, $errorHandler);
if ($errorHandler->hasErrors()) {
foreach ($errorHandler->getErrors() as $error) {
// $error is an ordinary PhpParser\Error
}
}

$compiler= new Compiler;
$compiler->mode = $mode;
$jscode = $compiler->prettyPrint($ast);
return $jscode;
}

public static function compileFile($input,$output=null){
public static function compileFile($input,$output=null,$mode='js'){
$code = @file_get_contents($input);
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
Expand All @@ -32,6 +35,7 @@ public static function compileFile($input,$output=null){
}

$compiler= new Compiler;
$compiler->mode = $mode;
$jscode = $compiler->prettyPrint($ast);
if($output){
file_put_contents($output,$jscode);
Expand Down

0 comments on commit a9eb6f6

Please sign in to comment.