Skip to content

Commit 24e8df3

Browse files
committed
Test Math constants + fix dot helper test + prepare lambda
1 parent 1b34197 commit 24e8df3

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

examples/calcul.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ a = 4;
22
b = 1;
33
c = 3;
44

5-
return b + a * c;
5+
return round(Math.PI * 100) + b + a * c;

examples/calcul.return

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13
1+
327

src/JsPhpize/Nodes/Assignation.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ class Assignation extends Value
2121
*/
2222
protected $operator;
2323

24-
public function __construct($operator, Assignable $leftHand, Value $rightHand)
24+
public function __construct($operator, Assignable $leftHand, Node $rightHand)
2525
{
2626
if (!$leftHand->isAssignable()) {
2727
throw new Exception($leftHand->getNonAssignableReason(), 9);
2828
}
2929

30+
if (!($rightHand instanceof Value) && (!($rightHand instanceof Block) || $rightHand->type !== 'function')) {
31+
throw new Exception('Only Value instance or Function block could be assigned.', 19);
32+
}
33+
3034
$this->operator = $operator;
3135
$this->leftHand = $leftHand;
3236
$this->rightHand = $rightHand;

src/JsPhpize/Parser/Parser.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use JsPhpize\Lexer\Token;
88
use JsPhpize\Nodes\Assignation;
99
use JsPhpize\Nodes\Block;
10+
use JsPhpize\Nodes\Value;
1011
use JsPhpize\Nodes\BracketsArray;
1112
use JsPhpize\Nodes\Constant;
1213
use JsPhpize\Nodes\Dyiade;
@@ -90,13 +91,24 @@ protected function exceptionInfos()
9091

9192
protected function unexpected($token)
9293
{
93-
// var_dump($token);
94-
// var_dump($this->stack[0]->instructions[2]->instructions[0]);
95-
// exit;
96-
9794
throw new Exception('Unexpected ' . $token->type . rtrim(' ' . ($token->value ?: '')) . $this->exceptionInfos(), 8);
9895
}
9996

97+
protected function parseLambda(Value $parameters)
98+
{
99+
$lambda = new Block('function');
100+
$lambda->setValue($parameters);
101+
$next = $this->next();
102+
if ($next) {
103+
if ($next->is('{')) {
104+
$this->skip();
105+
$this->parseBlock($lambda);
106+
}
107+
}
108+
109+
return $lambda;
110+
}
111+
100112
protected function parseParentheses()
101113
{
102114
$parentheses = new Parenthesis();
@@ -105,6 +117,10 @@ protected function parseParentheses()
105117
while ($token = $this->next()) {
106118
$debug[] = $token;
107119
if ($token->is(')')) {
120+
$next = $this->get(0);
121+
if ($next && $next->is('lambda')) {
122+
return $this->parseLambda($parentheses);
123+
}
108124
return $parentheses;
109125
}
110126
if ($expectComma) {
@@ -237,6 +253,10 @@ protected function parseVariable($name)
237253
continue;
238254
}
239255

256+
if ($next->is('lambda')) {
257+
return $this->parseLambda(new Variable($name, $children));
258+
}
259+
240260
break;
241261
}
242262

@@ -303,7 +323,8 @@ protected function parseValue($token)
303323
if ($token->isAssignation()) {
304324
$this->skip();
305325
$arguments = array();
306-
$value = new Assignation($token->type, $value, $this->expectValue($this->next()));
326+
$valueToAssign = $this->expectValue($this->next());
327+
$value = new Assignation($token->type, $value, $valueToAssign);
307328

308329
continue;
309330
}

tests/mainMethods.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testCompileFile()
1919
: (method_exists($base, $method = "get" . ucfirst($key))
2020
? $base->$method()
2121
: (method_exists($base, $key)
22-
? $base->$key()
22+
? array($base, $key)
2323
: null
2424
)
2525
)
@@ -68,7 +68,7 @@ public function testCompileFile()
6868
: (method_exists($base, $method = "get" . ucfirst($key))
6969
? $base->$method()
7070
: (method_exists($base, $key)
71-
? $base->$key()
71+
? array($base, $key)
7272
: null
7373
)
7474
)
@@ -176,7 +176,7 @@ public function testCompileSource()
176176
: (method_exists($base, $method = "get" . ucfirst($key))
177177
? $base->$method()
178178
: (method_exists($base, $key)
179-
? $base->$key()
179+
? array($base, $key)
180180
: null
181181
)
182182
)

0 commit comments

Comments
 (0)