/
inout.php
317 lines (291 loc) · 11.9 KB
/
inout.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
global $CFG;
define('MOODLE_INTERNAL', 1);
$CFG = new stdClass();
$CFG->dirroot = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
$CFG->libdir = $CFG->dirroot . '/lib';
require_once($CFG->dirroot .'/lib/classes/text.php');
require_once($CFG->dirroot .'/blocks/formal_langs/language_cpp_parseable_language.php');
require_once($CFG->dirroot .'/question/type/correctwriting/normalandcomp/classes/one_dim_node.php');
require_once($CFG->dirroot .'/question/type/correctwriting/normalandcomp/classes/k_dim_node.php');
require_once($CFG->dirroot .'/question/type/correctwriting/normalandcomp/classes/binary_node.php');
require_once($CFG->dirroot .'/question/type/correctwriting/normalandcomp/classes/operand.php');
/**
* Печать дерево в формат DOT
*
* \param [in] file указатель на выходный файл
* \param [in] curNode указатель на текущий узел
*
*/
function print_tree_to_dot($file, $curNode) {
static $globalid = 0; // следующий идентификатор для узлов
$id = $globalid; // идентификатор для данный узла
// инкремент общий индетификатор
++$globalid;
// печать определение для данного узла
if (is_a($curNode, 'qtype_correctwriting_operand')){
fwrite($file, $id.' [label = '.$curNode->name);
}
else {
fwrite($file, $id.' [label = "'.$curNode->get_label(get_class($curNode)).'"');
}
fwrite($file,"]\n");
// переход на сыновья
if (is_subclass_of($curNode, 'qtype_correctwriting_one_dim_node')) { // унарный узел
$next = $globalid;
print_tree_to_dot($file, $curNode->children);
fwrite($file, $id.' -> '.$next."\n");
}
elseif (is_subclass_of($curNode, 'qtype_correctwriting_k_dim_node')) { // двоичной узел
foreach ($curNode->childrens as $value) {
$next = $globalid;
print_tree_to_dot($file, $value);
fwrite($file, $id.' -> '.$next."\n");
}
}
elseif (is_subclass_of($curNode, 'qtype_correctwriting_binary_node')) { // k-dim узел
$next = $globalid;
print_tree_to_dot($file, $curNode->left);
fwrite($file, $id.' -> '.$next."\n");
$next = $globalid;
print_tree_to_dot($file, $curNode->right);
fwrite($file, $id.' -> '.$next."\n");
}
}
/**
* \brief Функция сравнения двух деревьев
*
* \param [in] tree1 указатель на узел первого дерева
* \param [in] tree2 указатель на узел второго дерева
* \param [in] isPrintDiff флаг: печать ли разницы двух деревьев
* \return true если два дерева равны, в противном случае false
*
*/
function is_tree_equal($tree1, $tree2, $isPrintDiff = FALSE) {
if (get_class($tree1) != get_class($tree2)) {
// печать разницы при тестировать
if($isPrintDiff){
echo "____ type of node diff\n";
echo "__________ result = ".get_class($tree1)."\n";
echo "__________ expected = ".get_class($tree2)."\n";
}
return false;
}
if (is_a($tree1, 'qtype_correctwriting_operand')){ // операнд
if ($tree1->name != $tree2->name){
// печать разницы при тестировать
if ($isPrintDiff){
echo "____ name of operand diff\n";
echo "__________ result = ".$tree1->name."\n";
echo "__________ expected = ".$tree2->name."\n";
}
return false;
}
if ($tree1->number != $tree2->number){
// печать разницы при тестировать
if ($isPrintDiff){
echo "____ number of operand diff\n";
echo "__________ result = ".$tree1->number."\n";
echo "__________ expected = ".$tree2->number."\n";
}
return false;
}
}
elseif (is_subclass_of($tree1, 'qtype_correctwriting_one_dim_node')) { // унарный
return is_tree_equal($tree1->children, $tree2->children, $isPrintDiff);
}
elseif (is_subclass_of($tree1, 'qtype_correctwriting_binary_node')) { // бинарный
return is_tree_equal($tree1->left, $tree2->left, $isPrintDiff) &&
is_tree_equal($tree1->right, $tree2->right, $isPrintDiff);
}
elseif (is_subclass_of($tree1, 'qtype_correctwriting_k_dim_node')) { // k-dim
$res = count($tree1->childrens) == count($tree2->childrens);
$i = 0;
while ($res && $i < count($tree1->childrens)) {
$res = $res && is_tree_equal($tree1->childrens[$i], $tree2->childrens[$i], $isPrintDiff);
$i ++;
}
return $res;
}
return TRUE;
}
function readExp($filename) {
//$filename = '..\testinput\expession.txt';
$file = fopen($filename, "r");
if ($file !== FALSE) {
$res = fgets($file);
fclose($file);
$res = rtrim($res);
}
else {
throw new Exception("file not found");
}
if (strlen($res) == 0) {
throw new Exception("no expession in file");
}
return $res;
}
/**
* Функция для создания экземплю узла по именю класса
*/
function getInstane($classname) {
switch ($classname) {
case 'identifier':
return new qtype_correctwriting_operand();
case 'expr_logical_or':
return new qtype_correctwriting_or_logic_operator();
case 'expr_logical_and':
return new qtype_correctwriting_and_logic_operator();
case 'expr_plus':
return new qtype_correctwriting_plus_operator();
case 'expr_multiply':
return new qtype_correctwriting_multi_operator();
case 'expr_logical_not':
return new qtype_correctwriting_not_logic_operator();
case 'expr_unary_minus':
return new qtype_correctwriting_unary_minus_operator();
case 'expr_dereference':
return new qtype_correctwriting_dereference_operator();
case 'expr_take_adress':
return new qtype_correctwriting_reference_operator();
case 'expr_function_call':
return new qtype_correctwriting_pow_function();
case 'expr_assign':
return new qtype_correctwriting_assign_operator();
case 'expr_minus':
return new qtype_correctwriting_minus_operator();
case 'expr_modulosign':
return new qtype_correctwriting_mod_operator();
case 'expr_division':
return new qtype_correctwriting_div_operator();
case 'expr_equal':
return new qtype_correctwriting_equal_operator();
case 'expr_notequal':
return new qtype_correctwriting_not_equal_operator();
case 'try_value_access':
return new qtype_correctwriting_mem_acc_operator();
case 'try_pointer_access':
return new qtype_correctwriting_pt_mem_acc_operator();
case 'expr_array_access':
return new qtype_correctwriting_subscript_operator();
case 'expr_rightshift':
return new qtype_correctwriting_shift_right_operator();
case 'expr_leftshift':
return new qtype_correctwriting_shift_left_operator();
case 'expr_lesser_or_equal':
return new qtype_correctwriting_less_equal_operator();
case 'expr_greater_or_equal':
return new qtype_correctwriting_greater_equal_operator();
case 'expr_lesser':
return new qtype_correctwriting_less_operator();
case 'expr_greater':
return new qtype_correctwriting_greater_operator();
case 'expr_plus_assign':
return new qtype_correctwriting_plus_assign_operator();
case 'expr_minus_assign':
return new qtype_correctwriting_minus_assign_operator();
case 'expr_multiply_assign':
return new qtype_correctwriting_multi_assign_operator();
case 'expr_division_assign':
return new qtype_correctwriting_div_assign_operator();
case 'expr_leftshift_assign':
return new qtype_correctwriting_shl_assign_operator();
case 'expr_rightshift_assign':
return new qtype_correctwriting_shr_assign_operator();
}
return null;
}
/**
* Функция для создания дерева от лексического дерева
*/
function filter_node($node) {
$curNode = null; // указатель на текущий узел
$nodetype = $node->type(); // тип узла
$leftchild = null; // указатель на левый сын
$rightchild = null; // указатель на правый сын
if ($nodetype === 'identifier' || $nodetype === 'numeric') { // операнд
$curNode = new qtype_correctwriting_operand();
$curNode->name = $node->value();
$curNode->treeinstring = $curNode->name;
if ($nodetype === 'numeric') {
$tmp = $curNode->name->string();
$curNode->number = ($tmp == intval($tmp)) ? intval($tmp) : doubleval($tmp);
}
}
elseif ($nodetype === 'expr_function_call') { // pow() функция
if ($node->childs[0]->value() != 'pow') {
$fname = $node->childs[0]->value();
throw new Exception("funtion $fname not suported");
}
$leftchild = filter_node($node->childs[2]->childs[0]);
$rightchild = filter_node($node->childs[2]->childs[2]);
$curNode = new qtype_correctwriting_pow_function();
$curNode->left = $leftchild;
$curNode->right = $rightchild;
$curNode->treeinstring = "pow() ".$leftchild->treeinstring." ".$rightchild->treeinstring;
}
elseif ($nodetype === 'expr_property_access'){ // операции . и ->
$leftchild = filter_node($node->childs[0]->childs[0]);
$rightchild = filter_node($node->childs[1]);
$curNode = getInstane($node->childs[0]->type());
$curNode->left = $leftchild;
$curNode->right = $rightchild;
$curNode->treeinstring = $node->childs[0]->childs[1]->value()." ".$leftchild->treeinstring." ".$rightchild->treeinstring;
}
elseif ($nodetype === 'expr_brackets') { // скобки
return filter_node($node->childs[1]);
}
elseif (in_array($nodetype, array('expr_logical_or', 'expr_logical_and', 'expr_plus', 'expr_multiply'))) {
// k-dim узел
$leftchild = filter_node($node->childs[0]);
$rightchild = filter_node($node->childs[2]);
$curNode = getInstane($nodetype);
array_push($curNode->childrens, $leftchild);
array_push($curNode->childrens, $rightchild);
$curNode->go_up_childrens();
$curNode->treeinstring = $node->childs[1]->value();
foreach ($curNode->childrens as $value) {
$curNode->treeinstring .= " ".$value->treeinstring;
}
}
elseif (in_array($nodetype, array('expr_logical_not', 'expr_unary_minus', 'expr_dereference', 'expr_take_adress'))) {
// one-dim узел
$curNode = getInstane($nodetype);
$curNode->children = filter_node($node->childs[1]);
$curNode->treeinstring = $node->childs[0]->value()." ".$curNode->children->treeinstring;
}
elseif (in_array($nodetype, array('expr_assign', 'expr_minus', 'expr_modulosign', 'expr_division',
'expr_notequal', 'expr_equal', 'expr_array_access', 'expr_rightshift',
'expr_leftshift', 'expr_lesser_or_equal', 'expr_greater_or_equal',
'expr_lesser', 'expr_greater', 'expr_plus_assign', 'expr_minus_assign',
'expr_multiply_assign', 'expr_division_assign', 'expr_leftshift_assign',
'expr_rightshift_assign'))) {
// двойчный узел
$leftchild = filter_node($node->childs[0]);
$rightchild = filter_node($node->childs[2]);
$curNode = getInstane($nodetype);
$curNode->left = $leftchild;
$curNode->right = $rightchild;
$curNode->treeinstring = $node->childs[1]->value()." ".$leftchild->treeinstring." ".$rightchild->treeinstring;
}
else {
// ошибка
throw new Exception("not supported operator ".$nodetype." ".$node->value());
}
return $curNode;
}
function build_tree($expression) {
// создать лексическое дерево
$lang = new block_formal_langs_language_cpp_parseable_language();
if (isset($donotstripcomments)) {
$lang->parser()->set_strip_comments(false);
}
$result = $lang->create_from_string($expression);
if (count($result->syntaxtree) > 1 || $result->syntaxtree[0]->type()==='operators') {
throw new Exception("Expression invalid");
}
// создать дерево свое
$root = filter_node($result->syntaxtree[0]);
return $root;
}
?>