about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-01-19 18:46:39 -0800
committerKartik K. Agaram <vc@akkartik.com>2016-01-19 18:46:39 -0800
commitbb33c5e89993338f6e554210e87b27a5220ead98 (patch)
tree59cab2622fa19c0aa0f625c989145dfa59d2b1dc
parent5002169370c854b6f532b420225b80e7ef18d6ad (diff)
downloadmu-bb33c5e89993338f6e554210e87b27a5220ead98.tar.gz
2573 - check product type of 'new'
I realize that my current doesn't allow nesting a:b:c linear type syntax
inside a dilated property. So you can't currently say:

  (recipe address:number)

Need to fix that at some point. Non-trivial since linear syntax is
oblivious to dilated syntax. I should probably make the dilated syntax
more fundamental and introduce it at an earlier layer.
-rw-r--r--038new.cc33
-rw-r--r--055parse_tree.cc9
-rw-r--r--070text.mu4
-rw-r--r--chessboard.mu2
4 files changed, 38 insertions, 10 deletions
diff --git a/038new.cc b/038new.cc
index 33902c3a..21273cfa 100644
--- a/038new.cc
+++ b/038new.cc
@@ -35,14 +35,39 @@ NEW,
 put(Recipe_ordinal, "new", NEW);
 :(before "End Primitive Recipe Checks")
 case NEW: {
+  const recipe& caller = get(Recipe, r);
   if (inst.ingredients.empty() || SIZE(inst.ingredients) > 2) {
-    raise_error << maybe(get(Recipe, r).name) << "'new' requires one or two ingredients, but got " << inst.to_string() << '\n' << end();
+    raise_error << maybe(caller.name) << "'new' requires one or two ingredients, but got " << inst.to_string() << '\n' << end();
     break;
   }
   // End NEW Check Special-cases
   reagent type = inst.ingredients.at(0);
   if (!is_mu_type_literal(type)) {
-    raise_error << maybe(get(Recipe, r).name) << "first ingredient of 'new' should be a type, but got " << type.original_string << '\n' << end();
+    raise_error << maybe(caller.name) << "first ingredient of 'new' should be a type, but got " << type.original_string << '\n' << end();
+    break;
+  }
+  if (inst.products.empty()) {
+    raise_error << maybe(caller.name) << "result of 'new' should never be ignored\n" << end();
+    break;
+  }
+  reagent product(inst.products.at(0));
+  canonize_type(product);
+  drop_address_from_type(product);
+  if (SIZE(inst.ingredients) > 1) {
+    // array allocation, so drop an 'array' as well from product.type
+    type_tree* tmp = product.type;
+    if (tmp->value != get(Type_ordinal, "array")) {
+      raise_error << maybe(caller.name) << "result of 'new' should start with 'address:array:...' in " << inst.to_string() << '\n' << end();
+      break;
+    }
+    product.type = tmp->right;
+    tmp->right = NULL;
+    delete tmp;
+  }
+  reagent expected_product("x:"+type.name);
+  // End Post-processing(expected_product) When Checking 'new'
+  if (!types_strictly_match(product, expected_product)) {
+    raise_error << maybe(caller.name) << "product of 'new' has incorrect type: " << inst.to_string() << '\n' << end();
     break;
   }
   break;
@@ -277,8 +302,8 @@ if (Free_list[size]) {
 recipe main [
   1:address:number <- new number:type
   abandon 1:address:number
-  2:address:number <- new number:type, 2  # different size
-  3:boolean <- equal 1:address:number, 2:address:number
+  2:address:array:number <- new number:type, 2  # different size
+  3:boolean <- equal 1:address:number, 2:address:array:number
 ]
 # no reuse
 +mem: storing 0 in location 3
diff --git a/055parse_tree.cc b/055parse_tree.cc
index 33537344..947093b9 100644
--- a/055parse_tree.cc
+++ b/055parse_tree.cc
@@ -71,10 +71,13 @@ container bar [
 
 :(scenario dilated_reagent_with_new)
 recipe main [
-  x:address:number <- new {(foo bar): type}
+  x:address:address:number <- new {(address number): type}
 ]
-# type isn't defined so size is meaningless, but at least we parse the type correctly
-+new: size of <"foo" : <"bar" : <>>> is 1
++new: size of <"address" : <"number" : <>>> is 1
 
+:(before "End Post-processing(expected_product) When Checking 'new'")
+expected_product.properties.at(0).second = parse_string_tree(expected_product.properties.at(0).second);
+delete expected_product.type;
+expected_product.type = new_type_tree(expected_product.properties.at(0).second);
 :(before "End Post-processing(type_name) When Converting 'new'")
 type_name = parse_string_tree(type_name);
diff --git a/070text.mu b/070text.mu
index 930b4587..37acdfd5 100644
--- a/070text.mu
+++ b/070text.mu
@@ -1080,7 +1080,7 @@ recipe split s:address:array:character, delim:character -> result:address:array:
   {
     empty?:boolean <- equal len, 0
     break-unless empty?
-    result <- new location:type, 0
+    result <- new {(address array character): type}, 0
     reply
   }
   # count #pieces we need room for
@@ -1095,7 +1095,7 @@ recipe split s:address:array:character, delim:character -> result:address:array:
     loop
   }
   # allocate space
-  result <- new location:type, count
+  result <- new {(address array character): type}, count
   # repeatedly copy slices start..end until delimiter into result[curr-result]
   curr-result:number <- copy 0
   start:number <- copy 0
diff --git a/chessboard.mu b/chessboard.mu
index 3347e253..f1bd8853 100644
--- a/chessboard.mu
+++ b/chessboard.mu
@@ -116,7 +116,7 @@ recipe new-board initial-position:address:array:character -> board:address:array
   correct-length?:boolean <- equal len, 64
   assert correct-length?, [chessboard had incorrect size]
   # board is an array of pointers to files; file is an array of characters
-  board <- new location:type, 8
+  board <- new {(address array character): type}, 8
   col:number <- copy 0
   {
     done?:boolean <- equal col, 8
391'>391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640