Changeset 14744 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
- Timestamp:
- 03/10/17 21:42:09 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
r14733 r14744 22 22 [PushExpression(StackType.Code, "CODE.DO")] 23 23 public class CodeDoExpression : StatelessExpression { 24 public override voidEval(IPushInterpreter interpreter) {24 public override bool Eval(IPushInterpreter interpreter) { 25 25 // not enough arguments on stack 26 if (interpreter.CodeStack.Count == 0) return ;26 if (interpreter.CodeStack.Count == 0) return false; 27 27 28 28 var codePopExpression = ExpressionTable.GetStatelessExpression<CodePopExpression>(); 29 29 interpreter.ExecStack.Push(codePopExpression, interpreter.CodeStack.Top); 30 31 return true; 30 32 } 31 33 } … … 36 38 [PushExpression(StackType.Code, "CODE.DO*")] 37 39 public class CodeDoXExpression : StatelessExpression { 38 public override voidEval(IPushInterpreter interpreter) {40 public override bool Eval(IPushInterpreter interpreter) { 39 41 // not enough arguments on stack 40 if (interpreter.CodeStack.Count == 0) return ;42 if (interpreter.CodeStack.Count == 0) return false; 41 43 42 44 var expression = interpreter.CodeStack.Pop(); 43 45 interpreter.ExecStack.Push(expression); 46 47 return true; 44 48 } 45 49 } … … 50 54 [PushExpression(StackType.Code, "CODE.NOOP")] 51 55 public class CodeNoopExpression : StatelessExpression { 52 public override voidEval(IPushInterpreter interpreter) {53 // nothing to do56 public override bool Eval(IPushInterpreter interpreter) { 57 return false; 54 58 } 55 59 } … … 61 65 [PushExpression(StackType.Code, "CODE.QUOTE")] 62 66 public class CodeQuoteExpression : StatelessExpression { 63 public override voidEval(IPushInterpreter interpreter) {67 public override bool Eval(IPushInterpreter interpreter) { 64 68 // not enough arguments on stack 65 if (interpreter.ExecStack.Count == 0) return ;69 if (interpreter.ExecStack.Count == 0) return false; 66 70 67 71 var expression = interpreter.ExecStack.Pop(); 68 72 interpreter.CodeStack.Push(expression); 73 74 return true; 69 75 } 70 76 } … … 77 83 [PushExpression(StackType.Code, "CODE.IF")] 78 84 public class CodeIfExpression : StatelessExpression { 79 public override voidEval(IPushInterpreter interpreter) {85 public override bool Eval(IPushInterpreter interpreter) { 80 86 // not enough arguments on stack 81 if ((interpreter.BooleanStack.Count == 0) || (interpreter.CodeStack.Count < 2)) return ;87 if ((interpreter.BooleanStack.Count == 0) || (interpreter.CodeStack.Count < 2)) return false; 82 88 83 89 var condition = interpreter.BooleanStack.Pop(); … … 85 91 86 92 interpreter.ExecStack.Push(condition ? items[0] : items[1]); 93 94 return true; 87 95 } 88 96 } … … 94 102 [PushExpression(StackType.Code, "CODE.APPEND")] 95 103 public class CodeAppendExpression : StatelessExpression { 96 public override voidEval(IPushInterpreter interpreter) {97 if (interpreter.CodeStack.Count < 2) return ;104 public override bool Eval(IPushInterpreter interpreter) { 105 if (interpreter.CodeStack.Count < 2) return false; 98 106 99 107 var first = interpreter.CodeStack.Pop(); … … 113 121 114 122 // if size > maxPointsInProgram this expressions results in a NOOP 115 if (size > interpreter.Configuration.MaxPointsInProgram) return ;123 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 116 124 117 125 result = new Expression[size]; … … 128 136 129 137 // if size > maxPointsInProgram this expressions results in a NOOP 130 if (size > interpreter.Configuration.MaxPointsInProgram) return ;138 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 131 139 132 140 result = new Expression[size]; … … 141 149 142 150 // if size > maxPointsInProgram this expressions results in a NOOP 143 if (size > interpreter.Configuration.MaxPointsInProgram) return ;151 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 144 152 145 153 result = new Expression[size]; … … 154 162 155 163 interpreter.CodeStack.SetTop(expression); 164 165 return true; 156 166 } 157 167 } … … 163 173 [PushExpression(StackType.Code, "CODE.ATOM")] 164 174 public class CodeAtomExpression : StatelessExpression { 165 public override voidEval(IPushInterpreter interpreter) {166 if (interpreter.CodeStack.Count == 0) return ;175 public override bool Eval(IPushInterpreter interpreter) { 176 if (interpreter.CodeStack.Count == 0) return false; 167 177 168 178 var expression = interpreter.CodeStack.Pop(); … … 170 180 171 181 interpreter.BooleanStack.Push(!isExpandExpression); 182 return true; 172 183 } 173 184 } … … 181 192 [PushExpression(StackType.Code, "CODE.CAR")] 182 193 public class CodeCarExpression : StatelessExpression { 183 public override voidEval(IPushInterpreter interpreter) {184 if ((interpreter.CodeStack.Count == 0) || (interpreter.CodeStack.Top.GetType() !=185 typeof(ExecExpandExpression))) return;194 public override bool Eval(IPushInterpreter interpreter) { 195 if ((interpreter.CodeStack.Count == 0) || 196 (interpreter.CodeStack.Top.GetType() != typeof(ExecExpandExpression))) return false; 186 197 187 198 var expand = interpreter.CodeStack.Top as ExecExpandExpression; 188 199 189 if (expand.State.IsEmpty) return ;200 if (expand.State.IsEmpty) return false; 190 201 var first = expand.State.Expressions[expand.State.Expressions.Count - 1]; 191 202 192 203 interpreter.CodeStack.SetTop(first); 204 return true; 193 205 } 194 206 } … … 202 214 [PushExpression(StackType.Code, "CODE.CDR")] 203 215 public class CodeCdrExpression : StatelessExpression { 204 public override voidEval(IPushInterpreter interpreter) {205 if (interpreter.CodeStack.Count == 0) return ;206 207 Ex pression expandExpression = null;216 public override bool Eval(IPushInterpreter interpreter) { 217 if (interpreter.CodeStack.Count == 0) return false; 218 219 ExecExpandExpression expandExpression; 208 220 var top = interpreter.CodeStack.Top; 209 221 … … 211 223 var expand = (ExecExpandExpression)top; 212 224 213 if (expand.State.IsEmpty) return ;225 if (expand.State.IsEmpty) return false; 214 226 var length = expand.State.Expressions.Count - 1; 215 227 var newExpressions = new Expression[length]; … … 223 235 224 236 interpreter.CodeStack.SetTop(expandExpression); 237 return true; 225 238 } 226 239 } … … 233 246 [PushExpression(StackType.Code, "CODE.CONS")] 234 247 public class CodeConsExpression : StatelessExpression { 235 public override voidEval(IPushInterpreter interpreter) {248 public override bool Eval(IPushInterpreter interpreter) { 236 249 if (interpreter.CodeStack.Count < 2) 237 return ;250 return false; 238 251 239 252 ExecExpandExpression result; … … 243 256 var size = first.State.Expressions.Count + 1; 244 257 245 if (size > interpreter.Configuration.MaxPointsInProgram) return ;258 if (size > interpreter.Configuration.MaxPointsInProgram) return false; 246 259 247 260 var expressions = new Expression[size]; … … 256 269 257 270 interpreter.CodeStack.SetTop(result); 271 return true; 258 272 } 259 273 } … … 269 283 [PushExpression(StackType.Code, "CODE.CONTAINER")] 270 284 public class CodeContainerExpression : StatelessExpression { 271 public override voidEval(IPushInterpreter interpreter) {285 public override bool Eval(IPushInterpreter interpreter) { 272 286 if ((interpreter.CodeStack.Count < 2) || 273 287 (interpreter.CodeStack[interpreter.CodeStack.Count - 2].GetType() != 274 typeof(ExecExpandExpression))) return ;288 typeof(ExecExpandExpression))) return false; 275 289 276 290 var target = interpreter.CodeStack.Pop(); … … 281 295 282 296 interpreter.CodeStack.SetTop(result); 297 return true; 283 298 } 284 299 … … 307 322 [PushExpression(StackType.Code, "CODE.CONTAINS")] 308 323 public class CodeContainsExpression : StatelessExpression { 309 public override voidEval(IPushInterpreter interpreter) {324 public override bool Eval(IPushInterpreter interpreter) { 310 325 if (interpreter.CodeStack.Count < 2 || 311 326 !interpreter.CodeStack[interpreter.CodeStack.Count - 2].CanExpand) 312 return ;327 return false; 313 328 314 329 var values = interpreter.CodeStack.Pop(2); … … 317 332 318 333 interpreter.BooleanStack.Push(contains); 334 335 return true; 319 336 } 320 337 } … … 327 344 [PushExpression(StackType.Code, "CODE.DEFINITION")] 328 345 public class CodeDefinitionExpression : StatelessExpression { 329 public override voidEval(IPushInterpreter interpreter) {346 public override bool Eval(IPushInterpreter interpreter) { 330 347 if ((interpreter.NameStack.Count == 0) || 331 !interpreter.CustomExpressions.ContainsKey(interpreter.NameStack.Top)) return ;348 !interpreter.CustomExpressions.ContainsKey(interpreter.NameStack.Top)) return false; 332 349 333 350 var name = interpreter.NameStack.Pop(); … … 335 352 336 353 interpreter.CodeStack.Push(definition); 354 355 return true; 337 356 } 338 357 } … … 363 382 [PushExpression(StackType.Code, "CODE.DISCREPANCY")] 364 383 public class CodeDiscrepancyExpression : StatelessExpression { 365 public override voidEval(IPushInterpreter interpreter) {366 if (interpreter.CodeStack.Count < 2) return ;384 public override bool Eval(IPushInterpreter interpreter) { 385 if (interpreter.CodeStack.Count < 2) return false; 367 386 368 387 var expressions = interpreter.CodeStack.Pop(2); … … 376 395 377 396 interpreter.IntegerStack.Push(discrepancy); 397 398 return true; 378 399 } 379 400 … … 418 439 [PushExpression(StackType.Code, "CODE.EXTRACT")] 419 440 public class CodeExtractExpression : StatelessExpression { 420 public override voidEval(IPushInterpreter interpreter) {441 public override bool Eval(IPushInterpreter interpreter) { 421 442 if (interpreter.IntegerStack.Count == 0 || 422 443 interpreter.CodeStack.Count == 0 || 423 444 !interpreter.CodeStack.Top.CanExpand) 424 return ;445 return false; 425 446 426 447 var expression = (ExecExpandExpression)interpreter.CodeStack.Top; … … 429 450 430 451 interpreter.CodeStack.SetTop(result); 452 453 return true; 431 454 } 432 455 } … … 437 460 [PushExpression(StackType.Code, "CODE.FROMBOOLEAN")] 438 461 public class CodeFromBooleanExpression : StatelessExpression { 439 public override voidEval(IPushInterpreter interpreter) {440 if (interpreter.BooleanStack.Count == 0) return ;462 public override bool Eval(IPushInterpreter interpreter) { 463 if (interpreter.BooleanStack.Count == 0) return false; 441 464 442 465 var value = interpreter.BooleanStack.Pop(); … … 444 467 445 468 interpreter.CodeStack.Push(expression); 469 470 return true; 446 471 } 447 472 } … … 452 477 [PushExpression(StackType.Code, "CODE.FROMFLOAT")] 453 478 public class CodeFromFloatExpression : StatelessExpression { 454 public override voidEval(IPushInterpreter interpreter) {455 if (interpreter.FloatStack.Count == 0) return ;479 public override bool Eval(IPushInterpreter interpreter) { 480 if (interpreter.FloatStack.Count == 0) return false; 456 481 457 482 var value = interpreter.FloatStack.Pop(); … … 459 484 460 485 interpreter.CodeStack.Push(expression); 486 487 return true; 461 488 } 462 489 } … … 467 494 [PushExpression(StackType.Code, "CODE.FROMINTEGER")] 468 495 public class CodeFromIntegerExpression : StatelessExpression { 469 public override voidEval(IPushInterpreter interpreter) {470 if (interpreter.IntegerStack.Count == 0) return ;496 public override bool Eval(IPushInterpreter interpreter) { 497 if (interpreter.IntegerStack.Count == 0) return false; 471 498 472 499 var value = interpreter.IntegerStack.Pop(); … … 474 501 475 502 interpreter.CodeStack.Push(expression); 503 504 return true; 476 505 } 477 506 } … … 482 511 [PushExpression(StackType.Code, "CODE.FROMNAME")] 483 512 public class CodeFromNameExpression : StatelessExpression { 484 public override voidEval(IPushInterpreter interpreter) {485 if (interpreter.NameStack.Count == 0) return ;513 public override bool Eval(IPushInterpreter interpreter) { 514 if (interpreter.NameStack.Count == 0) return false; 486 515 487 516 var value = interpreter.NameStack.Pop(); … … 489 518 490 519 interpreter.CodeStack.Push(expression); 520 521 return true; 491 522 } 492 523 } … … 498 529 [PushExpression(StackType.Code, "CODE.CODEINSERT")] 499 530 public class CodeInsertExpression : StatelessExpression { 500 public override voidEval(IPushInterpreter interpreter) {531 public override bool Eval(IPushInterpreter interpreter) { 501 532 if (interpreter.IntegerStack.Count == 0 || 502 533 interpreter.CodeStack.Count < 2 || 503 534 !interpreter.CodeStack.Top.CanExpand) 504 return ;535 return false; 505 536 506 537 var source = interpreter.CodeStack[interpreter.CodeStack.Count - 2]; … … 522 553 var result = new ExecExpandExpression(newExpressions); 523 554 interpreter.CodeStack.SetTop(result); 555 556 return true; 524 557 } 525 558 } … … 533 566 [PushExpression(StackType.Code, "CODE.LENGTH")] 534 567 public class CodeLengthExpression : StatelessExpression { 535 public override voidEval(IPushInterpreter interpreter) {536 if (interpreter.CodeStack.Count == 0) return ;568 public override bool Eval(IPushInterpreter interpreter) { 569 if (interpreter.CodeStack.Count == 0) return false; 537 570 538 571 var expression = interpreter.CodeStack.Pop(); … … 543 576 544 577 interpreter.IntegerStack.Push(count); 578 579 return true; 545 580 } 546 581 } … … 551 586 [PushExpression(StackType.Code, "CODE.LIST")] 552 587 public class CodeListExpression : StatelessExpression { 553 public override voidEval(IPushInterpreter interpreter) {554 if (interpreter.CodeStack.Count < 2) return ;588 public override bool Eval(IPushInterpreter interpreter) { 589 if (interpreter.CodeStack.Count < 2) return false; 555 590 556 591 var first = interpreter.CodeStack.Pop(); … … 559 594 560 595 interpreter.CodeStack.SetTop(expandExpression); 596 597 return true; 561 598 } 562 599 } … … 568 605 [PushExpression(StackType.Code, "CODE.MEMBER")] 569 606 public class CodeMemberExpression : StatelessExpression { 570 public override voidEval(IPushInterpreter interpreter) {571 if (interpreter.CodeStack.Count < 2) return ;607 public override bool Eval(IPushInterpreter interpreter) { 608 if (interpreter.CodeStack.Count < 2) return false; 572 609 573 610 var expressions = interpreter.CodeStack.Pop(2); … … 578 615 579 616 interpreter.BooleanStack.Push(contains); 617 618 return true; 580 619 } 581 620 } … … 588 627 [PushExpression(StackType.Code, "CODE.NTH")] 589 628 public class CodeNthExpression : StatelessExpression { 590 public override voidEval(IPushInterpreter interpreter) {591 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return ;629 public override bool Eval(IPushInterpreter interpreter) { 630 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return false; 592 631 593 632 var n = interpreter.IntegerStack.Pop(); 594 633 var expression = interpreter.CodeStack.Top; 595 634 596 Expression nthExpression = null;635 Expression nthExpression; 597 636 598 637 if (expression.CanExpand) { … … 612 651 613 652 interpreter.CodeStack.SetTop(nthExpression); 653 654 return true; 614 655 } 615 656 } … … 625 666 [PushExpression(StackType.Code, "CODE.NTHCDR")] 626 667 public class CodeNthCdrExpression : StatelessExpression { 627 public override voidEval(IPushInterpreter interpreter) {628 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return ;668 public override bool Eval(IPushInterpreter interpreter) { 669 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return false; 629 670 630 671 var n = interpreter.IntegerStack.Pop(); … … 648 689 649 690 interpreter.CodeStack.SetTop(nthExpression); 691 692 return true; 650 693 } 651 694 } … … 656 699 [PushExpression(StackType.Code, "CODE.NULL")] 657 700 public class CodeNullExpression : StatelessExpression { 658 public override voidEval(IPushInterpreter interpreter) {659 if (interpreter.CodeStack.Count == 0) return ;701 public override bool Eval(IPushInterpreter interpreter) { 702 if (interpreter.CodeStack.Count == 0) return false; 660 703 661 704 var result = interpreter.CodeStack.Pop().Equals(ExecExpandExpression.Empty); 662 663 705 interpreter.BooleanStack.Push(result); 706 707 return true; 664 708 } 665 709 } … … 671 715 [PushExpression(StackType.Code, "CODE.POSITION")] 672 716 public class CodePositionExpression : StatelessExpression { 673 public override voidEval(IPushInterpreter interpreter) {674 if (interpreter.CodeStack.Count < 2) return ;717 public override bool Eval(IPushInterpreter interpreter) { 718 if (interpreter.CodeStack.Count < 2) return false; 675 719 676 720 var expressions = interpreter.CodeStack.Pop(2); … … 688 732 689 733 interpreter.IntegerStack.Push(position); 734 735 return true; 690 736 } 691 737 } … … 697 743 [PushExpression(StackType.Code, "CODE.SIZE")] 698 744 public class CodeSizeExpression : StatelessExpression { 699 public override voidEval(IPushInterpreter interpreter) {700 if (interpreter.CodeStack.Count == 0) return ;745 public override bool Eval(IPushInterpreter interpreter) { 746 if (interpreter.CodeStack.Count == 0) return false; 701 747 702 748 var expression = interpreter.CodeStack.Pop(); … … 706 752 707 753 interpreter.IntegerStack.Push(points); 754 755 return true; 708 756 } 709 757 } … … 717 765 [PushExpression(StackType.Code, "CODE.SUBST")] 718 766 public class CodeSubstitutionExpression : StatelessExpression { 719 public override voidEval(IPushInterpreter interpreter) {767 public override bool Eval(IPushInterpreter interpreter) { 720 768 if ((interpreter.CodeStack.Count < 3) || (interpreter.CodeStack.Top.GetType() != 721 typeof(ExecExpandExpression))) return ;769 typeof(ExecExpandExpression))) return false; 722 770 723 771 var expressions = interpreter.CodeStack.Pop(2); … … 736 784 737 785 interpreter.CodeStack.SetTop(result); 786 787 return true; 738 788 } 739 789 }
Note: See TracChangeset
for help on using the changeset viewer.