Skip to content

Commit 3bcee03

Browse files
committed
Implement namespace syntax
1 parent b98e883 commit 3bcee03

30 files changed

+392
-165
lines changed

src/Compiler/Builder/Attributes/StackMapTable.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPJava\Compiler\Builder\Attributes\Architects\Frames\SameFrameExtended;
1010
use PHPJava\Compiler\Builder\Attributes\Architects\Frames\SameLocals1StackItemFrame;
1111
use PHPJava\Compiler\Builder\Finder\Result\ConstantPoolFinderResult;
12-
use PHPJava\Compiler\Builder\Structures\Info\StringInfo;
1312
use PHPJava\Compiler\Emulator\Accumulator;
1413
use PHPJava\Compiler\Emulator\Mnemonics\AbstractOperationCode;
1514
use PHPJava\Compiler\Lang\Assembler\Traits\Calculatable;
@@ -18,9 +17,7 @@
1817
use PHPJava\Core\JVM\Stream\BinaryWriter;
1918
use PHPJava\Core\PHPJava;
2019
use PHPJava\Exceptions\AssembleStructureException;
21-
use PHPJava\Kernel\Maps\OpCode;
2220
use PHPJava\Kernel\Maps\VerificationTypeTag;
23-
use PHPJava\Kernel\Mnemonics\OperationCodeInterface;
2421
use PHPJava\Kernel\Types\_Byte;
2522
use PHPJava\Kernel\Types\_Char;
2623
use PHPJava\Kernel\Types\_Float;
@@ -77,38 +74,6 @@ public function beginPrepare(): Attribute
7774
break;
7875
}
7976
}
80-
81-
foreach ($this->operations as $operation) {
82-
$mnemonic = Runtime::MNEMONIC_NAMESPACE . '\\' . $operation->getMnemonic();
83-
/**
84-
* @var OperationCodeInterface $opcodeInstance
85-
*/
86-
$opcodeInstance = new $mnemonic();
87-
88-
if (!$opcodeInstance->isStackingOperation()) {
89-
continue;
90-
}
91-
switch ($operation->getOpCode()) {
92-
case OpCode::_ldc:
93-
case OpCode::_ldc_w:
94-
case OpCode::_ldc2_w:
95-
/**
96-
* @var ConstantPoolFinderResult $finderResult
97-
*/
98-
$finderResult = $operation->getOperand(0)->getValue();
99-
switch (get_class($finderResult->getResult(false)->getEntry())) {
100-
case StringInfo::class:
101-
$this->getEnhancedConstantPool()
102-
->addClass(\PHPJava\Packages\java\lang\_String::class);
103-
break;
104-
default:
105-
throw new AssembleStructureException(
106-
'Unsupported entry type: ' . get_class($finderResult->getResult()->getEntry())
107-
);
108-
}
109-
break;
110-
}
111-
}
11277
return parent::beginPrepare();
11378
}
11479

src/Compiler/Emulator/Mnemonics/_ldc.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace PHPJava\Compiler\Emulator\Mnemonics;
33

44
use PHPJava\Compiler\Builder\Finder\Result\ConstantPoolFinderResult;
5+
use PHPJava\Compiler\Builder\Structures\Info\IntegerInfo;
56
use PHPJava\Compiler\Builder\Structures\Info\StringInfo;
67
use PHPJava\Exceptions\AssembleStructureException;
78
use PHPJava\Kernel\Maps\VerificationTypeTag;
@@ -17,6 +18,16 @@ public function execute(): void
1718
*/
1819
$finderResult = $this->operation->getOperand(0)->getValue();
1920
switch (get_class($finderResult->getResult()->getEntry())) {
21+
case IntegerInfo::class:
22+
$this->getEnhancedConstantPool()
23+
->addClass(\PHPJava\Packages\java\lang\Integer::class);
24+
25+
$this->accumulator->pushToOperandStack(
26+
[
27+
VerificationTypeTag::ITEM_Integer,
28+
]
29+
);
30+
break;
2031
case StringInfo::class:
2132
$this->getEnhancedConstantPool()
2233
->addClass(\PHPJava\Packages\java\lang\_String::class);

src/Compiler/Lang/Assembler/AbstractAssembler.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
namespace PHPJava\Compiler\Lang\Assembler;
33

44
use PHPJava\Compiler\Builder\Method;
5-
use PHPJava\Compiler\Lang\Assembler\Store\Store;
5+
use PHPJava\Compiler\Lang\Assembler\Traits\ClassAssemblerManageable;
66
use PHPJava\Compiler\Lang\Assembler\Traits\CollectionManageable;
77
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
88
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
9+
use PHPJava\Compiler\Lang\Assembler\Traits\MethodAssemblerManageable;
910
use PHPJava\Compiler\Lang\Assembler\Traits\StoreManageable;
10-
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
11+
use PHPJava\Compiler\Lang\Assembler\Traits\StreamManageable;
1112
use PhpParser\Node;
1213

1314
abstract class AbstractAssembler implements AssemblerInterface, ParameterServiceInterface
@@ -16,22 +17,15 @@ abstract class AbstractAssembler implements AssemblerInterface, ParameterService
1617
use StoreManageable;
1718
use NamespaceManageable;
1819
use CollectionManageable;
20+
use StreamManageable;
21+
use MethodAssemblerManageable;
22+
use ClassAssemblerManageable;
1923

2024
/**
2125
* @var Node
2226
*/
2327
protected $node;
2428

25-
/**
26-
* @var StreamReaderInterface
27-
*/
28-
protected $streamReader;
29-
30-
/**
31-
* @var Store
32-
*/
33-
protected $store;
34-
3529
/**
3630
* @var Method
3731
*/
@@ -48,15 +42,4 @@ public function __construct(Node $node)
4842
}
4943

