Skip to content

Commit 7fb9949

Browse files
committed
Fix map initialization with keys consisting of complex expressions
1 parent f57b0cd commit 7fb9949

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 1.1.1 / 2017-10-31
7+
8+
* Fixed map initialization with keys consisting of complex expressions
9+
(@thekid)
10+
611
## 1.1.0 / 2017-10-31
712

813
* Implemented trait usage, including aliasing via `as`. See issue #14

src/main/php/lang/ast/Emitter.class.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,12 @@ protected function emitArray($node) {
254254
}
255255

256256
$this->out->write('[');
257-
foreach ($node->value as $key => $value) {
258-
$this->out->write($key.'=>');
259-
$this->emit($value);
257+
foreach ($node->value as $pair) {
258+
if ($pair[0]) {
259+
$this->emit($pair[0]);
260+
$this->out->write('=>');
261+
}
262+
$this->emit($pair[1]);
260263
$this->out->write(',');
261264
}
262265
$this->out->write(']');

src/main/php/lang/ast/Parse.class.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,15 @@ private function setup() {
219219
});
220220

221221
$this->prefix('[', function($node) {
222-
$i= 0;
223222
$values= [];
224223
while (']' !== $this->token->symbol->id) {
225224
$expr= $this->expression(0);
226225

227226
if ('=>' === $this->token->symbol->id) {
228227
$this->token= $this->advance();
229-
$values[$expr->value]= $this->expression(0);
228+
$values[]= [$expr, $this->expression(0)];
230229
} else {
231-
$values[$i++]= $expr;
230+
$values[]= [null, $expr];
232231
}
233232

234233
if (']' === $this->token->symbol->id) break;

src/test/php/lang/ast/unittest/emit/ArraysTest.class.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ public function run() {
4848

4949
$this->assertEquals([1, 2], $r);
5050
}
51+
52+
#[@test]
53+
public function init_with_variable() {
54+
$r= $this->run('class <T> {
55+
public function run() {
56+
$KEY= "key";
57+
return [$KEY => "value"];
58+
}
59+
}');
60+
61+
$this->assertEquals(['key' => 'value'], $r);
62+
}
63+
64+
#[@test]
65+
public function init_with_member_variable() {
66+
$r= $this->run('class <T> {
67+
private static $KEY= "key";
68+
public function run() {
69+
return [self::$KEY => "value"];
70+
}
71+
}');
72+
73+
$this->assertEquals(['key' => 'value'], $r);
74+
}
5175
}

src/test/php/lang/ast/unittest/parse/LiteralsTest.class.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,33 @@ public function empty_array() {
5454

5555
#[@test]
5656
public function int_array() {
57-
$this->assertNodes([['[' => [['(literal)' => '1'], ['(literal)' => '2']]]], $this->parse('[1, 2];'));
57+
$this->assertNodes(
58+
[['[' => [[null, ['(literal)' => '1']], [null, ['(literal)' => '2']]]]],
59+
$this->parse('[1, 2];')
60+
);
5861
}
5962

6063
#[@test]
6164
public function key_value_map() {
62-
$this->assertNodes([['[' => ['"key"' => ['(literal)' => '"value"']]]], $this->parse('["key" => "value"];'));
65+
$this->assertNodes(
66+
[['[' => [[['(literal)' => '"key"'], ['(literal)' => '"value"']]]]],
67+
$this->parse('["key" => "value"];')
68+
);
6369
}
6470

6571
#[@test]
6672
public function dangling_comma_in_array() {
67-
$this->assertNodes([['[' => [['(literal)' => '1']]]], $this->parse('[1, ];'));
73+
$this->assertNodes(
74+
[['[' => [[null, ['(literal)' => '1']]]]],
75+
$this->parse('[1, ];')
76+
);
6877
}
6978

7079
#[@test]
7180
public function dangling_comma_in_key_value_map() {
72-
$this->assertNodes([['[' => ['"key"' => ['(literal)' => '"value"']]]], $this->parse('["key" => "value", ];'));
81+
$this->assertNodes(
82+
[['[' => [[['(literal)' => '"key"'], ['(literal)' => '"value"']]]]],
83+
$this->parse('["key" => "value", ];')
84+
);
7385
}
7486
}

src/test/php/lang/ast/unittest/parse/OperatorTest.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function assignment_to_offset() {
7575
#[@test]
7676
public function destructuring_assignment() {
7777
$this->assertNodes(
78-
[['=' => [['[' => [['(variable)' => 'a'], ['(variable)' => 'b']]], ['(' => [['result' => 'result'], []]]]]],
78+
[['=' => [['[' => [[null, ['(variable)' => 'a']], [null, ['(variable)' => 'b']]]], ['(' => [['result' => 'result'], []]]]]],
7979
$this->parse('[$a, $b]= result();')
8080
);
8181
}

0 commit comments

Comments
 (0)