Skip to content

Commit 8be692f

Browse files
committed
Refactoring
1 parent 0f70915 commit 8be692f

18 files changed

+245
-288
lines changed

src/Compiler/Lang/Assembler/AbstractAssembler.php

Lines changed: 14 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
11
<?php
22
namespace PHPJava\Compiler\Lang\Assembler;
33

4-
use PHPJava\Compiler\Builder\Collection\ConstantPool;
5-
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
4+
use PHPJava\Compiler\Builder\Method;
65
use PHPJava\Compiler\Lang\Assembler\Store\Store;
6+
use PHPJava\Compiler\Lang\Assembler\Traits\CollectionManageable;
7+
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
8+
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
79
use PHPJava\Compiler\Lang\Assembler\Traits\StoreManageable;
810
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
9-
use PHPJava\Exceptions\AssembleStructureException;
1011
use PhpParser\Node;
1112

12-
abstract class AbstractAssembler implements AssemblerInterface
13+
abstract class AbstractAssembler implements AssemblerInterface, ParameterServiceInterface
1314
{
15+
use ConstantPoolManageable;
1416
use StoreManageable;
15-
16-
/**
17-
* @var ConstantPool
18-
*/
19-
protected $constantPool;
20-
21-
/**
22-
* @var ConstantPoolFinder
23-
*/
24-
protected $constantPoolFinder;
17+
use NamespaceManageable;
18+
use CollectionManageable;
2519

2620
/**
2721
* @var Node
2822
*/
2923
protected $node;
3024

31-
/**
32-
* @var AssemblerInterface
33-
*/
34-
protected $parentAssembler;
35-
3625
/**
3726
* @var StreamReaderInterface
3827
*/
3928
protected $streamReader;
4029

41-
protected $namespace;
42-
4330
/**
4431
* @var Store
4532
*/
4633
protected $store;
4734

48-
public static function factory(Node $node): AssemblerInterface
35+
/**
36+
* @var Method
37+
*/
38+
protected $method;
39+
40+
public static function factory(Node $node): self
4941
{
5042
return new static($node);
5143
}
@@ -57,52 +49,6 @@ public function __construct(Node $node)
5749

5850
abstract public function assemble();
5951

60-
public function setNamespace(?array $namespace): AssemblerInterface
61-
{
62-
$this->namespace = $namespace;
63-
return $this;
64-
}
65-
66-
public function getNamespace(): ?array
67-
{
68-
return $this->namespace;
69-
}
70-
71-
public function setParentAssembler(AssemblerInterface $parentAssembler): AssemblerInterface
72-
{
73-
$this->parentAssembler = $parentAssembler;
74-
$this->constantPool = $this->parentAssembler->getConstantPool();
75-
$this->constantPoolFinder = $this->parentAssembler->getConstantPoolFinder();
76-
return $this;
77-
}
78-
79-
public function getParentAssembler(): ?AssemblerInterface
80-
{
81-
return $this->parentAssembler;
82-
}
83-
84-
public function getConstantPool(): ConstantPool
85-
{
86-
if (!isset($this->constantPool)) {
87-
throw new AssembleStructureException(
88-
'The ConstantPool is not set. ' .
89-
'You must to set the ConstantPool.'
90-
);
91-
}
92-
return $this->constantPool;
93-
}
94-
95-
public function getConstantPoolFinder(): ConstantPoolFinder
96-
{
97-
if (!isset($this->constantPoolFinder)) {
98-
throw new AssembleStructureException(
99-
'The ConstantPoolFinder is not set. ' .
100-
'You must to set the ConstantPoolFinder.'
101-
);
102-
}
103-
return $this->constantPoolFinder;
104-
}
105-
10652
public function setStreamReader(StreamReaderInterface $streamReader): AssemblerInterface
10753
{
10854
$this->streamReader = $streamReader;
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
<?php
22
namespace PHPJava\Compiler\Lang\Assembler;
33

4-
use PHPJava\Compiler\Builder\Attributes\Architects\Operation;
5-
use PHPJava\Compiler\Builder\Collection\ConstantPool;
6-
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
74
use PHPJava\Compiler\Lang\Assembler\Store\Store;
85
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
96
use PhpParser\Node;
107

118
/**
129
* <Only for use from `OperationManageable` trait>.
13-
* @method Operation getOperation()
14-
* @method AssemblerInterface setOperation(Operation $operation)
1510
* @method AssemblerInterface setStore(Store $store)
1611
* @method Store getStore()
1712
*/
1813
interface AssemblerInterface
1914
{
2015
public function __construct(Node $parser);
2116

22-
public static function factory(Node $node): AssemblerInterface;
23-
2417
/**
2518
* Must to set array or void.
2619
*
2720
* @return array|void
2821
*/
2922
public function assemble();
3023

31-
public function setParentAssembler(AssemblerInterface $parentAssembler): AssemblerInterface;
32-
33-
public function getConstantPoolFinder(): ConstantPoolFinder;
34-
35-
public function getConstantPool(): ConstantPool;
36-
3724
public function setStreamReader(StreamReaderInterface $stream): AssemblerInterface;
3825

3926
public function getStreamReader(): StreamReaderInterface;
40-
41-
public function setNamespace(?array $namespace): AssemblerInterface;
42-
43-
public function getNamespace(): ?array;
4427
}

src/Compiler/Lang/Assembler/ClassAssembler.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@
1212
use PHPJava\Compiler\Builder\Structures\Info\Utf8Info;
1313
use PHPJava\Compiler\Compiler;
1414
use PHPJava\Compiler\Lang\Assembler\Store\Store;
15+
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
1516
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
17+
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
1618
use PHPJava\Kernel\Resolvers\SDKVersionResolver;
1719
use PHPJava\Packages\java\lang\_Object;
1820
use PhpParser\Node;
1921

2022
/**
21-
* @property \PhpParser\Node\Stmt\Class_ $node
23+
* @property \PhpParser\Node\Stmt\Class_ $node
2224
*/
23-
class ClassAssembler extends AbstractAssembler implements AssemblerInterface
25+
class ClassAssembler extends AbstractAssembler
2426
{
27+
use OperationManageable;
2528
use ConstantPoolEnhanceable;
29+
use Bindable;
2630

2731
/**
2832
* @var Methods
@@ -42,12 +46,10 @@ public function assemble(): void
4246
$this->methods = new Methods();
4347

4448
foreach ($this->node->getMethods() as $method) {
45-
MethodAssembler::factory($method)
49+
$this->setOperation(new Operation())
4650
->setStore(new Store())
47-
->setOperation(new Operation())
48-
->setParentAssembler($this)
49-
->setStreamReader($this->getStreamReader())
50-
->setNamespace($this->getNamespace())
51+
->bindRequired(MethodAssembler::factory($method))
52+
->setCollection($this->methods)
5153
->assemble();
5254
}
5355

src/Compiler/Lang/Assembler/MethodAssembler.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
use PHPJava\Compiler\Builder\Attributes\Code;
66
use PHPJava\Compiler\Builder\Attributes\StackMapTable;
77
use PHPJava\Compiler\Builder\Collection\Attributes;
8+
use PHPJava\Compiler\Builder\Collection\Methods;
89
use PHPJava\Compiler\Builder\Method;
910
use PHPJava\Compiler\Builder\Signatures\Descriptor;
11+
use PHPJava\Compiler\Lang\Assembler\Processors\StatementProcessor;
12+
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
1013
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
1114
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\LocalVariableAssignable;
1215
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\LocalVariableLoadable;
1316
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
14-
use PHPJava\Compiler\Lang\Assembler\Traits\StatementParseable;
1517
use PHPJava\Kernel\Maps\OpCode;
1618
use PHPJava\Kernel\Types\_Void;
1719
use PHPJava\Utilities\ArrayTool;
1820

1921
/**
2022
* @method ClassAssembler getParentAssembler()
23+
* @method Methods getCollection()
2124
* @property \PhpParser\Node\Stmt\ClassMethod $node
2225
*/
23-
class MethodAssembler extends AbstractAssembler implements AssemblerInterface
26+
class MethodAssembler extends AbstractAssembler
2427
{
2528
use OperationManageable;
26-
use StatementParseable;
2729
use ConstantPoolEnhanceable;
2830
use LocalVariableAssignable;
2931
use LocalVariableLoadable;
32+
use Bindable;
3033

3134
protected $attribute;
3235

@@ -62,9 +65,7 @@ public function assemble(): void
6265
));
6366

6467
// Add to methods section
65-
$this
66-
->getParentAssembler()
67-
->getMethods()
68+
$this->getCollection()
6869
->add($method);
6970

7071
$this->attribute = new Attributes();
@@ -113,17 +114,12 @@ public function assemble(): void
113114
$operations = [];
114115
$defaultLocalVariableOperations = $this->getStore()->getAll();
115116

116-
$parsed = $this->parseStatement(
117-
$this->node->getStmts()
117+
ArrayTool::concat(
118+
$operations,
119+
...$this->bindRequired(StatementProcessor::factory())
120+
->execute($this->node->getStmts())
118121
);
119122

120-
if (!empty($parsed)) {
121-
ArrayTool::concat(
122-
$operations,
123-
...$parsed
124-
);
125-
}
126-
127123
$operations[] = \PHPJava\Compiler\Builder\Generator\Operation\Operation::create(
128124
OpCode::_return
129125
);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace PHPJava\Compiler\Lang\Assembler;
3+
4+
use PHPJava\Compiler\Builder\Attributes\Architects\Operation;
5+
use PHPJava\Compiler\Builder\Collection\ConstantPool;
6+
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
7+
use PHPJava\Compiler\Lang\Assembler\Processors\AbstractProcessor;
8+
use PHPJava\Compiler\Lang\Assembler\Store\Store;
9+
10+
/**
11+
* @method AbstractAssembler|AbstractProcessor setConstantPool(ConstantPool $constantPool)
12+
* @method ConstantPool getConstantPool()
13+
* @method AbstractAssembler|AbstractProcessor setNamespace(?array $namespace)
14+
* @method null|array getNamespace()
15+
* @method AbstractAssembler|AbstractProcessor setStore(Store $store)
16+
* @method Store getStore()
17+
* @method Operation getOperation()
18+
* @method AbstractAssembler|AbstractProcessor setOperation(Operation $operation)
19+
* @method AbstractAssembler|AbstractProcessor setConstantPoolFinder(ConstantPoolFinder $constantPoolFinder)
20+
* @method ConstantPoolFinder getConstantPoolFinder()
21+
*/
22+
interface ParameterServiceInterface
23+
{
24+
}

src/Compiler/Lang/Assembler/Processors/AbstractProcessor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PHPJava\Compiler\Builder\Attributes\Architects\Operation;
55
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
66
use PHPJava\Compiler\Lang\Assembler\Enhancer\ConstantPoolEnhancer;
7+
use PHPJava\Compiler\Lang\Assembler\ParameterServiceInterface;
78
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
89
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
910
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
@@ -17,19 +18,19 @@
1718
* @method ConstantPoolFinder getConstantPoolFinder()
1819
* @method StreamReaderInterface getStreamReader()
1920
*/
20-
abstract class AbstractProcessor implements ProcessorInterface
21+
abstract class AbstractProcessor implements ProcessorInterface, ParameterServiceInterface
2122
{
2223
use ConstantPoolManageable;
2324
use ConstantPoolEnhanceable;
2425
use StoreManageable;
2526
use NamespaceManageable;
2627
use Bindable;
2728

28-
public static function factory(): ProcessorInterface
29+
public static function factory(): self
2930
{
3031
static $instance;
3132
return $instance = $instance ?? new static();
3233
}
3334

34-
abstract public function execute(array $expressions, ?callable $callback = null): array;
35+
abstract public function execute(array $nodes, ?callable $callback = null): array;
3536
}

src/Compiler/Lang/Assembler/Processors/ExpressionProcessor.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NumberLoadable;
2020
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\Outputable;
2121
use PHPJava\Compiler\Lang\Assembler\Traits\OperationManageable;
22-
use PHPJava\Compiler\Lang\Assembler\Traits\ParentRecurseable;
2322
use PHPJava\Exceptions\AssembleStructureException;
2423
use PHPJava\Kernel\Maps\OpCode;
2524
use PHPJava\Kernel\Resolvers\MnemonicResolver;
2625
use PHPJava\Kernel\Types\_Int;
2726
use PHPJava\Packages\java\lang\Integer;
2827
use PHPJava\Utilities\ArrayTool;
28+
use PhpParser\Node;
2929

3030
class ExpressionProcessor extends AbstractProcessor implements ProcessorInterface
3131
{
3232
use OperationManageable;
3333
use OperationCalculatableFromNode;
34-
use ParentRecurseable;
3534
use ConstLoadableFromNode;
3635
use MagicConstLoadableFromNode;
3736
use StringLoadableFromNode;
@@ -48,11 +47,14 @@ class ExpressionProcessor extends AbstractProcessor implements ProcessorInterfac
4847
use LocalVariableAssignable;
4948
use LocalVariableLoadable;
5049

51-
public function execute(array $expressions, ?callable $callback = null): array
50+
/**
51+
* @param Node[] $nodes
52+
*/
53+
public function execute(array $nodes, ?callable $callback = null): array
5254
{
5355
$operations = [];
5456
$classType = null;
55-
foreach ($expressions as $expression) {
57+
foreach ($nodes as $expression) {
5658
$nodeType = get_class($expression);
5759
switch ($nodeType) {
5860
case \PhpParser\Node\Expr\Assign::class:
@@ -218,6 +220,17 @@ public function execute(array $expressions, ?callable $callback = null): array
218220
)
219221
);
220222
break;
223+
case \PhpParser\Node\Expr\BinaryOp\Smaller::class:
224+
ArrayTool::concat(
225+
$operations,
226+
...$this->assembleCalculateOperationFromNode(
227+
$expression->left,
228+
$expression->right,
229+
OpCode::_if_icmplt,
230+
$callback
231+
)
232+
);
233+
break;
221234
case \PhpParser\Node\Expr\BinaryOp\Identical::class:
222235
/**
223236
* @var \PhpParser\Node\Expr\BinaryOp\Identical $conditionNode

0 commit comments

Comments
 (0)