-
Notifications
You must be signed in to change notification settings - Fork 52
/
convert.php
94 lines (90 loc) · 2.52 KB
/
convert.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
#!/usr/bin/php
<?php
function showUsage()
{
echo 'Usage: convert.php --if <inputfile.php> --of <output> [-v | --verbose] [-h | --help]' , "\n";
}
$debug = false;
/* If there are arguments, cycle through em */
if ($_SERVER['argc'] > 4) {
for($i = 1; $i <= $_SERVER['argc']; ++$i) {
if(@$_SERVER['argv'][$i] == '-v' OR @$_SERVER['argv'][$i] == '--verbose') {
$debug = true;
}
if(@$_SERVER['argv'][$i] == '--if') {
$input = '';
$input = $_SERVER['argv'][$i + 1];
}
if(@$_SERVER['argv'][$i] == '--of') {
$output = '';
$output = $_SERVER['argv'][$i + 1];
$outputfile = $output . '.cpp';
}
if(@$_SERVER['argv'][$i] == '--help' OR @$_SERVER['argv'][$i] == '-h') {
showUsage();
}
}
} else {
showUsage();
die ("Too Few Arguments\n");
}
if(!(isset($input) && strlen(trim($input)) && is_file($input))) {
showUsage();
die ($input . " isn't a file\n");
}
if(!(isset($output) && strlen(trim($output)))) {
showUsage();
die ($output . "isn't a valid output file\n");
}
if($debug == true) {
define('DEBUG', true);
} else {
define('DEBUG', false);
}
if (substr(PHP_OS, 0, 3) == 'WIN') {
define('BINARYPHP_OS', 'Windows');
} else {
define('BINARYPHP_OS', 'Unix');
}
error_reporting(E_ALL);
if(!empty($_SERVER['DOCUMENT_ROOT']))
header('Content-Type: text/plain');
require_once 'tokenizer.php';
require_once 'tokenflow.php';
require_once 'functions.php';
$tokenizer = new Tokenizer();
$tokenizer->Parse($input);
$tokenizer->Strip();
$gen = new TokenGenerator($tokenizer);
list($code, $flags) = $gen->Convert();
if(DEBUG)
$flags .= ' -g';
$lines = explode("\n", $code);
if(DEBUG == true) {
foreach($lines as $line => $cod)
echo $line + 1, ': ', $cod, "\n";
}
$fp = fopen($output . '.cpp', 'wt');
fwrite($fp, $code);
fclose($fp);
echo "\n\n", 'Compiling:', "\n";
touch('log');
if(BINARYPHP_OS == 'Windows') {
$comp = 'cl -c /nologo /MT /W3 /GX /FD -DWIN32 /Fotest.obj ' . $outputfile . ' >> log';
echo $comp, "\n\n";
shell_exec($comp);
if(DEBUG==true)
$opt='-debug';
else
$opt='-$comp';
$release = 'link ' .$output . '.obj -nologo ' .$opt .' -incremental:no -opt:ref '.$flags.' -out:' . $output . ' >> log';
echo $comp, "\n\n";
shell_exec($comp);
} else {
$comp = 'g++ -o ' . $output . ' ' .$outputfile. ' '. $flags . ' >> log 2>> log';
echo $comp, "\n\n";
shell_exec($comp);
}
echo implode(null, file('log'));
unlink('log');
?>