Skip to content

Commit 6b72859

Browse files
committed
Handle dyadic operators
1 parent 1549178 commit 6b72859

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

examples/calcul-vars.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a = 2;
2+
b = 4;
3+
a * b;

examples/calcul-vars.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$a = 2;
2+
$b = 4;
3+
$a * $b;

src/JsPhpize/Lexer/Token.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ public function isComparison()
2121
return in_array($this->type, array('===', '!==', '>=', '<=', '<>', '!=', '==', '>', '<'));
2222
}
2323

24+
public function isLogical()
25+
{
26+
return in_array($this->type, array('&&', '||', '!'));
27+
}
28+
29+
public function isBinary()
30+
{
31+
return in_array($this->type, array('&', '|', '^', '~', '>>', '<<', '>>>'));
32+
}
33+
34+
public function isArithmetic()
35+
{
36+
return in_array($this->type, array('+', '-', '/', '*', '%', '**', '--', '++'));
37+
}
38+
39+
public function expectRightMember()
40+
{
41+
return $this->isArithmetic() || $this->isLogical() || $this->isBinary() || $this->isComparison();
42+
}
43+
2444
public function isAssignation()
2545
{
2646
return substr($this->type, -1) === '=' && !$this->isComparison();

src/JsPhpize/Nodes/Block.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function let($variable, $prefix)
5151

5252
public function addNodes($nodes)
5353
{
54+
if ($nodes === array('$b')) {
55+
throw new \Exception("Error Processing Request", 1);
56+
57+
}
5458
$nodes = array_filter(is_array($nodes) ? $nodes : func_get_args());
5559
$this->nodes = array_merge($this->nodes, $nodes);
5660
}

src/JsPhpize/Parser/Parser.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,20 @@ protected function getCurrentBlock()
134134
return end($this->stack);
135135
}
136136

137+
protected function visitToken($token)
138+
{
139+
$block = $this->getCurrentBlock();
140+
$method = 'visit' . ucfirst($token->type);
141+
$token = method_exists($this, $method)
142+
? $this->$method($token)
143+
: $this->visitNode($token);
144+
if (!is_array($token)) {
145+
$token = array($token);
146+
}
147+
148+
return $token;
149+
}
150+
137151
public function parseBlock($block)
138152
{
139153
$this->stack[] = $block;
@@ -149,14 +163,7 @@ public function parseBlock($block)
149163
if ($token->type === '}') {
150164
return;
151165
}
152-
$method = 'visit' . ucfirst($token->type);
153-
$token = method_exists($this, $method)
154-
? $this->$method($token)
155-
: $this->visitNode($token);
156-
if (!is_array($token)) {
157-
$token = array($token);
158-
}
159-
$block->addNodes($token);
166+
$block->addNodes($this->visitToken($token));
160167
}
161168
array_pop($this->stack);
162169
}

src/JsPhpize/Parser/Visitor.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,15 @@ protected function visitVariable($token)
100100
$next = $this->get(0);
101101
$afterNext = $this->get(1);
102102
}
103-
if (count($variableParts) === 1) {
104-
return $variableParts[0];
103+
$variable = count($variableParts) === 1
104+
? $variableParts[0]
105+
: 'call_user_func(' . $this->getHelper('dot') . ', ' . implode(', ', $variableParts) . ')';
106+
while ($next && $next->expectRightMember() && $next->type !== '+') {
107+
$this->skip();
108+
$variable .= ' ' . $next . ' ' . implode(' ', $this->visitToken($this->next()));
109+
$next = $this->current();
105110
}
106111

107-
return 'call_user_func(' . $this->getHelper('dot') . ', ' . implode(', ', $variableParts) . ')';
112+
return $variable;
108113
}
109114
}

0 commit comments

Comments
 (0)