KWLetNode.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWLetNode.h"
  7. #import "KWExampleNodeVisitor.h"
  8. @interface KWLetNode ()
  9. @property (nonatomic, weak) KWLetNode *parent;
  10. @property (nonatomic, strong) KWLetNode *child;
  11. @property (nonatomic, strong) KWLetNode *next;
  12. @property (nonatomic, weak) KWLetNode *previous;
  13. @end
  14. @implementation KWLetNode
  15. @synthesize objectRef = _objectRef;
  16. - (instancetype)initWithSymbolName:(NSString *)aSymbolName objectRef:(__autoreleasing id *)anObjectRef block:(id (^)(void))block
  17. {
  18. if ((self = [super init])) {
  19. _symbolName = [aSymbolName copy];
  20. _objectRef = anObjectRef;
  21. _block = [block copy];
  22. }
  23. return self;
  24. }
  25. + (instancetype)letNodeWithSymbolName:(NSString *)aSymbolName objectRef:(__autoreleasing id *)anObjectRef block:(id (^)(void))block
  26. {
  27. return [[self alloc] initWithSymbolName:aSymbolName objectRef:anObjectRef block:block];
  28. }
  29. #pragma mark - Evaluating nodes
  30. - (id)evaluate
  31. {
  32. id result = nil;
  33. if (self.child) {
  34. result = [self.child evaluate];
  35. }
  36. else if (self.block) {
  37. result = self.block();
  38. }
  39. *self.objectRef = result;
  40. return result;
  41. }
  42. - (void)evaluateTree
  43. {
  44. [self evaluate];
  45. [self.next evaluateTree];
  46. }
  47. #pragma mark - Managing node relationships
  48. - (void)addLetNode:(KWLetNode *)aNode
  49. {
  50. if (![aNode isEqual:self]) {
  51. if ([aNode.symbolName isEqualToString:self.symbolName]) {
  52. [self addChild:aNode];
  53. }
  54. else if (self.next) {
  55. [self.next addLetNode:aNode];
  56. }
  57. else {
  58. self.next = aNode;
  59. }
  60. }
  61. }
  62. - (void)addChild:(KWLetNode *)aNode
  63. {
  64. if (self.child && ![self.child isEqual:aNode]) {
  65. [self.child addChild:aNode];
  66. }
  67. else {
  68. self.child = aNode;
  69. }
  70. }
  71. - (void)setNext:(KWLetNode *)aNode
  72. {
  73. aNode.previous = self;
  74. _next = aNode;
  75. }
  76. - (void)setChild:(KWLetNode *)aNode
  77. {
  78. aNode.parent = self;
  79. _child = aNode;
  80. }
  81. - (void)unlink
  82. {
  83. [self.next unlink];
  84. self.previous.next = nil;
  85. self.previous = nil;
  86. [self.child unlink];
  87. self.parent.child = nil;
  88. self.parent = nil;
  89. }
  90. #pragma mark - Accepting visitors
  91. - (void)acceptExampleNodeVisitor:(id<KWExampleNodeVisitor>)aVisitor
  92. {
  93. [aVisitor visitLetNode:self];
  94. }
  95. #pragma mark - Describing nodes
  96. - (NSString *)description
  97. {
  98. return [NSString stringWithFormat:@"%@ {%@\n}", [[self class] description], [self recursiveDescription]];
  99. }
  100. - (NSString *)nodeDescription
  101. {
  102. return [NSString stringWithFormat:@"<%@ \"%@\">", [[self class] description], self.block ? self.block() : nil];
  103. }
  104. - (NSString *)recursiveDescription
  105. {
  106. if (!self.parent) {
  107. NSMutableString *description = [NSMutableString stringWithFormat:@"\n\t%@:\n\t\t%@", self.symbolName, [self nodeDescription]];
  108. if (self.child) [description appendFormat:@"%@", [self.child recursiveDescription]];
  109. if (self.next) [description appendString:[self.next recursiveDescription]];
  110. return [description copy];
  111. }
  112. else {
  113. NSMutableString *description = [NSMutableString stringWithFormat:@",\n\t\t%@", [self nodeDescription]];
  114. if (self.child) [description appendString:[self.child recursiveDescription]];
  115. return [description copy];
  116. }
  117. }
  118. @end