5044
abstract public function assemble();
51-
52-
public function setStreamReader(StreamReaderInterface $streamReader): AssemblerInterface
53-
{
54-
$this->streamReader = $streamReader;
55-
return $this;
56-
}
57-
58-
public function getStreamReader(): StreamReaderInterface
59-
{
60-
return $this->streamReader;
61-
}
6245
}

src/Compiler/Lang/Assembler/AssemblerInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace PHPJava\Compiler\Lang\Assembler;
33

44
use PHPJava\Compiler\Lang\Assembler\Store\Store;
5-
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
65
use PhpParser\Node;
76

87
/**
@@ -20,8 +19,4 @@ public function __construct(Node $parser);
2019
* @return array|void
2120
*/
2221
public function assemble();
23-
24-
public function setStreamReader(StreamReaderInterface $stream): AssemblerInterface;
25-
26-
public function getStreamReader(): StreamReaderInterface;
2722
}

src/Compiler/Lang/Assembler/Bundler/PHPStandardClass.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public function assemble(): void
106106
(new ClassFileStructure())
107107
->setMinorVersion($minorVersion)
108108
->setMajorVersion($majorVersion)
109+
->setAccessFlags(
110+
(new \PHPJava\Compiler\Builder\Signatures\ClassAccessFlag())
111+
->enableSuper()
112+
->enablePublic()
113+
->make()
114+
)
109115
->setThisClass(
110116
$this->getEnhancedConstantPool()
111117
->findClass($className)
@@ -151,14 +157,9 @@ public function assemble(): void
151157
);
152158

153159
$compiler->compile(
154-
fopen(
155-
sprintf(
156-
'%s/%s.class',
157-
$this->getStreamReader()->getDistributeDirectory(),
158-
$className
159-
),
160-
'w+'
161-
)
160+
$this
161+
->getStreamReader()
162+
->getDistributeStreamByClassPath($className)
162163
);
163164
}
164165
}

src/Compiler/Lang/Assembler/ClassAssembler.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ class ClassAssembler extends AbstractAssembler
3737

3838
public function assemble(): void
3939
{
40-
$this->className = $this->node->name->name;
40+
$this->className = implode(
41+
'.',
42+
array_merge(
43+
$this->getNamespace() ?? [],
44+
[$this->node->name->name]
45+
)
46+
);
4147
[$majorVersion, $minorVersion] = SDKVersionResolver::resolveByVersion(8);
4248

4349
$this->constantPool = new ConstantPool();
@@ -92,7 +98,7 @@ public function assemble(): void
9298
$this->getConstantPoolFinder()
9399
->find(
94100
Utf8Info::class,
95-
$this->className . '.php'
101+
$this->node->name->name . '.php'
96102
)
97103
)
98104
->beginPrepare()
@@ -101,20 +107,17 @@ public function assemble(): void
101107
)
102108
->setConstantPool(
103109
$this->constantPool
104-
->add(Utf8Info::factory($this->className . '.php'))
110+
->add(Utf8Info::factory($this->node->name->name . '.php'))
105111
->toArray()
106112
)
107113
);
108114

