Changeset 14952 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
- Timestamp:
- 05/10/17 11:23:05 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/CodeExpressions.cs
r14875 r14952 10 10 using System.Linq; 11 11 using Attributes; 12 13 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 14 12 15 using Interpreter; 13 16 using Stack; … … 19 22 /// </summary> 20 23 [PushExpression(StackTypes.Code, "CODE.DO", StackTypes.Exec)] 24 [StorableClass] 21 25 public class CodeDoExpression : StatelessExpression { 22 public override bool Eval(IInternalPushInterpreter interpreter) { 23 // not enough arguments on stack 24 if (interpreter.CodeStack.Count == 0) return false; 25 26 public CodeDoExpression() { } 27 [StorableConstructor] 28 public CodeDoExpression(bool deserializing) : base(deserializing) { } 29 30 public override bool IsNoop(IInternalPushInterpreter interpreter) { 31 return interpreter.CodeStack.IsEmpty; 32 } 33 34 public override void Eval(IInternalPushInterpreter interpreter) { 26 35 var codePopExpression = ExpressionTable.GetStatelessExpression<CodePopExpression>(); 27 36 interpreter.ExecStack.Push(codePopExpression, interpreter.CodeStack.Top); 28 29 return true;30 37 } 31 38 } … … 35 42 /// </summary> 36 43 [PushExpression(StackTypes.Code, "CODE.DO*", StackTypes.Exec)] 44 [StorableClass] 37 45 public class CodeDoXExpression : StatelessExpression { 38 public override bool Eval(IInternalPushInterpreter interpreter) { 39 // not enough arguments on stack 40 if (interpreter.CodeStack.Count == 0) return false; 41 46 public CodeDoXExpression() { } 47 [StorableConstructor] 48 public CodeDoXExpression(bool deserializing) : base(deserializing) { } 49 50 public override bool IsNoop(IInternalPushInterpreter interpreter) { 51 return interpreter.CodeStack.IsEmpty; 52 } 53 54 public override void Eval(IInternalPushInterpreter interpreter) { 42 55 var expression = interpreter.CodeStack.Pop(); 43 56 interpreter.ExecStack.Push(expression); 44 57 } 58 } 59 60 /// <summary> 61 /// Does nothing. 62 /// </summary> 63 [PushExpression(StackTypes.Code, "CODE.NOOP")] 64 [StorableClass] 65 public class CodeNoopExpression : StatelessExpression { 66 public CodeNoopExpression() { } 67 [StorableConstructor] 68 public CodeNoopExpression(bool deserializing) : base(deserializing) { } 69 70 public override bool IsNoop(IInternalPushInterpreter interpreter) { 45 71 return true; 46 72 } 47 } 48 49 /// <summary> 50 /// Does nothing. 51 /// </summary> 52 [PushExpression(StackTypes.Code, "CODE.NOOP")] 53 public class CodeNoopExpression : StatelessExpression { 54 public override bool Eval(IInternalPushInterpreter interpreter) { 55 return false; 73 74 public override void Eval(IInternalPushInterpreter interpreter) { 75 // do nothing 56 76 } 57 77 } … … 62 82 /// </summary> 63 83 [PushExpression(StackTypes.Code, "CODE.QUOTE", StackTypes.Exec)] 84 [StorableClass] 64 85 public class CodeQuoteExpression : StatelessExpression { 65 public override bool Eval(IInternalPushInterpreter interpreter) { 66 // not enough arguments on stack 67 if (interpreter.ExecStack.Count == 0) return false; 68 86 public CodeQuoteExpression() { } 87 [StorableConstructor] 88 public CodeQuoteExpression(bool deserializing) : base(deserializing) { } 89 90 public override bool IsNoop(IInternalPushInterpreter interpreter) { 91 return interpreter.ExecStack.IsEmpty; 92 } 93 94 public override void Eval(IInternalPushInterpreter interpreter) { 69 95 var expression = interpreter.ExecStack.Pop(); 70 96 interpreter.CodeStack.Push(expression); 71 72 return true;73 97 } 74 98 } … … 80 104 /// </summary> 81 105 [PushExpression(StackTypes.Code, "CODE.IF", StackTypes.Exec | StackTypes.Boolean)] 106 [StorableClass] 82 107 public class CodeIfExpression : StatelessExpression { 83 public override bool Eval(IInternalPushInterpreter interpreter) { 84 // not enough arguments on stack 85 if (interpreter.BooleanStack.Count == 0 || 86 interpreter.CodeStack.Count < 2) 87 return false; 88 108 public CodeIfExpression() { } 109 [StorableConstructor] 110 public CodeIfExpression(bool deserializing) : base(deserializing) { } 111 112 public override bool IsNoop(IInternalPushInterpreter interpreter) { 113 return interpreter.BooleanStack.IsEmpty || interpreter.CodeStack.Count < 2; 114 } 115 116 public override void Eval(IInternalPushInterpreter interpreter) { 89 117 var condition = interpreter.BooleanStack.Pop(); 90 118 var first = interpreter.CodeStack[1]; … … 93 121 interpreter.CodeStack.Remove(2); 94 122 interpreter.ExecStack.Push(condition ? first : second); 95 96 return true;97 123 } 98 124 } … … 103 129 /// </summary> 104 130 [PushExpression(StackTypes.Code, "CODE.APPEND")] 131 [StorableClass] 105 132 public class CodeAppendExpression : StatelessExpression { 106 public override bool Eval(IInternalPushInterpreter interpreter) { 107 if (interpreter.CodeStack.Count < 2) return false; 108 133 public CodeAppendExpression() { } 134 [StorableConstructor] 135 public CodeAppendExpression(bool deserializing) : base(deserializing) { } 136 137 public override bool IsNoop(IInternalPushInterpreter interpreter) { 138 return interpreter.CodeStack.Count < 2; 139 } 140 141 public override void Eval(IInternalPushInterpreter interpreter) { 109 142 var first = interpreter.CodeStack.Top; 110 143 var second = interpreter.CodeStack[1]; … … 116 149 firstProgram = (PushProgram)first; 117 150 118 if (firstProgram.Depth > interpreter.Configuration.MaxDepth) return false;151 if (firstProgram.Depth > interpreter.Configuration.MaxDepth) return; 119 152 120 153 if (second.IsProgram) { … … 123 156 if (secondProgram.Depth > interpreter.Configuration.MaxDepth || 124 157 firstProgram.Count + secondProgram.Count > interpreter.Configuration.MaxPointsInProgram) 125 return false;126 } else if (firstProgram.Count + 1 > interpreter.Configuration.MaxPointsInProgram) return false;158 return; 159 } else if (firstProgram.Count + 1 > interpreter.Configuration.MaxPointsInProgram) return; 127 160 } else if (second.IsProgram) { 128 161 secondProgram = (PushProgram)second; 129 162 130 163 if (secondProgram.Depth > interpreter.Configuration.MaxDepth 131 || secondProgram.Count + 1 > interpreter.Configuration.MaxPointsInProgram) return false;164 || secondProgram.Count + 1 > interpreter.Configuration.MaxPointsInProgram) return; 132 165 } else if (interpreter.Configuration.MaxPointsInProgram <= 2) { 133 return false;166 return; 134 167 } 135 168 … … 153 186 154 187 interpreter.CodeStack.SetTop(result); 155 156 return true;157 188 } 158 189 } … … 163 194 /// </summary> 164 195 [PushExpression(StackTypes.Code, "CODE.ATOM", StackTypes.Boolean)] 196 [StorableClass] 165 197 public class CodeAtomExpression : StatelessExpression { 166 public override bool Eval(IInternalPushInterpreter interpreter) { 167 if (interpreter.CodeStack.Count == 0) return false; 168 198 public CodeAtomExpression() { } 199 [StorableConstructor] 200 public CodeAtomExpression(bool deserializing) : base(deserializing) { } 201 202 public override bool IsNoop(IInternalPushInterpreter interpreter) { 203 return interpreter.CodeStack.IsEmpty; 204 } 205 206 public override void Eval(IInternalPushInterpreter interpreter) { 169 207 var expression = interpreter.CodeStack.Pop(); 170 208 var isExpandExpression = expression.IsProgram; 171 209 172 210 interpreter.BooleanStack.Push(!isExpandExpression); 173 return true;174 211 } 175 212 } … … 182 219 /// </summary> 183 220 [PushExpression(StackTypes.Code, "CODE.CAR")] 221 [StorableClass] 184 222 public class CodeCarExpression : StatelessExpression { 185 public override bool Eval(IInternalPushInterpreter interpreter) { 186 if (interpreter.CodeStack.Count == 0 || 187 !interpreter.CodeStack.Top.IsProgram) return false; 188 189 var expand = interpreter.CodeStack.Top as PushProgram; 190 191 if (expand.IsEmpty) return false; 223 public CodeCarExpression() { } 224 [StorableConstructor] 225 public CodeCarExpression(bool deserializing) : base(deserializing) { } 226 227 public override bool IsNoop(IInternalPushInterpreter interpreter) { 228 return interpreter.CodeStack.IsEmpty || 229 !interpreter.CodeStack.Top.IsProgram || 230 ((PushProgram)interpreter.CodeStack.Top).IsEmpty; 231 } 232 233 public override void Eval(IInternalPushInterpreter interpreter) { 234 var expand = (PushProgram)interpreter.CodeStack.Top; 192 235 var first = expand.Expressions[expand.Expressions.Count - 1]; 193 236 194 237 interpreter.CodeStack.SetTop(first); 195 return true;196 238 } 197 239 } … … 204 246 /// </summary> 205 247 [PushExpression(StackTypes.Code, "CODE.CDR")] 248 [StorableClass] 206 249 public class CodeCdrExpression : StatelessExpression { 207 public override bool Eval(IInternalPushInterpreter interpreter) { 208 if (interpreter.CodeStack.Count == 0) return false; 209 250 public CodeCdrExpression() { } 251 [StorableConstructor] 252 public CodeCdrExpression(bool deserializing) : base(deserializing) { } 253 254 public override bool IsNoop(IInternalPushInterpreter interpreter) { 255 return interpreter.CodeStack.IsEmpty || 256 (interpreter.CodeStack.Top.IsProgram && ((PushProgram)interpreter.CodeStack.Top).IsEmpty); 257 } 258 259 public override void Eval(IInternalPushInterpreter interpreter) { 210 260 PushProgram result; 211 261 var top = interpreter.CodeStack.Top; … … 213 263 if (top.IsProgram) { 214 264 var program = (PushProgram)top; 215 216 if (program.IsEmpty) return false;217 218 265 var expressions = program.CopyExpressions(interpreter.PoolContainer.ExpressionListPool); 219 266 expressions.RemoveAt(expressions.Count - 1); 220 267 221 268 result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions as IReadOnlyList<Expression>); 222 } else { 223 result = PushProgram.Empty; 224 } 269 } else result = PushProgram.Empty; 270 225 271 226 272 interpreter.CodeStack.SetTop(result); 227 return true;228 273 } 229 274 } … … 235 280 /// </summary> 236 281 [PushExpression(StackTypes.Code, "CODE.CONS")] 282 [StorableClass] 237 283 public class CodeConsExpression : StatelessExpression { 238 public override bool Eval(IInternalPushInterpreter interpreter) { 239 if (interpreter.CodeStack.Count < 2 || 240 (interpreter.CodeStack.Top.IsProgram && 241 ((PushProgram)interpreter.CodeStack.Top).Expressions.Count + 1 > interpreter.Configuration.MaxPointsInProgram)) 242 return false; 243 284 public CodeConsExpression() { } 285 [StorableConstructor] 286 public CodeConsExpression(bool deserializing) : base(deserializing) { } 287 288 public override bool IsNoop(IInternalPushInterpreter interpreter) { 289 return interpreter.CodeStack.Count < 2 || 290 (interpreter.CodeStack.Top.IsProgram && 291 ((PushProgram)interpreter.CodeStack.Top).Expressions.Count + 1 > interpreter.Configuration.MaxPointsInProgram); 292 } 293 294 public override void Eval(IInternalPushInterpreter interpreter) { 244 295 var expressions = interpreter.PoolContainer.ExpressionListPool.Get(); 245 296 var first = interpreter.CodeStack.Pop(); … … 254 305 var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions); 255 306 interpreter.CodeStack.SetTop(result); 256 257 return true;258 307 } 259 308 } … … 268 317 /// </summary> 269 318 [PushExpression(StackTypes.Code, "CODE.CONTAINER")] 319 [StorableClass] 270 320 public class CodeContainerExpression : StatelessExpression { 271 public override bool Eval(IInternalPushInterpreter interpreter) { 272 if ((interpreter.CodeStack.Count < 2) || 273 (interpreter.CodeStack[1].GetType() != 274 typeof(PushProgram))) return false; 275 321 public CodeContainerExpression() { } 322 [StorableConstructor] 323 public CodeContainerExpression(bool deserializing) : base(deserializing) { } 324 325 public override bool IsNoop(IInternalPushInterpreter interpreter) { 326 return (interpreter.CodeStack.Count < 2) || 327 (interpreter.CodeStack[1].GetType() != typeof(PushProgram)); 328 } 329 330 public override void Eval(IInternalPushInterpreter interpreter) { 276 331 var target = interpreter.CodeStack.Pop(); 277 332 var source = interpreter.CodeStack.Top; … … 281 336 282 337 interpreter.CodeStack.SetTop(result); 283 return true;284 338 } 285 339 286 340 private static PushProgram GetContainer(PushProgram current, Expression target) { 287 if ( current == target)341 if (Equals(current, target)) 288 342 return null; 289 343 … … 307 361 /// </summary> 308 362 [PushExpression(StackTypes.Code, "CODE.CONTAINS", StackTypes.Boolean)] 363 [StorableClass] 309 364 public class CodeContainsExpression : StatelessExpression { 310 public override bool Eval(IInternalPushInterpreter interpreter) { 311 if (interpreter.CodeStack.Count < 2 || 312 !interpreter.CodeStack[1].IsProgram) 313 return false; 314 365 public CodeContainsExpression() { } 366 [StorableConstructor] 367 public CodeContainsExpression(bool deserializing) : base(deserializing) { } 368 369 public override bool IsNoop(IInternalPushInterpreter interpreter) { 370 return interpreter.CodeStack.Count < 2 || !interpreter.CodeStack[1].IsProgram; 371 } 372 373 public override void Eval(IInternalPushInterpreter interpreter) { 315 374 var second = (PushProgram)interpreter.CodeStack[1]; 316 375 var first = interpreter.CodeStack.Top; … … 319 378 var contains = second.Expressions.Contains(first); 320 379 interpreter.BooleanStack.Push(contains); 321 return true;322 380 } 323 381 } … … 329 387 /// </summary> 330 388 [PushExpression(StackTypes.Code, "CODE.DEFINITION", StackTypes.Name)] 389 [StorableClass] 331 390 public class CodeDefinitionExpression : StatelessExpression { 332 public override bool Eval(IInternalPushInterpreter interpreter) { 333 if ((interpreter.NameStack.Count == 0) || 334 !interpreter.CustomExpressions.ContainsKey(interpreter.NameStack.Top)) return false; 335 391 public CodeDefinitionExpression() { } 392 [StorableConstructor] 393 public CodeDefinitionExpression(bool deserializing) : base(deserializing) { } 394 395 public override bool IsNoop(IInternalPushInterpreter interpreter) { 396 return interpreter.NameStack.IsEmpty || 397 !interpreter.CustomExpressions.ContainsKey(interpreter.NameStack.Top); 398 } 399 400 public override void Eval(IInternalPushInterpreter interpreter) { 336 401 var name = interpreter.NameStack.Pop(); 337 402 var definition = interpreter.CustomExpressions[name]; 338 403 339 404 interpreter.CodeStack.Push(definition); 340 341 return true;342 405 } 343 406 } … … 369 432 /// </summary> 370 433 [PushExpression(StackTypes.Code, "CODE.DISCREPANCY", StackTypes.Integer)] 434 [StorableClass] 371 435 public class CodeDiscrepancyExpression : StatelessExpression { 372 public override bool Eval(IInternalPushInterpreter interpreter) { 373 if (interpreter.CodeStack.Count < 2) return false; 374 436 public CodeDiscrepancyExpression() { } 437 [StorableConstructor] 438 public CodeDiscrepancyExpression(bool deserializing) : base(deserializing) { } 439 440 public override bool IsNoop(IInternalPushInterpreter interpreter) { 441 return interpreter.CodeStack.Count < 2; 442 } 443 444 public override void Eval(IInternalPushInterpreter interpreter) { 375 445 var second = interpreter.CodeStack[1]; 376 446 var first = interpreter.CodeStack.Top; … … 385 455 var discrepancy = GetDiscrepancy(firstItems, secondItems); 386 456 interpreter.IntegerStack.Push(discrepancy); 387 388 return true;389 457 } 390 458 … … 429 497 /// </summary> 430 498 [PushExpression(StackTypes.Code, "CODE.EXTRACT", StackTypes.Integer)] 499 [StorableClass] 431 500 public class CodeExtractExpression : StatelessExpression { 432 public override bool Eval(IInternalPushInterpreter interpreter) { 433 if (interpreter.IntegerStack.Count == 0 || 434 interpreter.CodeStack.Count == 0 || 435 !interpreter.CodeStack.Top.IsProgram) 436 return false; 437 501 public CodeExtractExpression() { } 502 [StorableConstructor] 503 public CodeExtractExpression(bool deserializing) : base(deserializing) { } 504 505 public override bool IsNoop(IInternalPushInterpreter interpreter) { 506 return interpreter.IntegerStack.IsEmpty || 507 interpreter.CodeStack.IsEmpty || 508 !interpreter.CodeStack.Top.IsProgram; 509 } 510 511 public override void Eval(IInternalPushInterpreter interpreter) { 438 512 var expression = (PushProgram)interpreter.CodeStack.Top; 439 513 var index = (int)Math.Abs(interpreter.IntegerStack.Pop() % expression.TotalCount); … … 441 515 442 516 interpreter.CodeStack.SetTop(result); 443 444 return true;445 517 } 446 518 } … … 450 522 /// </summary> 451 523 [PushExpression(StackTypes.Code, "CODE.FROMBOOLEAN", StackTypes.Boolean)] 524 [StorableClass] 452 525 public class CodeFromBooleanExpression : StatelessExpression { 453 public override bool Eval(IInternalPushInterpreter interpreter) { 454 if (interpreter.BooleanStack.Count == 0) return false; 455 526 public CodeFromBooleanExpression() { } 527 [StorableConstructor] 528 public CodeFromBooleanExpression(bool deserializing) : base(deserializing) { } 529 530 public override bool IsNoop(IInternalPushInterpreter interpreter) { 531 return interpreter.BooleanStack.IsEmpty; 532 } 533 534 public override void Eval(IInternalPushInterpreter interpreter) { 456 535 var value = interpreter.BooleanStack.Pop(); 457 536 var expression = new BooleanPushExpression(value); 458 537 459 538 interpreter.CodeStack.Push(expression); 460 461 return true;462 539 } 463 540 } … … 467 544 /// </summary> 468 545 [PushExpression(StackTypes.Code, "CODE.FROMFLOAT", StackTypes.Float)] 546 [StorableClass] 469 547 public class CodeFromFloatExpression : StatelessExpression { 470 public override bool Eval(IInternalPushInterpreter interpreter) { 471 if (interpreter.FloatStack.Count == 0) return false; 472 548 public CodeFromFloatExpression() { } 549 [StorableConstructor] 550 public CodeFromFloatExpression(bool deserializing) : base(deserializing) { } 551 552 public override bool IsNoop(IInternalPushInterpreter interpreter) { 553 return interpreter.FloatStack.IsEmpty; 554 } 555 556 public override void Eval(IInternalPushInterpreter interpreter) { 473 557 var value = interpreter.FloatStack.Pop(); 474 558 var expression = new FloatPushExpression(value); 475 559 476 560 interpreter.CodeStack.Push(expression); 477 478 return true;479 561 } 480 562 } … … 484 566 /// </summary> 485 567 [PushExpression(StackTypes.Code, "CODE.FROMINTEGER", StackTypes.Integer)] 568 [StorableClass] 486 569 public class CodeFromIntegerExpression : StatelessExpression { 487 public override bool Eval(IInternalPushInterpreter interpreter) { 488 if (interpreter.IntegerStack.Count == 0) return false; 489 570 public CodeFromIntegerExpression() { } 571 [StorableConstructor] 572 public CodeFromIntegerExpression(bool deserializing) : base(deserializing) { } 573 574 public override bool IsNoop(IInternalPushInterpreter interpreter) { 575 return interpreter.IntegerStack.IsEmpty; 576 } 577 578 public override void Eval(IInternalPushInterpreter interpreter) { 490 579 var value = interpreter.IntegerStack.Pop(); 491 580 var expression = new IntegerPushExpression(value); 492 581 493 582 interpreter.CodeStack.Push(expression); 494 495 return true;496 583 } 497 584 } … … 501 588 /// </summary> 502 589 [PushExpression(StackTypes.Code, "CODE.FROMNAME", StackTypes.Name)] 590 [StorableClass] 503 591 public class CodeFromNameExpression : StatelessExpression { 504 public override bool Eval(IInternalPushInterpreter interpreter) { 505 if (interpreter.NameStack.Count == 0) return false; 506 592 public CodeFromNameExpression() { } 593 [StorableConstructor] 594 public CodeFromNameExpression(bool deserializing) : base(deserializing) { } 595 596 public override bool IsNoop(IInternalPushInterpreter interpreter) { 597 return interpreter.NameStack.IsEmpty; 598 } 599 600 public override void Eval(IInternalPushInterpreter interpreter) { 507 601 var value = interpreter.NameStack.Pop(); 508 602 var expression = new NameDefineXExecExpression(value); 509 603 510 604 interpreter.CodeStack.Push(expression); 511 512 return true;513 605 } 514 606 } … … 519 611 /// </summary> 520 612 [PushExpression(StackTypes.Code, "CODE.CODEINSERT", StackTypes.Integer)] 613 [StorableClass] 521 614 public class CodeInsertExpression : StatelessExpression { 522 public override bool Eval(IInternalPushInterpreter interpreter) { 523 if (interpreter.IntegerStack.Count == 0 || 524 interpreter.CodeStack.Count < 2 || 525 !interpreter.CodeStack.Top.IsProgram) 526 return false; 527 615 public CodeInsertExpression() { } 616 [StorableConstructor] 617 public CodeInsertExpression(bool deserializing) : base(deserializing) { } 618 619 public override bool IsNoop(IInternalPushInterpreter interpreter) { 620 return interpreter.IntegerStack.IsEmpty || 621 interpreter.CodeStack.Count < 2 || 622 !interpreter.CodeStack.Top.IsProgram; 623 } 624 625 public override void Eval(IInternalPushInterpreter interpreter) { 528 626 var source = interpreter.CodeStack[1]; 529 627 var target = (PushProgram)interpreter.CodeStack.Pop(); … … 545 643 var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, newExpressions as IReadOnlyList<Expression>); 546 644 interpreter.CodeStack.SetTop(result); 547 548 return true;549 645 } 550 646 } … … 557 653 /// </summary> 558 654 [PushExpression(StackTypes.Code, "CODE.LENGTH", StackTypes.Integer)] 655 [StorableClass] 559 656 public class CodeLengthExpression : StatelessExpression { 560 public override bool Eval(IInternalPushInterpreter interpreter) { 561 if (interpreter.CodeStack.Count == 0) return false; 562 657 public CodeLengthExpression() { } 658 [StorableConstructor] 659 public CodeLengthExpression(bool deserializing) : base(deserializing) { } 660 661 public override bool IsNoop(IInternalPushInterpreter interpreter) { 662 return interpreter.CodeStack.IsEmpty; 663 } 664 665 public override void Eval(IInternalPushInterpreter interpreter) { 563 666 var expression = interpreter.CodeStack.Pop(); 564 667 var count = 1; … … 568 671 569 672 interpreter.IntegerStack.Push(count); 570 571 return true;572 673 } 573 674 } … … 577 678 /// </summary> 578 679 [PushExpression(StackTypes.Code, "CODE.LIST")] 680 [StorableClass] 579 681 public class CodeListExpression : StatelessExpression { 580 public override bool Eval(IInternalPushInterpreter interpreter) { 581 if (interpreter.CodeStack.Count < 2 || 582 (interpreter.CodeStack.Top.IsProgram && ((PushProgram)interpreter.CodeStack.Top).Depth == interpreter.Configuration.MaxDepth) || 583 (interpreter.CodeStack[1].IsProgram && ((PushProgram)interpreter.CodeStack[1]).Depth == interpreter.Configuration.MaxDepth)) 584 return false; 585 682 public CodeListExpression() { } 683 [StorableConstructor] 684 public CodeListExpression(bool deserializing) : base(deserializing) { } 685 686 public override bool IsNoop(IInternalPushInterpreter interpreter) { 687 return interpreter.CodeStack.Count < 2 || 688 (interpreter.CodeStack.Top.IsProgram && ((PushProgram)interpreter.CodeStack.Top).Depth == interpreter.Configuration.MaxDepth) || 689 (interpreter.CodeStack[1].IsProgram && ((PushProgram)interpreter.CodeStack[1]).Depth == interpreter.Configuration.MaxDepth); 690 } 691 692 public override void Eval(IInternalPushInterpreter interpreter) { 586 693 var first = interpreter.CodeStack.Pop(); 587 694 var second = interpreter.CodeStack.Top; … … 592 699 593 700 var expandExpression = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions); 594 595 701 interpreter.CodeStack.SetTop(expandExpression); 596 597 return true;598 702 } 599 703 } … … 604 708 /// </summary> 605 709 [PushExpression(StackTypes.Code, "CODE.MEMBER", StackTypes.Boolean)] 710 [StorableClass] 606 711 public class CodeMemberExpression : StatelessExpression { 607 public override bool Eval(IInternalPushInterpreter interpreter) { 608 if (interpreter.CodeStack.Count < 2) return false; 609 712 public CodeMemberExpression() { } 713 [StorableConstructor] 714 public CodeMemberExpression(bool deserializing) : base(deserializing) { } 715 716 public override bool IsNoop(IInternalPushInterpreter interpreter) { 717 return interpreter.CodeStack.Count < 2; 718 } 719 720 public override void Eval(IInternalPushInterpreter interpreter) { 610 721 var first = interpreter.CodeStack[1]; 611 722 var second = interpreter.CodeStack.Top; … … 617 728 618 729 interpreter.BooleanStack.Push(contains); 619 620 return true;621 730 } 622 731 } … … 628 737 /// </summary> 629 738 [PushExpression(StackTypes.Code, "CODE.NTH", StackTypes.Integer)] 739 [StorableClass] 630 740 public class CodeNthExpression : StatelessExpression { 631 public override bool Eval(IInternalPushInterpreter interpreter) { 632 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return false; 633 741 public CodeNthExpression() { } 742 [StorableConstructor] 743 public CodeNthExpression(bool deserializing) : base(deserializing) { } 744 745 public override bool IsNoop(IInternalPushInterpreter interpreter) { 746 return interpreter.CodeStack.IsEmpty || interpreter.IntegerStack.IsEmpty; 747 } 748 749 public override void Eval(IInternalPushInterpreter interpreter) { 634 750 var n = interpreter.IntegerStack.Pop(); 635 751 var expression = interpreter.CodeStack.Top; … … 652 768 653 769 interpreter.CodeStack.SetTop(nthExpression); 654 655 return true;656 770 } 657 771 } … … 666 780 /// </summary> 667 781 [PushExpression(StackTypes.Code, "CODE.NTHCDR", StackTypes.Integer)] 782 [StorableClass] 668 783 public class CodeNthCdrExpression : StatelessExpression { 669 public override bool Eval(IInternalPushInterpreter interpreter) { 670 if ((interpreter.CodeStack.Count == 0) || (interpreter.IntegerStack.Count == 0)) return false; 671 784 public CodeNthCdrExpression() { } 785 [StorableConstructor] 786 public CodeNthCdrExpression(bool deserializing) : base(deserializing) { } 787 788 public override bool IsNoop(IInternalPushInterpreter interpreter) { 789 return interpreter.CodeStack.IsEmpty || interpreter.IntegerStack.IsEmpty; 790 } 791 792 public override void Eval(IInternalPushInterpreter interpreter) { 672 793 var n = interpreter.IntegerStack.Pop(); 673 794 var expression = interpreter.CodeStack.Top; … … 690 811 691 812 interpreter.CodeStack.SetTop(nthExpression); 692 693 return true;694 813 } 695 814 } … … 699 818 /// </summary> 700 819 [PushExpression(StackTypes.Code, "CODE.NULL", StackTypes.Boolean)] 820 [StorableClass] 701 821 public class CodeNullExpression : StatelessExpression { 702 public override bool Eval(IInternalPushInterpreter interpreter) { 703 if (interpreter.CodeStack.Count == 0) return false; 704 822 public CodeNullExpression() { } 823 [StorableConstructor] 824 public CodeNullExpression(bool deserializing) : base(deserializing) { } 825 826 public override bool IsNoop(IInternalPushInterpreter interpreter) { 827 return interpreter.CodeStack.IsEmpty; 828 } 829 830 public override void Eval(IInternalPushInterpreter interpreter) { 705 831 var top = interpreter.CodeStack.Pop(); 706 832 var result = top.IsProgram && ((PushProgram)top).IsEmpty; 707 833 interpreter.BooleanStack.Push(result); 708 709 return true;710 834 } 711 835 } … … 716 840 /// </summary> 717 841 [PushExpression(StackTypes.Code, "CODE.POSITION", StackTypes.Integer)] 842 [StorableClass] 718 843 public class CodePositionExpression : StatelessExpression { 719 public override bool Eval(IInternalPushInterpreter interpreter) { 720 if (interpreter.CodeStack.Count < 2) return false; 721 844 public CodePositionExpression() { } 845 [StorableConstructor] 846 public CodePositionExpression(bool deserializing) : base(deserializing) { } 847 848 public override bool IsNoop(IInternalPushInterpreter interpreter) { 849 return interpreter.CodeStack.Count < 2; 850 } 851 852 public override void Eval(IInternalPushInterpreter interpreter) { 722 853 var second = interpreter.CodeStack[1]; 723 854 var first = interpreter.CodeStack.Top; … … 733 864 734 865 interpreter.IntegerStack.Push(position); 735 736 return true;737 866 } 738 867 } … … 743 872 /// </summary> 744 873 [PushExpression(StackTypes.Code, "CODE.SIZE", StackTypes.Integer)] 874 [StorableClass] 745 875 public class CodeSizeExpression : StatelessExpression { 746 public override bool Eval(IInternalPushInterpreter interpreter) { 747 if (interpreter.CodeStack.Count == 0) return false; 748 876 public CodeSizeExpression() { } 877 [StorableConstructor] 878 public CodeSizeExpression(bool deserializing) : base(deserializing) { } 879 880 public override bool IsNoop(IInternalPushInterpreter interpreter) { 881 return interpreter.CodeStack.IsEmpty; 882 } 883 884 public override void Eval(IInternalPushInterpreter interpreter) { 749 885 var expression = interpreter.CodeStack.Pop(); 750 886 var points = expression.IsProgram … … 753 889 754 890 interpreter.IntegerStack.Push(points); 755 756 return true;757 891 } 758 892 } … … 765 899 /// </summary> 766 900 [PushExpression(StackTypes.Code, "CODE.SUBST")] 901 [StorableClass] 767 902 public class CodeSubstitutionExpression : StatelessExpression { 768 public override bool Eval(IInternalPushInterpreter interpreter) { 769 if ((interpreter.CodeStack.Count < 3) || 770 (interpreter.CodeStack.Top.GetType() != typeof(PushProgram))) 771 return false; 772 903 public CodeSubstitutionExpression() { } 904 [StorableConstructor] 905 public CodeSubstitutionExpression(bool deserializing) : base(deserializing) { } 906 907 public override bool IsNoop(IInternalPushInterpreter interpreter) { 908 return (interpreter.CodeStack.Count < 3) || 909 (interpreter.CodeStack.Top.GetType() != typeof(PushProgram)); 910 } 911 912 public override void Eval(IInternalPushInterpreter interpreter) { 773 913 var third = interpreter.CodeStack[2]; 774 914 var second = interpreter.CodeStack[1]; … … 789 929 790 930 var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, newExpressions); 791 792 931 interpreter.CodeStack.SetTop(result); 793 794 return true;795 932 } 796 933 }
Note: See TracChangeset
for help on using the changeset viewer.