/
sic.php
277 lines (224 loc) · 9.91 KB
/
sic.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
#note shitty noting: SYMTB : symbol table
# OPTB : operand table (without object code)
# OPTB_WOC : operand table (with object code)
# include my config
include_once("./config.php");
$figureDirectory = getcwd() . "\\Figure\\"; ## \\ -> \
$exitProgram = false;
function help_menu() {
echo "------------------------------------------------------------------------\n";
echo "1. Enter check for found out object code\n";
echo "2. Enter SIC or SIC/XE code file name (include extension)\n";
echo "3. Enter command statement type, default by SIC/XE\n";
echo "\n";
echo "\t Usage : check target_name [type]\n";
echo "\t Example : check Figure2.1.txt SIC\n";
echo "\t Example : check Figure2.5.txt SIC/XE\n";
echo "\t Example : check Figure2.5.csv\n";
echo "\n";
echo "\n";
echo "Other Usage :\n";
$menuOption = array(
array("show_progress", "show program progress"),
array("show_file" , "show files" ),
array("delete_result", "delete result folder"),
array("help" , "show help menu" ),
array("quit" , "Exit the program" ),
array("exit" , "Exit the program" ),
array("" , "Exit the program" ),
array("cls" , "Clear the scrren" ),
array("optab" ,"Display the optable" )
);
$menuMask = "%15s : %-s\n";
foreach($menuOption as $_menuOption) {
printf($menuMask, $_menuOption[0], $_menuOption[1]);
}
}
function show_progress() {
echo "------------------------------------------------------------------------\n";
echo "Program progress : " . PHP_EOL;
$readmeFile = dirname(__FILE__).'/readme.txt';
do_overwrite($readmeFile,false);
$handle = @fopen($readmeFile, "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo " " . $buffer ;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
}
function delete_result() {
delTree(RESULT_PATH);
}
function show_file() {
$figureDirectory = str_replace('/', '\\', dirname(__FILE__).FIGURE_PATH);
echo "\n";
echo "Current Figure directory : ".$figureDirectory."\n";
echo "The file in Figure direcotry \n";
echo "\n";
## just listing the program file...
$programFileArr = array("symtb.csv");
echo "Csv file : \n";
$line = 0;
foreach(glob(FIGURE_PATH."*.csv" ) as $fileName) {
++$line;
if($line %2 == 1) echo "\t";
## avoid print out program files..
if(in_array($fileName,$programFileArr) == false)
echo "".basename($fileName)."\t"; ## print out the file name in a path
if($line % 2 == 0)
echo "\n";
} // end foreach
echo "\n";
echo "Txt file : \n";
$line = 0;
foreach(glob(FIGURE_PATH."*.txt" ) as $fileName) {
++$line;
if($line %2 == 1) echo "\t";
## avoid print out program files..
if(in_array($fileName,$programFileArr) == false)
echo "".basename($fileName)."\t"; ## print out the file name in a path
if($line % 2 == 0) echo "\n";
} // end foreach
echo "\n";
} // end show_file function
help_menu();
while(!$exitProgram) {
createFolder();
echo "\n";
echo "Enter your command : \n";
## set fopen sources for system standard input
$handle = fopen ("php://stdin","r");
$command = fgets($handle);
$command = trim($command);
$command = preg_replace('/\s+/', ' ',$command); // Remove multiple whitespaces
$argument = explode(" ", $command);
if(count($argument) == 0) // avoid undefined variable
$argument[2] = $argument[1] = $argument[0] = "";
if(!isset($argument[2]))
$argument[2] = '';
$validCommand = array('quit', 'exit', 'show_progress', 'delete_result', 'help', 'show_file', 'cls', 'optab', '', 'check');
if(!in_array($argument[0], $validCommand)) {
echo "Invalid command\n";
help_menu();
}
else if($argument[0] == '' || $argument[0] == "quit" || $argument[0] =="exit") {
echo "";
echo "\n";
$exitProgram = true;
}
else if($argument[0] == 'show_progress') {show_progress(); }
else if($argument[0] == 'delete_result') {delete_result(); }
else if($argument[0] == 'help') {help_menu(); }
else if($argument[0] == 'show_file') {show_file(); }
else if($argument[0] == "cls") {system("cls"); }## $exitProgram = false;
else if($argument[0] == "optab") {display_optab(); } ## $exitProgram = false;
else if($argument[0] == 'check') { ## determine the opcode table
if(empty($argument[1])) {
echo "Empty target_name\n";
continue;
}
$ReadFile = new ReadFile; ## use my ReadFile class [ReadFile.php] to store the operand table into array and return it
## seem the defaultDirectory never finish it..
## let disable" absolution path " use in readFile
if( ($OPTB = $ReadFile->readFile(FIGURE_PATH.$argument[1])) == false) {
echo "Error in source file..\n";
}
else {
$path_parts = pathinfo($argument[1]);
$OUTPUT_LOG_PREFIX = date("Y-m-d-His")."_".$path_parts['filename'].".".$path_parts['extension']."_";;
echo "\n";
## ## convert operand table into operand table(with object code)
## ## fix into respond column...
$OPTB_WOC = optb_to_optb_woc ($OPTB);
if($argument[2] == '') $argument[2] = 'SIC/XE';
$GenerateObjectCode = new GenerateObjectCode($OPTB_WOC, $argument[2]);
echo "\n";
if($GenerateObjectCode->displayObjectCode() == false) {
echo "Error, check your operand table\n";
}
// ObjectCode
// Operation table with object code array to html
$OPTB_WOC = $GenerateObjectCode->getObjectCode();
$OPTB_passOne = $GenerateObjectCode->getAfterPassOne();
print_r_to_html(RESULT_PATH.$OUTPUT_LOG_PREFIX."optb-woc.html", $OPTB_WOC,"Operation Table with Object Code");
print_r_to_html(RESULT_PATH.$OUTPUT_LOG_PREFIX."optb-passOne.html", $OPTB_passOne,"Operation Table passOne");
// Symbol table array into to html
$SYMTAB = $GenerateObjectCode->getSYMTAB();
print_r_to_html(RESULT_PATH.$OUTPUT_LOG_PREFIX."symtab.html", $SYMTAB,"Symbol Table");
// generate result file
$OPTB_WOCtxtFile = RESULT_PATH.$OUTPUT_LOG_PREFIX."objectcode.txt";
$GenerateObjectCode->writeTxt($OPTB_WOCtxtFile);
$GenerateObjectCode->writeCsv(RESULT_PATH.$OUTPUT_LOG_PREFIX."optb-woc.csv");
$GenerateObjectCode->writeTxtPassOne(RESULT_PATH.$OUTPUT_LOG_PREFIX."optb-passOne.txt");
// operation code array to html
print_r_to_html(RESULT_PATH.$OUTPUT_LOG_PREFIX."optab.html", $OPTAB,"Operation Code Table");
echo "You can review the result in sub folder \"result\"\n";
// ObjectProgram
$GenerateObjectProgram = new GenerateObjectProgram($OPTB_WOC);
$obectProgramTxtFile = RESULT_PATH.$OUTPUT_LOG_PREFIX."object-program.txt";
$GenerateObjectProgram->writeTxt($obectProgramTxtFile);
//*********************************************************
// find out wrong object code
// my generate code compare to original object code
$originalCodeFile = ORIGIN_FILE_PATH.$path_parts['filename'].'-objectcode.txt';
if(file_exists($originalCodeFile)) {
$orignalCode = csv_to_array($originalCodeFile,"\t");
echo "Compare Target : " . ORIGIN_FILE_PATH.$path_parts['filename'].'-objectcode.txt'."\n";
$comKey = array("LOC", "LABEL", "OPCODE", "OPERAND", "OBJECTCODE");
$keep = true;
$OPTB_WOC = array_array_key_filter($OPTB_WOC, $comKey, $keep);
$orignalCode = array_array_key_filter($orignalCode, $comKey, $keep);
//print_r($OPTB_WOC);
//print_r($orignalCode);
//print_r( array_diff($OPTB_WOC, $orignalCode) );
echo "LOC Differentiations : \n";
print_r( array_diff(array_column($OPTB_WOC, 'LOC'), array_column($orignalCode, 'LOC')) );
echo "LABEL Differentiations : \n";
print_r( array_diff(array_column($OPTB_WOC, 'LABEL'), array_column($orignalCode, 'LABEL')) );
echo "OPCODE Differentiations : \n";
print_r( array_diff(array_column($OPTB_WOC, 'OPCODE'), array_column($orignalCode, 'OPCODE')) );
echo "OPERAND Differentiations : \n";
print_r( array_diff(array_column($OPTB_WOC, 'OPERAND'), array_column($orignalCode, 'OPERAND')) );
echo "OBJECTCODE Differentiations : \n";
print_r( array_diff(array_column($OPTB_WOC, 'OBJECTCODE'), array_column($orignalCode, 'OBJECTCODE')) );
// compare two files line by line
}
else {
echo PHP_EOL;
echo $originalCodeFile . PHP_EOL. 'File do not exist' . PHP_EOL;
echo 'program could not check the code whether correct or not' . PHP_EOL;
}
echo "-----------------Diff show me the differ : ------------------\n";
$originalOPTB_WOCFile = ORIGIN_FILE_PATH.$path_parts['filename'].'-objectcode.txt';
if(file_exists($originalOPTB_WOCFile)) {
$diffOPTB_WOC = Diff::compareFiles($originalOPTB_WOCFile, $OPTB_WOCtxtFile);
echo Diff::toString($diffOPTB_WOC);
create_file(RESULT_PATH.$OUTPUT_LOG_PREFIX."diff-OPTB_WOC.html", Diff::toHTML($diffOPTB_WOC));
echo "\n";
}
else {
echo $originalOPTB_WOCFile ."\n" . 'do not exist' . "\n";
}
$orignalObjectPFile = ORIGIN_FILE_PATH.$path_parts['filename'].'-objectprogram.txt';
if(file_exists($orignalObjectPFile)) {
$diffObjectP = Diff::compareFiles($orignalObjectPFile, $obectProgramTxtFile);
echo Diff::toString($diffObjectP);
create_file(RESULT_PATH.$OUTPUT_LOG_PREFIX."diff-ObjectProgram.html", Diff::toHTML($diffObjectP));
echo "\n";
}
else {
echo $orignalObjectPFile ."\n" . 'do not exist' . "\n";
}
echo "-------------------------Diff end ----------------------------\n";
compare_array_in_html_js(RESULT_PATH.$OUTPUT_LOG_PREFIX."compare_html_js.html", $OPTB_WOC, $orignalCode, "Compare Array", false);
} ## end ## print out OPTB
} ## end ## determine the opcode table
} ## end ## while
echo "\n";
echo "------------------------------------------------------------------------\n";
?>