109115
$compiler->compile(
110-
fopen(
111-
sprintf(
112-
'%s/%s.class',
113-
$this->getStreamReader()->getDistributeDirectory(),
116+
$this
117+
->getStreamReader()
118+
->getDistributeStreamByClassPath(
114119
$this->className
115-
),
116-
'w+'
117-
)
120+
)
118121
);
119122
}
120123

src/Compiler/Lang/Assembler/MethodAssembler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public function assemble(): void
136136
ArrayTool::concat(
137137
$operations,
138138
...$this->bindParameters(StatementProcessor::factory())
139+
->setMethodAssembler($this)
140+
->setClassAssembler($this->getClassAssembler())
139141
->execute($this->node->getStmts())
140142
);
141143

src/Compiler/Lang/Assembler/ParameterServiceInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* @method AbstractAssembler|AbstractProcessor setOperation(Operation $operation)
1919
* @method AbstractAssembler|AbstractProcessor setConstantPoolFinder(ConstantPoolFinder $constantPoolFinder)
2020
* @method ConstantPoolFinder getConstantPoolFinder()
21+
* @method AbstractAssembler|AbstractProcessor setMethodAssembler(MethodAssembler $methodAssembler)
22+
* @method MethodAssembler getMethodAssembler()
23+
* @method AbstractAssembler|AbstractProcessor setClassAssembler(ClassAssembler $methodAssembler)
24+
* @method ClassAssembler getClassAssembler()
2125
*/
2226
interface ParameterServiceInterface
2327
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
use PHPJava\Compiler\Lang\Assembler\Enhancer\ConstantPoolEnhancer;
77
use PHPJava\Compiler\Lang\Assembler\ParameterServiceInterface;
88
use PHPJava\Compiler\Lang\Assembler\Traits\Bindable;
9+
use PHPJava\Compiler\Lang\Assembler\Traits\ClassAssemblerManageable;
910
use PHPJava\Compiler\Lang\Assembler\Traits\ConstantPoolManageable;
1011
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\ConstantPoolEnhanceable;
1112
use PHPJava\Compiler\Lang\Assembler\Traits\Enhancer\Operation\NamespaceManageable;
13+
use PHPJava\Compiler\Lang\Assembler\Traits\MethodAssemblerManageable;
1214
use PHPJava\Compiler\Lang\Assembler\Traits\StoreManageable;
15+
use PHPJava\Compiler\Lang\Assembler\Traits\StreamManageable;
1316
use PHPJava\Compiler\Lang\Stream\StreamReaderInterface;
1417

1518
/**
@@ -25,6 +28,9 @@ abstract class AbstractProcessor implements ProcessorInterface, ParameterService
2528
use StoreManageable;
2629
use NamespaceManageable;
2730
use Bindable;
31+
use StreamManageable;
32+
use MethodAssemblerManageable;
33+
use ClassAssemblerManageable;
2834

2935
public static function factory(): self
3036
{

src/Compiler/Lang/Assembler/Processors/Traits/MagicConstLoadableFromNode.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ private function assembleLoadMagicConstFromNode(Node $expression, ?string &$clas
3030
/**
3131
* @var ClassAssembler $class
3232
*/
33-
$class = $this->recurseParentUntilBy(
34-
ClassAssembler::class
35-
);
33+
$class = $this->getClassAssembler();
3634

3735
$this->getEnhancedConstantPool()
3836
->addString($class->getClassName());
@@ -52,16 +50,12 @@ private function assembleLoadMagicConstFromNode(Node $expression, ?string &$clas
5250
/**
5351
* @var ClassAssembler $class
5452
*/
55-
$class = $this->recurseParentUntilBy(
56-
ClassAssembler::class
57-
);
53+
$class = $this->getClassAssembler();
5854

5955
/**
6056
* @var MethodAssembler $method
6157
*/
62-
$method = $this->recurseParentUntilBy(
63-
MethodAssembler::class
64-
);
58+
$method = $this->getMethodAssembler();
6559

6660
$methodName = $class->getClassName() . '::' . $method->getMethodName();
6761

@@ -132,9 +126,7 @@ private function assembleLoadMagicConstFromNode(Node $expression, ?string &$clas
132126
/**
133127
* @var MethodAssembler $method
134128
*/
135-
$method = $this->recurseParentUntilBy(
136-
MethodAssembler::class
137-
);
129+
$method = $this->getMethodAssembler();
138130

139131
$methodName = $method->getMethodName();
140132

0 commit comments

Comments
 (0)