Revisión | 586efdb424a2688ab0dfd47709bc0f10b3cdfbb6 (tree) |
---|---|
Tiempo | 2018-11-17 23:28:20 |
Autor | dhrname <dhrname@user...> |
Commiter | dhrname |
Modify the ST_parse
@@ -419,6 +419,7 @@ static inline void free_stacklist (ST_Stack_List s) | ||
419 | 419 | } |
420 | 420 | |
421 | 421 | /*ST_parse関数 |
422 | + * N-木のfreetreeは、後でfreenode関数で解放するためのもの | |
422 | 423 | * トークン配列arrayでパーサ解析 |
423 | 424 | * 連結リストのlistは束縛変数の値を格納している |
424 | 425 | * 返り値はパースされたノードリスト |
@@ -431,7 +432,7 @@ static inline void free_stacklist (ST_Stack_List s) | ||
431 | 432 | * 2, y x.xλ |
432 | 433 | * 3, 「y x」(x) |
433 | 434 | * 4,「yをx」(x)*/ |
434 | -ST_Node ST_parse(ST_Token_Mode *array, size_t length, ST_Ordered_Pair *list) | |
435 | +ST_Node ST_parse(ST_Node freetree, ST_Token_Mode *array, size_t length, ST_Ordered_Pair *list) | |
435 | 436 | { |
436 | 437 | if (ST_isEmpty(list)) |
437 | 438 | { |
@@ -473,7 +474,10 @@ ST_Node ST_parse(ST_Token_Mode *array, size_t length, ST_Ordered_Pair *list) | ||
473 | 474 | ST_Node seq = ST_EMPTY_NODE; |
474 | 475 | |
475 | 476 | /*変数seqが空ノードに初期化されるので、この変数にキャッシュしておく*/ |
476 | - ST_Node seq_cache = ST_createNode(ST_EMPTY_NODE); | |
477 | + ST_Node seq_cache = ST_createNode(freetree); | |
478 | + | |
479 | + /*返り値となるルート*/ | |
480 | + ST_Node root = seq_cache; | |
477 | 481 | |
478 | 482 | for (int_fast32_t i=length-1; i>=0; i--) |
479 | 483 | { |
@@ -499,7 +503,7 @@ ST_Node ST_parse(ST_Token_Mode *array, size_t length, ST_Ordered_Pair *list) | ||
499 | 503 | case ST_KAGIKAKKO_END_TOKEN: |
500 | 504 | /*新しく子ノードとして追加したものの末子ノードが現在ノードになる |
501 | 505 | *その現在ノードをseq_cacheに入れる*/ |
502 | - seq_cache = ST_insertBefore(ST_getParentNode(seq), ST_createNode(ST_EMPTY_NODE), seq); | |
506 | + seq_cache = ST_insertBefore(seq_cache, ST_createNode(ST_EMPTY_NODE), seq); | |
503 | 507 | seq = ST_EMPTY_NODE; |
504 | 508 | break; |
505 | 509 | case ST_KAGIKAKKO_BEGIN_TOKEN: |
@@ -513,8 +517,15 @@ ST_Node ST_parse(ST_Token_Mode *array, size_t length, ST_Ordered_Pair *list) | ||
513 | 517 | } |
514 | 518 | } |
515 | 519 | free_stacklist(bound_vstacklist); |
516 | - | |
517 | - return seq; | |
520 | + | |
521 | + if (ST_isEmptyNode(seq)) | |
522 | + { | |
523 | + return ST_EMPTY_NODE; | |
524 | + } | |
525 | + else | |
526 | + { | |
527 | + return root; | |
528 | + } | |
518 | 529 | } |
519 | 530 | |
520 | 531 | /*ST_eval 関数 |
@@ -28,7 +28,7 @@ ST_Ordered_Pair* ST_tokenize(ST_Char*, ST_Token_Mode*); | ||
28 | 28 | /*ST_parse関数 |
29 | 29 | * トークン配列からパーサを作る |
30 | 30 | * 返却値は解析リスト*/ |
31 | -ST_Node ST_parse(ST_Token_Mode*, size_t, ST_Ordered_Pair*); | |
31 | +ST_Node ST_parse(ST_Node, ST_Token_Mode*, size_t, ST_Ordered_Pair*); | |
32 | 32 | |
33 | 33 | /*ベータ簡約を用いて、解析リストの評価を行う*/ |
34 | 34 | ST_Node ST_eval(ST_Ordered_Pair*, ST_Node); |
@@ -277,24 +277,26 @@ int main(int argc, char **argv) | ||
277 | 277 | |
278 | 278 | init_token(tokens); |
279 | 279 | length = sizeof(tokens)/sizeof(tokens[0]); |
280 | - ST_parse(tokens, length, ST_EMPTY); | |
281 | - assert(ST_isEmptyNode(ST_parse(tokens, length, ST_EMPTY))); | |
280 | + /*解放用のツリー*/ | |
281 | + ST_Node freetree = ST_createNode(ST_EMPTY_NODE); | |
282 | + ST_parse(freetree, tokens, length, ST_EMPTY); | |
283 | + assert(ST_isEmptyNode(ST_parse(freetree, tokens, length, ST_EMPTY))); | |
282 | 284 | varlist = ST_tokenize(u8"aBa愛", tokens); |
283 | - assert(ST_isEmptyNode(ST_parse(tokens, length, varlist))); | |
285 | + assert(ST_isEmptyNode(ST_parse(freetree, tokens, length, varlist))); | |
284 | 286 | ST_freelist(varlist); |
285 | 287 | init_token(tokens1); |
286 | 288 | length = sizeof(tokens1)/sizeof(tokens1[0]); |
287 | - ST_parse(tokens1, length, ST_EMPTY); | |
288 | - assert(ST_isEmptyNode(ST_parse(tokens1, length, ST_EMPTY))); | |
289 | + ST_parse(freetree, tokens1, length, ST_EMPTY); | |
290 | + assert(ST_isEmptyNode(ST_parse(freetree, tokens1, length, ST_EMPTY))); | |
289 | 291 | varlist = ST_tokenize(u8"aBa愛", tokens1); |
290 | - assert(ST_isEmptyNode(ST_parse(tokens1, length, varlist))); | |
292 | + assert(ST_isEmptyNode(ST_parse(freetree, tokens1, length, varlist))); | |
291 | 293 | ST_freelist(varlist); |
292 | 294 | |
293 | 295 | ST_Token_Mode tokens2[7] = {0}; |
294 | 296 | init_token(tokens2); |
295 | 297 | length = sizeof(tokens2) / sizeof(tokens2[0]); |
296 | 298 | varlist = ST_tokenize(u8"(愛あ)(愛)", tokens2); |
297 | - assert(ST_isEmptyNode(ST_parse(tokens2, length, varlist))); | |
299 | + assert(ST_isEmptyNode(ST_parse(freetree, tokens2, length, varlist))); | |
298 | 300 | assert(ST_first(ST_second(varlist)) != ST_first(varlist)); |
299 | 301 | ST_freelist(varlist); |
300 | 302 | init_token(tokens2); |
@@ -303,23 +305,23 @@ int main(int argc, char **argv) | ||
303 | 305 | tokens2[5] = 0; |
304 | 306 | tokens2[6] = 0; |
305 | 307 | varlist = ST_tokenize(u8"「愛あ」(愛)", tokens2); |
306 | - assert(!ST_isEmptyNode(ST_parse(tokens2, length, varlist))); | |
308 | + assert(!ST_isEmptyNode(ST_parse(freetree, tokens2, length, varlist))); | |
307 | 309 | assert(ST_first(ST_second(varlist)) == ST_first(varlist)); |
308 | 310 | ST_freelist(varlist); |
309 | - /* | |
311 | + | |
310 | 312 | ST_Token_Mode tokens3[12] = {0}; |
311 | 313 | length = sizeof(tokens3) / sizeof(tokens3[0]); |
312 | 314 | varlist = ST_tokenize(u8"「愛「愛」(愛)」(愛)", tokens3); |
313 | - assert(!ST_isEmptyNode(ST_parse(tokens3, length, varlist))); | |
315 | + assert(!ST_isEmptyNode(ST_parse(freetree, tokens3, length, varlist))); | |
314 | 316 | assert(ST_first(ST_second(varlist)) != ST_first(varlist)); |
315 | 317 | assert(ST_getItem(varlist, 1) != ST_first(varlist)); |
316 | 318 | assert(ST_getItem(varlist, 0) == ST_first(varlist)); |
317 | 319 | ST_freelist(varlist); |
318 | - | |
320 | + /* | |
319 | 321 | ST_Token_Mode tokens4[12] = {0}; |
320 | 322 | length = sizeof(tokens4) / sizeof(tokens4[0]); |
321 | 323 | varlist = ST_tokenize(u8"「愛」(愛)「愛」(愛)", tokens4); |
322 | - assert(!ST_isEmptyNode(ST_parse(tokens4, length, varlist))); | |
324 | + assert(!ST_isEmptyNode(ST_parse(freetree, tokens4, length, varlist))); | |
323 | 325 | assert(ST_first(ST_second(varlist)) == ST_first(varlist)); |
324 | 326 | assert(ST_getItem(varlist, 2) != ST_first(varlist)); |
325 | 327 | assert(ST_getItem(varlist, 3) != ST_first(varlist)); |
@@ -330,7 +332,7 @@ int main(int argc, char **argv) | ||
330 | 332 | ST_Token_Mode tokens5[12] = {0}; |
331 | 333 | length = sizeof(tokens5) / sizeof(tokens5[0]); |
332 | 334 | varlist = ST_tokenize(u8"「謎」(謎)「愛」(愛)", tokens5); |
333 | - assert(!ST_isEmptyNode(ST_parse(tokens5, length, varlist))); | |
335 | + assert(!ST_isEmptyNode(ST_parse(freetree, tokens5, length, varlist))); | |
334 | 336 | assert(ST_first(ST_second(varlist)) == ST_first(varlist)); |
335 | 337 | assert(ST_getItem(varlist, 2) != ST_first(varlist)); |
336 | 338 | assert(ST_getItem(varlist, 3) != ST_first(varlist)); |
@@ -340,7 +342,7 @@ int main(int argc, char **argv) | ||
340 | 342 | |
341 | 343 | init_token_length(tokens5, length); |
342 | 344 | varlist = ST_tokenize(u8"謎は「愛を愛する」(愛)", tokens5); |
343 | - ST_Node btr = ST_parse(tokens5, length, varlist); | |
345 | + ST_Node btr = ST_parse(freetree, tokens5, length, varlist); | |
344 | 346 | assert( ST_isEmpty(ST_getPreviousNode(ST_getPreviousNode(btr))) ); |
345 | 347 | assert( ST_isEmpty(ST_getPreviousNode(ST_getPreviousNode(ST_getLastChild(btr)))) ); |
346 | 348 | assert( ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0)); |
@@ -352,7 +354,7 @@ int main(int argc, char **argv) | ||
352 | 354 | |
353 | 355 | init_token_length(tokens5, length); |
354 | 356 | varlist = ST_tokenize(u8"「謎」(謎)「愛」(愛)", tokens5); |
355 | - btr = ST_parse(tokens5, length, varlist); | |
357 | + btr = ST_parse(freetree, tokens5, length, varlist); | |
356 | 358 | assert( ST_isEmpty(ST_getPreviousNode(ST_getPreviousNode(btr))) ); |
357 | 359 | assert( ST_isEmpty(ST_getPreviousNode(ST_getLastChild(btr))) ); |
358 | 360 | assert( ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0)); |
@@ -370,7 +372,7 @@ int main(int argc, char **argv) | ||
370 | 372 | |
371 | 373 | init_token_length(tokens5, length); |
372 | 374 | varlist = ST_tokenize(u8"「「謎」(謎)愛」(愛)", tokens5); |
373 | - btr = ST_parse(tokens5, length, varlist); | |
375 | + btr = ST_parse(freetree, tokens5, length, varlist); | |
374 | 376 | assert( ST_isEmpty(ST_getPreviousNode(btr)) ); |
375 | 377 | assert( ST_isEmpty(ST_getPreviousNode(ST_getPreviousNode(ST_getLastChild(btr)))) ); |
376 | 378 | assert( ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0)); |
@@ -385,7 +387,7 @@ int main(int argc, char **argv) | ||
385 | 387 | |
386 | 388 | init_token_length(tokens5, length); |
387 | 389 | varlist = ST_tokenize(u8"「涙を思考する言葉」(世界)", tokens5); |
388 | - btr = ST_parse(tokens5, length, varlist); | |
390 | + btr = ST_parse(freetree, tokens5, length, varlist); | |
389 | 391 | assert(ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
390 | 392 | lastvar = ST_getLastChild(btr); |
391 | 393 | assert( ST_first(ST_getNodeValue(lastvar)) == ST_getItem(varlist, 1) ); |
@@ -399,7 +401,7 @@ int main(int argc, char **argv) | ||
399 | 401 | ST_Token_Mode tokens6[24] = {0}; |
400 | 402 | length = sizeof(tokens6) / sizeof(tokens6[0]); |
401 | 403 | varlist = ST_tokenize(u8"「言葉」(世界)「「涙」(謎)愛」(謎)", tokens6); |
402 | - btr = ST_parse(tokens6, length, varlist); | |
404 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
403 | 405 | assert( ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
404 | 406 | assert( ST_getBoundVariable(ST_getNodeValue(ST_setChurchNumber(1, ST_getPreviousNode, btr))) == ST_getItem(varlist, 4) ); |
405 | 407 | lastvar = ST_getLastChild(btr); |
@@ -415,7 +417,7 @@ int main(int argc, char **argv) | ||
415 | 417 | |
416 | 418 | init_token_length(tokens6, length); |
417 | 419 | varlist = ST_tokenize(u8"「言葉」(半角括弧)「「涙」(謎)愛」(謎)", tokens6); |
418 | - btr = ST_parse(tokens6, length, varlist); | |
420 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
419 | 421 | assert(ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
420 | 422 | assert(ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(ST_getLastChild(btr)))) == ST_getItem(varlist, 2) ); |
421 | 423 | assert(ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(btr))) == ST_getItem(varlist, 4) ); |
@@ -424,7 +426,7 @@ int main(int argc, char **argv) | ||
424 | 426 | |
425 | 427 | init_token_length(tokens6, length); |
426 | 428 | varlist = ST_tokenize(u8"「「涙」(謎)言葉」(世界)「愛」(謎)", tokens6); |
427 | - btr = ST_parse(tokens6, length, varlist); | |
429 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
428 | 430 | assert(ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
429 | 431 | assert(ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(btr))) == ST_getItem(varlist, 2) ); |
430 | 432 | assert(ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(ST_getLastChild(ST_getPreviousNode(btr))))) == ST_getItem(varlist, 4) ); |
@@ -439,7 +441,7 @@ int main(int argc, char **argv) | ||
439 | 441 | |
440 | 442 | init_token_length(tokens6, length); |
441 | 443 | varlist = ST_tokenize(u8"「「「涙」(謎)言葉」(世界)愛」(謎)", tokens6); |
442 | - btr = ST_parse(tokens6, length, varlist); | |
444 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
443 | 445 | assert( ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
444 | 446 | lastvar = ST_getLastChild(btr); |
445 | 447 | assert( ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(lastvar))) == ST_getItem(varlist, 2) ); |
@@ -452,7 +454,7 @@ int main(int argc, char **argv) | ||
452 | 454 | |
453 | 455 | init_token_length(tokens6, length); |
454 | 456 | varlist = ST_tokenize(u8"「「涙」(謎)「言葉」(世界)愛」(謎)", tokens6); |
455 | - btr = ST_parse(tokens6, length, varlist); | |
457 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
456 | 458 | assert(ST_getBoundVariable(ST_getNodeValue(btr)) == ST_getItem(varlist, 0) ); |
457 | 459 | lastvar = ST_getLastChild(btr); |
458 | 460 | assert(ST_getBoundVariable(ST_getNodeValue(ST_getPreviousNode(lastvar))) == ST_getItem(varlist, 2) ); |
@@ -467,7 +469,7 @@ int main(int argc, char **argv) | ||
467 | 469 | length = sizeof(tokens2) / sizeof(tokens2[0]); |
468 | 470 | init_token_length(tokens2, length); |
469 | 471 | varlist = ST_tokenize(u8"「涙」(謎)", tokens2); |
470 | - btr = ST_parse(tokens2, length, varlist); | |
472 | + btr = ST_parse(freetree, tokens2, length, varlist); | |
471 | 473 | ST_Node evalresult = ST_eval(varlist, btr); |
472 | 474 | assert(btr != evalresult); |
473 | 475 | assert( ST_getBoundVariable(ST_getNodeValue(evalresult)) == ST_getItem(varlist, 0) ); |
@@ -480,7 +482,7 @@ int main(int argc, char **argv) | ||
480 | 482 | length = sizeof(tokens6) / sizeof(tokens6[0]); |
481 | 483 | init_token_length(tokens6, length); |
482 | 484 | varlist = ST_tokenize(u8"「海」(愛)とは、「涙」(謎)", tokens6); |
483 | - btr = ST_parse(tokens6, length, varlist); | |
485 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
484 | 486 | evalresult = ST_eval(varlist, btr); |
485 | 487 | assert( ST_getBoundVariable(ST_getNodeValue(evalresult)) == ST_getItem(varlist, 0) ); |
486 | 488 | assert( ST_first(ST_getNodeValue(ST_getLastChild(evalresult))) == ST_getItem(varlist, 1) ); |
@@ -494,7 +496,7 @@ int main(int argc, char **argv) | ||
494 | 496 | |
495 | 497 | init_token_length(tokens6, length); |
496 | 498 | varlist = ST_tokenize(u8"「海」(愛)とは、「謎」(謎)", tokens6); |
497 | - btr = ST_parse(tokens6, length, varlist); | |
499 | + btr = ST_parse(freetree, tokens6, length, varlist); | |
498 | 500 | evalresult = ST_eval(varlist, btr); |
499 | 501 | assert( ST_getBoundVariable(ST_getNodeValue(evalresult)) == ST_getItem(varlist, 2) ); |
500 | 502 | assert( ST_first(ST_getNodeValue(ST_getLastChild(evalresult))) == ST_getItem(varlist, 3) ); |