-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntryMethod.php
100 lines (91 loc) · 2.74 KB
/
EntryMethod.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class EntryMethod
{
/** @var string */
private $fileName;
/** @var string */
private $dir;
/** @var string */
private $methodCode;
/** @var string */
private $cMethodCode;
/**
* @param string $fileName
* @param string $methodCode
* @param string $dir
*/
public function __construct(string $fileName, string $methodCode, string $dir)
{
$this->fileName = $fileName;
if (!is_file($fileName)) {
throw new Exception('文件不存在' . $fileName);
}
$this->methodCode = $methodCode;
$this->dir = $dir;
}
/**
* @return string
*/
public function getCCodeDefineString(): string
{
if (empty($this->methodCode)) {
return '';
}
$this->parse();
return $this->cMethodCode;
}
/**
* @return void
*/
private function parse()
{
$uniqSourceLowerCase = uniqSourceLowerCase($this->fileName);
$coverCCode = $this->parseFunctionSource2C();
$this->cMethodCode = <<<MAIN_FUNCTION
int main(void) {
struct ${uniqSourceLowerCase}_attributes *this = (struct ${uniqSourceLowerCase}_attributes *)malloc(sizeof(struct ${uniqSourceLowerCase}_attributes));
if (NULL != this) {
$coverCCode
free(this);
}
return 0;
}
MAIN_FUNCTION;
}
/**
* @return string
*/
private function parseFunctionSource2C(): string
{
$userDefineFunctionMapping = $defaultTypesMapping = $userDefineTypeMapping = [];
$defaultTypes = getDefaultTypes();
foreach (getTypesToCTypeMapping($this->fileName, $this->dir) as $functionUse => $realCType) {
if (in_array($functionUse, $defaultTypes)) {
$defaultTypesMapping[$functionUse] = $realCType;
} else {
$userDefineTypeMapping[$functionUse] = $realCType;
}
}
foreach (getTypesToCFunctionPrefixMapping($this->fileName, $this->dir) as $functionUse => $realCFuncPrefix) {
$userDefineFunctionMapping[$functionUse] = $realCFuncPrefix;
}
$functionBodyUserCode = '';
foreach (explode(PHP_EOL, $this->methodCode) as $line) {
if (0 === strpos($line, ' ')) {
if (!empty($functionBodyUserCode)) {
$functionBodyUserCode .= PHP_EOL;
}
$functionBodyUserCode .= ' ' . $line;
}
}
$replacer = new VariableNameReplacer(
$functionBodyUserCode,
$userDefineTypeMapping,
$defaultTypesMapping,
$userDefineFunctionMapping,
$this->fileName,
$this->dir
);
return $replacer->getReplacedCode();
}
}