Skip to content

Commit

Permalink
Handle class instances creations
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jun 8, 2017
1 parent 04f1f09 commit 6a54bb0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Date();
1 change: 1 addition & 0 deletions examples/new.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new Date();
5 changes: 5 additions & 0 deletions src/JsPhpize/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
{
$function = $functionCall->function;
$arguments = $functionCall->arguments;
$applicant = $functionCall->applicant;
$arguments = $this->visitNodesArray($arguments, $indent, ', ');
$dynamicCall = 'call_user_func(' .
$this->visitNode($function, $indent) .
Expand All @@ -199,6 +200,10 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent)
$name = $function->name;
$staticCall = $name . '(' . $arguments . ')';

if ($applicant === 'new') {
return $staticCall;
}

if (in_array($name, array(
'array',
'echo',
Expand Down
8 changes: 7 additions & 1 deletion src/JsPhpize/Nodes/FunctionCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ class FunctionCall extends Value
*/
protected $arguments;

public function __construct(Value $function, array $arguments)
/**
* @var null|string
*/
protected $applicant;

public function __construct(Value $function, array $arguments, $applicant = null)
{
$this->function = $function;
$this->arguments = $arguments;
$this->applicant = $applicant;
}
}
4 changes: 3 additions & 1 deletion src/JsPhpize/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,12 @@ protected function parseKeywordStatement($token)
$name = $token->value;
$keyword = new Block($name);
switch ($name) {
case 'new':
case 'clone':
case 'return':
case 'continue':
case 'break':
$this->handleOptionalValue($keyword, $this->get(0));
$this->handleOptionalValue($keyword, $this->get(0), $name);
break;
case 'case':
$value = $this->expectValue($this->next());
Expand Down
24 changes: 11 additions & 13 deletions src/JsPhpize/Parser/TokenExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ protected function getInstructionFromToken($token)
}
}

protected function getValueFromToken($token)
protected function getValueFromToken($token, $previousToken = null, $applicant = null)
{
$value = $this->getInitialValue($token);
if ($value) {
$this->appendFunctionsCalls($value);
$this->appendFunctionsCalls($value, $previousToken, $applicant);
}

return $value;
}

protected function handleOptionalValue($keyword, $afterKeyword)
protected function handleOptionalValue($keyword, $afterKeyword, $applicant)
{
if (!$afterKeyword->is(';')) {
$value = $this->expectValue($this->next());
$value = $this->expectValue($this->next(), $keyword, $applicant);
$keyword->setValue($value);
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ protected function getInitialValue($token)
}
}

protected function appendFunctionsCalls(&$value)
protected function appendFunctionsCalls(&$value, $previousToken = null, $applicant = null)
{
while ($token = $this->get(0)) {
if ($token->is('{') || $token->expectNoLeftMember()) {
Expand All @@ -145,8 +145,7 @@ protected function appendFunctionsCalls(&$value)
}
if ($token->is('(')) {
$this->skip();
$arguments = array();
$value = new FunctionCall($value, $this->parseParentheses()->nodes);
$value = new FunctionCall($value, $this->parseParentheses()->nodes, $applicant);

continue;
}
Expand All @@ -158,17 +157,16 @@ protected function appendFunctionsCalls(&$value)
}
if ($token->isAssignation()) {
$this->skip();
$arguments = array();
$valueToAssign = $this->expectValue($this->next());
$valueToAssign = $this->expectValue($this->next(), $previousToken);
$value = new Assignation($token->type, $value, $valueToAssign);

continue;
}

$this->skip();
$nextValue = $this->expectValue($this->next());
$nextValue = $this->expectValue($this->next(), $previousToken);
$value = new Dyiade($token->type, $value, $nextValue);
$token = $this->get(0);
$this->get(0);

continue;
}
Expand All @@ -177,15 +175,15 @@ protected function appendFunctionsCalls(&$value)
}
}

protected function expectValue($next, $token = null)
protected function expectValue($next, $token = null, $applicant = null)
{
if (!$next) {
if ($token) {
throw $this->unexpected($token);
}
throw new Exception('Value expected after ' . $this->exceptionInfos(), 20);
}
$value = $this->getValueFromToken($next);
$value = $this->getValueFromToken($next, $token, $applicant);
if (!$value) {
throw $this->unexpected($next);
}
Expand Down

0 comments on commit 6a54bb0

Please sign in to comment.